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