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