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. 

Tuesday, May 15, 2012

Change the MTU of your interface

Command to change the MTU of your interface

1
2
ifconfig <interface> mtu <value> up
ifconfig eth1 mtu 1000 up

--ahamed


tcpdump - capturing output to a file

Command to capture tcpdump output to pcap format for analysis with Wireshark or other tools

tcpdump -i <interface> -w <output file>
tcpdump -i eth1 -w output.pcap

Have a good day!


Sunday, March 18, 2012

echo $SHELL and echo $0

"How do you identify your current shell?" is one of the frequently asked interview question.


Some would say "echo $SHELL" and some would say "echo $0". So, which is the right answer?


See for yourself!
root > grep ^root /etc/passwd
root:x:0:0:root:/root:/bin/bash

root > bash
root > echo $SHELL
/bin/bash
root > echo $0
-bash

root > ksh
root > echo $SHELL
/bin/bash
root > echo $0
ksh
Got it?


$SHELL will always print the login shell. $0 will print the current shell.


Good Day!