使用unsigned char数组填充struct in6_addr

时间:2018-04-08 20:47:15

标签: c++ dns lookup

我正在使用C ++进行 DNS查找工具,我正在尝试从中获取IPv6 unsigned char *(以不可读的格式存储),将其复制到struct in6_addr,然后我想将其转换并打印出来。

struct in6_addr tmp2;
char buf[41];
memcpy(tmp2.s6_addr, answ[i].rdata, 128); 
cout << answ[i].name << " IN AAAA " << inet_ntop(AF_INET6, tmp2.s6_addr, buf, 128) << endl; 

我的输出应该是这样的,

www.domain.name.cz. IN AAAA 2001:67c:1220:809::93e5:917

但不知怎的,它看起来像这样。

www.domain.name.cz IN AAAA 106:7c12:2008:900::

生成RDATA

u_char *ReadName(unsigned char *readResponse, unsigned char *buffer, int *count) {
unsigned char *name;
unsigned int p = 0, jumped = 0, offset;
int i, j;

*count = 1;
name = (unsigned char *) malloc(256);

name[0] = '\0';

//read the names in 3www6google3com format
while (*readResponse != 0) {
    if (*readResponse >= 192) {
        offset = (*readResponse) * 256 + *(readResponse + 1) - 49152; //49152 = 11000000 00000000 ;)
        readResponse = buffer + offset - 1;
        jumped = 1; //we have jumped to another location so counting wont go up!
    } else {
        name[p++] = *readResponse;
    }

    readResponse = readResponse + 1;

    if (jumped == 0) {
        *count = *count + 1; //if we havent jumped to another location then we can count up
    }
}

name[p] = '\0'; //string complete
if (jumped == 1) {
    *count = *count + 1; //number of steps we actually moved forward in the packet
}

//now convert 3www6google3com0 to www.google.com
for (i = 0; i < (int) strlen((const char *) name); i++) {
    p = name[i];
    for (j = 0; j < (int) p; j++) {
        name[i] = name[i + 1];
        i = i + 1;
    }
    name[i] = '.';
}
name[i - 1] = '\0'; //remove the last dot
return name;

感谢您的帮助!

0 个答案:

没有答案
相关问题