Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert IP for reverse ip lookup

I have an IP address e.g. char *ip = "192.168.1.13" and need to convert it into this "13.1.168.192" Is there a proper possibility to that in C already or do I have to do it myself by simply making tokens and putting them together again?

like image 476
wasp256 Avatar asked May 04 '13 10:05

wasp256


Video Answer


4 Answers

You could always use inet_pton to convert it to binary form, making it easy to reverse, and then convert back to text with inet_ntop.


Remember that an IPv4 address is only a 32-bit unsigned integer. Swapping the bytes around of that is very easy.

Something like this:

const char ip[] = "192.168.0.1";
char reversed_ip[INET_ADDRSTRLEN];

in_addr_t addr;

/* Get the textual address into binary format */
inet_pton(AF_INET, ip, &addr);

/* Reverse the bytes in the binary address */
addr =
    ((addr & 0xff000000) >> 24) |
    ((addr & 0x00ff0000) >>  8) |
    ((addr & 0x0000ff00) <<  8) |
    ((addr & 0x000000ff) << 24);

/* And lastly get a textual representation back again */
inet_ntop(AF_INET, &addr, reversed_ip, sizeof(reversed_ip));

After this code, the variable reversed_ip contains the revered address as a string.

like image 93
Some programmer dude Avatar answered Sep 30 '22 11:09

Some programmer dude


int a,b,c,d;
char *ip = "192.168.1.13"
char ip2[16];
sscanf(ip,"%d.%d.%d.%d",&a,&b,&c,&d);
sprintf(ip2, "%d.%d.%d.%d", d, c, b, a);
like image 32
MOHAMED Avatar answered Sep 30 '22 10:09

MOHAMED


#include <stdio.h>
#include <string.h>

void swap(char *a, char *b){
    char wk = *a;
    *a = *b;
    *b = wk;
}

void strpartrev(char *s, int len){
    int i,j;
    for(i=0,j=len-1; i<len/2 ;++i,--j)
        swap(s + i, s + j);
}

char *strwordrev(char *str, char delimiter){
    //str change destructively
    int sp = -1, wordlen=0;
    char stack[16], *p=str;

    while(*p){
        if(*p == delimiter){
            strpartrev(stack + sp - wordlen + 1, wordlen);
            wordlen = 0;
        } else {
            ++wordlen;
        }
        stack[++sp] = *p++;
    }
    strpartrev(stack + sp - wordlen + 1 , wordlen);
    p = str;
    do{
        *p++ = stack[sp--];
    }while(sp>=0);

    return str;
}

char *strWordRev(char *str, char delimiter){
    //str change destructively
    char *head, *tail;
    int len = strlen(str);

    head = str;
    while(tail = strchr(head, delimiter)){
        strpartrev(head, tail - head);
        head = tail + 1;
    }
    strpartrev(head, str + len - head);
    strpartrev(str, len);

    return str;
}

int main(void){
    char *ip = "192.168.1.13";
    char rip[16];

    strcpy(rip, ip);
    printf("%s\n", strWordRev(rip, '.'));//13.1.168.192

    return 0;
}
like image 45
BLUEPIXY Avatar answered Sep 30 '22 11:09

BLUEPIXY


Split the ip into an array and use code to reverse the array.

here's a neat array reversing bit of code i found here: http://www.programmingsimplified.com/c-program-reverse-array

#include <stdio.h>

int main()
{
   int n, c, d, a[100], b[100];

   printf("Enter the number of elements in array\n");
   scanf("%d", &n);

   printf("Enter the array elements\n");

   for (c = 0; c < n ; c++)
      scanf("%d", &a[c]);

   /*
    * Copying elements into array b starting from end of array a
    */

   for (c = n - 1, d = 0; c >= 0; c--, d++)
      b[d] = a[c];

   /*
    * Copying reversed array into original.
    * Here we are modifying original array, this is optional.
    */

   for (c = 0; c < n; c++)
      a[c] = b[c];

   printf("Reverse array is\n");

   for (c = 0; c < n; c++)
      printf("%d\n", a[c]);

   return 0;
}
like image 41
Chris Avatar answered Sep 30 '22 10:09

Chris