Monday, April 29, 2013

The lshw command

The lshw command lists the hardware information in your system. I came across this because I was having issues with wifi connectivity in Ubuntu 12.04. It just keeps disconnecting after a while and connects back. So, wanted to check the wifi card on and look for possible solutions.

root@Imperfecto_:~# lshw
imperfecto_               
    description: Desktop Computer
    product: Parrot ()
    vendor: GOOGLE
    version: 1.0
    serial: 123456789
    width: 64 bits
    capabilities: smbios-2.7 dmi-2.7 vsyscall32
    configuration: boot=normal chassis=desktop
  *-core
       description: Motherboard
       physical id: 0

and so on...

And I was interested in my wifi card, so I gave it some filters
root@Imperfecto_:~# lshw -C network
  *-network               
       description: Wireless interface
       product: AR9462 Wireless Network Adapter
       vendor: Atheros Communications Inc.
       physical id: 0
       bus info: pci@0000:01:00.0
       logical name: wlan0
       version: 01

and so on...

Installing the gnome desktop in Ubuntu 12.04

Yeah, I also didn't like the Unity which comes by default in Ubuntu 12.04. Just install the gnome-session-fallback and you are good to go


sudo apt-get install gnome-session-fallback

#In the newer version you may want to try
sudo apt-get install gnome-session-flashback


agentBUSH@ChrUbuntu:~# sudo apt-get install gnome-session-fallback
Reading package lists... Done
... bla bla bla ...
Processing triggers for libc-bin ...
ldconfig deferred processing now taking place
agentBUSH@ChrUbuntu:~# 

After installation, log out and at the log-in screen you will have the gnome-classic.

Friday, April 26, 2013

How does httpd serves multiple client listening on a single port 80?

Take a look at this post

Thursday, April 25, 2013

Program to print pattern

Program to print the following pattern (see below for the pattern). More patterns will be posted later.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include "stdio.h"
#include "stdlib.h"
#include "string.h"

static void print_it(unsigned len)
{
        if(!len) return;

        unsigned i;
        char *star, *buf, fmt[16]="";

        star = malloc(sizeof(char) * len+1);
        if(!star) return;
        buf = malloc(sizeof(char) * len+1);
        if(!buf) return;

        for(i=0; i<len; i++)
                strcat(star, "*");
        snprintf(fmt, 16, "%%%ds\n", len);
        for(i=len; i>0; i--){
                strcpy(buf, star+i-1);
                printf(fmt, buf);
        }
        free(star);
        free(buf);
        return;
}

int main(int argc, char **argv)
{
        print_it(12);
        return 0;
}


[root@Imperfecto_1 ~]gcc run.c && ./a.out
           *
          **
         ***
        ****
       *****
      ******
     *******
    ********
   *********
  **********
 ***********
************


Valgrind Output
[root@Imperfecto_1 ~]valgrind ./a.out

==5472== HEAP SUMMARY:
==5472==     in use at exit: 0 bytes in 0 blocks
==5472==   total heap usage: 2 allocs, 2 frees, 26 bytes allocated
==5472== 
==5472== All heap blocks were freed -- no leaks are possible


Split functionality of awk implementation in C


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include "stdio.h"
#include "stdlib.h"
#include "string.h"

static unsigned split(const char *in, char ***t_out, const char *delim)
{

        if( !*in || !*delim ){
                return 0;
        }

        unsigned i_len = strlen(in);
        char *buf = NULL;
        char *ptr = NULL;
        unsigned t_len, i=0;

        buf = malloc(sizeof(char) * i_len);
        if(!buf) return 0;

        if(!memcpy(buf, in, i_len)) return 0;

        ptr = strtok(buf, delim);
        if(ptr) {
                t_len = strlen(ptr);
                *t_out = (char **)malloc(sizeof(char*));
                (*t_out)[0] = (char *)malloc(sizeof(char) * t_len);
                memcpy((*t_out)[0], ptr, t_len);
        }
        while(ptr){
                ptr = strtok(NULL, delim);
                if(ptr) {
                        i++;
                        t_len = strlen(ptr);
                        *t_out = (char **)realloc(*t_out, sizeof(char *) * (i+1));
                        (*t_out)[i] = (char *)malloc(sizeof(char) * t_len);
                        memcpy((*t_out)[i], ptr, t_len);
                }
        }
        free(buf);
        return i;
}

