Portál AbcLinuxu, 6. května 2025 22:44

Dotaz: inet_pton IPv4/IPv6

9.2.2012 10:16 miso | skóre: 36 | blog: iSCSI_initiator_howto | Praha
inet_pton IPv4/IPv6
Přečteno: 202×
Odpovědět | Admin
Zdravim,

rad by som dosiahol nasledovne:

vstup: IPv4 adresa (10.20.30.40)

vystup: IPv4 adresa v tvare IPv6 (::ffff:10.20.30.40) tak, ako je uvedene v priklade napr. tu

Je to vobec mozne pomocou funkcie inet_pton? Zaujima ma to ciste zo zvedavosti, pokial to nejde, workaround nepotrebujem.

Nasledujuci kod vypise IPv4 adresu v "klasickom" IPv6 tvare (xxxx:xxxx:xxxx:xxxx:xxxx:xxxx), teda nezmyselnu adresu:
#include < sys/socket.h >
#include < netinet/in.h >
#include < arpa/inet.h >
#include < stdio.h >
#include < stdlib.h >

int main(int argc, char *argv[])
{
    struct in_addr addr;
    unsigned char buf[sizeof(struct in6_addr)];
    char str[INET6_ADDRSTRLEN];

    if (argc<2)
    {
        fprintf(stderr, "IPv4 address required as an argument.\n");
        exit(EXIT_FAILURE);
    }
    if (inet_aton(argv[1], &addr) == 0)
    {
        perror("inet_aton");
        exit(EXIT_FAILURE);
    }
    if (inet_pton(AF_INET, argv[1], buf) != 1)
    {
        perror("inet_pton");
        exit(EXIT_FAILURE);
    }
    if (inet_ntop(AF_INET6, buf, str, INET6_ADDRSTRLEN)==NULL)
    {
        perror("inet_ntop");
        exit(EXIT_FAILURE);
    }
    printf("%s\n", str);
    exit(EXIT_SUCCESS);
}
Project Satan infects Calculon with Werecar virus

Řešení dotazu:


Nástroje: Začni sledovat (1) ?Zašle upozornění na váš email při vložení nového komentáře.

Odpovědi

Řešení 1× (miso (tazatel))
9.2.2012 14:08 miso | skóre: 36 | blog: iSCSI_initiator_howto | Praha
Rozbalit Rozbalit vše Re: inet_pton IPv4/IPv6
Odpovědět | | Sbalit | Link | Blokovat | Admin
Vyriesil som to nasledovne:
#define _GNU_SOURCE
#include < string.h>
#include < sys/socket.h>
#include < netinet/in.h>
#include < arpa/inet.h>
#include < stdio.h>
#include < stdlib.h>

#define IPV6_ADDRESS_SIZE sizeof(struct in6_addr)

int main(int argc, char *argv[])
{
    struct in_addr addr;
    unsigned char buf_v4[sizeof(struct in_addr)];
    unsigned char buf_v6[IPV6_ADDRESS_SIZE] = {0};
    unsigned char str[INET6_ADDRSTRLEN];

    if (argc<2)
    {
        fprintf(stderr, "IPv4 address required as an argument.\n");
        exit(EXIT_FAILURE);
    }

    if (inet_aton(argv[1], &addr) == 0)
    {
        perror("inet_aton");
        exit(EXIT_FAILURE);
    }

    if (inet_pton(AF_INET, argv[1], buf_v4) != 1)
    {
        perror("inet_pton");
        exit(EXIT_FAILURE);
    }

    *(buf_v6 + IPV6_ADDRESS_SIZE - 5) = 255;
    *(buf_v6 + IPV6_ADDRESS_SIZE - 6) = 255;
    mempcpy(buf_v6 + IPV6_ADDRESS_SIZE - 4, buf_v4, 4);

    if (inet_ntop(AF_INET6, buf_v6, str, INET6_ADDRSTRLEN)==NULL)
    {
        perror("inet_ntop");
        exit(EXIT_FAILURE);
    }

    printf("%s\n", str);
    exit(EXIT_SUCCESS);
}
Project Satan infects Calculon with Werecar virus

Založit nové vláknoNahoru

Tiskni Sdílej: Linkuj Jaggni to Vybrali.sme.sk Google Del.icio.us Facebook

ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.