简体   繁体   English

在C ++中为DNSBL制作反向ipv6

[英]make a reverse ipv6 for DNSBL in c++

i got a string called ipv6 with IPv6 address. 我有一个名为ipv6的字符串,带有IPv6地址。 I want to use the IP into DNSBL as zen spamhaus or others DNS. 我想将IP用作zen spamhaus或其他DNS到DNSBL中。 How can i do it ? 我该怎么做 ? The following code does not work as expected into compressed IPv6. 以下代码无法在压缩的IPv6中正常工作。 How can i zero fill ? 我如何将零填充? All the code in C++. C ++中的所有代码。

std::string invertirv6 (const std::string &str) {
    struct in6_addr *addr;
    inet_pton(AF_INET6,str.c_str(),&addr);
    char str2[41];

    sprintf(str2,"%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x",
    (int)addr->s6_addr[15], (int)addr->s6_addr[14],
    (int)addr->s6_addr[13], (int)addr->s6_addr[12],
    (int)addr->s6_addr[11], (int)addr->s6_addr[10],
    (int)addr->s6_addr[9], (int)addr->s6_addr[8],
    (int)addr->s6_addr[7], (int)addr->s6_addr[6],
    (int)addr->s6_addr[5], (int)addr->s6_addr[4],
    (int)addr->s6_addr[3], (int)addr->s6_addr[2],
    (int)addr->s6_addr[1], (int)addr->s6_addr[0]);
    string retorno = str2;
    return retorno;
}

This program has multiple issues: 该程序有多个问题:

  1. string retorno = str2; has no namespace, and 'string' was not declared in this scope is the compiler error. 没有命名空间,并且'string' was not declared in this scope编译器错误。 Easy enough to fix. 易于修复。

     std::string retorno = str2; 
  2. struct in6_addr *addr; only allocates space for a pointer to a struct, but the memory for the struct itself is never allocated. 只为指向结构的指针分配空间,但从未为结构本身分配内存。 This is the immediate cause of the crash. 这是崩溃的直接原因。

    I'll just quick fix this by allocating it on the stack, and because it's no longer a pointer, access the struct members with . 我将通过在堆栈上分配它来快速解决此问题,并且由于它不再是指针,请使用来访问struct成员. rather than -> . 而不是->

     struct in6_addr addr; 

    Once you fix that, though, your program will crash again, due to: 但是,一旦解决此问题,由于以下原因,您的程序将再次崩溃:

  3. The char str2 is too small for the line being sprintf ed into it, which requires 47 printing characters and a null terminator. 对于要sprintf到其中的行, char str2太小,它需要47个打印字符和一个空终止符。

    That's easily enough patched: 修补起来很容易:

     char str2[48]; 
  4. The explicit typecast to int is unnecessary and can be omitted. 显式类型转换为int是不必要的,可以省略。

A working program would look like this: 工作程序如下所示:

#include <cstdio>
#include <string>
#include <arpa/inet.h>
#include <sys/socket.h>

std::string invertirv6 (const std::string &str) {
    struct in6_addr addr;
    inet_pton(AF_INET6,str.c_str(),&addr);
    char str2[48];

    sprintf(str2,"%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x",
    addr.s6_addr[15], addr.s6_addr[14],
    addr.s6_addr[13], addr.s6_addr[12],
    addr.s6_addr[11], addr.s6_addr[10],
    addr.s6_addr[9], addr.s6_addr[8],
    addr.s6_addr[7], addr.s6_addr[6],
    addr.s6_addr[5], addr.s6_addr[4],
    addr.s6_addr[3], addr.s6_addr[2],
    addr.s6_addr[1], addr.s6_addr[0]);
    std::string retorno = str2;
    return retorno;
}

int main(void) {
    std::string a = "2001:db8:fda8:eca7:b54:6685:c8ba:6891";
    std::string b = invertirv6(a);

    printf("%s\n", b.c_str());
}

This resolves all the crashes and prints output: 这样可以解决所有崩溃问题并打印输出:

91.68.ba.c8.85.66.54.0b.a7.ec.a8.fd.b8.0d.01.20

But I think that that is not exactly the output you say you want. 但是我认为那并不是您想要的输出。 You will need to change the sprintf call to print exactly what you want in the format you want it. 您将需要更改sprintf调用,以所需的格式精确打印所需的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM