2 * Copyright (C) 2005-2009 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file net.c Networking-related helper functions. */
10 * Since glibc 2.8, the _GNU_SOURCE feature test macro must be defined in order
11 * to obtain the definition of the ucred structure.
17 /* At least NetBSD needs these. */
25 #define AI_ADDRCONFIG 0
38 /** Information about one encrypted connection. */
40 /** Function used to decrypt received data. */
42 /** Function used to encrypt data to be sent. */
45 * Context-dependent data (crypt keys), passed verbatim to the above
50 /** Array holding per fd crypt data. */
51 static struct crypt_data *crypt_data_array;
52 /** Current size of the crypt data array. */
53 static unsigned cda_size = 0;
56 * Activate encryption for one file descriptor.
58 * \param fd The file descriptor.
59 * \param recv_f The function used for decrypting received data.
60 * \param send_f The function used for encrypting before sending.
61 * \param private_data User data supplied by the caller.
63 void enable_crypt(int fd, crypt_function *recv_f, crypt_function *send_f,
66 if (fd + 1 > cda_size) {
67 crypt_data_array = para_realloc(crypt_data_array,
68 (fd + 1) * sizeof(struct crypt_data));
69 memset(crypt_data_array + cda_size, 0,
70 (fd + 1 - cda_size) * sizeof(struct crypt_data));
73 crypt_data_array[fd].recv = recv_f;
74 crypt_data_array[fd].send = send_f;
75 crypt_data_array[fd].private_data = private_data;
76 PARA_INFO_LOG("rc4 encryption activated for fd %d\n", fd);
80 * Deactivate encryption for a given fd.
82 * \param fd The file descriptor.
84 * This must be called if and only if \p fd was activated via enable_crypt().
86 void disable_crypt(int fd)
88 if (cda_size < fd + 1)
90 crypt_data_array[fd].recv = NULL;
91 crypt_data_array[fd].send = NULL;
92 crypt_data_array[fd].private_data = NULL;
96 * Parse and validate IPv4 address/netmask string.
98 * \param cidr Address in CIDR notation
99 * \param addr Copy of the IPv4 address part of \a cidr
100 * \param addrlen Size of \a addr in bytes
101 * \param netmask Value of the netmask part in \a cidr or the
102 * default of 32 if not specified.
104 * \return Pointer to \a addr if succesful, NULL on error.
107 char *parse_cidr(const char *cidr,
108 char *addr, ssize_t addrlen,
111 const char *o = cidr;
112 char *c = addr, *end = c + (addrlen - 1);
116 if (cidr == NULL || addrlen < 1)
119 for (o = cidr; (*c = *o == '/'? '\0' : *o); c++, o++)
124 if (para_atoi32(++o, netmask) < 0 ||
125 *netmask < 0 || *netmask > 0x20)
128 if (is_valid_ipv4_address(addr))
137 * Match string as a candidate IPv4 address.
139 * \param address The string to match.
140 * \return True if \a address has "dot-quad" format.
142 static bool is_v4_dot_quad(const char *address)
147 assert(!regcomp(&r, "^([0-9]+\\.){3}[0-9]+$", REG_EXTENDED|REG_NOSUB));
148 result = regexec(&r, address, 0, NULL, 0) == 0;
154 * Perform basic syntax checking on the host-part of an URL:
156 * - Since ':' is invalid in IPv4 addresses and DNS names, the
157 * presence of ':' causes interpretation as IPv6 address;
158 * - next the first-match-wins algorithm from RFC 3986 is applied;
159 * - else the string is considered as DNS name, to be resolved later.
161 * \param host The host string to check.
162 * \return True if \a host passes the syntax checks.
164 * \sa RFC 3986, 3.2.2; RFC 1123, 2.1; RFC 1034, 3.5
166 static bool host_string_ok(const char *host)
168 if (host == NULL || *host == '\0')
170 if (strchr(host, ':') != NULL)
171 return is_valid_ipv6_address(host);
172 if (is_v4_dot_quad(host))
173 return is_valid_ipv4_address(host);
178 * Parse and validate URL string.
180 * The URL syntax is loosely based on RFC 3986, supporting one of
181 * - "["host"]"[:port] for native IPv6 addresses and
182 * - host[:port] for IPv4 hostnames and DNS names.
184 * Native IPv6 addresses must be enclosed in square brackets, since
185 * otherwise there is an ambiguity with the port separator `:'.
186 * The 'port' part is always considered to be a number; if absent,
187 * it is set to -1, to indicate that a default port is to be used.
189 * The following are valid examples:
197 * \param url The URL string to take apart.
198 * \param host To return the copied host part of \a url.
199 * \param hostlen The maximum length of \a host.
200 * \param port To return the port number (if any) of \a url.
202 * \return Pointer to \a host, or NULL if failed.
203 * If NULL is returned, \a host and \a portnum are undefined. If no
204 * port number was present in \a url, \a portnum is set to -1.
206 * \sa RFC 3986, 3.2.2/3.2.3
208 char *parse_url(const char *url,
209 char *host, ssize_t hostlen,
213 char *c = host, *end = c + (hostlen - 1);
217 if (o == NULL || hostlen < 1)
221 for (++o; (*c = *o == ']' ? '\0' : *o); c++, o++)
225 if (*o++ != ']' || (*o != '\0' && *o != ':'))
228 for (; (*c = *o == ':'? '\0' : *o); c++, o++)
234 if (para_atoi32(++o, port) < 0 ||
235 *port < 0 || *port > 0xffff)
238 if (host_string_ok(host))
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 * Resolve IPv4/IPv6 address and create a ready-to-use active or passive socket.
278 * \param l3type The layer-3 type (\p AF_INET, \p AF_INET6, \p AF_UNSPEC).
279 * \param l4type The layer-4 type (\p IPPROTO_xxx).
280 * \param passive Whether this is a passive (1) or active (0) socket.
281 * \param host Remote or local hostname or IPv/6 address string.
282 * \param port_number Decimal port number.
284 * This creates a ready-made IPv4/v6 socket structure after looking up the
285 * necessary parameters. The interpretation of \a host depends on the value of
287 * - on a passive socket host is interpreted as an interface IPv4/6 address
288 * (can be left NULL);
289 * - on an active socket, \a host is the peer DNS name or IPv4/6 address
291 * - \a port_number is in either case the numeric port number (not service
294 * Furthermore, bind(2) is called on passive sockets, and connect(2) on active
295 * sockets. The algorithm tries all possible address combinations until it
298 * \return This function returns 1 on success and \a -E_ADDRESS_LOOKUP when no
299 * matching connection could be set up (with details in the error log).
301 * \sa ipv6(7), getaddrinfo(3), bind(2), connect(2).
303 int makesock(unsigned l3type, unsigned l4type, int passive,
304 const char *host, unsigned short port_number)
306 struct addrinfo *local = NULL, *src,
307 *remote = NULL, *dst, hints;
308 int rc, on = 1, sockfd = -1,
309 socktype = sock_type(l4type);
310 char port[6]; /* port number has at most 5 digits */
312 sprintf(port, "%u", port_number);
313 /* Set up address hint structure */
314 memset(&hints, 0, sizeof(hints));
315 hints.ai_family = l3type;
316 hints.ai_socktype = socktype;
318 * getaddrinfo does not support SOCK_DCCP, so for the sake of lookup
319 * (and only then) pretend to be UDP.
321 if (l4type == IPPROTO_DCCP)
322 hints.ai_socktype = SOCK_DGRAM;
324 /* only use addresses available on the host */
325 hints.ai_flags = AI_ADDRCONFIG;
326 if (l3type == AF_INET6)
327 /* use v4-mapped-v6 if no v6 addresses found */
328 hints.ai_flags |= AI_V4MAPPED | AI_ALL;
330 if (passive && host == NULL)
331 hints.ai_flags |= AI_PASSIVE;
333 /* Obtain local/remote address information */
334 if ((rc = getaddrinfo(host, port, &hints, passive ? &local : &remote))) {
335 PARA_ERROR_LOG("can not resolve %s address %s#%s: %s.\n",
337 host? host : (passive? "[loopback]" : "[localhost]"),
338 port, gai_strerror(rc));
339 return -E_ADDRESS_LOOKUP;
342 /* Iterate over all src/dst combination, exhausting dst first */
343 for (src = local, dst = remote; src != NULL || dst != NULL; /* no op */ ) {
344 if (src && dst && src->ai_family == AF_INET
345 && dst->ai_family == AF_INET6)
346 goto get_next_dst; /* v4 -> v6 is not possible */
348 sockfd = socket(src ? src->ai_family : dst->ai_family,
354 * Set those options that need to be set before establishing
355 * the connection. Reuse the address on passive (listening)
356 * sockets to avoid failure on restart.
358 if (passive && setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
359 &on, sizeof(on)) == -1) {
360 PARA_ERROR_LOG("can not set SO_REUSEADDR: %s\n",
362 return -ERRNO_TO_PARA_ERROR(errno);
366 if (bind(sockfd, src->ai_addr, src->ai_addrlen) < 0) {
370 if (!dst) /* bind-only completed successfully */
374 if (dst && connect(sockfd, dst->ai_addr, dst->ai_addrlen) == 0)
375 break; /* connection completed successfully */
378 if (dst && (dst = dst->ai_next))
381 if (src && (src = src->ai_next)) /* restart inner loop */
387 freeaddrinfo(remote);
389 if (src == NULL && dst == NULL) {
390 PARA_ERROR_LOG("can not create %s socket %s#%s.\n",
391 layer4_name(l4type), host? host : (passive?
392 "[loopback]" : "[localhost]"), port);
393 return -ERRNO_TO_PARA_ERROR(errno);
399 * Create a passive / listening socket.
401 * \param l3type The network-layer type (\p AF_xxx).
402 * \param l4type The transport-layer type (\p IPPROTO_xxx).
403 * \param port The decimal port number to listen on.
405 * \return Positive integer (socket descriptor) on success, negative value
408 * \sa makesock(), ip(7), ipv6(7), bind(2), listen(2).
410 int para_listen(unsigned l3type, unsigned l4type, unsigned short port)
412 int ret, fd = makesock(l3type, l4type, 1, NULL, port);
415 ret = listen(fd, BACKLOG);
418 return -ERRNO_TO_PARA_ERROR(errno);
420 PARA_INFO_LOG("listening on %s port %u, fd %d\n",
421 layer4_name(l4type), port, fd);
427 * Print numeric host and port number (beware - uses static char).
429 * \param sa The IPv4/IPv6 socket address to use.
430 * \param len The length of \p sa.
432 * \sa getnameinfo(3).
434 static char *host_and_port(struct sockaddr *sa, socklen_t len)
436 static char output[NI_MAXHOST + NI_MAXSERV + 2];
437 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
440 ret = getnameinfo(sa, len, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf),
441 NI_NUMERICHOST | NI_NUMERICSERV);
443 PARA_WARNING_LOG("hostname lookup error (%s).\n",
445 sprintf(output, "(unknown)");
447 sprintf(output, "%s#%s", hbuf, sbuf);
452 * Look up the local or remote side of a connected socket structure.
454 * \param fd The socket descriptor of the connected socket.
455 * \param getname Either \p getsockname() for local, or \p getpeername() for
458 * \return A static character string identifying hostname and port of the
461 * \sa getsockname(2), getpeername(2).
463 static char *__get_sock_name(int fd, int (*getname)(int, struct sockaddr*,
466 struct sockaddr_storage ss;
467 socklen_t sslen = sizeof(ss);
469 if (getname(fd, (struct sockaddr *)&ss, &sslen) < 0) {
470 static char *dont_know = "(don't know)";
471 PARA_ERROR_LOG("can not determine address from fd %d: %s\n",
472 fd, strerror(errno));
475 return host_and_port((struct sockaddr *)&ss, sslen);
479 * Look up the local side of a connected socket structure.
481 * \param sockfd The file descriptor of the socket.
483 * \return A pointer to a static buffer containing hostname an port. This
484 * buffer must not be freed by the caller.
488 char *local_name(int sockfd)
490 return __get_sock_name(sockfd, getsockname);
494 * Look up the remote side of a connected socket structure.
496 * \param sockfd The file descriptor of the socket.
498 * \return Analogous to the return value of \ref local_name() but for the
503 char *remote_name(int sockfd)
505 return __get_sock_name(sockfd, getpeername);
509 * Extract IPv4 or IPv6-mapped-IPv4 address from sockaddr_storage.
510 * \param ss Container of IPv4/6 address
511 * \return Extracted IPv4 address (different from 0) or 0 if unsuccessful.
515 struct in_addr extract_v4_addr(const struct sockaddr_storage *ss)
517 struct in_addr ia = {.s_addr = 0};
519 if (ss->ss_family == AF_INET)
520 ia.s_addr = ((struct sockaddr_in *)ss)->sin_addr.s_addr;
521 if (ss->ss_family == AF_INET6) {
522 const struct in6_addr v6_addr = ((struct sockaddr_in6 *)ss)->sin6_addr;
524 if (IN6_IS_ADDR_V4MAPPED(&v6_addr))
525 memcpy(&ia.s_addr, &(v6_addr.s6_addr[12]), 4);
531 * Encrypt and send a binary buffer.
533 * \param fd The file descriptor.
534 * \param buf The buffer to be encrypted and sent.
535 * \param len The length of \a buf.
537 * Check if encryption is available. If yes, encrypt the given buffer. Send
538 * out the buffer, encrypted or not, and try to resend the remaining part in
539 * case of short writes.
543 int send_bin_buffer(int fd, const char *buf, size_t len)
546 crypt_function *cf = NULL;
549 PARA_CRIT_LOG("len == 0\n");
550 if (fd + 1 <= cda_size)
551 cf = crypt_data_array[fd].send;
553 void *private = crypt_data_array[fd].private_data;
554 /* RC4 may write more than len to the output buffer */
555 unsigned char *outbuf = para_malloc(ROUND_UP(len, 8));
556 (*cf)(len, (unsigned char *)buf, outbuf, private);
557 ret = write_all(fd, (char *)outbuf, &len);
560 ret = write_all(fd, buf, &len);
565 * Encrypt and send null terminated buffer.
567 * \param fd The file descriptor.
568 * \param buf The null-terminated buffer to be send.
570 * This is equivalent to send_bin_buffer(fd, buf, strlen(buf)).
574 int send_buffer(int fd, const char *buf)
576 return send_bin_buffer(fd, buf, strlen(buf));
581 * Send and encrypt a buffer given by a format string.
583 * \param fd The file descriptor.
584 * \param fmt A format string.
588 __printf_2_3 int send_va_buffer(int fd, const char *fmt, ...)
593 PARA_VSPRINTF(fmt, msg);
594 ret = send_buffer(fd, msg);
600 * Receive and decrypt.
602 * \param fd The file descriptor.
603 * \param buf The buffer to write the decrypted data to.
604 * \param size The size of \a buf.
606 * Receive at most \a size bytes from file descriptor \a fd. If encryption is
607 * available, decrypt the received buffer.
609 * \return The number of bytes received on success, negative on errors.
613 __must_check int recv_bin_buffer(int fd, char *buf, size_t size)
616 crypt_function *cf = NULL;
618 if (fd + 1 <= cda_size)
619 cf = crypt_data_array[fd].recv;
621 unsigned char *tmp = para_malloc(size);
622 void *private = crypt_data_array[fd].private_data;
623 n = recv(fd, tmp, size, 0);
626 unsigned char *b = (unsigned char *)buf;
627 (*cf)(numbytes, tmp, b, private);
631 n = recv(fd, buf, size, 0);
633 return -ERRNO_TO_PARA_ERROR(errno);
638 * Receive, decrypt and write terminating NULL byte.
640 * \param fd The file descriptor.
641 * \param buf The buffer to write the decrypted data to.
642 * \param size The size of \a buf.
644 * Read and decrypt at most \a size - 1 bytes from file descriptor \a fd and
645 * write a NULL byte at the end of the received data.
647 * \return The return value of the underlying call to \a recv_bin_buffer().
649 * \sa recv_bin_buffer()
651 int recv_buffer(int fd, char *buf, size_t size)
656 n = recv_bin_buffer(fd, buf, size - 1);
665 * Wrapper around the accept system call.
667 * \param fd The listening socket.
668 * \param addr Structure which is filled in with the address of the peer socket.
669 * \param size Should contain the size of the structure pointed to by \a addr.
671 * Accept incoming connections on \a addr. Retry if interrupted.
673 * \return The new file descriptor on success, negative on errors.
677 int para_accept(int fd, void *addr, socklen_t size)
682 new_fd = accept(fd, (struct sockaddr *) addr, &size);
683 while (new_fd < 0 && errno == EINTR);
684 return new_fd < 0? -ERRNO_TO_PARA_ERROR(errno) : new_fd;
688 * Prepare a structure for \p AF_UNIX socket addresses.
690 * \param u Pointer to the struct to be prepared.
691 * \param name The socket pathname.
693 * This just copies \a name to the sun_path component of \a u.
695 * \return Positive on success, \p -E_NAME_TOO_LONG if \a name is longer
696 * than \p UNIX_PATH_MAX.
698 static int init_unix_addr(struct sockaddr_un *u, const char *name)
700 if (strlen(name) >= UNIX_PATH_MAX)
701 return -E_NAME_TOO_LONG;
702 memset(u->sun_path, 0, UNIX_PATH_MAX);
703 u->sun_family = PF_UNIX;
704 strcpy(u->sun_path, name);
709 * Prepare, create, and bind a socket for local communication.
711 * \param name The socket pathname.
712 * \param unix_addr Pointer to the \p AF_UNIX socket structure.
713 * \param mode The desired mode of the socket.
715 * This function creates a local socket for sequenced, reliable,
716 * two-way, connection-based byte streams.
718 * \return The file descriptor, on success, negative on errors.
724 int create_local_socket(const char *name, struct sockaddr_un *unix_addr,
729 ret = init_unix_addr(unix_addr, name);
732 ret = socket(PF_UNIX, SOCK_STREAM, 0);
734 return -ERRNO_TO_PARA_ERROR(errno);
736 ret = bind(fd, (struct sockaddr *) unix_addr, UNIX_PATH_MAX);
738 ret = -ERRNO_TO_PARA_ERROR(errno);
742 if (chmod(name, mode) < 0)
751 * Prepare, create, and connect to a Unix domain socket for local communication.
753 * \param name The socket pathname.
755 * This function creates a local socket for sequenced, reliable, two-way,
756 * connection-based byte streams.
758 * \return The file descriptor, on success, negative on errors.
760 * \sa create_local_socket(), unix(7), connect(2).
762 int create_remote_socket(const char *name)
764 struct sockaddr_un unix_addr;
767 ret = init_unix_addr(&unix_addr, name);
770 fd = socket(PF_UNIX, SOCK_STREAM, 0);
772 return -ERRNO_TO_PARA_ERROR(errno);
773 if (connect(fd, (struct sockaddr *)&unix_addr, sizeof(unix_addr)) == -1) {
774 ret = -ERRNO_TO_PARA_ERROR(errno);
784 ssize_t send_cred_buffer(int sock, char *buf)
786 return send_buffer(sock, buf);
788 int recv_cred_buffer(int fd, char *buf, size_t size)
790 return recv_buffer(fd, buf, size) > 0? 1 : -E_RECVMSG;
792 #else /* HAVE_UCRED */
794 * Send \p NULL-terminated buffer and Unix credentials of the current process.
796 * \param sock The socket file descriptor.
797 * \param buf The buffer to be sent.
799 * \return On success, this call returns the number of characters sent. On
800 * error, \p -E_SENDMSG is returned.
802 * \sa sendmsg(2), okir's Black Hats Manual.
804 ssize_t send_cred_buffer(int sock, char *buf)
806 char control[sizeof(struct cmsghdr) + sizeof(struct ucred)];
808 struct cmsghdr *cmsg;
809 static struct iovec iov;
815 iov.iov_len = strlen(buf);
819 /* compose the message */
820 memset(&msg, 0, sizeof(msg));
823 msg.msg_control = control;
824 msg.msg_controllen = sizeof(control);
825 /* attach the ucred struct */
826 cmsg = CMSG_FIRSTHDR(&msg);
827 cmsg->cmsg_level = SOL_SOCKET;
828 cmsg->cmsg_type = SCM_CREDENTIALS;
829 cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred));
830 *(struct ucred *)CMSG_DATA(cmsg) = c;
831 msg.msg_controllen = cmsg->cmsg_len;
832 ret = sendmsg(sock, &msg, 0);
838 static void dispose_fds(int *fds, unsigned num)
842 for (i = 0; i < num; i++)
847 * Receive a buffer and the Unix credentials of the sending process.
849 * \param fd the socket file descriptor.
850 * \param buf the buffer to store the message.
851 * \param size the size of \a buffer.
853 * \return negative on errors, the user id on success.
855 * \sa recvmsg(2), okir's Black Hats Manual.
857 int recv_cred_buffer(int fd, char *buf, size_t size)
861 struct cmsghdr *cmsg;
867 setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &yes, sizeof(int));
868 memset(&msg, 0, sizeof(msg));
869 memset(buf, 0, size);
874 msg.msg_control = control;
875 msg.msg_controllen = sizeof(control);
876 if (recvmsg(fd, &msg, 0) < 0)
878 result = -E_SCM_CREDENTIALS;
879 cmsg = CMSG_FIRSTHDR(&msg);
881 if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type
882 == SCM_CREDENTIALS) {
883 memcpy(&cred, CMSG_DATA(cmsg), sizeof(struct ucred));
886 if (cmsg->cmsg_level == SOL_SOCKET
887 && cmsg->cmsg_type == SCM_RIGHTS) {
888 dispose_fds((int *) CMSG_DATA(cmsg),
889 (cmsg->cmsg_len - CMSG_LEN(0))
892 cmsg = CMSG_NXTHDR(&msg, cmsg);
896 #endif /* HAVE_UCRED */
899 * Receive a buffer and check for a pattern.
901 * \param fd The file descriptor to receive from.
902 * \param pattern The expected pattern.
903 * \param bufsize The size of the internal buffer.
905 * \return Positive if \a pattern was received, negative otherwise.
907 * This function tries to receive at most \a bufsize bytes from file descriptor
908 * \a fd. If at least \p strlen(\a pattern) bytes were received, the beginning
909 * of the received buffer is compared with \a pattern, ignoring case.
911 * \sa recv_buffer(), \sa strncasecmp(3).
913 int recv_pattern(int fd, const char *pattern, size_t bufsize)
915 size_t len = strlen(pattern);
916 char *buf = para_malloc(bufsize + 1);
917 int ret = -E_RECV_PATTERN, n = recv_buffer(fd, buf, bufsize + 1);
921 if (strncasecmp(buf, pattern, len))
926 PARA_NOTICE_LOG("n = %d, did not receive pattern '%s'\n", n,
929 PARA_NOTICE_LOG("recvd: %s\n", buf);