Change year in Copyright comment from 2007 to 2008.
[paraslash.git] / net.c
1 /*
2 * Copyright (C) 2005-2008 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file net.c Networking-related helper functions. */
8
9 #include <netdb.h>
10
11 /* At least NetBSD needs these. */
12 #ifndef AI_V4MAPPED
13 #define AI_V4MAPPED 0
14 #endif
15 #ifndef AI_ALL
16 #define AI_ALL 0
17 #endif
18 #ifndef AI_ADDRCONFIG
19 #define AI_ADDRCONFIG 0
20 #endif
21
22
23 #include "para.h"
24 #include "error.h"
25 #include "net.h"
26 #include "string.h"
27
28
29 /** Information about one encrypted connection. */
30 struct crypt_data {
31 /** Function used to decrypt received data. */
32 crypt_function *recv;
33 /** Function used to encrypt data to be sent. */
34 crypt_function *send;
35 /**
36 * Context-dependent data (crypt keys), passed verbatim to the above
37 * crypt functions.
38 */
39 void *private_data;
40 };
41 /** Array holding per fd crypt data. */
42 static struct crypt_data *crypt_data_array;
43 /** Current size of the crypt data array. */
44 static unsigned cda_size = 0;
45
46 /**
47 * Activate encryption for one file descriptor.
48 *
49 * \param fd The file descriptor.
50 * \param recv_f The function used for decrypting received data.
51 * \param send_f The function used for encrypting before sending.
52 * \param private_data User data supplied by the caller.
53 */
54 void enable_crypt(int fd, crypt_function *recv_f, crypt_function *send_f,
55 void *private_data)
56 {
57 if (fd + 1 > cda_size) {
58 crypt_data_array = para_realloc(crypt_data_array,
59 (fd + 1) * sizeof(struct crypt_data));
60 memset(crypt_data_array + cda_size, 0,
61 (fd + 1 - cda_size) * sizeof(struct crypt_data));
62 cda_size = fd + 1;
63 }
64 crypt_data_array[fd].recv = recv_f;
65 crypt_data_array[fd].send = send_f;
66 crypt_data_array[fd].private_data = private_data;
67 PARA_INFO_LOG("rc4 encryption activated for fd %d\n", fd);
68 }
69
70 /**
71 * Deactivate encryption for a given fd.
72 *
73 * \param fd The file descriptor.
74 *
75 * This must be called if and only if \p fd was activated via enable_crypt().
76 */
77 void disable_crypt(int fd)
78 {
79 if (cda_size < fd + 1)
80 return;
81 crypt_data_array[fd].recv = NULL;
82 crypt_data_array[fd].send = NULL;
83 crypt_data_array[fd].private_data = NULL;
84 }
85
86
87 /**
88 * Determine the socket type for a given layer-4 protocol.
89 *
90 * \param l4type The symbolic name of the transport-layer protocol.
91 *
92 * \sa ip(7), socket(2)
93 */
94 static inline int sock_type(const unsigned l4type)
95 {
96 switch (l4type) {
97 case IPPROTO_UDP: return SOCK_DGRAM;
98 case IPPROTO_TCP: return SOCK_STREAM;
99 case IPPROTO_DCCP: return SOCK_DCCP;
100 }
101 return -1; /* not supported here */
102 }
103
104 /**
105 * Pretty-print transport-layer name.
106 */
107 static const char *layer4_name(const unsigned l4type)
108 {
109 switch (l4type) {
110 case IPPROTO_UDP: return "UDP";
111 case IPPROTO_TCP: return "TCP";
112 case IPPROTO_DCCP: return "DCCP";
113 }
114 return "UNKNOWN PROTOCOL";
115 }
116
117 /**
118 * Resolve IPv4/IPv6 address and create a ready-to-use active or passive socket.
119 *
120 * \param l3type The layer-3 type (\p AF_INET, \p AF_INET6, \p AF_UNSPEC).
121 * \param l4type The layer-4 type (\p IPPROTO_xxx).
122 * \param passive Whether this is a passive (1) or active (0) socket.
123 * \param host Remote or local hostname or IPv/6 address string.
124 * \param port_number Decimal port number.
125 *
126 * This creates a ready-made IPv4/v6 socket structure after looking up the
127 * necessary parameters. The interpretation of \a host depends on the value of
128 * \a passive:
129 * - on a passive socket host is interpreted as an interface IPv4/6 address
130 * (can be left NULL);
131 * - on an active socket, \a host is the peer DNS name or IPv4/6 address
132 * to connect to;
133 * - \a port_number is in either case the numeric port number (not service
134 * string).
135 *
136 * Furthermore, bind(2) is called on passive sockets, and connect(2) on active
137 * sockets. The algorithm tries all possible address combinations until it
138 * succeeds.
139 *
140 * \return This function returns 1 on success and \a -E_ADDRESS_LOOKUP when no
141 * matching connection could be set up (with details in the error log).
142 *
143 * \sa ipv6(7), getaddrinfo(3), bind(2), connect(2).
144 */
145 int makesock(unsigned l3type, unsigned l4type, int passive,
146 const char *host, unsigned short port_number)
147 {
148 struct addrinfo *local = NULL, *src,
149 *remote = NULL, *dst, hints;
150 char *port = make_message("%u", port_number);
151 int rc, on = 1, sockfd = -1,
152 socktype = sock_type(l4type);
153
154 /* Set up address hint structure */
155 memset(&hints, 0, sizeof(hints));
156 hints.ai_family = l3type;
157 /* getaddrinfo does not really work well with SOCK_DCCP */
158 if (socktype == SOCK_DGRAM || socktype == SOCK_STREAM)
159 hints.ai_socktype = socktype;
160
161 /* only use addresses available on the host */
162 hints.ai_flags = AI_ADDRCONFIG;
163 if (l3type == AF_INET6)
164 /* use v4-mapped-v6 if no v6 addresses found */
165 hints.ai_flags |= AI_V4MAPPED | AI_ALL;
166
167 if (passive && host == NULL)
168 hints.ai_flags |= AI_PASSIVE;
169
170 /* Obtain local/remote address information */
171 if ((rc = getaddrinfo(host, port, &hints, passive ? &local : &remote))) {
172 PARA_ERROR_LOG("can not resolve %s address %s#%s: %s.\n",
173 layer4_name(l4type),
174 host? : (passive? "[loopback]" : "[localhost]"),
175 port, gai_strerror(rc));
176 return -E_ADDRESS_LOOKUP;
177 }
178
179 /* Iterate over all src/dst combination, exhausting dst first */
180 for (src = local, dst = remote; src != NULL || dst != NULL; /* no op */ ) {
181 if (src && dst && src->ai_family == AF_INET
182 && dst->ai_family == AF_INET6)
183 goto get_next_dst; /* v4 -> v6 is not possible */
184
185 sockfd = socket(src ? src->ai_family : dst->ai_family,
186 socktype, l4type);
187 if (sockfd < 0)
188 goto get_next_dst;
189
190 /*
191 * Set those options that need to be set before establishing
192 * the connection. Reuse the address on passive (listening)
193 * sockets to avoid failure on restart.
194 */
195 if (passive && setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
196 &on, sizeof(on)) == -1) {
197 PARA_ERROR_LOG("can not set SO_REUSEADDR: %s\n",
198 strerror(errno));
199 return -ERRNO_TO_PARA_ERROR(errno);
200 }
201
202 if (src) {
203 if (bind(sockfd, src->ai_addr, src->ai_addrlen) < 0) {
204 close(sockfd);
205 goto get_next_src;
206 }
207 if (!dst) /* bind-only completed successfully */
208 break;
209 }
210
211 if (dst && connect(sockfd, dst->ai_addr, dst->ai_addrlen) == 0)
212 break; /* connection completed successfully */
213 close(sockfd);
214 get_next_dst:
215 if (dst && (dst = dst->ai_next))
216 continue;
217 get_next_src:
218 if (src && (src = src->ai_next)) /* restart inner loop */
219 dst = remote;
220 }
221 if (local)
222 freeaddrinfo(local);
223 if (remote)
224 freeaddrinfo(remote);
225
226 if (src == NULL && dst == NULL) {
227 PARA_ERROR_LOG("can not create %s socket %s#%s.\n",
228 layer4_name(l4type), host? : (passive?
229 "[loopback]" : "[localhost]"), port);
230 return -ERRNO_TO_PARA_ERROR(errno);
231 }
232 return sockfd;
233 }
234
235 /**
236 * Create a passive / listening socket.
237 *
238 * \param l3type The network-layer type (\p AF_xxx).
239 * \param l4type The transport-layer type (\p IPPROTO_xxx).
240 * \param port The decimal port number to listen on.
241 *
242 * \return Positive integer (socket descriptor) on success, negative value
243 * otherwise.
244 *
245 * \sa makesock(), ip(7), ipv6(7), bind(2), listen(2).
246 */
247 int para_listen(unsigned l3type, unsigned l4type, unsigned short port)
248 {
249 int ret, fd = makesock(l3type, l4type, 1, NULL, port);
250
251 if (fd > 0) {
252 ret = listen(fd, BACKLOG);
253 if (ret < 0) {
254 close(fd);
255 return -ERRNO_TO_PARA_ERROR(errno);
256 }
257 PARA_INFO_LOG("listening on %s port %u, fd %d\n",
258 layer4_name(l4type), port, fd);
259 }
260 return fd;
261 }
262
263 /**
264 * Print numeric host and port number (beware - uses static char).
265 *
266 * \param sa The IPv4/IPv6 socket address to use.
267 * \param len The length of \p sa.
268 *
269 * \sa getnameinfo(3).
270 */
271 static char *host_and_port(struct sockaddr *sa, socklen_t len)
272 {
273 static char output[NI_MAXHOST + NI_MAXSERV + 2];
274 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
275 int ret;
276
277 ret = getnameinfo(sa, len, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf),
278 NI_NUMERICHOST | NI_NUMERICSERV);
279 if (ret) {
280 PARA_WARNING_LOG("hostname lookup error (%s).\n",
281 gai_strerror(ret));
282 sprintf(output, "(unknown)");
283 } else
284 sprintf(output, "%s#%s", hbuf, sbuf);
285 return output;
286 }
287
288 /**
289 * Look up the local or remote side of a connected socket structure.
290 *
291 * \param fd The socket descriptor of the connected socket.
292 * \param getname Either \p getsockname() for local, or \p getpeername() for
293 * remote side.
294 *
295 * \return A static character string identifying hostname and port of the
296 * chosen side.
297 *
298 * \sa getsockname(2), getpeername(2).
299 */
300 static char *__get_sock_name(int fd, int (*getname)(int, struct sockaddr*,
301 socklen_t *))
302 {
303 struct sockaddr_storage ss;
304 socklen_t sslen = sizeof(ss);
305
306 if (getname(fd, (struct sockaddr *)&ss, &sslen) < 0) {
307 static char *dont_know = "(don't know)";
308 PARA_ERROR_LOG("can not determine address from fd %d: %s\n",
309 fd, strerror(errno));
310 return dont_know;
311 }
312 return host_and_port((struct sockaddr *)&ss, sslen);
313 }
314
315 char *local_name(int sockfd)
316 {
317 return __get_sock_name(sockfd, getsockname);
318 }
319
320 char *remote_name(int sockfd)
321 {
322 return __get_sock_name(sockfd, getpeername);
323 }
324
325 /**
326 * Extract IPv4 or IPv6-mapped-IPv4 address from sockaddr_storage.
327 * \param ss Container of IPv4/6 address
328 * \return Extracted IPv4 address (different from 0) or 0 if unsuccessful.
329 *
330 * \sa RFC 3493
331 */
332 struct in_addr extract_v4_addr(const struct sockaddr_storage *ss)
333 {
334 struct in_addr ia = {.s_addr = 0};
335
336 if (ss->ss_family == AF_INET)
337 ia.s_addr = ((struct sockaddr_in *)ss)->sin_addr.s_addr;
338 if (ss->ss_family == AF_INET6) {
339 const struct in6_addr v6_addr = ((struct sockaddr_in6 *)ss)->sin6_addr;
340
341 if (IN6_IS_ADDR_V4MAPPED(&v6_addr))
342 memcpy(&ia.s_addr, &(v6_addr.s6_addr[12]), 4);
343 }
344 return ia;
345 }
346
347 /*
348 * Send out a buffer, resend on short writes.
349 *
350 * \param fd The file descriptor.
351 * \param buf The buffer to be sent.
352 * \param len The length of \a buf.
353 *
354 * \return Standard. In any case, the number of bytes actually sent is stored
355 * in \a len.
356 */
357 static int sendall(int fd, const char *buf, size_t *len)
358 {
359 size_t total = *len;
360
361 assert(total);
362 *len = 0;
363 while (*len < total) {
364 int ret = send(fd, buf + *len, total - *len, 0);
365 if (ret == -1)
366 return -ERRNO_TO_PARA_ERROR(errno);
367 *len += ret;
368 }
369 return 1;
370 }
371
372 /**
373 * Encrypt and send a binary buffer.
374 *
375 * \param fd The file descriptor.
376 * \param buf The buffer to be encrypted and sent.
377 * \param len The length of \a buf.
378 *
379 * Check if encryption is available. If yes, encrypt the given buffer. Send
380 * out the buffer, encrypted or not, and try to resend the remaing part in case
381 * of short writes.
382 *
383 * \return Standard.
384 */
385 int send_bin_buffer(int fd, const char *buf, size_t len)
386 {
387 int ret;
388 crypt_function *cf = NULL;
389
390 if (!len)
391 PARA_CRIT_LOG("%s", "len == 0\n");
392 if (fd + 1 <= cda_size)
393 cf = crypt_data_array[fd].send;
394 if (cf) {
395 void *private = crypt_data_array[fd].private_data;
396 /* RC4 may write more than len to the output buffer */
397 unsigned char *outbuf = para_malloc(ROUND_UP(len, 8));
398 (*cf)(len, (unsigned char *)buf, outbuf, private);
399 ret = sendall(fd, (char *)outbuf, &len);
400 free(outbuf);
401 } else
402 ret = sendall(fd, buf, &len);
403 return ret;
404 }
405
406 /**
407 * Encrypt and send null terminated buffer.
408 *
409 * \param fd The file descriptor.
410 * \param buf The null-terminated buffer to be send.
411 *
412 * This is equivalent to send_bin_buffer(fd, buf, strlen(buf)).
413 *
414 * \return Standard.
415 */
416 int send_buffer(int fd, const char *buf)
417 {
418 return send_bin_buffer(fd, buf, strlen(buf));
419 }
420
421
422 /**
423 * Send and encrypt a buffer given by a format string.
424 *
425 * \param fd The file descriptor.
426 * \param fmt A format string.
427 *
428 * \return Standard.
429 */
430 __printf_2_3 int send_va_buffer(int fd, const char *fmt, ...)
431 {
432 char *msg;
433 int ret;
434
435 PARA_VSPRINTF(fmt, msg);
436 ret = send_buffer(fd, msg);
437 free(msg);
438 return ret;
439 }
440
441 /**
442 * Receive and decrypt.
443 *
444 * \param fd The file descriptor.
445 * \param buf The buffer to write the decrypted data to.
446 * \param size The size of \a buf.
447 *
448 * Receive at most \a size bytes from file descriptor \a fd. If encryption is
449 * available, decrypt the received buffer.
450 *
451 * \return The number of bytes received on success, negative on errors.
452 *
453 * \sa recv(2)
454 */
455 __must_check int recv_bin_buffer(int fd, char *buf, size_t size)
456 {
457 ssize_t n;
458 crypt_function *cf = NULL;
459
460 if (fd + 1 <= cda_size)
461 cf = crypt_data_array[fd].recv;
462 if (cf) {
463 unsigned char *tmp = para_malloc(size);
464 void *private = crypt_data_array[fd].private_data;
465 n = recv(fd, tmp, size, 0);
466 if (n > 0) {
467 size_t numbytes = n;
468 unsigned char *b = (unsigned char *)buf;
469 (*cf)(numbytes, tmp, b, private);
470 }
471 free(tmp);
472 } else
473 n = recv(fd, buf, size, 0);
474 if (n == -1)
475 return -ERRNO_TO_PARA_ERROR(errno);
476 return n;
477 }
478
479 /**
480 * Receive, decrypt and write terminating NULL byte.
481 *
482 * \param fd The file descriptor.
483 * \param buf The buffer to write the decrypted data to.
484 * \param size The size of \a buf.
485 *
486 * Read and decrypt at most \a size - 1 bytes from file descriptor \a fd and
487 * write a NULL byte at the end of the received data.
488 *
489 * \return The return value of the underlying call to \a recv_bin_buffer().
490 *
491 * \sa recv_bin_buffer()
492 */
493 int recv_buffer(int fd, char *buf, size_t size)
494 {
495 int n;
496
497 assert(size);
498 n = recv_bin_buffer(fd, buf, size - 1);
499 if (n >= 0)
500 buf[n] = '\0';
501 else
502 *buf = '\0';
503 return n;
504 }
505
506 /**
507 * Wrapper around the accept system call.
508 *
509 * \param fd The listening socket.
510 * \param addr Structure which is filled in with the address of the peer socket.
511 * \param size Should contain the size of the structure pointed to by \a addr.
512 *
513 * Accept incoming connections on \a addr. Retry if interrupted.
514 *
515 * \return The new file descriptor on success, negative on errors.
516 *
517 * \sa accept(2).
518 */
519 int para_accept(int fd, void *addr, socklen_t size)
520 {
521 int new_fd;
522
523 do
524 new_fd = accept(fd, (struct sockaddr *) addr, &size);
525 while (new_fd < 0 && errno == EINTR);
526 return new_fd < 0? -ERRNO_TO_PARA_ERROR(errno) : new_fd;
527 }
528
529 /**
530 * Prepare a structure for \p AF_UNIX socket addresses.
531 *
532 * \param u Pointer to the struct to be prepared.
533 * \param name The socket pathname.
534 *
535 * This just copies \a name to the sun_path component of \a u.
536 *
537 * \return Positive on success, \p -E_NAME_TOO_LONG if \a name is longer
538 * than \p UNIX_PATH_MAX.
539 */
540 static int init_unix_addr(struct sockaddr_un *u, const char *name)
541 {
542 if (strlen(name) >= UNIX_PATH_MAX)
543 return -E_NAME_TOO_LONG;
544 memset(u->sun_path, 0, UNIX_PATH_MAX);
545 u->sun_family = PF_UNIX;
546 strcpy(u->sun_path, name);
547 return 1;
548 }
549
550 /**
551 * Prepare, create, and bind a socket for local communication.
552 *
553 * \param name The socket pathname.
554 * \param unix_addr Pointer to the \p AF_UNIX socket structure.
555 * \param mode The desired mode of the socket.
556 *
557 * This function creates a local socket for sequenced, reliable,
558 * two-way, connection-based byte streams.
559 *
560 * \return The file descriptor, on success, negative on errors.
561 *
562 * \sa socket(2)
563 * \sa bind(2)
564 * \sa chmod(2)
565 */
566 int create_local_socket(const char *name, struct sockaddr_un *unix_addr,
567 mode_t mode)
568 {
569 int fd, ret;
570
571 ret = init_unix_addr(unix_addr, name);
572 if (ret < 0)
573 return ret;
574 ret = socket(PF_UNIX, SOCK_STREAM, 0);
575 if (ret < 0)
576 return -ERRNO_TO_PARA_ERROR(errno);
577 fd = ret;
578 ret = bind(fd, (struct sockaddr *) unix_addr, UNIX_PATH_MAX);
579 if (ret < 0) {
580 ret = -ERRNO_TO_PARA_ERROR(errno);
581 goto err;
582 }
583 ret = -E_CHMOD;
584 if (chmod(name, mode) < 0)
585 goto err;
586 return fd;
587 err:
588 close(fd);
589 return ret;
590 }
591
592 /**
593 * Prepare, create, and connect to a Unix domain socket for local communication.
594 *
595 * \param name The socket pathname.
596 *
597 * This function creates a local socket for sequenced, reliable, two-way,
598 * connection-based byte streams.
599 *
600 * \return The file descriptor, on success, negative on errors.
601 *
602 * \sa create_local_socket(), unix(7), connect(2).
603 */
604 int create_remote_socket(const char *name)
605 {
606 struct sockaddr_un unix_addr;
607 int fd, ret;
608
609 ret = init_unix_addr(&unix_addr, name);
610 if (ret < 0)
611 return ret;
612 fd = socket(PF_UNIX, SOCK_STREAM, 0);
613 if (fd < 0)
614 return -ERRNO_TO_PARA_ERROR(errno);
615 if (connect(fd, (struct sockaddr *)&unix_addr, sizeof(unix_addr)) == -1) {
616 ret = -ERRNO_TO_PARA_ERROR(errno);
617 goto err;
618 }
619 return fd;
620 err:
621 close(fd);
622 return ret;
623 }
624
625 #ifndef HAVE_UCRED
626 ssize_t send_cred_buffer(int sock, char *buf)
627 {
628 return send_buffer(sock, buf);
629 }
630 int recv_cred_buffer(int fd, char *buf, size_t size)
631 {
632 return recv_buffer(fd, buf, size) > 0? 1 : -E_RECVMSG;
633 }
634 #else /* HAVE_UCRED */
635 /**
636 * Send \p NULL-terminated buffer and Unix credentials of the current process.
637 *
638 * \param sock The socket file descriptor.
639 * \param buf The buffer to be sent.
640 *
641 * \return On success, this call returns the number of characters sent. On
642 * error, \p -E_SENDMSG is returned.
643 *
644 * \sa sendmsg(2), okir's Black Hats Manual.
645 */
646 ssize_t send_cred_buffer(int sock, char *buf)
647 {
648 char control[sizeof(struct cmsghdr) + 10];
649 struct msghdr msg;
650 struct cmsghdr *cmsg;
651 static struct iovec iov;
652 struct ucred c;
653 int ret;
654
655 /* Response data */
656 iov.iov_base = buf;
657 iov.iov_len = strlen(buf);
658 c.pid = getpid();
659 c.uid = getuid();
660 c.gid = getgid();
661 /* compose the message */
662 memset(&msg, 0, sizeof(msg));
663 msg.msg_iov = &iov;
664 msg.msg_iovlen = 1;
665 msg.msg_control = control;
666 msg.msg_controllen = sizeof(control);
667 /* attach the ucred struct */
668 cmsg = CMSG_FIRSTHDR(&msg);
669 cmsg->cmsg_level = SOL_SOCKET;
670 cmsg->cmsg_type = SCM_CREDENTIALS;
671 cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred));
672 *(struct ucred *)CMSG_DATA(cmsg) = c;
673 msg.msg_controllen = cmsg->cmsg_len;
674 ret = sendmsg(sock, &msg, 0);
675 if (ret < 0)
676 ret = -E_SENDMSG;
677 return ret;
678 }
679
680 static void dispose_fds(int *fds, unsigned num)
681 {
682 int i;
683
684 for (i = 0; i < num; i++)
685 close(fds[i]);
686 }
687
688 /**
689 * Receive a buffer and the Unix credentials of the sending process.
690 *
691 * \param fd the socket file descriptor.
692 * \param buf the buffer to store the message.
693 * \param size the size of \a buffer.
694 *
695 * \return negative on errors, the user id on success.
696 *
697 * \sa recvmsg(2), okir's Black Hats Manual.
698 */
699 int recv_cred_buffer(int fd, char *buf, size_t size)
700 {
701 char control[255];
702 struct msghdr msg;
703 struct cmsghdr *cmsg;
704 struct iovec iov;
705 int result = 0;
706 int yes = 1;
707 struct ucred cred;
708
709 setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &yes, sizeof(int));
710 memset(&msg, 0, sizeof(msg));
711 memset(buf, 0, size);
712 iov.iov_base = buf;
713 iov.iov_len = size;
714 msg.msg_iov = &iov;
715 msg.msg_iovlen = 1;
716 msg.msg_control = control;
717 msg.msg_controllen = sizeof(control);
718 if (recvmsg(fd, &msg, 0) < 0)
719 return -E_RECVMSG;
720 result = -E_SCM_CREDENTIALS;
721 cmsg = CMSG_FIRSTHDR(&msg);
722 while (cmsg) {
723 if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type
724 == SCM_CREDENTIALS) {
725 memcpy(&cred, CMSG_DATA(cmsg), sizeof(struct ucred));
726 result = cred.uid;
727 } else
728 if (cmsg->cmsg_level == SOL_SOCKET
729 && cmsg->cmsg_type == SCM_RIGHTS) {
730 dispose_fds((int *) CMSG_DATA(cmsg),
731 (cmsg->cmsg_len - CMSG_LEN(0))
732 / sizeof(int));
733 }
734 cmsg = CMSG_NXTHDR(&msg, cmsg);
735 }
736 return result;
737 }
738 #endif /* HAVE_UCRED */
739
740 /**
741 * Receive a buffer and check for a pattern.
742 *
743 * \param fd The file descriptor to receive from.
744 * \param pattern The expected pattern.
745 * \param bufsize The size of the internal buffer.
746 *
747 * \return Positive if \a pattern was received, negative otherwise.
748 *
749 * This function creates a buffer of size \a bufsize and tries
750 * to receive at most \a bufsize bytes from file descriptor \a fd.
751 * If at least \p strlen(\a pattern) bytes were received, the beginning of
752 * the received buffer is compared with \a pattern, ignoring case.
753 *
754 * \sa recv_buffer(), \sa strncasecmp(3).
755 */
756 int recv_pattern(int fd, const char *pattern, size_t bufsize)
757 {
758 size_t len = strlen(pattern);
759 char *buf = para_malloc(bufsize + 1);
760 int ret = -E_RECV_PATTERN, n = recv_buffer(fd, buf, bufsize);
761
762 if (n < len)
763 goto out;
764 if (strncasecmp(buf, pattern, len))
765 goto out;
766 ret = 1;
767 out:
768 if (ret < 0) {
769 PARA_NOTICE_LOG("n = %d, did not receive pattern '%s'\n", n,
770 pattern);
771 if (n > 0)
772 PARA_NOTICE_LOG("recvd: %s\n", buf);
773 }
774 free(buf);
775 return ret;
776 }