xref: /unit/src/nxt_socket_msg.c (revision 1996)
1*1996St.nateldemoura@f5.com /*
2*1996St.nateldemoura@f5.com  * Copyright (C) Igor Sysoev
3*1996St.nateldemoura@f5.com  * Copyright (C) NGINX, Inc.
4*1996St.nateldemoura@f5.com  */
5*1996St.nateldemoura@f5.com 
6*1996St.nateldemoura@f5.com #include <nxt_main.h>
7*1996St.nateldemoura@f5.com #include <nxt_socket_msg.h>
8*1996St.nateldemoura@f5.com 
9*1996St.nateldemoura@f5.com 
10*1996St.nateldemoura@f5.com ssize_t
11*1996St.nateldemoura@f5.com nxt_sendmsg(nxt_socket_t s, nxt_iobuf_t *iob, nxt_uint_t niob,
12*1996St.nateldemoura@f5.com     const nxt_send_oob_t *oob)
13*1996St.nateldemoura@f5.com {
14*1996St.nateldemoura@f5.com     struct msghdr  msg;
15*1996St.nateldemoura@f5.com 
16*1996St.nateldemoura@f5.com     msg.msg_name = NULL;
17*1996St.nateldemoura@f5.com     msg.msg_namelen = 0;
18*1996St.nateldemoura@f5.com     msg.msg_iov = iob;
19*1996St.nateldemoura@f5.com     msg.msg_iovlen = niob;
20*1996St.nateldemoura@f5.com     /* Flags are cleared just to suppress valgrind warning. */
21*1996St.nateldemoura@f5.com     msg.msg_flags = 0;
22*1996St.nateldemoura@f5.com 
23*1996St.nateldemoura@f5.com     if (oob != NULL && oob->size != 0) {
24*1996St.nateldemoura@f5.com         msg.msg_control = (void *) oob->buf;
25*1996St.nateldemoura@f5.com         msg.msg_controllen = oob->size;
26*1996St.nateldemoura@f5.com 
27*1996St.nateldemoura@f5.com     } else {
28*1996St.nateldemoura@f5.com         msg.msg_control = NULL;
29*1996St.nateldemoura@f5.com         msg.msg_controllen = 0;
30*1996St.nateldemoura@f5.com     }
31*1996St.nateldemoura@f5.com 
32*1996St.nateldemoura@f5.com     return sendmsg(s, &msg, 0);
33*1996St.nateldemoura@f5.com }
34*1996St.nateldemoura@f5.com 
35*1996St.nateldemoura@f5.com 
36*1996St.nateldemoura@f5.com ssize_t
37*1996St.nateldemoura@f5.com nxt_recvmsg(nxt_socket_t s, nxt_iobuf_t *iob, nxt_uint_t niob,
38*1996St.nateldemoura@f5.com     nxt_recv_oob_t *oob)
39*1996St.nateldemoura@f5.com {
40*1996St.nateldemoura@f5.com     ssize_t        n;
41*1996St.nateldemoura@f5.com     struct msghdr  msg;
42*1996St.nateldemoura@f5.com 
43*1996St.nateldemoura@f5.com     msg.msg_name = NULL;
44*1996St.nateldemoura@f5.com     msg.msg_namelen = 0;
45*1996St.nateldemoura@f5.com     msg.msg_iov = iob;
46*1996St.nateldemoura@f5.com     msg.msg_iovlen = niob;
47*1996St.nateldemoura@f5.com     msg.msg_control = oob->buf;
48*1996St.nateldemoura@f5.com     msg.msg_controllen = sizeof(oob->buf);
49*1996St.nateldemoura@f5.com 
50*1996St.nateldemoura@f5.com     n = recvmsg(s, &msg, 0);
51*1996St.nateldemoura@f5.com 
52*1996St.nateldemoura@f5.com     if (nxt_fast_path(n != -1)) {
53*1996St.nateldemoura@f5.com         oob->size = msg.msg_controllen;
54*1996St.nateldemoura@f5.com     }
55*1996St.nateldemoura@f5.com 
56*1996St.nateldemoura@f5.com     return n;
57*1996St.nateldemoura@f5.com }
58