How to use htonl in IPv6

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • linuxer

    How to use htonl in IPv6

    Hello all,
    I have the following program, which will take ipv4 address as input
    in command line and convert it from host byte order to network byte
    order. I need similar thing to be done for IPv6. Any help is
    appriciated. Thanks in advance.

    #include <stdio.h>
    main( int argc, char *argv[] )
    {
    unsigned int ip, b1, b2, b3, b4;
    ip=b1=b2=b3=b4= 0;

    if (sscanf(argv[1], "%d.%d.%d.% d", &b1, &b2, &b3, &b4)
    == 4 && b1<=255U && b2<=255U && b3<=255U && b4<=255U) {
    //For IPv4 address
    ip = htonl( (b1<<24)|(b2<<1 6)|(b3<<8)|b4 );
    }
    printf("IP_TO_L ONG:%ld\n", ip);
    }

  • Alf P. Steinbach

    #2
    Re: How to use htonl in IPv6

    * linuxer:[color=blue]
    > Hello all,
    > I have the following program, which will take ipv4 address as input
    > in command line and convert it from host byte order to network byte
    > order. I need similar thing to be done for IPv6. Any help is
    > appriciated. Thanks in advance.
    >
    > #include <stdio.h>
    > main( int argc, char *argv[] )
    > {
    > unsigned int ip, b1, b2, b3, b4;
    > ip=b1=b2=b3=b4= 0;
    >
    > if (sscanf(argv[1], "%d.%d.%d.% d", &b1, &b2, &b3, &b4)
    > == 4 && b1<=255U && b2<=255U && b3<=255U && b4<=255U) {
    > //For IPv4 address
    > ip = htonl( (b1<<24)|(b2<<1 6)|(b3<<8)|b4 );
    > }
    > printf("IP_TO_L ONG:%ld\n", ip);
    > }[/color]

    "htonl" is not part of standard C++ or the standard C++ library.

    So you're off-topic, and I don't know where to send you, except,
    use a search engine such as Google.

    The above code should not even compile.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?

    Comment

    • Maxim Yegorushkin

      #3
      Re: How to use htonl in IPv6


      linuxer wrote:[color=blue]
      > Hello all,
      > I have the following program, which will take ipv4 address as input
      > in command line and convert it from host byte order to network byte
      > order. I need similar thing to be done for IPv6. Any help is
      > appriciated. Thanks in advance.[/color]

      On Linux use inet_ntop().
      man inet_ntop

      Comment

      Working...