2 .\" This file and its contents are supplied under the terms of the
3 .\" Common Development and Distribution License ("CDDL"), version 1.0.
4 .\" You may only use this file in accordance with the terms of version
7 .\" A full copy of the text of the CDDL should have accompanied this
8 .\" source. A copy of the CDDL is also available via the Internet at
9 .\" http://www.illumos.org/license/CDDL.
12 .\" Copyright 2015, Joyent, Inc.
23 .Nm sockaddr_storage ,
25 .Nd Socket Address Structures
35 .Sy struct sockaddr_dl
41 .Sy struct sockaddr_in
47 .Sy struct sockaddr_in6
52 .Sy struct sockaddr_ll
57 .Sy struct sockaddr_storage
62 .Sy struct sockaddr_un
67 family of structures are designed to represent network addresses for
68 different networking protocols.
71 is a generic structure that is used across calls to various socket
80 Applications do not use the
82 directly, but instead cast the appropriate networking family specific
85 .Sy struct sockaddr * .
87 Every structure in the
89 family begins with a member of the same type, the
91 though the different structures all have different names for the member.
92 For example, the structure
94 has the following members defined:
95 .Bd -literal -offset indent
96 sa_family_t sa_family /* address family */
97 char sa_data[] /* socket address (variable-length data) */
102 corresponds to the socket family that's actually in use.
103 The following table describes the mapping between the address family and the
104 corresponding socket structure that's used.
105 Note that both the generic
108 .Sy struct sockaddr_storage
109 are not included, because these are both generic structures.
111 .Sy struct sockaddr_storage
112 can be found in the next section.
113 .Bl -column -offset indent ".Sy Socket Structure" ".Sy Address Family"
114 .It Sy Socket Structure Ta Sy Address Family
115 .It struct sockaddr_dl Ta AF_LINK
116 .It struct sockaddr_in Ta AF_INET
117 .It struct sockaddr_in6 Ta AF_INET6
118 .It struct sockaddr_ll Ta AF_PACKET
119 .It struct sockaddr_un Ta AF_UNIX
121 .Ss struct sockaddr_storage
126 that is not associated with an address family.
127 Instead, it is large enough to hold the contents of any of the other
130 It can be used to embed sufficient storage for a
132 of any type within a larger structure.
134 The structure only has a single member defined.
135 While there are other members that are used to pad out the size of the
136 .Sy struct sockaddr_storage ,
137 they are not defined and must not be consumed.
138 The only valid member is:
139 .Bd -literal -offset indent
140 sa_family_t ss_family /* address family */
144 .Sy struct sockaddr_storage
145 is useful when running a service that accepts traffic over both
149 where it is common to use a single socket for both address families.
150 In that case, rather than guessing whether a
151 .Sy struct sockaddr_in
153 .Sy struct sockaddr_in6
154 is more appropriate, one can simply use a
155 .Sy struct sockaddr_storage
156 and cast to the appropriate family-specific structure type based on the
159 .Ss struct sockaddr_in
162 is the socket type which is used for for the Internet Protocol version
164 It has the following members defined:
165 .Bd -literal -offset indent
166 sa_family_t sin_family /* address family */
167 in_port_t sin_port /* IP port */
168 struct in_addr sin_addr /* IP address */
173 must always have the value
181 describe the IP address and IP port to use.
182 In the case of a call to
184 these represent the remote IP address and port to which the connection
188 these represent the IP address and port on the local host to which the socket
192 these represent the remote IP address and port of the machine whose
193 connection was accepted.
198 .Sy Network Byte Order .
199 On many systems, this differs from the native host byte order.
200 Applications should read from the member with the function
202 and write to the member with the function
206 is the four byte IPv4 address.
207 It is also stored in network byte order.
208 The common way to write out the address is to use the function
209 .Xr inet_pton 3SOCKET
210 which converts between a human readable IP address such as "10.1.2.3"
211 and the corresponding representation.
213 Example 1 shows how to prepare an IPv4 socket and deal with
219 for more information on IPv4, socket options, etc.
220 .Ss struct sockaddr_in6
225 for the Internet Protocol version six (IPv6).
227 .Sy struct sockaddr_in ,
229 .Sy struct sockaddr_in6
230 has additional members beyond those shown here which are required to be
231 initialized to zero through a function such as
236 .Sy struct sockaddr_in6
237 is not zeroed before use, applications will experience undefined behavior.
239 .Sy struct sockaddr_in6
240 has the following public members:
241 .Bd -literal -offset indent
242 sa_family_t sin6_family /* address family */
243 in_port_t sin6_port /* IPv6 port */
244 struct in6_addr sin6_addr /* IPv6 address */
245 uint32_t sin6_flowinfo; /* traffic class and flow info */
246 uint32_t sin6_scope_id; /* interface scope */
251 must always have the value
257 are the IPv6 equivalents of the
258 .Sy struct sockaddr_in
262 Like their IPv4 counterparts, both of these members must be in network
266 describes the IPv6 port and should be manipulated with the functions
272 describes the 16-byte IPv6 address.
273 In addition to the function
274 .Xr inet_pton 3SOCKET ,
277 defines many macros for manipulating and testing IPv6 addresses.
281 contains the traffic class and flow label associated with the IPv6
285 may contain an identifier which varies based on the scope of the address
288 Applications do not need to initialize
290 it will be populated by the operating system as a result of various library
293 Example 2 shows how to prepare an IPv6 socket.
294 For more information on
299 .Ss struct sockaddr_un
302 structure specifies the address of a socket used to communicate between
303 processes running on a single system, commonly known as a
304 .Em UNIX domain socket .
305 Sockets of this type are identified by a path in the file system.
307 .Sy struct sockaddr_un
308 has the following members:
309 .Bd -literal -offset indent
310 sa_family_t sun_family /* address family */
311 char sun_path[108] /* path name */
316 must always have the value
322 terminated array of characters that specify a file system path.
323 The maximum length of any such path, including the
325 terminator, is 108 bytes.
326 .Ss struct sockaddr_dl
329 structure is used to describe a layer 2 link-level address.
330 This is used as part of various socket ioctls, such as those for
332 The structure has the following members:
333 .Bd -literal -offset indent
334 ushort_t sdl_family; /* address family */
335 ushort_t sdl_index; /* if != 0, system interface index */
336 uchar_t sdl_type; /* interface type */
337 uchar_t sdl_nlen; /* interface name length */
338 uchar_t sdl_alen; /* link level address length */
339 uchar_t sdl_slen; /* link layer selector length */
340 char sdl_data[244]; /* contains both if name and ll address
345 must always have the value
349 is non-zero this refers to the interface identifier that corresponds to
351 .Sy struct sockaddr_dl .
352 This identifier is the same identifier that's shown by tools like
354 and used in the SIOC* set of socket ioctls.
357 refers to the media that is used for the socket.
358 The most common case is that the medium for the interface is Ethernet which has
361 The full set of types is derived from RFC1573 and recorded in the file
365 describes the length of a selector, if it exists, for the specified
367 This is used in protocols such as Trill.
374 members together describe a character string containing the interface name and
375 the link-layer network address.
376 The name starts at the beginning of
380 The name of the interface occupies the next
385 The link-layer network address begins immediately after the interface name,
390 .Sy LLADDR(struct sockaddr_dl *)
391 returns the start of the link-layer network address.
392 The interpretation of the link-layer address depends on the value of
394 For example, if the type is
396 then the address is expressed as a 6-byte MAC address.
397 .Ss struct sockaddr_ll
400 is used as part of a socket type which is responsible for packet
404 It is generally designed for use with Ethernet networks.
406 .Sy struct sockaddr_ll
408 .Bd -literal -offset indent
409 uint16_t sll_family; /* address family */
410 uint16_t sll_protocol; /* link layer protocol */
411 int32_t sll_ifindex; /* interface index */
412 uint16_t sll_hatype; /* ARP hardware type */
413 uint8_t sll_pkttype; /* packet type */
414 uint8_t sll_halen; /* hardware address length */
415 uint8_t sll_addr[8]; /* hardware type */
424 refers to a link-layer protocol.
425 For example, when capturing Ethernet frames the value of
428 This member is written in network byte order and applications should use
432 to read and write the member.
436 is the interface's index.
437 It is used as an identifier in various ioctls and included in the output of
441 it should be filled in with the index that corresponds to the interface
442 for which packets should be captured on.
446 describes the type of the packet based on a list of types in the header
448 .In netpacket/packet.h .
450 .Sy PACKET_OUTGOING ,
451 a packet that was leaving the host and has been looped back for packet capture;
453 a packet that was destined for this host;
454 .Sy PACKET_BROADCAST ,
455 a packet that was broadcast across the link-layer;
456 .Sy PACKET_MULTICAST ,
457 a packet that was sent to a link-layer multicast address; and
458 .Sy PACKET_OTHERHOST ,
459 a packet that was captured only because the device in question was in
464 contains the hardware type as defined by
466 The list of types can be found in
470 contains the length, in bytes, of the hardware address, while the member
472 contains the actual address in network byte order.
477 to connect to a remote host
479 The following example shows how one would open a socket and prepare it
480 to connect to the remote host whose address is the IP address 127.0.0.1
482 This program should be compiled with the C compiler
484 and linked against the libraries libsocket and libnsl.
485 If this example was named ip4.c, then the full link line would be
486 .Ic cc ip4.c -lsocket -lnsl .
488 #include <sys/types.h>
489 #include <sys/socket.h>
491 #include <netinet/in.h>
492 #include <inttypes.h>
499 struct sockaddr_in in;
501 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
506 bzero(&in, sizeof (struct sockaddr_in));
507 in.sin_family = AF_INET;
508 in.sin_port = htons(80);
509 if (inet_pton(AF_INET, "127.0.0.1", &in.sin_addr) != 1) {
514 if (connect(sock, (struct sockaddr *)&in,
515 sizeof (struct sockaddr_in)) != 0) {
529 to bind to a local address
531 The following example shows how one would open a socket and prepare it
532 to bind to the local IPv6 address ::1 port on port 12345.
533 This program should be compiled with the C compiler
535 and linked against the libraries libsocket and libnsl.
536 If this example was named ip6.c, then the full compiler line would be
537 .Ic cc ip6.c -lsocket -lnsl .
539 #include <sys/types.h>
540 #include <sys/socket.h>
542 #include <netinet/in.h>
543 #include <inttypes.h>
550 struct sockaddr_in6 in6;
552 if ((sock6 = socket(AF_INET6, SOCK_STREAM, 0)) < 0) {
557 bzero(&in6, sizeof (struct sockaddr_in6));
558 in6.sin6_family = AF_INET6;
559 in6.sin6_port = htons(12345);
560 if (inet_pton(AF_INET6, "::1", &in6.sin6_addr) != 1) {
565 if (bind(sock6, (struct sockaddr *)&in6,
566 sizeof (struct sockaddr_in6)) != 0) {
571 /* use server socket */
581 .Xr connect 3SOCKET ,