Thread: How do I find numeric values for bind TCP/IPv6

  1. #1
    Registered User
    Join Date
    Jul 2017
    Posts
    28

    How do I find numeric values for bind TCP/IPv6

    Background:
    This is a C code for Linux question, so do not run when you see a different language plz.


    I am working in the LabVIEW environment on a sbRIO running Linux.

    I am trying to program IPv6 communication using C code for Linux.



    Question:
    The problem that I am currently having is finding the correct integers not constants to put into the various commands socket, bind etc... All of the examples on the web show constants instead of the actual integer value which I need.


    How/where can I find these integer values?


    What I Tried:
    I thought it might be in the header file, so I downloaded Cygwin and searched it for socket.h. I found it.


    After reading through socket.h from my Cygwin directory, I managed to find the numeric value for AF_INET6. Using the int socket(int domain, int type, int protocol) gives me an error if I use:

    domain = 23; // AF_INET6

    type = 1; // SOCK_STREAM

    protocol = 0; // DEFAULT


    however, it works if I use:



    domain = 10; // AF_CCITT

    type = 1; // SOCK_STREAM
    protocol = 0; // DEFAULT



    Reference: socket(2) - Linux manual page

    So how/where can I find these integer values and why is it not working when I put in the integer value for AF_INET6? Do different compilers/versions of Linux have different header files?


    Thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    I suppose it depends what version of Linux you're running on your sbRIO. Not all are necessarily IPv6 enabled (or capable) out of the box.

    For sure, just grabbing random header files from other platforms will lead you nowhere.

    Try this command
    Code:
    $ grep -r AF_INET6 /usr/include
    /usr/include/glib-2.0/gio/gioenums.h: * identical to the system defines %AF_INET, %AF_INET6 and %AF_UNIX,
    /usr/include/glib-2.0/gio/gioenums.h:  G_SOCKET_FAMILY_IPV6 = GLIB_SYSDEF_AF_INET6
    /usr/include/x86_64-linux-gnu/bits/socket.h:#define AF_INET6	PF_INET6
    /usr/include/valgrind/vki/vki-linux.h:#define VKI_AF_INET6	10	/* IP version 6			*/
    /usr/include/valgrind/vki/vki-linux.h:	unsigned short int	sin6_family;    /* AF_INET6 */
    /usr/include/valgrind/vki/vki-solaris.h:#define VKI_AF_INET6 AF_INET6
    /usr/include/valgrind/vki/vki-darwin.h:#define	VKI_AF_INET6	AF_INET6
    /usr/include/linux/l2tp.h:	__kernel_sa_family_t l2tp_family; /* AF_INET6 */
    /usr/include/linux/in6.h: *	Types and definitions for AF_INET6 
    /usr/include/linux/in6.h:	unsigned short int	sin6_family;    /* AF_INET6 */
    /usr/include/linux/if_link.h: *       [AF_INET6] = {
    <<snipped voluminous output >>
    If you don't see anything like this, then you need a different distro on your target.

    Or this program.
    Code:
    $ cat foo.c
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    
    int main()
    {
      printf("AF_INET6=%d\n", AF_INET6 );
      return 0;
    }
    $ ./a.out 
    AF_INET6=10
    If it doesn't compile, you're stuck.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2017
    Posts
    28
    Quote Originally Posted by Salem View Post
    I suppose it depends what version of Linux you're running on your sbRIO. Not all are necessarily IPv6 enabled (or capable) out of the box.

    For sure, just grabbing random header files from other platforms will lead you nowhere.

    Try this command
    Code:
    $ grep -r AF_INET6 /usr/include
    /usr/include/glib-2.0/gio/gioenums.h: * identical to the system defines %AF_INET, %AF_INET6 and %AF_UNIX,
    /usr/include/glib-2.0/gio/gioenums.h:  G_SOCKET_FAMILY_IPV6 = GLIB_SYSDEF_AF_INET6
    /usr/include/x86_64-linux-gnu/bits/socket.h:#define AF_INET6    PF_INET6
    /usr/include/valgrind/vki/vki-linux.h:#define VKI_AF_INET6    10    /* IP version 6            */
    /usr/include/valgrind/vki/vki-linux.h:    unsigned short int    sin6_family;    /* AF_INET6 */
    /usr/include/valgrind/vki/vki-solaris.h:#define VKI_AF_INET6 AF_INET6
    /usr/include/valgrind/vki/vki-darwin.h:#define    VKI_AF_INET6    AF_INET6
    /usr/include/linux/l2tp.h:    __kernel_sa_family_t l2tp_family; /* AF_INET6 */
    /usr/include/linux/in6.h: *    Types and definitions for AF_INET6 
    /usr/include/linux/in6.h:    unsigned short int    sin6_family;    /* AF_INET6 */
    /usr/include/linux/if_link.h: *       [AF_INET6] = {
    <<snipped voluminous output >>
    If you don't see anything like this, then you need a different distro on your target.

    Or this program.
    Code:
    $ cat foo.c
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    
    int main()
    {
      printf("AF_INET6=%d\n", AF_INET6 );
      return 0;
    }
    $ ./a.out 
    AF_INET6=10
    If it doesn't compile, you're stuck.

    It compiled and returned AF_INET6 = 10

    So, I managed to find the socket.h header file that defines some of these values. In the file AF_INET6 = 10 and SOCK_STREAM = 1.


    Now I need to test the bind command whose syntax is:

    Code:
       
    struct sockaddr_in6 {
          sa_family_t     sin6_family;   /* AF_INET6 */
          in_port_t       sin6_port;     /* port number */
          uint32_t        sin6_flowinfo; /* IPv6 flow information */
          struct in6_addr sin6_addr;     /* IPv6 address */
          uint32_t        sin6_scope_id; /* Scope ID (new in 2.4) */
    };
    
    struct in6_addr {
          unsigned char   s6_addr[16];   /* IPv6 address */
    };
    Reference: bind(2) - Linux manual page

    I know how to define: sin6_family = 10


    I assume that I can define sin6_port = 3333 (arbitrary number between 1024 and 5000)


    I also assume that s6_addr = "0000000000000001" since we only have 16 char available. Is this assumption correct?


    What is sin6_flowinfo and sin6_scope_id and how should I define them? I did not see them in the header file.


    Thanks.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well you could read this through.
    Beej's Guide to Network Programming
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 07-07-2017, 08:17 AM
  2. Error when someone enter non numeric values.
    By Austin Cutrona in forum C++ Programming
    Replies: 1
    Last Post: 02-06-2017, 01:32 PM
  3. problem with ipv6 sockets on bind
    By mushy in forum C Programming
    Replies: 1
    Last Post: 10-06-2010, 11:56 AM
  4. Replies: 10
    Last Post: 06-10-2010, 09:52 PM
  5. How to define a polymorphic function for numeric values
    By Adrian20XX in forum C++ Programming
    Replies: 5
    Last Post: 02-22-2009, 02:00 PM

Tags for this Thread