mood: Speed up int_sqrt().
[paraslash.git] / dccp_recv.c
1 /*
2 * Copyright (C) 2006 Andre Noll <maan@tuebingen.mpg.de>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file dccp_recv.c paraslash's dccp receiver */
8
9 /*
10 * based on client.c of dccp-cs-0.01.tar.bz2,
11 * (C) 2005 Ian McDonald <imcdnzl@gmail.com>
12 */
13
14 #include <netinet/in.h>
15 #include <sys/socket.h>
16 #include <regex.h>
17 #include <sys/types.h>
18 #include <arpa/inet.h>
19 #include <sys/un.h>
20 #include <netdb.h>
21 #include <lopsub.h>
22
23 #include "recv_cmd.lsg.h"
24 #include "para.h"
25 #include "error.h"
26 #include "list.h"
27 #include "sched.h"
28 #include "buffer_tree.h"
29 #include "recv.h"
30 #include "string.h"
31 #include "net.h"
32 #include "fd.h"
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 static int dccp_recv_open(struct receiver_node *rn)
67 {
68 struct lls_parse_result *lpr = rn->lpr;
69 struct flowopts *fo = NULL;
70 uint8_t *ccids = NULL;
71 int fd, ret, i;
72 const struct lls_opt_result *r_c = RECV_CMD_OPT_RESULT(DCCP, CCID, lpr);
73 const char *host = RECV_CMD_OPT_STRING_VAL(DCCP, HOST, lpr);
74 uint32_t port = RECV_CMD_OPT_UINT32_VAL(DCCP, PORT, lpr);
75 unsigned given;
76
77 ret = dccp_recv_ccid_support_check(lpr);
78 if (ret < 0)
79 return ret;
80 /* Copy CCID preference list (u8 array required) */
81 given = lls_opt_given(r_c);
82 if (given) {
83 ccids = para_malloc(given);
84 fo = flowopt_new();
85 for (i = 0; i < given; i++)
86 ccids[i] = lls_int32_val(i, r_c);
87 OPT_ADD(fo, SOL_DCCP, DCCP_SOCKOPT_CCID, ccids, i);
88 }
89
90 fd = makesock(IPPROTO_DCCP, 0, host, port, fo);
91 flowopt_cleanup(fo);
92 free(ccids);
93 if (fd < 0)
94 return fd;
95 /*
96 * Disable unused CCIDs: the receiver does not send any application
97 * data to the server. By shutting down this unused path we reduce
98 * internal processing costs, as the unused CCIDs (in the kernel) are
99 * then bypassed.
100 */
101 if (shutdown(fd, SHUT_WR) < 0) {
102 ret = -ERRNO_TO_PARA_ERROR(errno);
103 goto err;
104 }
105 ret = mark_fd_nonblocking(fd);
106 if (ret < 0)
107 goto err;
108 rn->btrp = btr_pool_new("dccp_recv", 320 * 1024);
109 rn->fd = fd;
110 return 1;
111 err:
112 close(fd);
113 return ret;
114 }
115
116 static void dccp_recv_pre_select(struct sched *s, void *context)
117 {
118 struct receiver_node *rn = context;
119
120 if (generic_recv_pre_select(s, rn) <= 0)
121 return;
122 para_fd_set(rn->fd, &s->rfds, &s->max_fileno);
123 }
124
125 static int dccp_recv_post_select(struct sched *s, void *context)
126 {
127 struct receiver_node *rn = context;
128 struct btr_node *btrn = rn->btrn;
129 struct iovec iov[2];
130 int ret, iovcnt;
131 size_t num_bytes;
132
133 ret = task_get_notification(rn->task);
134 if (ret < 0)
135 goto out;
136 ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
137 if (ret <= 0)
138 goto out;
139 iovcnt = btr_pool_get_buffers(rn->btrp, iov);
140 ret = -E_DCCP_OVERRUN;
141 if (iovcnt == 0)
142 goto out;
143 ret = readv_nonblock(rn->fd, iov, iovcnt, &s->rfds, &num_bytes);
144 if (num_bytes == 0)
145 goto out;
146 if (num_bytes <= iov[0].iov_len) /* only the first buffer was filled */
147 btr_add_output_pool(rn->btrp, num_bytes, btrn);
148 else { /* both buffers contain data */
149 btr_add_output_pool(rn->btrp, iov[0].iov_len, btrn);
150 btr_add_output_pool(rn->btrp, num_bytes - iov[0].iov_len, btrn);
151 }
152 out:
153 if (ret < 0)
154 btr_remove_node(&rn->btrn);
155 return ret;
156 }
157
158 /** See \ref recv_init(). */
159 const struct receiver lsg_recv_cmd_com_dccp_user_data = {
160 .open = dccp_recv_open,
161 .close = dccp_recv_close,
162 .pre_select = dccp_recv_pre_select,
163 .post_select = dccp_recv_post_select,
164 };