2013-05-04 122 views
4

我有一個IP地址,例如char *ip = "192.168.1.13"並且需要將它轉換成這個"13.1.168.192" C中已經存在一個合適的可能性,或者我必須通過簡單地將令牌再次放在一起來自己完成嗎?將IP轉換爲反向ip查找

+0

我不認爲有在一般的字符串這樣的功能,但有一個共同的伎倆:字符串反向每個「字」(即'「291.861.1.31」') ,然後反轉整個字符串。 – dasblinkenlight 2013-05-04 10:28:27

+1

首先,IPv4地址是32位。這就是它在所有與套接字相關的功能中的表現。也許用'htons'和朋友改變字節順序? – KAction 2013-05-04 10:28:54

+0

你是否正在顛倒IP地址來混淆反向DNS? – 2013-05-04 10:30:19

回答

9

您可以隨時使用inet_pton將其轉換爲二進制格式,使其易於翻轉,然後轉換回inet_ntop的文本。


請記住,IPv4地址只是一個32位無符號整數。交換周圍的字節非常簡單。

事情是這樣的:

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)); 

此代碼後,變量reversed_ip包含崇敬地址爲一個字符串。

+0

它得到了這個到目前爲止unsigned char buf [sizeof(struct in_addr)]; \t inet_pton(AF_INET,「192.168.1.13」,buf);但我不確定如何現在扭轉它? – wasp256 2013-05-04 10:57:43

+0

@ wasp256添加了一些示例(但未經測試)的代碼。 – 2013-05-04 11:52:30

+0

@ wasp256現在已經測試過了,工作正常。 – 2013-05-04 13:57:21

1

將ip拆分成數組並使用代碼來反轉數組。

這裏的代碼整齊地排列扭轉位我發現這裏: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; 
} 
1

您可以將字符串轉換爲addrinfogetaddrinfo。指定AI_NUMERICHOST以防止DNS查找。

addrinfo hint; 
memset(&hint, 0, sizeof(addrinfo)); 
hint.ai_socktype = SOCK_STREAM; 
hint.ai_protocol = IPPROTO_TCP; 
hint.ai_family = AF_INET; 
hint.ai_flags = AI_NUMERICHOST; // prevents dns lookup 

addrinfo *result = nullptr; 
if (getaddrinfo(name, nullptr, &hint, &result)) 
    return false; 

uint32_t ip; 
memcpy(&ip, &result, sizeof(uint32_t)); 

uint32_t ipreversed = htonl(ip); 

freeaddrinfo(result); 
+0

這是否也與IPv6地址一起工作? – dpb 2016-04-01 14:04:00

3
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); 
2
#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; 
}