Merge topic branch t/openssl-3 into master
[paraslash.git] / dccp_recv.c
1 /* Copyright (C) 2006 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
2
3 /** \file dccp_recv.c paraslash's dccp receiver */
4
5 /*
6 * based on client.c of dccp-cs-0.01.tar.bz2,
7 * (C) 2005 Ian McDonald <imcdnzl@gmail.com>
8 */
9
10 #include <netinet/in.h>
11 #include <sys/socket.h>
12 #include <regex.h>
13 #include <sys/types.h>
14 #include <arpa/inet.h>
15 #include <sys/un.h>
16 #include <netdb.h>
17 #include <lopsub.h>
18
19 #include "recv_cmd.lsg.h"
20 #include "para.h"
21 #include "error.h"
22 #include "list.h"
23 #include "sched.h"
24 #include "buffer_tree.h"
25 #include "recv.h"
26 #include "string.h"
27 #include "net.h"
28 #include "fd.h"
29
30 #ifndef DCCP_SOCKOPT_CCID
31 #define DCCP_SOCKOPT_CCID 13 /**< Sets both TX/RX CCID. */
32 #endif
33
34 static void dccp_recv_close(struct receiver_node *rn)
35 {
36 if (rn->fd > 0)
37 close(rn->fd);
38 btr_pool_free(rn->btrp);
39 }
40
41 /* Check whether the host supports the requested 'ccid' arguments. */
42 static int dccp_recv_ccid_support_check(const struct lls_parse_result *lpr)
43 {
44 uint8_t *ccids;
45 int i, j, ret, nccids;
46 unsigned given = RECV_CMD_OPT_GIVEN(DCCP, CCID, lpr);
47
48 ret = dccp_available_ccids(&ccids);
49 if (ret < 0)
50 return ret;
51 nccids = ret;
52 for (i = 0; i < given; i++) {
53 uint32_t val = lls_uint32_val(i,
54 RECV_CMD_OPT_RESULT(DCCP, CCID, lpr));
55 for (j = 0; j < nccids && ccids[j] != val; j++)
56 ;
57 if (j == nccids) {
58 PARA_ERROR_LOG("'CCID-%u' not supported on this host\n",
59 val);
60 return -ERRNO_TO_PARA_ERROR(EINVAL);
61 }
62 }
63 return 1;
64 }
65
66 /** Flowopt shortcut */
67 #define OPT_ADD(fo, lev, opt, val, len) flowopt_add(fo, lev, opt, #opt, val, len)
68
69 static int dccp_recv_open(struct receiver_node *rn)
70 {
71 struct lls_parse_result *lpr = rn->lpr;
72 struct flowopts *fo = NULL;
73 uint8_t *ccids = NULL;
74 int fd, ret, i;
75 const struct lls_opt_result *r_c = RECV_CMD_OPT_RESULT(DCCP, CCID, lpr);
76 const char *host = RECV_CMD_OPT_STRING_VAL(DCCP, HOST, lpr);
77 uint32_t port = RECV_CMD_OPT_UINT32_VAL(DCCP, PORT, lpr);
78 unsigned given;
79
80 ret = dccp_recv_ccid_support_check(lpr);
81 if (ret < 0)
82 return ret;
83 /* Copy CCID preference list (u8 array required) */
84 given = lls_opt_given(r_c);
85 if (given) {
86 ccids = alloc(given);
87 fo = flowopt_new();
88 for (i = 0; i < given; i++)
89 ccids[i] = lls_int32_val(i, r_c);
90 OPT_ADD(fo, SOL_DCCP, DCCP_SOCKOPT_CCID, ccids, i);
91 }
92
93 fd = makesock(IPPROTO_DCCP, false, host, port, fo);
94 flowopt_cleanup(fo);
95 free(ccids);
96 if (fd < 0)
97 return fd;
98 /*
99 * Disable unused CCIDs: the receiver does not send any application
100 * data to the server. By shutting down this unused path we reduce
101 * internal processing costs, as the unused CCIDs (in the kernel) are
102 * then bypassed.
103 */
104 if (shutdown(fd, SHUT_WR) < 0) {
105 ret = -ERRNO_TO_PARA_ERROR(errno);
106 goto err;
107 }
108 ret = mark_fd_nonblocking(fd);
109 if (ret < 0)
110 goto err;
111 rn->btrp = btr_pool_new("dccp_recv", 320 * 1024);
112 rn->fd = fd;
113 return 1;
114 err:
115 close(fd);
116 return ret;
117 }
118
119 static void dccp_recv_pre_monitor(struct sched *s, void *context)
120 {
121 struct receiver_node *rn = context;
122
123 if (generic_recv_pre_monitor(s, rn) <= 0)
124 return;
125 sched_monitor_readfd(rn->fd, s);
126 }
127
128 static int dccp_recv_post_monitor(__a_unused struct sched *s, void *context)
129 {
130 struct receiver_node *rn = context;
131 struct btr_node *btrn = rn->btrn;
132 struct iovec iov[2];
133 int ret, iovcnt;
134 size_t num_bytes;
135
136 ret = task_get_notification(rn->task);
137 if (ret < 0)
138 goto out;
139 ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
140 if (ret <= 0)
141 goto out;
142 iovcnt = btr_pool_get_buffers(rn->btrp, iov);
143 ret = -E_DCCP_OVERRUN;
144 if (iovcnt == 0)
145 goto out;
146 ret = readv_nonblock(rn->fd, iov, iovcnt, &num_bytes);
147 if (num_bytes == 0)
148 goto out;
149 if (num_bytes <= iov[0].iov_len) /* only the first buffer was filled */
150 btr_add_output_pool(rn->btrp, num_bytes, btrn);
151 else { /* both buffers contain data */
152 btr_add_output_pool(rn->btrp, iov[0].iov_len, btrn);
153 btr_add_output_pool(rn->btrp, num_bytes - iov[0].iov_len, btrn);
154 }
155 out:
156 if (ret < 0)
157 btr_remove_node(&rn->btrn);
158 return ret;
159 }
160
161 const struct receiver lsg_recv_cmd_com_dccp_user_data = {
162 .open = dccp_recv_open,
163 .close = dccp_recv_close,
164 .pre_monitor = dccp_recv_pre_monitor,
165 .post_monitor = dccp_recv_post_monitor,
166 };