Print this page
9704 move socket functions to libc
   1 SOCKADDR(3SOCKET)          Sockets Library Functions         SOCKADDR(3SOCKET)
   2 
   3 NAME
   4      sockaddr, sockaddr_dl, sockaddr_in, sockaddr_in6, sockaddr_ll,
   5      sockaddr_storage, sockaddr_un - Socket Address Structures
   6 



   7 SYNOPSIS
   8      #include <sys/socket.h>
   9 
  10      struct sockaddr sock;
  11 
  12      #include <sys/socket.h>
  13      #include <net/if_dl.h>
  14 
  15      struct sockaddr_dl dl_sock;
  16 
  17      #include <sys/socket.h>
  18      #include <netinet/in.h>
  19 
  20      struct sockaddr_in in_sock;
  21 
  22      #include <sys/socket.h>
  23      #include <netinet/in.h>
  24 
  25      struct sockaddr_in6 in6_sock;
  26 
  27      #include <sys/socket.h>
  28 
  29      struct sockaddr_ll ll_sock;
  30 
  31      #include <sys/socket.h>
  32 
  33      struct sockaddr_storage storage_sock;
  34 
  35      #include <sys/un.h>
  36 
  37      struct sockaddr_un un_sock;
  38 
  39 DESCRIPTION
  40      The sockaddr family of structures are designed to represent network
  41      addresses for different networking protocols.  The structure struct
  42      sockaddr is a generic structure that is used across calls to various
  43      socket library routines (libsocket(3LIB)) such as accept(3SOCKET) and
  44      bind(3SOCKET).  Applications do not use the struct sockaddr directly, but
  45      instead cast the appropriate networking family specific sockaddr
  46      structure to a struct sockaddr *.
  47 
  48      Every structure in the sockaddr family begins with a member of the same
  49      type, the sa_family_t, though the different structures all have different
  50      names for the member.  For example, the structure struct sockaddr has the
  51      following members defined:
  52 
  53            sa_family_t     sa_family       /* address family */
  54            char            sa_data[]       /* socket address (variable-length data) */
  55 
  56      The member sa_family corresponds to the socket family that's actually in
  57      use.  The following table describes the mapping between the address
  58      family and the corresponding socket structure that's used.  Note that
  59      both the generic struct sockaddr and the struct sockaddr_storage are not
  60      included, because these are both generic structures.  More on the struct
  61      sockaddr_storage can be found in the next section.
  62 
  63            Socket Structure        Address Family
  64            struct sockaddr_dl      AF_LINK
  65            struct sockaddr_in      AF_INET
  66            struct sockaddr_in6     AF_INET6


  78      they are not defined and must not be consumed.  The only valid member is:
  79 
  80            sa_family_t     ss_family       /* address family */
  81 
  82      For example, struct sockaddr_storage is useful when running a service
  83      that accepts traffic over both IPv4 and IPv6 where it is common to use a
  84      single socket for both address families.  In that case, rather than
  85      guessing whether a struct sockaddr_in or a struct sockaddr_in6 is more
  86      appropriate, one can simply use a struct sockaddr_storage and cast to the
  87      appropriate family-specific structure type based on the value of the
  88      member ss_family.
  89 
  90    struct sockaddr_in
  91      The sockaddr_in is the socket type which is used for for the Internet
  92      Protocol version four (IPv4).  It has the following members defined:
  93 
  94            sa_family_t     sin_family      /* address family */
  95            in_port_t       sin_port        /* IP port */
  96            struct in_addr  sin_addr        /* IP address */
  97 
  98      The member sin_family must always have the value AF_INET for IPv4.  The
  99      members sin_port and sin_addr describe the IP address and IP port to use.
 100      In the case of a call to connect(3SOCKET) these represent the remote IP
 101      address and port to which the connection is being made.  In the case of
 102      bind(3SOCKET) these represent the IP address and port on the local host
 103      to which the socket is to be bound.  In the case of accept(3SOCKET) these
 104      represent the remote IP address and port of the machine whose connection
 105      was accepted.
 106 
 107      The member sin_port is always stored in Network Byte Order.  On many
 108      systems, this differs from the native host byte order.  Applications
 109      should read from the member with the function ntohs(3SOCKET) and write to
 110      the member with the function htons(3SOCKET).  The member sin_addr is the
 111      four byte IPv4 address.  It is also stored in network byte order.  The
 112      common way to write out the address is to use the function inet_pton(3C)
 113      which converts between a human readable IP address such as "10.1.2.3" and
 114      the corresponding representation.
 115 
 116      Example 1 shows how to prepare an IPv4 socket and deal with network byte-
 117      order.  See inet(7P) and ip(7P) for more information on IPv4, socket
 118      options, etc.
 119 
 120    struct sockaddr_in6
 121      The sockaddr_in6 structure is the sockaddr for the Internet Protocol
 122      version six (IPv6).  Unlike the struct sockaddr_in, the struct
 123      sockaddr_in6 has additional members beyond those shown here which are
 124      required to be initialized to zero through a function such as bzero(3C)
 125      or memset(3C).  If the entire struct sockaddr_in6 is not zeroed before
 126      use, applications will experience undefined behavior.  The struct
 127      sockaddr_in6 has the following public members:
 128 
 129            sa_family_t     sin6_family     /* address family */
 130            in_port_t       sin6_port       /* IPv6 port */
 131            struct in6_addr sin6_addr       /* IPv6 address */
 132            uint32_t        sin6_flowinfo;  /* traffic class and flow info */
 133            uint32_t        sin6_scope_id;  /* interface scope */
 134 
 135      The member sin6_family must always have the value AF_INET6.  The members
 136      sin6_port and sin6_addr are the IPv6 equivalents of the struct
 137      sockaddr_in sin_port and sin_addr.  Like their IPv4 counterparts, both of
 138      these members must be in network byte order.  The member sin6_port
 139      describes the IPv6 port and should be manipulated with the functions
 140      ntohs(3SOCKET) and htons(3SOCKET).  The member sin6_addr describes the
 141      16-byte IPv6 address.  In addition to the function inet_pton(3C), the
 142      header file <netinet/in.h>   defines many macros for manipulating and
 143      testing IPv6 addresses.
 144 
 145      The member sin6_flowinfo contains the traffic class and flow label
 146      associated with the IPv6 header.  The member sin6_scope_id may contain an
 147      identifier which varies based on the scope of the address in sin6_addr.
 148      Applications do not need to initialize sin6_scope_id; it will be
 149      populated by the operating system as a result of various library calls.
 150 
 151      Example 2 shows how to prepare an IPv6 socket.  For more information on
 152      IPv6, please see inet6(7P) and ip6(7P).
 153 
 154    struct sockaddr_un
 155      The sockaddr_un structure specifies the address of a socket used to
 156      communicate between processes running on a single system, commonly known
 157      as a UNIX domain socket.  Sockets of this type are identified by a path
 158      in the file system.  The struct sockaddr_un has the following members:
 159 
 160            sa_family_t     sun_family      /* address family */
 161            char            sun_path[108]   /* path name */
 162 
 163      The member sun_family must always have the value AF_UNIX.  The member


 172 
 173            ushort_t        sdl_family;     /* address family */
 174            ushort_t        sdl_index;      /* if != 0, system interface index */
 175            uchar_t         sdl_type;       /* interface type */
 176            uchar_t         sdl_nlen;       /* interface name length */
 177            uchar_t         sdl_alen;       /* link level address length */
 178            uchar_t         sdl_slen;       /* link layer selector length */
 179            char            sdl_data[244];  /* contains both if name and ll address
 180 
 181      The member sdl_family must always have the value AF_LINK.  When the
 182      member sdl_index is non-zero this refers to the interface identifier that
 183      corresponds to the struct sockaddr_dl.  This identifier is the same
 184      identifier that's shown by tools like ifconfig(1M) and used in the SIOC*
 185      set of socket ioctls.  The member sdl_type refers to the media that is
 186      used for the socket.  The most common case is that the medium for the
 187      interface is Ethernet which has the value IFT_ETHER.  The full set of
 188      types is derived from RFC1573 and recorded in the file <net/if_types.h>.
 189      The member sdl_slen describes the length of a selector, if it exists, for
 190      the specified medium.  This is used in protocols such as Trill.
 191 
 192      The sdl_data, sdl_nlen and sdl_alen members together describe a character
 193      string containing the interface name and the link-layer network address.
 194      The name starts at the beginning of sdl_data, i.e. at sdl_data[0].  The
 195      name of the interface occupies the next sdl_nlen bytes and is not NUL
 196      terminated.  The link-layer network address begins immediately after the
 197      interface name, and is sdl_alen bytes long.  The macro LLADDR(struct
 198      sockaddr_dl *) returns the start of the link-layer network address.  The
 199      interpretation of the link-layer address depends on the value of
 200      sdl_type.  For example, if the type is IFT_ETHER then the address is
 201      expressed as a 6-byte MAC address.
 202 
 203    struct sockaddr_ll
 204      The sockaddr_ll is used as part of a socket type which is responsible for
 205      packet capture: AF_PACKET sockets.  It is generally designed for use with
 206      Ethernet networks.  The members of the struct sockaddr_ll are:
 207 
 208            uint16_t        sll_family;     /* address family */
 209            uint16_t        sll_protocol;   /* link layer protocol */
 210            int32_t         sll_ifindex;    /* interface index */
 211            uint16_t        sll_hatype;     /* ARP hardware type */
 212            uint8_t         sll_pkttype;    /* packet type */
 213            uint8_t         sll_halen;      /* hardware address length */
 214            uint8_t         sll_addr[8];    /* hardware type */
 215 
 216      The member sll_family must be AF_PACKET.  The member sll_protocol refers
 217      to a link-layer protocol.  For example, when capturing Ethernet frames
 218      the value of sll_protocol is the Ethertype.  This member is written in
 219      network byte order and applications should use htons(3SOCKET) and
 220      ntohs(3SOCKET) to read and write the member.
 221 
 222      The member sll_ifindex is the interface's index.  It is used as an
 223      identifier in various ioctls and included in the output of ifconfig(1M).
 224      When calling bind(3SOCKET) it should be filled in with the index that
 225      corresponds to the interface for which packets should be captured on.
 226 
 227      The member sll_pkttype describes the type of the packet based on a list
 228      of types in the header file <netpacket/packet.h>.    These types include:
 229      PACKET_OUTGOING, a packet that was leaving the host and has been looped
 230      back for packet capture; PACKET_HOST, a packet that was destined for this
 231      host; PACKET_BROADCAST, a packet that was broadcast across the link-
 232      layer; PACKET_MULTICAST, a packet that was sent to a link-layer multicast
 233      address; and PACKET_OTHERHOST, a packet that was captured only because
 234      the device in question was in promiscuous mode.
 235 
 236      The member sll_hatype contains the hardware type as defined by arp(7P).
 237      The list of types can be found in <net/if_arp.h>.    The member sll_halen
 238      contains the length, in bytes, of the hardware address, while the member
 239      sll_addr contains the actual address in network byte order.
 240 
 241 EXAMPLES
 242      Example 1 Preparing an IPv4 sockaddr_in to connect to a remote host



 243 
 244      The following example shows how one would open a socket and prepare it to
 245      connect to the remote host whose address is the IP address 127.0.0.1 on
 246      port 80.  This program should be compiled with the C compiler cc and
 247      linked against the libraries libsocket and libnsl.  If this example was
 248      named ip4.c, then the full link line would be cc ip4.c -lsocket -lnsl.
 249 
 250      #include <sys/types.h>
 251      #include <sys/socket.h>
 252      #include <stdio.h>
 253      #include <netinet/in.h>
 254      #include <inttypes.h>
 255      #include <strings.h>
 256 
 257      int
 258      main(void)
 259      {
 260              int sock;
 261              struct sockaddr_in in;
 262 
 263              if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
 264                      perror("socket");
 265                      return (1);
 266              }
 267 
 268              bzero(&in,     sizeof (struct sockaddr_in));
 269              in.sin_family = AF_INET;
 270              in.sin_port = htons(80);
 271              if (inet_pton(AF_INET, "127.0.0.1", &in.sin_addr) != 1) {
 272                      perror("inet_pton");
 273                      return (1);
 274              }
 275 
 276              if (connect(sock, (struct sockaddr *)&in,
 277                  sizeof (struct sockaddr_in)) != 0) {
 278                      perror("connect");
 279                      return (1);
 280              }
 281 
 282              /* use socket */
 283 
 284              return (0);
 285      }
 286 
 287      Example 2 Preparing an IPv6 sockaddr_in6 to bind to a local address



 288 
 289      The following example shows how one would open a socket and prepare it to
 290      bind to the local IPv6 address ::1 port on port 12345.  This program
 291      should be compiled with the C compiler cc and linked against the
 292      libraries libsocket and libnsl.  If this example was named ip6.c, then
 293      the full compiler line would be cc ip6.c -lsocket -lnsl.
 294 
 295      #include <sys/types.h>
 296      #include <sys/socket.h>
 297      #include <stdio.h>
 298      #include <netinet/in.h>
 299      #include <inttypes.h>
 300      #include <strings.h>
 301 
 302      int
 303      main(void)
 304      {
 305              int sock6;
 306              struct sockaddr_in6 in6;
 307 
 308              if ((sock6 = socket(AF_INET6, SOCK_STREAM, 0)) < 0) {
 309                      perror("socket");
 310                      return (1);
 311              }
 312 
 313              bzero(&in6, sizeof     (struct sockaddr_in6));
 314              in6.sin6_family = AF_INET6;
 315              in6.sin6_port = htons(12345);
 316              if (inet_pton(AF_INET6, "::1", &in6.sin6_addr) != 1) {
 317                      perror("inet_pton");
 318                      return (1);
 319              }
 320 
 321              if (bind(sock6, (struct sockaddr *)&in6,
 322                  sizeof (struct sockaddr_in6)) != 0) {
 323                      perror("bind");
 324                      return (1);
 325              }
 326 
 327              /* use server socket */
 328 
 329              return (0);
 330      }
 331 
 332 SEE ALSO
 333      socket(3HEAD), un.h(3HEAD), accept(3SOCKET), bind(3SOCKET),
 334      connect(3SOCKET), socket(3SOCKET), arp(7P), inet(7P), inet6(7P), ip(7P),
 335      ip6(7P)
 336 
 337 illumos                          April 9, 2016                         illumos
   1 SOCKADDR(3C)             Standard C Library Functions             SOCKADDR(3C)
   2 
   3 NAME
   4      sockaddr, sockaddr_dl, sockaddr_in, sockaddr_in6, sockaddr_ll,
   5      sockaddr_storage, sockaddr_un - Socket Address Structures
   6 
   7 LIBRARY
   8      Standard C Library (libc, -lc)
   9 
  10 SYNOPSIS
  11      #include <sys/socket.h>
  12 
  13      struct sockaddr sock;
  14 
  15      #include <sys/socket.h>
  16      #include <net/if_dl.h>
  17 
  18      struct sockaddr_dl dl_sock;
  19 
  20      #include <sys/socket.h>
  21      #include <netinet/in.h>
  22 
  23      struct sockaddr_in in_sock;
  24 
  25      #include <sys/socket.h>
  26      #include <netinet/in.h>
  27 
  28      struct sockaddr_in6 in6_sock;
  29 
  30      #include <sys/socket.h>
  31 
  32      struct sockaddr_ll ll_sock;
  33 
  34      #include <sys/socket.h>
  35 
  36      struct sockaddr_storage storage_sock;
  37 
  38      #include <sys/un.h>
  39 
  40      struct sockaddr_un un_sock;
  41 
  42 DESCRIPTION
  43      The sockaddr family of structures are designed to represent network
  44      addresses for different networking protocols.  The structure struct
  45      sockaddr is a generic structure that is used across calls to various
  46      socket routines such as accept(3C) and bind(3C).  Applications do not use
  47      the struct sockaddr directly, but instead cast the appropriate networking
  48      family specific sockaddr structure to a struct sockaddr *.

  49 
  50      Every structure in the sockaddr family begins with a member of the same
  51      type, the sa_family_t, though the different structures all have different
  52      names for the member.  For example, the structure struct sockaddr has the
  53      following members defined:
  54 
  55            sa_family_t     sa_family       /* address family */
  56            char            sa_data[]       /* socket address (variable-length data) */
  57 
  58      The member sa_family corresponds to the socket family that's actually in
  59      use.  The following table describes the mapping between the address
  60      family and the corresponding socket structure that's used.  Note that
  61      both the generic struct sockaddr and the struct sockaddr_storage are not
  62      included, because these are both generic structures.  More on the struct
  63      sockaddr_storage can be found in the next section.
  64 
  65            Socket Structure        Address Family
  66            struct sockaddr_dl      AF_LINK
  67            struct sockaddr_in      AF_INET
  68            struct sockaddr_in6     AF_INET6


  80      they are not defined and must not be consumed.  The only valid member is:
  81 
  82            sa_family_t     ss_family       /* address family */
  83 
  84      For example, struct sockaddr_storage is useful when running a service
  85      that accepts traffic over both IPv4 and IPv6 where it is common to use a
  86      single socket for both address families.  In that case, rather than
  87      guessing whether a struct sockaddr_in or a struct sockaddr_in6 is more
  88      appropriate, one can simply use a struct sockaddr_storage and cast to the
  89      appropriate family-specific structure type based on the value of the
  90      member ss_family.
  91 
  92    struct sockaddr_in
  93      The sockaddr_in is the socket type which is used for for the Internet
  94      Protocol version four (IPv4).  It has the following members defined:
  95 
  96            sa_family_t     sin_family      /* address family */
  97            in_port_t       sin_port        /* IP port */
  98            struct in_addr  sin_addr        /* IP address */
  99 
 100      The member sin_family must always have the value AF_INET for IPv4 .  The
 101      members sin_port and sin_addr describe the IP address and IP port to use.
 102      In the case of a call to connect(3C) these represent the remote IP
 103      address and port to which the connection is being made.  In the case of
 104      bind(3C) these represent the IP address and port on the local host to
 105      which the socket is to be bound.  In the case of accept(3C) these
 106      represent the remote IP address and port of the machine whose connection
 107      was accepted.
 108 
 109      The member sin_port is always stored in network byte order.  On many
 110      systems, this differs from the native host byte order.  Applications
 111      should read from the member with the function ntohs(3C) and write to the
 112      member with the function htons(3C).  The member sin_addr is the four byte
 113      IPv4 address.  It is also stored in network byte order.  The common way
 114      to write out the address is to use the function inet_pton(3C) which
 115      converts between a human readable IP address such as `10.1.2.3' and the
 116      corresponding representation.
 117 
 118      Example 1 shows how to prepare an IPv4 socket and deal with network byte-
 119      order.  See inet(7P) and ip(7P) for more information on IPv4, socket
 120      options, etc.
 121 
 122    struct sockaddr_in6
 123      The sockaddr_in6 structure is the sockaddr for the Internet Protocol
 124      version six (IPv6).  Unlike the struct sockaddr_in, the struct
 125      sockaddr_in6 has additional members beyond those shown here which are
 126      required to be initialized to zero through a function such as bzero(3C)
 127      or memset(3C).  If the entire struct sockaddr_in6 is not zeroed before
 128      use, applications will experience undefined behavior.  The struct
 129      sockaddr_in6 has the following public members:
 130 
 131            sa_family_t     sin6_family     /* address family */
 132            in_port_t       sin6_port       /* IPv6 port */
 133            struct in6_addr sin6_addr       /* IPv6 address */
 134            uint32_t        sin6_flowinfo;  /* traffic class and flow info */
 135            uint32_t        sin6_scope_id;  /* interface scope */
 136 
 137      The member sin6_family must always have the value AF_INET6.  The members
 138      sin6_port and sin6_addr are the IPv6 equivalents of the struct
 139      sockaddr_in sin_port and sin_addr.  Like their IPv4 counterparts, both of
 140      these members must be in network byte order.  The member sin6_port
 141      describes the IPv6 port and should be manipulated with the functions
 142      ntohs(3C) and htons(3C).  The member sin6_addr describes the 16-byte IPv6
 143      address.  In addition to the function inet_pton(3C), the header file
 144      <netinet/in.h> defines many macros   for manipulating and testing IPv6
 145      addresses.
 146 
 147      The member sin6_flowinfo contains the traffic class and flow label
 148      associated with the IPv6 header.  The member sin6_scope_id may contain an
 149      identifier which varies based on the scope of the address in sin6_addr.
 150      Applications do not need to initialize sin6_scope_id; it will be
 151      populated by the operating system as a result of various library calls.
 152 
 153      Example 2 shows how to prepare an IPv6 socket.  For more information on
 154      IPv6, please see inet6(7P) and ip6(7P).
 155 
 156    struct sockaddr_un
 157      The sockaddr_un structure specifies the address of a socket used to
 158      communicate between processes running on a single system, commonly known
 159      as a UNIX domain socket.  Sockets of this type are identified by a path
 160      in the file system.  The struct sockaddr_un has the following members:
 161 
 162            sa_family_t     sun_family      /* address family */
 163            char            sun_path[108]   /* path name */
 164 
 165      The member sun_family must always have the value AF_UNIX.  The member


 174 
 175            ushort_t        sdl_family;     /* address family */
 176            ushort_t        sdl_index;      /* if != 0, system interface index */
 177            uchar_t         sdl_type;       /* interface type */
 178            uchar_t         sdl_nlen;       /* interface name length */
 179            uchar_t         sdl_alen;       /* link level address length */
 180            uchar_t         sdl_slen;       /* link layer selector length */
 181            char            sdl_data[244];  /* contains both if name and ll address
 182 
 183      The member sdl_family must always have the value AF_LINK.  When the
 184      member sdl_index is non-zero this refers to the interface identifier that
 185      corresponds to the struct sockaddr_dl.  This identifier is the same
 186      identifier that's shown by tools like ifconfig(1M) and used in the SIOC*
 187      set of socket ioctls.  The member sdl_type refers to the media that is
 188      used for the socket.  The most common case is that the medium for the
 189      interface is Ethernet which has the value IFT_ETHER.  The full set of
 190      types is derived from RFC1573 and recorded in the file <net/if_types.h>.
 191      The member sdl_slen describes the length of a selector, if it exists, for
 192      the specified medium.  This is used in protocols such as Trill.
 193 
 194      The sdl_data, sdl_nlen, and sdl_alen members together describe a
 195      character string containing the interface name and the link-layer network
 196      address.  The name starts at the beginning of sdl_data, i.e. at
 197      sdl_data[0].  The name of the interface occupies the next sdl_nlen bytes
 198      and is not NUL terminated.  The link-layer network address begins
 199      immediately after the interface name, and is sdl_alen bytes long.  The
 200      macro LLADDR(struct sockaddr_dl *) returns the start of the link-layer
 201      network address.  The interpretation of the link-layer address depends on
 202      the value of sdl_type.  For example, if the type is IFT_ETHER then the
 203      address is expressed as a 6-byte MAC address.
 204 
 205    struct sockaddr_ll
 206      The sockaddr_ll is used as part of a socket type which is responsible for
 207      packet capture: AF_PACKET sockets.  It is generally designed for use with
 208      Ethernet networks.  The members of the struct sockaddr_ll are:
 209 
 210            uint16_t        sll_family;     /* address family */
 211            uint16_t        sll_protocol;   /* link layer protocol */
 212            int32_t         sll_ifindex;    /* interface index */
 213            uint16_t        sll_hatype;     /* ARP hardware type */
 214            uint8_t         sll_pkttype;    /* packet type */
 215            uint8_t         sll_halen;      /* hardware address length */
 216            uint8_t         sll_addr[8];    /* hardware type */
 217 
 218      The member sll_family must be AF_PACKET.  The member sll_protocol refers
 219      to a link-layer protocol.  For example, when capturing Ethernet frames
 220      the value of sll_protocol is the Ethertype.  This member is written in
 221      network byte order and applications should use htons(3C) and ntohs(3C) to
 222      read and write the member.
 223 
 224      The member sll_ifindex is the interface's index.  It is used as an
 225      identifier in various ioctls and included in the output of ifconfig(1M).
 226      When calling bind(3C) it should be filled in with the index that
 227      corresponds to the interface for which packets should be captured on.
 228 
 229      The member sll_pkttype describes the type of the packet based on a list
 230      of types in the header file <netpacket/packet.h>.    These types include:
 231      PACKET_OUTGOING, a packet that was leaving the host and has been looped
 232      back for packet capture; PACKET_HOST, a packet that was destined for this
 233      host; PACKET_BROADCAST, a packet that was broadcast across the link-
 234      layer; PACKET_MULTICAST, a packet that was sent to a link-layer multicast
 235      address; and PACKET_OTHERHOST, a packet that was captured only because
 236      the device in question was in promiscuous mode.
 237 
 238      The member sll_hatype contains the hardware type as defined by arp(7P).
 239      The list of types can be found in <net/if_arp.h>.    The member sll_halen
 240      contains the length, in bytes, of the hardware address, while the member
 241      sll_addr contains the actual address in network byte order.
 242 
 243 EXAMPLES
 244      Example 1 Preparing an IPv4 sockaddr_in to connect to a remote host
 245              The following example shows how one would open a socket and
 246              prepare it to connect to the remote host whose address is the IP
 247              address 127.0.0.1 on port 80.
 248 






 249              #include <sys/types.h>
 250              #include <sys/socket.h>
 251              #include <stdio.h>
 252              #include <netinet/in.h>
 253              #include <inttypes.h>
 254              #include <strings.h>
 255 
 256              int
 257              main(void)
 258              {
 259                      int sock;
 260                      struct sockaddr_in in;
 261 
 262                      if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
 263                              perror("socket");
 264                              return (1);
 265                      }
 266 
 267                      bzero(&in,     sizeof (struct sockaddr_in));
 268                      in.sin_family = AF_INET;
 269                      in.sin_port = htons(80);
 270                      if (inet_pton(AF_INET, "127.0.0.1", &in.sin_addr) != 1) {
 271                              perror("inet_pton");
 272                              return (1);
 273                      }
 274 
 275                      if (connect(sock, (struct sockaddr *)&in,
 276                          sizeof (struct sockaddr_in)) != 0) {
 277                              perror("connect");
 278                              return (1);
 279                      }
 280 
 281                      /* use socket */
 282 
 283                      return (0);
 284              }
 285 
 286      Example 2 Preparing an IPv6 sockaddr_in6 to bind to a local address
 287              The following example shows how one would open a socket and
 288              prepare it to bind to the local IPv6 address ::1 port on port
 289              12345.
 290 






 291              #include <sys/types.h>
 292              #include <sys/socket.h>
 293              #include <stdio.h>
 294              #include <netinet/in.h>
 295              #include <inttypes.h>
 296              #include <strings.h>
 297 
 298              int
 299              main(void)
 300              {
 301                      int sock6;
 302                      struct sockaddr_in6 in6;
 303 
 304                      if ((sock6 = socket(AF_INET6, SOCK_STREAM, 0)) < 0) {
 305                              perror("socket");
 306                              return (1);
 307                      }
 308 
 309                      bzero(&in6, sizeof     (struct sockaddr_in6));
 310                      in6.sin6_family = AF_INET6;
 311                      in6.sin6_port = htons(12345);
 312                      if (inet_pton(AF_INET6, "::1", &in6.sin6_addr) != 1) {
 313                              perror("inet_pton");
 314                              return (1);
 315                      }
 316 
 317                      if (bind(sock6, (struct sockaddr *)&in6,
 318                          sizeof (struct sockaddr_in6)) != 0) {
 319                              perror("bind");
 320                              return (1);
 321                      }
 322 
 323                      /* use server socket */
 324 
 325                      return (0);
 326              }
 327 
 328 SEE ALSO
 329      accept(3C), bind(3C), connect(3C), socket(3C), socket.h(3HEAD),
 330      un.h(3HEAD), arp(7P), inet(7P), inet6(7P), ip(7P), ip6(7P)

 331 
 332 illumos                         August 2, 2018                         illumos