Make crypo a per fd feature
[paraslash.git] / dccp_send.c
1 /*
2  * Copyright (C) 2006 Andre Noll <maan@systemlinux.org>
3  *
4  *     This program is free software; you can redistribute it and/or modify
5  *     it under the terms of the GNU General Public License as published by
6  *     the Free Software Foundation; either version 2 of the License, or
7  *     (at your option) any later version.
8  *
9  *     This program is distributed in the hope that it will be useful,
10  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *     GNU General Public License for more details.
13  *
14  *     You should have received a copy of the GNU General Public License
15  *     along with this program; if not, write to the Free Software
16  *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
17  */
18
19 /** \file dccp_send.c paraslash's dccp sender */
20
21 /*
22  * based on server.c of dccp-cs-0.01.tar.bz2,
23  * (C) 2005 Ian McDonald <imcdnzl@gmail.com>
24  */
25
26 #include "server.h"
27 #include "net.h"
28 #include "list.h"
29 #include "afs.h"
30 #include "send.h"
31 #include "dccp.h"
32 #include "error.h"
33 #include "string.h"
34 #include "fd.h"
35 #include "close_on_fork.h"
36 #include "server.cmdline.h"
37 extern struct gengetopt_args_info conf;
38 /** the list of connected clients **/
39 static struct list_head clients;
40 static int listen_fd = -1;
41 static struct sender *self;
42
43 /** describes one connected client */
44 struct dccp_client {
45         /** the dccp socket */
46         int fd;
47         /** address information about the client */
48         struct sockaddr_in addr;
49         /** the position of this client in the client list */
50         struct list_head node;
51         /** non-zero if audio file header has been sent */
52         int header_sent;
53 };
54
55 static void dccp_pre_select( int *max_fileno, fd_set *rfds,
56                 __a_unused fd_set *wfds)
57 {
58         if (listen_fd < 0)
59                 return;
60         FD_SET(listen_fd, rfds);
61         *max_fileno = PARA_MAX(*max_fileno, listen_fd);
62 }
63
64 static void dccp_post_select(fd_set *rfds, __a_unused fd_set *wfds)
65 {
66         struct dccp_client *dc;
67         int ret;
68
69         if (!FD_ISSET(listen_fd, rfds))
70                 return;
71         dc = para_calloc(sizeof(struct dccp_client));
72         ret = para_accept(listen_fd, &dc->addr, sizeof(struct sockaddr_in));
73         if (ret < 0) {
74                 PARA_ERROR_LOG("%s", PARA_STRERROR(-ret));
75                 return;
76         }
77         PARA_NOTICE_LOG("connection from %s\n", inet_ntoa(dc->addr.sin_addr));
78         dc->fd = ret;
79         list_add(&dc->node, &clients);
80         add_close_on_fork_list(dc->fd);
81         mark_fd_nonblock(dc->fd);
82 }
83
84 static int dccp_open(void)
85 {
86         struct sockaddr_in servaddr;
87         int ret;
88
89         ret = dccp_get_socket();
90         if (ret < 0)
91                 return ret;
92         listen_fd = ret;
93
94         bzero(&servaddr, sizeof(servaddr));
95         servaddr.sin_family = AF_INET;
96         servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
97         servaddr.sin_port = htons(conf.dccp_port_arg);
98         ret = bind(listen_fd, (struct sockaddr *)&servaddr, sizeof(servaddr));
99         if (ret < 0)
100                 return -E_DCCP_BIND;
101         ret = dccp_set_socket(listen_fd);
102         if (ret < 0)
103                 return ret;
104         ret = listen(listen_fd, 0);
105         if (ret < 0)
106                 return -E_DCCP_LISTEN;
107         PARA_DEBUG_LOG("listening on fd %d\n", listen_fd);
108         add_close_on_fork_list(listen_fd);
109         mark_fd_nonblock(listen_fd);
110         return 1;
111 }
112
113 static void dccp_shutdown_client(struct dccp_client *dc)
114 {
115         PARA_DEBUG_LOG("shutting down %s (fd %d)\n", inet_ntoa(dc->addr.sin_addr),
116                 dc->fd);
117         close(dc->fd);
118         del_close_on_fork_list(dc->fd);
119         list_del(&dc->node);
120         free(dc);
121 }
122
123 static int dccp_write(int fd, const char *buf, size_t len)
124 {
125         size_t send, written = 0;
126         int ret;
127 again:
128         send = PARA_MIN(1024, len - written);
129         ret = write(fd, buf + written, send);
130         if (ret < 0)
131                 goto err_out;
132         written += ret;
133         if (written >= len)
134                 return written;
135         ret = write_ok(fd);
136         if (ret > 0)
137                 goto again;
138 err_out:
139         return -E_DCCP_WRITE;
140 }
141
142 static void dccp_send(long unsigned current_chunk,
143                 __a_unused long unsigned chunks_sent, const char *buf, size_t len)
144 {
145         struct dccp_client *dc, *tmp;
146         int ret, header_len;
147         char *header_buf;
148
149         if (listen_fd < 0 || !len)
150                 return;
151
152         list_for_each_entry_safe(dc, tmp, &clients, node) {
153                 ret = write_ok(dc->fd);
154                 if (ret < 0) {
155                         dccp_shutdown_client(dc);
156                         continue;
157                 }
158                 if (!ret)
159                         continue;
160                 if (!dc->header_sent && current_chunk) {
161                         header_buf = afs_get_header(&header_len);
162                         if (header_buf && header_len > 0) {
163                                 ret = dccp_write(dc->fd, header_buf, header_len);
164                                 if (ret != header_len) {
165                                         int err = errno;
166                                         PARA_ERROR_LOG("header write: %d/%d (%s)\n",
167                                                 ret, header_len, ret < 0?
168                                                 strerror(err) : "");
169                                         dccp_shutdown_client(dc);
170                                         continue;
171                                 }
172                                 dc->header_sent = 1;
173                                 ret = write_ok(dc->fd);
174                                 if (ret < 0) {
175                                         dccp_shutdown_client(dc);
176                                         continue;
177                                 }
178                                 if (!ret)
179                                         continue;
180                         }
181                 }
182 //              PARA_DEBUG_LOG("writing %d bytes to fd %d\n", len, dc->fd);
183                 ret = dccp_write(dc->fd, buf, len);
184                 if (ret != len)
185                         dccp_shutdown_client(dc);
186         }
187 }
188
189 static void dccp_shutdown_clients(void)
190 {
191         struct dccp_client *dc, *tmp;
192
193         list_for_each_entry_safe(dc, tmp, &clients, node)
194                 dccp_shutdown_client(dc);
195 }
196
197 static char *dccp_info(void)
198 {
199         static char *buf;
200         int num_clients = 0;
201         struct dccp_client *dc, *tmp;
202
203         free(buf);
204         list_for_each_entry_safe(dc, tmp, &clients, node)
205                 num_clients++;
206         buf = make_message("dccp connected clients: %d\n", num_clients);
207         return buf;
208 }
209
210 static char *dccp_help(void)
211 {
212         return make_message("no help available\n");
213 }
214
215 /**
216  * the init function of the dccp sender
217  *
218  * \param s pointer to the dccp sender struct
219  *
220  * It initializes all function pointers of \a s and starts
221  * listening on the given port.
222  */
223 void dccp_send_init(struct sender *s)
224 {
225         int ret;
226
227         INIT_LIST_HEAD(&clients);
228         s->info = dccp_info;
229         s->send = dccp_send;
230         s->pre_select = dccp_pre_select;
231         s->post_select = dccp_post_select;
232         s->shutdown_clients = dccp_shutdown_clients;
233         s->help = dccp_help;
234         s->client_cmds[SENDER_ON] = NULL;
235         s->client_cmds[SENDER_OFF] = NULL;
236         s->client_cmds[SENDER_DENY] = NULL;
237         s->client_cmds[SENDER_ALLOW] = NULL;
238         s->client_cmds[SENDER_ADD] = NULL;
239         s->client_cmds[SENDER_DELETE] = NULL;
240         self = s;
241         ret = dccp_open();
242         if (ret < 0) {
243                 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
244                 s->status = SENDER_OFF;
245         } else
246                 s->status = SENDER_ON;
247 }