xref: /titanic_41/usr/src/man/man3socket/sockaddr.3socket (revision 31195b0801e768e4d679dec8083b07f95a019684)
1.\"
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
5.\" 1.0 of the CDDL.
6.\"
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.
10.\"
11.\"
12.\" Copyright (c) 2014, Joyent, Inc.
13.\"
14.Dd Nov 13, 2014
15.Dt SOCKADDR 3SOCKET
16.Os
17.Sh NAME
18.Nm sockaddr ,
19.Nm sockaddr_dl ,
20.Nm sockaddr_in ,
21.Nm sockaddr_in6 ,
22.Nm sockaddr_ll ,
23.Nm sockaddr_storage ,
24.Nm sockaddr_un
25.Nd Socket Address Structures
26.Sh SYNOPSIS
27.In sys/socket.h
28.Lp
29.Sy struct sockaddr
30.Em sock ;
31.Lp
32.In sys/socket.h
33.In net/if_dl.h
34.Lp
35.Sy struct sockaddr_dl
36.Em dl_sock ;
37.Lp
38.In sys/socket.h
39.In netinet/in.h
40.Lp
41.Sy struct sockaddr_in
42.Em in_sock ;
43.Lp
44.In sys/socket.h
45.In netinet/in6.h
46.Lp
47.Sy struct sockaddr_in6
48.Em in6_sock ;
49.Lp
50.In sys/socket.h
51.Lp
52.Sy struct sockaddr_ll
53.Em ll_sock ;
54.Lp
55.In sys/socket.h
56.Lp
57.Sy struct sockaddr_storage
58.Em storage_sock ;
59.Lp
60.In sys/un.h
61.Lp
62.Sy struct sockaddr_un
63.Em un_sock ;
64.Sh DESCRIPTION
65The
66.Nm
67family of structures are designed to represent network addresses for
68different networking protocols. The structure
69.Sy struct sockaddr
70is a generic structure that is used across calls to various socket
71library routines
72.Po
73.Xr libsocket 3LIB
74.Pc
75such as
76.Xr accept 3SOCKET
77and
78.Xr bind 3SOCKET .
79Applications do not use the
80.Sy struct sockaddr
81directly, but instead cast the appropriate networking family specific
82.Nm
83structure to a
84.Sy struct sockaddr * .
85.Lp
86Every structure in the
87.Nm
88family begins with a member of the same type, the
89.Sy sa_family_t ,
90though the different structures all have different names for the member.
91For example, the structure
92.Sy struct sockaddr
93has the following members defined:
94.Bd -literal -offset indent
95sa_family_t	sa_family	/* address family */
96char		sa_data[]	/* socket address (variable-length data) */
97.Ed
98.Lp
99The member
100.Em sa_family
101corresponds to the socket family that's actually in use. The following
102table describes the mapping between the address family and the
103corresponding socket structure that's used. Note that both the generic
104.Sy struct sockaddr
105and the
106.Sy struct sockaddr_storage
107are not included, because these are both generic structures. More on the
108.Sy struct sockaddr_storage
109can be found in the next section.
110.Bl -column -offset indent ".Sy Socket Structure" ".Sy Address Family"
111.It Sy Socket Structure Ta Sy Address Family
112.It struct sockaddr_dl Ta AF_LINK
113.It struct sockaddr_in Ta AF_INET
114.It struct sockaddr_in6 Ta AF_INET6
115.It struct sockaddr_ll Ta AF_PACKET
116.It struct sockaddr_un Ta AF_UNIX
117.El
118.Ss struct sockaddr_storage
119The
120.Sy sockaddr_storage
121structure is a
122.Nm
123that is not associated with an address family. Instead, it is large
124enough to hold the contents of any of the other
125.Nm
126structures. It can be used to embed sufficient storage for a
127.Sy sockaddr
128of any type within a larger structure.
129.Lp
130The structure only has a single member defined. While there are other
131members that are used to pad out the size of the
132.Sy struct sockaddr_storage ,
133they are not defined and must not be consumed. The only valid
134member is:
135.Bd -literal -offset indent
136sa_family_t	ss_family	/* address family */
137.Ed
138.Lp
139For example,
140.Sy struct sockaddr_storage
141is useful when running a service that accepts traffic over both
142.Sy IPv4
143and
144.Sy IPv6
145where it is common to use a single socket for both address families. In that
146case, rather than guessing whether a
147.Sy struct sockaddr_in
148or a
149.Sy struct sockaddr_in6
150is more appropriate, one can simply use a
151.Sy struct sockaddr_storage
152and cast to the appropriate family-specific structure type based on the
153value of the member
154.Em ss_family .
155.Ss struct sockaddr_in
156The
157.Sy sockaddr_in
158is the socket type which is used for for the Internet Protocol version
159four (IPv4). It has the following members defined:
160.Bd -literal -offset indent
161sa_family_t	sin_family	/* address family */
162in_port_t	sin_port	/* IP port */
163struct in_addr	sin_addr	/* IP address */
164.Ed
165.Lp
166The member
167.Em sin_family
168must always have the value
169.Sy AF_INET
170for
171.Sy IPv4 .
172The members
173.Em sin_port
174and
175.Em sin_addr
176describe the IP address and IP port to use. In the case of a call to
177.Xr connect 3SOCKET
178these represent the remote IP address and port to which the connection
179is being made. In the case of
180.Xr bind 3SOCKET
181these represent the IP address and port on the local host to which the socket
182is to be bound. In the case of
183.Xr accept 3SOCKET
184these represent the remote IP address and port of the machine whose
185connection was accepted.
186.Lp
187The member
188.Em sin_port
189is always stored in
190.Sy Network Byte Order .
191On many systems, this differs from the native host byte order.
192Applications should read from the member with the function
193.Xr ntohs 3SOCKET
194and write to the member with the function
195.Xr htons 3SOCKET .
196The member
197.Em sin_addr
198is the four byte IPv4 address. It is also stored in network byte order.
199The common way to write out the address is to use the function
200.Xr inet_pton 3SOCKET
201which converts between a human readable IP address such as "10.1.2.3"
202and the corresponding representation.
203.Lp
204Example 1 shows how to prepare an IPv4 socket and deal with
205network byte-order. See
206.Xr inet 7P
207and
208.Xr ip 7P
209for more information on IPv4, socket options, etc.
210.Ss struct sockaddr_in6
211The
212.Sy sockaddr_in6
213structure is the
214.Nm
215for the Internet Protocol version six (IPv6). Unlike the
216.Sy struct sockaddr_in ,
217the
218.Sy struct sockaddr_in6
219has additional members beyond those shown here which are required to be
220initialized to zero through a function such as
221.Xr bzero 3C
222or
223.Xr memset 3C .
224If the entire
225.Sy struct sockaddr_in6
226is not zeroed before use, applications will experience undefined behavior. The
227.Sy struct sockaddr_in6
228has the following public members:
229.Bd -literal -offset indent
230sa_family_t	sin6_family	/* address family */
231in_port_t	sin6_port	/* IPv6 port */
232struct in6_addr	sin6_addr	/* IPv6 address */
233uint32_t	sin6_flowinfo;	/* traffic class and flow info */
234uint32_t	sin6_scope_id;	/* interface scope */
235.Ed
236.Lp
237The member
238.Em sin6_family
239must always have the value
240.Sy AF_INET6 .
241The members
242.Em sin6_port
243and
244.Em sin6_addr
245are the IPv6 equivalents of the
246.Sy struct sockaddr_in
247.Em sin_port
248and
249.Em sin_addr .
250Like their IPv4 counterparts, both of these members must be in network
251byte order. The member
252.Em sin6_port
253describes the IPv6 port and should be manipulated with the functions
254.Xr ntohs 3SOCKET
255and
256.Xr htons 3SOCKET .
257The member
258.Em sin6_addr
259describes the 16-byte IPv6 address. In addition to the function
260.Xr inet_pton 3SOCKET ,
261the header file
262.In netinet/in.h
263defines many macros for manipulating and testing IPv6 addresses.
264.Lp
265The member
266.Em sin6_flowinfo
267contains the traffic class and flow label associated with the IPv6
268header. The member
269.Em sin6_scope_id
270may contain an identifier which varies based on the scope of the address
271in
272.Em sin6_addr .
273Applications do not need to initialize
274.Em sin6_scope_id ;
275it will be populated by the operating system as a result of various library
276calls.
277.Lp
278Example 2 shows how to prepare an IPv6 socket. For more information on
279IPv6, please see
280.Xr inet6 7P
281and
282.Xr ip6 7P .
283.Ss struct sockaddr_un
284The
285.Sy sockaddr_un
286structure specifies the address of a socket used to communicate between
287processes running on a single system, commonly known as a
288.Em UNIX domain socket .
289Sockets of this type are identified by a path in the file system. The
290.Sy struct sockaddr_un
291has the following members:
292.Bd -literal -offset indent
293sa_family_t	sun_family	/* address family */
294char		sun_path[108]	/* path name */
295.Ed
296.Lp
297The member
298.Em sun_family
299must always have the value
300.Sy AF_UNIX .
301The member
302.Em sun_path
303is populated with a
304.Sy NUL
305terminated array of characters that specify a file system path. The maximum
306length of any such path, including the
307.Sy NUL
308terminator, is 108 bytes.
309.Ss struct sockaddr_dl
310The
311.Sy sockaddr_dl
312structure is used to describe a layer 2 link-level address. This is used
313as part of various socket ioctls, such as those for
314.Xr arp 7P .
315The structure has the following members:
316.Bd -literal -offset indent
317ushort_t	sdl_family;	/* address family */
318ushort_t	sdl_index;	/* if != 0, system interface index */
319uchar_t		sdl_type;	/* interface type */
320uchar_t		sdl_nlen;	/* interface name length */
321uchar_t		sdl_alen;	/* link level address length */
322uchar_t		sdl_slen;	/* link layer selector length */
323char		sdl_data[244];	/* contains both if name and ll address
324.Ed
325.Lp
326The member
327.Em sdl_family
328must always have the value
329.Sy AF_LINK .
330When the member
331.Em sdl_index
332is non-zero this refers to the interface identifier that corresponds to
333the
334.Sy struct sockaddr_dl .
335This identifier is the same identifier that's shown by tools like
336.Xr ifconfig 1M
337and used in the SIOC* set of socket ioctls. The member
338.Em sdl_type
339refers to the media that is used for the socket. The most common case is
340that the medium for the interface is Ethernet which has the value
341.Sy IFT_ETHER .
342The full set of types is derived from RFC1573 and recorded in the file
343.In net/if_types.h .
344The member
345.Em sdl_slen
346describes the length of a selector, if it exists, for the specified
347medium. This is used in protocols such as Trill.
348.Lp
349The
350.Em sdl_data ,
351.Em sdl_nlen
352and
353.Em sdl_alen
354members together describe a character string containing the interface name and
355the link-layer network address. The name starts at the beginning of
356.Em sdl_data ,
357i.e. at
358.Em sdl_data[0] .
359The name of the interface occupies the next
360.Em sdl_nlen
361bytes and is not
362.Sy NUL
363terminated. The link-layer network address begins immediately after the
364interface name, and is
365.Em sdl_alen
366bytes long. The macro
367.Sy LLADDR(struct sockaddr_dl *)
368returns the start of the link-layer network address.
369The interpretation of the link-layer address depends on the value of
370.Em sdl_type .
371For example, if the type is
372.Sy IFT_ETHER
373then the address is expressed as a 6-byte MAC address.
374.Ss struct sockaddr_ll
375The
376.Sy sockaddr_ll
377is used as part of a socket type which is responsible for packet
378capture:
379.Sy AF_PACKET
380sockets. It is generally designed for use with Ethernet networks. The members
381of the
382.Sy struct sockaddr_ll
383are:
384.Bd -literal -offset indent
385uint16_t        sll_family;	/* address family */
386uint16_t        sll_protocol;	/* link layer protocol */
387int32_t         sll_ifindex;	/* interface index */
388uint16_t        sll_hatype;	/* ARP hardware type */
389uint8_t         sll_pkttype;	/* packet type */
390uint8_t         sll_halen;	/* hardware address length */
391uint8_t         sll_addr[8];	/* hardware type */
392.Ed
393.Lp
394The member
395.Em sll_family
396must be
397.Sy AF_PACKET .
398The member
399.Em sll_protocol
400refers to a link-layer protocol. For example, when capturing Ethernet frames
401the value of
402.Em sll_protocol
403is the Ethertype. This member is written in network byte order and
404applications should use
405.Xr htons 3SOCKET
406and
407.Xr ntohs 3SOCKET
408to read and write the member.
409.Lp
410The member
411.Em sll_ifindex
412is the interface's index. It is used as an identifier in various ioctls
413and included in the output of
414.Xr ifconfig 1M .
415When calling
416.Xr bind 3SOCKET
417it should be filled in with the index that corresponds to the interface
418for which packets should be captured on.
419.Lp
420The member
421.Em sll_pkttype
422describes the type of the packet based on a list of types in the header
423file
424.In netpacket/packet.h .
425These types include:
426.Sy PACKET_OUTGOING ,
427a packet that was leaving the host and has been looped back for packet capture;
428.Sy PACKET_HOST ,
429a packet that was destined for this host;
430.Sy PACKET_BROADCAST ,
431a packet that was broadcast across the link-layer;
432.Sy PACKET_MULTICAST ,
433a packet that was sent to a link-layer multicast address; and
434.Sy PACKET_OTHERHOST ,
435a packet that was captured only because the device in question was in
436promiscuous mode.
437.Lp
438The member
439.Em sll_hatype
440contains the hardware type as defined by
441.Xr arp 7P .
442The list of types can be found in
443.In net/if_arp.h .
444The member
445.Em sll_halen
446contains the length, in bytes, of the hardware address, while the member
447.Em sll_addr
448contains the actual address in network byte order.
449.Sh EXAMPLES
450.Sy Example 1
451Preparing an IPv4
452.Sy sockaddr_in
453to connect to a remote host
454.Lp
455The following example shows how one would open a socket and prepare it
456to connect to the remote host whose address is the IP address 127.0.0.1
457on port 80. This program should be compiled with the C compiler
458.Sy cc
459and linked against the libraries libsocket and libnsl. If this example
460was named ip4.c, then the full link line would be
461.Ic cc ip4.c -lsocket -lnsl .
462.Bd -literal
463#include <sys/types.h>
464#include <sys/socket.h>
465#include <stdio.h>
466#include <netinet/in.h>
467#include <inttypes.h>
468#include <strings.h>
469
470int
471main(void)
472{
473	int sock;
474	struct sockaddr_in in;
475
476	if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
477		perror("socket");
478		return (1);
479	}
480
481	bzero(&in, sizeof (struct sockaddr_in));
482	in.sin_family = AF_INET;
483	in.sin_port = htons(80);
484	if (inet_pton(AF_INET, "127.0.0.1", &in.sin_addr) != 1) {
485		perror("inet_pton");
486		return (1);
487	}
488
489	if (connect(sock, (struct sockaddr *)&in,
490	    sizeof (struct sockaddr_in)) != 0) {
491		perror("connect");
492		return (1);
493	}
494
495	/* use socket */
496
497	return (0);
498}
499.Ed
500.Lp
501.Sy Example 2
502Preparing an IPv6
503.Sy sockaddr_in6
504to bind to a local address
505.Lp
506The following example shows how one would open a socket and prepare it
507to bind to the local IPv6 address ::1 port on port 12345. This program
508should be compiled with the C compiler
509.Sy cc
510and linked aginst the libraries libsocket and libnsl. If this example
511was named ip6.c, then the full compiler line would be
512.Ic cc ip6.c -lsocket -lnsl .
513.Bd -literal
514#include <sys/types.h>
515#include <sys/socket.h>
516#include <stdio.h>
517#include <netinet/in.h>
518#include <inttypes.h>
519#include <strings.h>
520
521int
522main(void)
523{
524	int sock6;
525	struct sockaddr_in6 in6;
526
527	if ((sock6 = socket(AF_INET6, SOCK_STREAM, 0)) < 0) {
528		perror("socket");
529		return (1);
530	}
531
532	bzero(&in6, sizeof (struct sockaddr_in6));
533	in6.sin6_family = AF_INET6;
534	in6.sin6_port = htons(12345);
535	if (inet_pton(AF_INET6, "::1", &in6.sin6_addr) != 1) {
536		perror("inet_pton");
537		return (1);
538	}
539
540	if (bind(sock6, (struct sockaddr *)&in6,
541	    sizeof (struct sockaddr_in6)) != 0) {
542		perror("bind");
543		return (1);
544	}
545
546	/* use server socket */
547
548	return (0);
549}
550.Ed
551.Sh SEE ALSO
552.Xr socket 3HEAD ,
553.Xr uh.h 3HEAD ,
554.Xr accept 3SOCKET ,
555.Xr bind 3SOCKET ,
556.Xr connect 3SOCKET ,
557.Xr socket 3SOCKET ,
558.Xr arp 7P ,
559.Xr inet 7P ,
560.Xr inet6 7P ,
561.Xr ip 7P ,
562.Xr ip6 7P ,
563