1 /* Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
3 /** \file net.c Networking-related helper functions. */
7 #include <netinet/in.h>
10 #include <sys/types.h>
11 #include <sys/socket.h>
21 /* Whether the given address conforms to the IPv4 address format. */
22 static inline bool is_valid_ipv4_address(const char *address)
24 struct in_addr test_it;
25 return inet_pton(AF_INET, address, &test_it) != 0;
29 * Parse and validate IPv4 address/netmask string.
31 * \param cidr Address in CIDR notation
32 * \param addr Copy of the IPv4 address part of \a cidr
33 * \param addrlen Size of \a addr in bytes
34 * \param netmask Value of the netmask part in \a cidr or the
35 * default of 32 if not specified.
37 * \return Pointer to \a addr if successful, NULL on error.
40 char *parse_cidr(const char *cidr,
41 char *addr, ssize_t addrlen,
45 char *c = addr, *end = c + (addrlen - 1);
49 if (cidr == NULL || addrlen < 1)
52 for (o = cidr; (*c = *o == '/'? '\0' : *o); c++, o++)
57 if (para_atoi32(++o, netmask) < 0 ||
58 *netmask < 0 || *netmask > 0x20)
61 if (is_valid_ipv4_address(addr))
70 * Match string as a candidate IPv4 address.
72 * \param address The string to match.
73 * \return True if \a address has "dot-quad" format.
75 static bool is_v4_dot_quad(const char *address)
80 assert(para_regcomp(&r, "^([0-9]+\\.){3}[0-9]+$",
81 REG_EXTENDED | REG_NOSUB) >= 0);
82 result = regexec(&r, address, 0, NULL, 0) == 0;
87 /* Whether a string conforms to IPv6 address format (RFC 4291). */
88 static inline bool is_valid_ipv6_address(const char *address)
90 struct in6_addr test_it;
91 return inet_pton(AF_INET6, address, &test_it) != 0;
95 * Perform basic syntax checking on the host-part of an URL:
97 * - Since ':' is invalid in IPv4 addresses and DNS names, the
98 * presence of ':' causes interpretation as IPv6 address;
99 * - next the first-match-wins algorithm from RFC 3986 is applied;
100 * - else the string is considered as DNS name, to be resolved later.
102 * \param host The host string to check.
103 * \return True if \a host passes the syntax checks.
105 * \sa RFC 3986, 3.2.2; RFC 1123, 2.1; RFC 1034, 3.5.
107 static bool host_string_ok(const char *host)
109 if (host == NULL || *host == '\0')
111 if (strchr(host, ':') != NULL)
112 return is_valid_ipv6_address(host);
113 if (is_v4_dot_quad(host))
114 return is_valid_ipv4_address(host);
119 * Parse and validate URL string.
121 * The URL syntax is loosely based on RFC 3986, supporting one of
122 * - "["host"]"[:port] for native IPv6 addresses and
123 * - host[:port] for IPv4 hostnames and DNS names.
125 * Native IPv6 addresses must be enclosed in square brackets, since
126 * otherwise there is an ambiguity with the port separator `:'.
127 * The 'port' part is always considered to be a number; if absent,
128 * it is set to -1, to indicate that a default port is to be used.
130 * The following are valid examples:
138 * \param url The URL string to take apart.
139 * \param host To return the copied host part of \a url.
140 * \param hostlen The maximum length of \a host.
141 * \param port To return the port number (if any) of \a url.
143 * \return Pointer to \a host, or \p NULL if failed. If \p NULL is returned,
144 * \a host and \a port are undefined. If no port number was present in \a url,
145 * \a port is set to -1.
147 * \sa RFC 3986, 3.2.2/3.2.3.
149 char *parse_url(const char *url,
150 char *host, ssize_t hostlen,
154 char *c = host, *end = c + (hostlen - 1);
158 if (o == NULL || hostlen < 1)
162 for (++o; (*c = *o == ']' ? '\0' : *o); c++, o++)
166 if (*o++ != ']' || (*o != '\0' && *o != ':'))
169 for (; (*c = *o == ':'? '\0' : *o); c++, o++) {
170 if (c == end && o[1])
176 if (para_atoi32(++o, port) < 0 || *port < 0 || *port > 0xffff)
178 if (host_string_ok(host))
186 * Pretty-print a host/port pair.
188 * \param url NULL, or any string accepted by \ref parse_url().
189 * \param default_port Applies if url has no port.
191 * If the url argument is NULL, the function returns the string
192 * 0.0.0.0:default_port. Otherwise it calls \ref parse_url() to check the
193 * syntax of the input string given by url. On errors the string "?" is
194 * returned. Otherwise, if url contains a port, a copy of url is returned. If
195 * no port was supplied, a colon and the default port are appended to url.
197 * \return In all cases the returned string is a allocated with malloc(3) and
198 * has to be freed by the caller.
200 char *format_url(const char *url, int default_port)
202 char host[MAX_HOSTLEN];
206 return make_message("0.0.0.0:%d", default_port);
207 if (!parse_url(url, host, sizeof(host), &url_port))
208 return make_message("?");
210 return make_message("%s:%d", url, default_port);
212 return para_strdup(url);
216 * Stringify port number, resolve into service name where defined.
218 * \param port 2-byte port number, in host-byte-order.
219 * \param transport Transport protocol name (e.g. "udp", "tcp"), or NULL.
220 * \return Pointer to static result buffer.
222 * \sa getservbyport(3), services(5), nsswitch.conf(5).
224 const char *stringify_port(int port, const char *transport)
226 static char service[NI_MAXSERV];
228 if (port < 0 || port > 0xFFFF) {
229 snprintf(service, sizeof(service), "undefined (%d)", port);
231 struct servent *se = getservbyport(htons(port), transport);
234 snprintf(service, sizeof(service), "%d", port);
236 snprintf(service, sizeof(service), "%s", se->s_name);
242 #define SOCK_DCCP 6 /**< Linux socket type. */
246 * Determine the socket type for a given layer-4 protocol.
248 * \param l4type The symbolic name of the transport-layer protocol.
250 * \sa ip(7), socket(2).
252 static inline int sock_type(const unsigned l4type)
255 case IPPROTO_UDP: return SOCK_DGRAM;
256 case IPPROTO_TCP: return SOCK_STREAM;
257 case IPPROTO_DCCP: return SOCK_DCCP;
259 return -1; /* not supported here */
263 * Pretty-print transport-layer name.
265 static const char *layer4_name(const unsigned l4type)
268 case IPPROTO_UDP: return "UDP";
269 case IPPROTO_TCP: return "TCP";
270 case IPPROTO_DCCP: return "DCCP";
272 return "UNKNOWN PROTOCOL";
276 * Flowopts: Transport-layer independent encapsulation of socket options.
278 * These collect individual socket options into a queue, which is disposed of
279 * directly after makesock(). The 'pre_conn_opt' structure is for internal use
280 * only and should not be visible elsewhere.
282 * \sa setsockopt(2), \ref makesock().
284 struct pre_conn_opt {
285 int sock_level; /**< Second argument to setsockopt() */
286 int sock_option; /**< Third argument to setsockopt() */
287 char *opt_name; /**< Stringified \a sock_option */
288 void *opt_val; /**< Fourth argument to setsockopt() */
289 socklen_t opt_len; /**< Fifth argument to setsockopt() */
291 struct list_head node; /**< FIFO, as sockopt order matters. */
295 * List of pre-connection socket options to be set.
297 * This list contains transport-layer independent encapsulation of socket
298 * options that need to be registered prior to setting up a connection.
301 struct list_head sockopts;
305 * Allocate and initialize a flowopt queue.
307 * \return A new structure to be passed to \ref flowopt_add(). It is
308 * automatically deallocated in \ref makesock().
310 struct flowopts *flowopt_new(void)
312 struct flowopts *new = alloc(sizeof(*new));
314 init_list_head(&new->sockopts);
319 * Append new socket option to flowopt queue.
321 * \param fo The flowopt queue to append to.
322 * \param lev Level at which \a opt resides.
323 * \param opt New option to add.
324 * \param name Stringified name of \a opt.
325 * \param val The value to set \a opt to.
326 * \param len Length of \a val.
330 void flowopt_add(struct flowopts *fo, int lev, int opt,
331 const char *name, const void *val, int len)
333 struct pre_conn_opt *new = alloc(sizeof(*new));
335 new->sock_option = opt;
336 new->sock_level = lev;
337 new->opt_name = para_strdup(name);
343 new->opt_val = alloc(len);
345 memcpy(new->opt_val, val, len);
348 list_add_tail(&new->node, &fo->sockopts);
351 /** Set the entire bunch of pre-connection options at once. */
352 static void flowopt_setopts(int sockfd, struct flowopts *fo)
354 struct pre_conn_opt *pc;
359 list_for_each_entry(pc, &fo->sockopts, node)
360 if (setsockopt(sockfd, pc->sock_level, pc->sock_option,
361 pc->opt_val, pc->opt_len) < 0) {
362 PARA_EMERG_LOG("Can not set %s socket option: %s",
363 pc->opt_name, strerror(errno));
369 * Deallocate all resources of a flowopts structure.
371 * \param fo A pointer as returned from flowopt_new().
373 * It's OK to pass \p NULL here in which case the function does nothing.
375 void flowopt_cleanup(struct flowopts *fo)
377 struct pre_conn_opt *cur, *next;
382 list_for_each_entry_safe(cur, next, &fo->sockopts, node) {
391 * Resolve an IPv4/IPv6 address.
393 * \param l4type The layer-4 type (\p IPPROTO_xxx).
394 * \param passive Whether \p AI_PASSIVE should be included as hint.
395 * \param host Remote or local hostname or IPv/6 address string.
396 * \param port_number Used to set the port in each returned address structure.
397 * \param result addrinfo structures are returned here.
399 * The interpretation of \a host depends on the value of \a passive. On a
400 * passive socket host is interpreted as an interface IPv4/6 address (can be
401 * left NULL). On an active socket, \a host is the peer DNS name or IPv4/6
402 * address to connect to.
406 * \sa getaddrinfo(3).
408 int lookup_address(unsigned l4type, bool passive, const char *host,
409 int port_number, struct addrinfo **result)
412 char port[6]; /* port number has at most 5 digits */
413 struct addrinfo *addr = NULL, hints;
416 sprintf(port, "%d", port_number & 0xffff);
417 /* Set up address hint structure */
418 memset(&hints, 0, sizeof(hints));
419 hints.ai_family = AF_UNSPEC;
420 hints.ai_socktype = sock_type(l4type);
422 * getaddrinfo does not support SOCK_DCCP, so for the sake of lookup
423 * (and only then) pretend to be UDP.
425 if (l4type == IPPROTO_DCCP)
426 hints.ai_socktype = SOCK_DGRAM;
427 /* only use addresses available on the host */
428 hints.ai_flags = AI_ADDRCONFIG;
429 if (passive && host == NULL)
430 hints.ai_flags |= AI_PASSIVE;
431 /* Obtain local/remote address information */
432 ret = getaddrinfo(host, port, &hints, &addr);
434 PARA_ERROR_LOG("can not resolve %s address %s#%s: %s\n",
436 host? host : (passive? "[loopback]" : "[localhost]"),
437 port, gai_strerror(ret));
438 return -E_ADDRESS_LOOKUP;
445 * Create an active or passive socket.
447 * \param l4type \p IPPROTO_TCP, \p IPPROTO_UDP, or \p IPPROTO_DCCP.
448 * \param passive Whether to call bind(2) or connect(2).
449 * \param ai Address information as obtained from \ref lookup_address().
450 * \param fo Socket options to be set before making the connection.
452 * bind(2) is called on passive sockets, and connect(2) on active sockets. The
453 * algorithm tries all possible address combinations until it succeeds. If \a
454 * fo is supplied, options are set but cleanup must be performed in the caller.
456 * \return File descriptor on success, \p E_MAKESOCK on errors.
458 * \sa \ref lookup_address(), \ref makesock(), ip(7), ipv6(7), bind(2),
461 int makesock_addrinfo(unsigned l4type, bool passive, struct addrinfo *ai,
464 int ret = -E_MAKESOCK, on = 1;
466 for (; ai; ai = ai->ai_next) {
468 ret = socket(ai->ai_family, sock_type(l4type), l4type);
470 PARA_NOTICE_LOG("socket(): %s\n", strerror(errno));
474 flowopt_setopts(fd, fo);
476 if (connect(fd, ai->ai_addr, ai->ai_addrlen) < 0) {
477 PARA_NOTICE_LOG("connect(): %s\n",
485 * Reuse the address on passive sockets to avoid failure on
486 * restart (protocols using listen()) and when creating
487 * multiple listener instances (UDP multicast).
489 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on,
491 PARA_NOTICE_LOG("setsockopt(): %s\n", strerror(errno));
495 if (bind(fd, ai->ai_addr, ai->ai_addrlen) < 0) {
496 PARA_NOTICE_LOG("bind(): %s\n", strerror(errno));
506 * Resolve IPv4/IPv6 address and create a ready-to-use active or passive socket.
508 * \param l4type The layer-4 type (\p IPPROTO_xxx).
509 * \param passive Whether this is a passive or active socket.
510 * \param host Passed to \ref lookup_address().
511 * \param port_number Passed to \ref lookup_address().
512 * \param fo Passed to \ref makesock_addrinfo().
514 * This creates a ready-made IPv4/v6 socket structure after looking up the
515 * necessary parameters. The function first calls \ref lookup_address() and
516 * passes the address information to makesock_addrinfo() to create and
517 * initialize the socket.
519 * \return The newly created file descriptor on success, a negative error code
522 * \sa \ref lookup_address(), \ref makesock_addrinfo().
524 int makesock(unsigned l4type, bool passive, const char *host, uint16_t port_number,
528 int ret = lookup_address(l4type, passive, host, port_number, &ai);
531 ret = makesock_addrinfo(l4type, passive, ai, fo);
535 PARA_NOTICE_LOG("can not create %s socket %s#%d.\n",
536 layer4_name(l4type), host? host : (passive?
537 "[loopback]" : "[localhost]"), port_number);
543 * Create a passive / listening socket.
545 * \param l4type The transport-layer type (\p IPPROTO_xxx).
546 * \param addr Passed to \ref parse_url() if not NULL.
547 * \param port Ignored if addr contains a port number.
549 * \return Positive integer (socket descriptor) on success, negative value
552 * \sa \ref makesock(), ip(7), ipv6(7), bind(2), listen(2).
554 int para_listen(unsigned l4type, const char *addr, uint16_t port)
556 char host[MAX_HOSTLEN];
557 int ret, fd, addr_port;
560 if (!parse_url(addr, host, sizeof(host), &addr_port))
561 return -ERRNO_TO_PARA_ERROR(EINVAL);
566 fd = makesock(l4type, true /* passive */, addr, port,
567 NULL /* no flowopts */);
569 ret = listen(fd, BACKLOG);
573 return -ERRNO_TO_PARA_ERROR(ret);
575 PARA_INFO_LOG("listening on %s port %u, fd %d\n",
576 layer4_name(l4type), port, fd);
582 * Create a socket which listens on all network addresses.
584 * \param l4type See \ref para_listen().
585 * \param port See \ref para_listen().
587 * This is a simple wrapper for \ref para_listen() which passes a NULL pointer
588 * as the address information.
590 * \return See \ref para_listen().
592 int para_listen_simple(unsigned l4type, uint16_t port)
594 return para_listen(l4type, NULL, port);
598 * Determine IPv4/v6 socket address length.
599 * \param sa Container of IPv4 or IPv6 address.
600 * \return Address-family dependent address length.
602 static socklen_t salen(const struct sockaddr *sa)
604 assert(sa->sa_family == AF_INET || sa->sa_family == AF_INET6);
606 return sa->sa_family == AF_INET6
607 ? sizeof(struct sockaddr_in6)
608 : sizeof(struct sockaddr_in);
611 /** True if @ss holds a v6-mapped-v4 address (RFC 4291, 2.5.5.2) */
612 static bool SS_IS_ADDR_V4MAPPED(const struct sockaddr_storage *ss)
614 const struct sockaddr_in6 *ia6 = (const struct sockaddr_in6 *)ss;
616 return ss->ss_family == AF_INET6 && IN6_IS_ADDR_V4MAPPED(&ia6->sin6_addr);
620 * Process IPv4/v6 address, turn v6-mapped-v4 address into normal IPv4 address.
621 * \param ss Container of IPv4/6 address.
622 * \return Pointer to normalized address (may be static storage).
626 static const struct sockaddr *
627 normalize_ip_address(const struct sockaddr_storage *ss)
629 assert(ss->ss_family == AF_INET || ss->ss_family == AF_INET6);
631 if (SS_IS_ADDR_V4MAPPED(ss)) {
632 const struct sockaddr_in6 *ia6 = (const struct sockaddr_in6 *)ss;
633 static struct sockaddr_in ia;
635 ia.sin_family = AF_INET;
636 ia.sin_port = ia6->sin6_port;
637 memcpy(&ia.sin_addr.s_addr, &(ia6->sin6_addr.s6_addr[12]), 4);
638 return (const struct sockaddr *)&ia;
640 return (const struct sockaddr *)ss;
644 * Generic/fallback MTU values
646 * These are taken from RFC 1122, RFC 2460, and RFC 5405.
647 * - RFC 1122, 3.3.3 defines EMTU_S ("Effective MTU for sending") and recommends
648 * to use an EMTU_S size of of 576 bytes if the IPv4 path MTU is unknown;
649 * - RFC 2460, 5. requires a minimum IPv6 MTU of 1280 bytes;
650 * - RFC 5405, 3.2 recommends that if path MTU discovery is not done,
651 * UDP senders should use the respective minimum values of EMTU_S.
653 static inline int generic_mtu(const int af_type)
655 return af_type == AF_INET6 ? 1280 : 576;
658 /** Crude approximation of IP header overhead - neglecting options. */
659 static inline int estimated_header_overhead(const int af_type)
661 return af_type == AF_INET6 ? 40 : 20;
665 * Get the maximum transport-layer message size (MMS_S).
667 * \param sockfd The socket file descriptor.
669 * The socket must be connected. See RFC 1122, 3.3.3. If the protocol family
670 * could not be determined, \p AF_INET is assumed.
672 * \return The maximum message size of the address family type.
674 int generic_max_transport_msg_size(int sockfd)
676 struct sockaddr_storage ss = {.ss_family = 0};
677 socklen_t sslen = sizeof(ss);
678 int af_type = AF_INET;
680 if (getpeername(sockfd, (struct sockaddr *)&ss, &sslen) < 0) {
681 PARA_ERROR_LOG("can not determine remote address type: %s\n",
683 } else if (!SS_IS_ADDR_V4MAPPED(&ss)) {
684 af_type = ss.ss_family;
686 return generic_mtu(af_type) - estimated_header_overhead(af_type);
690 * Look up the remote side of a connected socket structure.
692 * \param fd The socket descriptor of the connected socket.
694 * \return A static character string identifying hostname and port of the
695 * chosen side in numeric host:port format.
697 * \sa getsockname(2), getpeername(2), \ref parse_url(), getnameinfo(3),
698 * services(5), nsswitch.conf(5).
700 char *remote_name(int fd)
702 struct sockaddr_storage ss = {.ss_family = 0};
703 const struct sockaddr *sa;
704 socklen_t sslen = sizeof(ss);
705 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
706 static char output[sizeof(hbuf) + sizeof(sbuf) + 4];
709 if (getpeername(fd, (struct sockaddr *)&ss, &sslen) < 0) {
710 PARA_ERROR_LOG("can not determine address from fd %d: %s\n",
711 fd, strerror(errno));
712 snprintf(output, sizeof(output), "(unknown)");
715 sa = normalize_ip_address(&ss);
716 ret = getnameinfo(sa, salen(sa), hbuf, sizeof(hbuf), sbuf,
717 sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV);
719 PARA_WARNING_LOG("hostname lookup error (%s).\n",
721 snprintf(output, sizeof(output), "(lookup error)");
722 } else if (sa->sa_family == AF_INET6)
723 snprintf(output, sizeof(output), "[%s]:%s", hbuf, sbuf);
725 snprintf(output, sizeof(output), "%s:%s", hbuf, sbuf);
730 * Extract IPv4 or IPv6-mapped-IPv4 address from sockaddr_storage.
732 * \param ss Container of IPv4/6 address.
733 * \param ia Extracted IPv4 address (different from 0) or 0 if unsuccessful.
737 void extract_v4_addr(const struct sockaddr_storage *ss, struct in_addr *ia)
739 const struct sockaddr *sa = normalize_ip_address(ss);
741 memset(ia, 0, sizeof(*ia));
742 if (sa->sa_family == AF_INET)
743 *ia = ((struct sockaddr_in *)sa)->sin_addr;
747 * Compare the address part of IPv4/6 addresses.
749 * \param sa1 First address.
750 * \param sa2 Second address.
752 * \return True iff the IP address of \a sa1 and \a sa2 match.
754 bool sockaddr_equal(const struct sockaddr *sa1, const struct sockaddr *sa2)
758 if (sa1->sa_family != sa2->sa_family)
760 if (sa1->sa_family == AF_INET) {
761 struct sockaddr_in *a1 = (typeof(a1))sa1,
762 *a2 = (typeof (a2))sa2;
763 return a1->sin_addr.s_addr == a2->sin_addr.s_addr;
764 } else if (sa1->sa_family == AF_INET6) {
765 struct sockaddr_in6 *a1 = (typeof(a1))sa1,
766 *a2 = (typeof (a2))sa2;
767 return !memcmp(a1, a2, sizeof(*a1));
773 * Receive data from a file descriptor.
775 * \param fd The file descriptor.
776 * \param buf The buffer to write the data to.
777 * \param size The size of \a buf.
779 * Receive at most \a size bytes from file descriptor \a fd.
781 * \return The number of bytes received on success, negative on errors, zero if
782 * the peer has performed an orderly shutdown.
786 __must_check int recv_bin_buffer(int fd, char *buf, size_t size)
790 n = recv(fd, buf, size, 0);
792 return -ERRNO_TO_PARA_ERROR(errno);
797 * Receive and write terminating NULL byte.
799 * \param fd The file descriptor.
800 * \param buf The buffer to write the data to.
801 * \param size The size of \a buf.
803 * Read at most \a size - 1 bytes from file descriptor \a fd and
804 * write a NULL byte at the end of the received data.
806 * \return The return value of the underlying call to \a recv_bin_buffer().
808 * \sa \ref recv_bin_buffer()
810 int recv_buffer(int fd, char *buf, size_t size)
815 n = recv_bin_buffer(fd, buf, size - 1);
824 * Wrapper around the accept system call.
826 * \param fd The listening socket.
827 * \param addr Structure which is filled in with the address of the peer socket.
828 * \param size Should contain the size of the structure pointed to by \a addr.
829 * \param new_fd Result pointer.
831 * Accept incoming connections on addr, retry if interrupted.
833 * \return Negative on errors, zero if no connections are present to be accepted,
838 int para_accept(int fd, void *addr, socklen_t size, int *new_fd)
843 ret = accept(fd, (struct sockaddr *) addr, &size);
844 while (ret < 0 && errno == EINTR);
850 if (errno == EAGAIN || errno == EWOULDBLOCK)
852 return -ERRNO_TO_PARA_ERROR(errno);
855 #ifndef DCCP_SOCKOPT_AVAILABLE_CCIDS
856 #define DCCP_SOCKOPT_AVAILABLE_CCIDS 12 /**< List of supported CCIDs. */
860 * Probe the list of DCCP CCIDs configured on this host.
861 * \param ccid_array Pointer to return statically allocated array in.
862 * \return Number of elements returned in \a ccid_array or error.
864 * NB: This feature is only available on Linux > 2.6.30; on older kernels
865 * ENOPROTOOPT ("Protocol not available") will be returned.
867 int dccp_available_ccids(uint8_t **ccid_array)
869 static uint8_t ccids[DCCP_MAX_HOST_CCIDS];
870 socklen_t nccids = sizeof(ccids);
873 ret = fd = makesock(IPPROTO_DCCP, true /* passive */, NULL, 0, NULL);
877 if (getsockopt(fd, SOL_DCCP, DCCP_SOCKOPT_AVAILABLE_CCIDS,
878 ccids, &nccids) < 0) {
881 PARA_ERROR_LOG("No DCCP_SOCKOPT_AVAILABLE_CCIDS: %s\n",
883 return -ERRNO_TO_PARA_ERROR(ret);
892 * The buffer size of the sun_path component of struct sockaddr_un.
894 * While glibc doesn't define UNIX_PATH_MAX, it documents it has being limited
895 * to 108 bytes. On NetBSD it is only 104 bytes though. We trust UNIX_PATH_MAX
896 * if it is defined and use the size of the ->sun_path member otherwise. This
897 * should be safe everywhere.
899 #ifndef UNIX_PATH_MAX
900 #define UNIX_PATH_MAX (sizeof(((struct sockaddr_un *)0)->sun_path))
904 * Prepare a structure for AF_UNIX socket addresses.
906 * This just copies name to the sun_path component of u, prepending a zero byte
907 * if abstract sockets are supported.
909 * The first call to this function tries to bind a socket to the abstract name
910 * space. The result of this test is stored in a static variable. Subsequent
911 * calls read this variable and create abstract sockets on systems that support
912 * them. If a NULL pointer is passed as the name, the function only
913 * initializes the static variable.
915 static int init_unix_addr(struct sockaddr_un *u, const char *name)
917 static int use_abstract;
919 memset(u->sun_path, 0, UNIX_PATH_MAX);
920 u->sun_family = PF_UNIX;
921 if (use_abstract == 0) { /* executed only once */
922 int fd = socket(PF_UNIX, SOCK_STREAM, 0);
923 memcpy(u->sun_path, "\0x\0", 3);
924 if (bind(fd, (struct sockaddr *)u, sizeof(*u)) == 0)
925 use_abstract = 1; /* yes */
927 use_abstract = -1; /* no */
929 PARA_NOTICE_LOG("%susing abstract socket namespace\n",
930 use_abstract == 1? "" : "not ");
934 if (strlen(name) + 1 >= UNIX_PATH_MAX)
935 return -E_NAME_TOO_LONG;
936 strcpy(u->sun_path + (use_abstract == 1? 1 : 0), name);
941 * Create a socket for local communication and listen on it.
943 * \param name The socket pathname.
945 * This function creates a passive local socket for sequenced, reliable,
946 * two-way, connection-based byte streams. The socket file descriptor is set to
947 * nonblocking mode and listen(2) is called to prepare the socket for
948 * accepting incoming connection requests.
950 * \return The file descriptor on success, negative error code on failure.
952 * \sa socket(2), \sa bind(2), \sa chmod(2), listen(2), unix(7).
954 int create_local_socket(const char *name)
956 struct sockaddr_un unix_addr;
959 ret = init_unix_addr(&unix_addr, name);
960 if (ret <= 0) /* error, or name was NULL */
962 ret = socket(PF_UNIX, SOCK_STREAM, 0);
964 return -ERRNO_TO_PARA_ERROR(errno);
966 ret = mark_fd_nonblocking(fd);
969 ret = bind(fd, (struct sockaddr *)&unix_addr, sizeof(unix_addr));
971 ret = -ERRNO_TO_PARA_ERROR(errno);
974 if (unix_addr.sun_path[0] != 0) { /* pathname socket */
975 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP
978 if (chmod(name, mode) < 0)
981 if (listen(fd , 5) < 0) {
982 ret = -ERRNO_TO_PARA_ERROR(errno);
992 * Prepare, create, and connect to a Unix domain socket for local communication.
994 * \param name The socket pathname.
996 * This function creates a local socket for sequenced, reliable, two-way,
997 * connection-based byte streams.
999 * \return The file descriptor of the connected socket on success, negative on
1002 * \sa \ref create_local_socket(), unix(7), connect(2).
1004 int connect_local_socket(const char *name)
1006 struct sockaddr_un unix_addr;
1009 PARA_DEBUG_LOG("connecting to %s\n", name);
1010 fd = socket(PF_UNIX, SOCK_STREAM, 0);
1012 return -ERRNO_TO_PARA_ERROR(errno);
1013 ret = init_unix_addr(&unix_addr, name);
1016 if (connect(fd, (struct sockaddr *)&unix_addr, sizeof(unix_addr)) != -1)
1018 ret = -ERRNO_TO_PARA_ERROR(errno);
1025 ssize_t send_cred_buffer(int sock, char *buf)
1027 return write_buffer(sock, buf);
1029 int recv_cred_buffer(int fd, char *buf, size_t size)
1031 return recv_buffer(fd, buf, size) > 0? 1 : -E_RECVMSG;
1033 #else /* HAVE_UCRED */
1036 * Send a buffer and the credentials of the current process to a socket.
1038 * \param sock The file descriptor of the sending socket.
1039 * \param buf The zero-terminated buffer to send.
1041 * \return On success, this call returns the number of bytes sent. On errors,
1042 * \p -E_SENDMSG is returned.
1044 * \sa \ref recv_cred_buffer, sendmsg(2), socket(7), unix(7).
1046 ssize_t send_cred_buffer(int sock, char *buf)
1048 char control[sizeof(struct cmsghdr) + sizeof(struct ucred)];
1050 struct cmsghdr *cmsg;
1051 static struct iovec iov;
1057 iov.iov_len = strlen(buf);
1061 /* compose the message */
1062 memset(&msg, 0, sizeof(msg));
1065 msg.msg_control = control;
1066 msg.msg_controllen = sizeof(control);
1067 /* attach the ucred struct */
1068 cmsg = CMSG_FIRSTHDR(&msg);
1069 cmsg->cmsg_level = SOL_SOCKET;
1070 cmsg->cmsg_type = SCM_CREDENTIALS;
1071 cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred));
1072 *(struct ucred *)CMSG_DATA(cmsg) = c;
1073 msg.msg_controllen = cmsg->cmsg_len;
1074 ret = sendmsg(sock, &msg, 0);
1080 static void dispose_fds(int *fds, unsigned num)
1084 for (i = 0; i < num; i++)
1089 * Receive a buffer and the Unix credentials of the sending process.
1091 * \param fd The file descriptor of the receiving socket.
1092 * \param buf The buffer to store the received message.
1093 * \param size The length of \a buf in bytes.
1095 * \return Negative on errors, the user id of the sending process on success.
1097 * \sa \ref send_cred_buffer and the references given there.
1099 int recv_cred_buffer(int fd, char *buf, size_t size)
1101 char control[255] __a_aligned(8);
1103 struct cmsghdr *cmsg;
1109 setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &yes, sizeof(int));
1110 memset(&msg, 0, sizeof(msg));
1111 memset(buf, 0, size);
1116 msg.msg_control = control;
1117 msg.msg_controllen = sizeof(control);
1118 if (recvmsg(fd, &msg, 0) < 0)
1120 result = -E_SCM_CREDENTIALS;
1121 cmsg = CMSG_FIRSTHDR(&msg);
1123 if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type
1124 == SCM_CREDENTIALS) {
1125 memcpy(&cred, CMSG_DATA(cmsg), sizeof(struct ucred));
1128 if (cmsg->cmsg_level == SOL_SOCKET
1129 && cmsg->cmsg_type == SCM_RIGHTS) {
1130 dispose_fds((int *)CMSG_DATA(cmsg),
1131 (cmsg->cmsg_len - CMSG_LEN(0))
1134 cmsg = CMSG_NXTHDR(&msg, cmsg);
1138 #endif /* HAVE_UCRED */