1 /* Copyright (C) 2006 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
3 /** \file dccp_recv.c paraslash's dccp receiver */
6 * based on client.c of dccp-cs-0.01.tar.bz2,
7 * (C) 2005 Ian McDonald <imcdnzl@gmail.com>
10 #include <netinet/in.h>
11 #include <sys/socket.h>
13 #include <sys/types.h>
14 #include <arpa/inet.h>
19 #include "recv_cmd.lsg.h"
24 #include "buffer_tree.h"
30 static void dccp_recv_close(struct receiver_node *rn)
34 btr_pool_free(rn->btrp);
37 /* Check whether the host supports the requested 'ccid' arguments. */
38 static int dccp_recv_ccid_support_check(const struct lls_parse_result *lpr)
41 int i, j, ret, nccids;
42 unsigned given = RECV_CMD_OPT_GIVEN(DCCP, CCID, lpr);
44 ret = dccp_available_ccids(&ccids);
48 for (i = 0; i < given; i++) {
49 uint32_t val = lls_uint32_val(i,
50 RECV_CMD_OPT_RESULT(DCCP, CCID, lpr));
51 for (j = 0; j < nccids && ccids[j] != val; j++)
54 PARA_ERROR_LOG("'CCID-%u' not supported on this host\n",
56 return -ERRNO_TO_PARA_ERROR(EINVAL);
62 static int dccp_recv_open(struct receiver_node *rn)
64 struct lls_parse_result *lpr = rn->lpr;
65 struct flowopts *fo = NULL;
66 uint8_t *ccids = NULL;
68 const struct lls_opt_result *r_c = RECV_CMD_OPT_RESULT(DCCP, CCID, lpr);
69 const char *host = RECV_CMD_OPT_STRING_VAL(DCCP, HOST, lpr);
70 uint32_t port = RECV_CMD_OPT_UINT32_VAL(DCCP, PORT, lpr);
73 ret = dccp_recv_ccid_support_check(lpr);
76 /* Copy CCID preference list (u8 array required) */
77 given = lls_opt_given(r_c);
79 ccids = para_malloc(given);
81 for (i = 0; i < given; i++)
82 ccids[i] = lls_int32_val(i, r_c);
83 OPT_ADD(fo, SOL_DCCP, DCCP_SOCKOPT_CCID, ccids, i);
86 fd = makesock(IPPROTO_DCCP, 0, host, port, fo);
92 * Disable unused CCIDs: the receiver does not send any application
93 * data to the server. By shutting down this unused path we reduce
94 * internal processing costs, as the unused CCIDs (in the kernel) are
97 if (shutdown(fd, SHUT_WR) < 0) {
98 ret = -ERRNO_TO_PARA_ERROR(errno);
101 ret = mark_fd_nonblocking(fd);
104 rn->btrp = btr_pool_new("dccp_recv", 320 * 1024);
112 static void dccp_recv_pre_select(struct sched *s, void *context)
114 struct receiver_node *rn = context;
116 if (generic_recv_pre_select(s, rn) <= 0)
118 para_fd_set(rn->fd, &s->rfds, &s->max_fileno);
121 static int dccp_recv_post_select(struct sched *s, void *context)
123 struct receiver_node *rn = context;
124 struct btr_node *btrn = rn->btrn;
129 ret = task_get_notification(rn->task);
132 ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
135 iovcnt = btr_pool_get_buffers(rn->btrp, iov);
136 ret = -E_DCCP_OVERRUN;
139 ret = readv_nonblock(rn->fd, iov, iovcnt, &s->rfds, &num_bytes);
142 if (num_bytes <= iov[0].iov_len) /* only the first buffer was filled */
143 btr_add_output_pool(rn->btrp, num_bytes, btrn);
144 else { /* both buffers contain data */
145 btr_add_output_pool(rn->btrp, iov[0].iov_len, btrn);
146 btr_add_output_pool(rn->btrp, num_bytes - iov[0].iov_len, btrn);
150 btr_remove_node(&rn->btrn);
154 const struct receiver lsg_recv_cmd_com_dccp_user_data = {
155 .open = dccp_recv_open,
156 .close = dccp_recv_close,
157 .pre_select = dccp_recv_pre_select,
158 .post_select = dccp_recv_post_select,