int main(int argc, char **argv)
{

        char *in = "split.implementation.of.awk";
        char **out = NULL;
        int i, len = split(in, &out, ".");

        if(out) {
                for(i=0; i<=len; i++){
                        printf("%s\n", out[i]);
                        free(out[i]);
                }
                free(out);
        }
        return 0;
}


[root@Imperfecto_1 ~] gcc -g run.c && ./a.out
split
implementation
of
awk

Valgrind output

[root@Imperfecto_1 ~]# valgrind ./a.out
==2320== malloc/free: in use at exit: 0 bytes in 0 blocks.
==2320== malloc/free: 9 allocs, 9 frees, 91 bytes allocated.
==2320== For counts of detected errors, rerun with: -v
==2320== All heap blocks were freed -- no leaks are possible.

Wednesday, April 24, 2013

Reverse bits in an Integer


A simple function to reverse the bits in an integer

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "stdio.h"
#include "stdlib.h"

/* Function to reverse the bits
 * Assuming 32 bits.
 * Simple functionality, get the last bit by right shift
 * and insert into the output by ORing and then left shifting.
 * There are better ways to do this.
 */
void reverse(unsigned *input)
{
        unsigned i=0, mask=0x1, res=0x0, tmp = 0x0;
        unsigned in = *input;

        for(i=0; i<32; i++){
                tmp = in & mask;
                in = in >> 1;

                res = res << 1;
                res |= tmp;
        }
        *input = res;
        return;

}

int main(int argc, char **argv)
{

        unsigned input = 0xffffbbbb;
        printf("%x\n", input);
        reverse(&input);
        printf("%x\n", input);

        return 0;
}

[root@Imperfecto_1 ~] gcc run.c && ./a.out
ffffbbbb
ddddffff

[root@Imperfecto_1 ~] echo "ibase=16;obase=2; FFFFBBBB" | bc
11111111111111111011101110111011

[root@Imperfecto_1 ~] echo "ibase=16;obase=2; DDDDFFFF" | bc
11011101110111011111111111111111

Convert IP to Integer

Formula would be :-

First Octet * (256 ^ 3) + Second Octet * (256 ^ 2) 
+ Third Octet * (256 ^ 1) + Fourth Octet

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "stdio.h"
#include "math.h"

/* Function to convert IP address into Integer
 * Input  : IP address array
 * Output : Converted IP
 */
unsigned int ip_to_int(unsigned in[])
{
        return ((in[0]*(pow(256,3))) + (in[1]*(pow(256,2))) +
                        (in[2]*(pow(256,1))) + (in[3]));
}

int main(int argc, char **argv)
{

        unsigned ip_addr[] = { 192, 168, 1, 124 };
        printf("Input  : %d.%d.%d.%d\n", ip_addr[0], ip_addr[1],
                                        ip_addr[2], ip_addr[3]);
        printf("Output : %u\n", ip_to_int(ip_addr));

        return 0;
}

[root@Imperfecto_1 ~] gcc run.c && ./a.out
Input  : 192.168.1.124
Output : 3232235900

Monday, April 22, 2013

sshd in chrubuntu

Looks like ChrUbuntu (Chrome + Ubuntu) also doesn't come with sshd installed. Let us install it.

apt-get install openssh-server

If your are not the root, use sudo

sudo apt-get install openssh-server

Post installation it runs the sshd service by default.
Verify it

root@ChrUbuntu:~# ps -eaf | grep sshd
root     14580     1  0 21:58 ?        00:00:00 /usr/sbin/sshd -D

Or start the service, either by

root@ChrUbuntu:~# /etc/init.d/ssh start

or by

root@ChrUbuntu:~# service ssh start

That is all it takes ;)

Saturday, April 20, 2013

To print the buffer in HEX

Nothing big, thought I will add this for my own reference. This will print the buffer in hex.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
static void
print_buffer(unsigned *buffer, int len)
{
   unsigned i = 0;
   for(i=0;i<len;i++){
      if(!(i%8)) 
          printf("\n ");
      printf(" 0x%02x",buffer[i]);
   }
   return;
}

You should get an output something like this based on your input
1
2
3
  0x74 0x65 0x73 0x74 0x20 0x6c 0x61 0x62
  0x65 0x6c 0xa0 0xba 0x9f 0x93 0x6c 0xda
  0x31 0x18 0x27 0xa6 0xf7 0x96 0xff 0xd5

Hope this helps someone.