First draft of the dccp sender/receiver pair.
[paraslash.git] / dccp_recv.c
1 #include "para.h"
2 #include "error.h"
3 #include "dccp.h"
4 #include "recv.h"
5 #include "string.h"
6 #include "net.h"
7
8 #include "dccp_recv.cmdline.h"
9
10 /* needed by getaddrinfo */
11 #include <sys/types.h>
12 #include <sys/socket.h>
13 #include <netdb.h>
14
15
16 #define DCCP_BUFSIZE 4096
17
18 /**
19 * data specific to the dccp receiver
20 *
21 * \sa receiver receiver_node
22 */
23 struct private_dccp_recv_data {
24 /** the file descriptor for the dccp socket */
25 int fd;
26 };
27
28
29 static void dccp_recv_close(struct receiver_node *rn)
30 {
31
32 struct private_dccp_recv_data *pdd = rn->private_data;
33
34 if (pdd && pdd->fd > 0)
35 close(pdd->fd);
36 free(rn->buf);
37 rn->buf = NULL;
38 free(rn->private_data);
39 rn->private_data = NULL;
40 }
41
42
43 static int dccp_recv_open(struct receiver_node *rn)
44 {
45 struct private_dccp_recv_data *pdd;
46 struct gengetopt_args_info *conf = rn->conf;
47 int ret;
48 struct addrinfo *ai;
49 char *tmp;
50
51 rn->buf = para_calloc(DCCP_BUFSIZE);
52 rn->private_data = para_calloc(sizeof(struct private_dccp_recv_data));
53 pdd = rn->private_data;
54 ret = dccp_get_socket();
55 if (ret < 0)
56 goto err_out;
57 pdd->fd = ret;
58
59 tmp = make_message("%d", conf->port_arg);
60 ret = getaddrinfo(conf->host_arg, tmp, NULL, &ai);
61 free(tmp);
62 if (ret) {
63 ret = -E_ADDR_INFO;
64 goto err_out;
65 }
66 ret = dccp_set_socket(pdd->fd);
67 if (ret < 0)
68 goto err_out;
69 PARA_NOTICE_LOG("connecting to %s:%d\n", conf->host_arg, conf->port_arg);
70 ret = -E_DCCP_CONNECT;
71 if (connect(pdd->fd, ai->ai_addr, ai->ai_addrlen) < 0)
72 goto err_out;
73 return 1;
74 err_out:
75 dccp_recv_close(rn);
76 return ret;
77 }
78
79 static void dccp_shutdown(void)
80 {
81 ; /* nothing to do */
82 }
83
84 static void *dccp_recv_parse_config(int argc, char **argv)
85 {
86 struct gengetopt_args_info *tmp = para_calloc(sizeof(struct gengetopt_args_info));
87
88 if (!dccp_recv_cmdline_parser(argc, argv, tmp))
89 return tmp;
90 free(tmp);
91 return NULL;
92 }
93
94 static int dccp_recv_pre_select(struct receiver_node *rn, fd_set *rfds,
95 __unused fd_set *wfds, __unused struct timeval *timeout)
96 {
97 struct private_dccp_recv_data *pdd = rn->private_data;
98
99 if (pdd)
100 FD_SET(pdd->fd, rfds);
101 return pdd->fd;
102 }
103
104 static int dccp_recv_post_select(struct receiver_node *rn, int select_ret,
105 fd_set *rfds, __unused fd_set *wfds)
106 {
107 int ret;
108 struct private_dccp_recv_data *pdd = rn->private_data;
109
110 if (!select_ret || !pdd || !FD_ISSET(pdd->fd, rfds))
111 return 1; /* nothing to do */
112 if (rn->loaded >= DCCP_BUFSIZE)
113 return -E_DCCP_OVERRUN;
114 ret = recv_bin_buffer(pdd->fd, rn->buf + rn->loaded,
115 DCCP_BUFSIZE - rn->loaded);
116 if (ret <= 0)
117 return ret;
118 rn->loaded += ret;
119 return 1;
120 }
121
122 /**
123 * the init function of the dccp receiver
124 *
125 * \param r pointer to the receiver struct to initialize
126 *
127 * Initialize all function pointers of \a r
128 */
129 void dccp_recv_init(struct receiver *r)
130 {
131 r->shutdown = dccp_shutdown;
132 r->open = dccp_recv_open;
133 r->close = dccp_recv_close;
134 r->pre_select = dccp_recv_pre_select;
135 r->post_select = dccp_recv_post_select;
136 r->parse_config = dccp_recv_parse_config;
137 }