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