mixer: Trivial whitespace fix.
[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 static void dccp_recv_close(struct receiver_node *rn)
31 {
32 if (rn->fd > 0)
33 close(rn->fd);
34 btr_pool_free(rn->btrp);
35 }
36
37 /* Check whether the host supports the requested 'ccid' arguments. */
38 static int dccp_recv_ccid_support_check(const struct lls_parse_result *lpr)
39 {
40 uint8_t *ccids;
41 int i, j, ret, nccids;
42 unsigned given = RECV_CMD_OPT_GIVEN(DCCP, CCID, lpr);
43
44 ret = dccp_available_ccids(&ccids);
45 if (ret < 0)
46 return ret;
47 nccids = ret;
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++)
52 ;
53 if (j == nccids) {
54 PARA_ERROR_LOG("'CCID-%u' not supported on this host\n",
55 val);
56 return -ERRNO_TO_PARA_ERROR(EINVAL);
57 }
58 }
59 return 1;
60 }
61
62 static int dccp_recv_open(struct receiver_node *rn)
63 {
64 struct lls_parse_result *lpr = rn->lpr;
65 struct flowopts *fo = NULL;
66 uint8_t *ccids = NULL;
67 int fd, ret, i;
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);
71 unsigned given;
72
73 ret = dccp_recv_ccid_support_check(lpr);
74 if (ret < 0)
75 return ret;
76 /* Copy CCID preference list (u8 array required) */
77 given = lls_opt_given(r_c);
78 if (given) {
79 ccids = para_malloc(given);
80 fo = flowopt_new();
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);
84 }
85
86 fd = makesock(IPPROTO_DCCP, 0, host, port, fo);
87 flowopt_cleanup(fo);
88 free(ccids);
89 if (fd < 0)
90 return fd;
91 /*
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
95 * then bypassed.
96 */
97 if (shutdown(fd, SHUT_WR) < 0) {
98 ret = -ERRNO_TO_PARA_ERROR(errno);
99 goto err;
100 }
101 ret = mark_fd_nonblocking(fd);
102 if (ret < 0)
103 goto err;
104 rn->btrp = btr_pool_new("dccp_recv", 320 * 1024);
105 rn->fd = fd;
106 return 1;
107 err:
108 close(fd);
109 return ret;
110 }
111
112 static void dccp_recv_pre_select(struct sched *s, void *context)
113 {
114 struct receiver_node *rn = context;
115
116 if (generic_recv_pre_select(s, rn) <= 0)
117 return;
118 para_fd_set(rn->fd, &s->rfds, &s->max_fileno);
119 }
120
121 static int dccp_recv_post_select(struct sched *s, void *context)
122 {
123 struct receiver_node *rn = context;
124 struct btr_node *btrn = rn->btrn;
125 struct iovec iov[2];
126 int ret, iovcnt;
127 size_t num_bytes;
128
129 ret = task_get_notification(rn->task);
130 if (ret < 0)
131 goto out;
132 ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
133 if (ret <= 0)
134 goto out;
135 iovcnt = btr_pool_get_buffers(rn->btrp, iov);
136 ret = -E_DCCP_OVERRUN;
137 if (iovcnt == 0)
138 goto out;
139 ret = readv_nonblock(rn->fd, iov, iovcnt, &s->rfds, &num_bytes);
140 if (num_bytes == 0)
141 goto out;
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);
147 }
148 out:
149 if (ret < 0)
150 btr_remove_node(&rn->btrn);
151 return ret;
152 }
153
154 /** See \ref recv_init(). */
155 const struct receiver lsg_recv_cmd_com_dccp_user_data = {
156 .open = dccp_recv_open,
157 .close = dccp_recv_close,
158 .pre_select = dccp_recv_pre_select,
159 .post_select = dccp_recv_post_select,
160 };