bdc7681e1de02469daaa0bc10698514eac563583
[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 static int dccp_write(int fd, const char *buf, size_t len)
112 {
113 size_t written = 0;
114 int ret = 0;
115
116 while (written < len) {
117 ret = write(fd, buf + written, PARA_MIN(1024, len - written));
118 /*
119 * Error handling: CCID3 has a sending wait queue which fills up and is
120 * emptied asynchronously. The EAGAIN case means that there is currently
121 * no space in the wait queue, but this can change at any moment and is
122 * thus not an error condition. Keep polling until an entry becomes free.
123 */
124 if (ret < 0 && errno == EAGAIN)
125 continue;
126 if (ret < 0)
127 return -E_DCCP_WRITE;
128 written += ret;
129 }
130 return written;
131 }
132
133 static void dccp_send(long unsigned current_chunk,
134 __a_unused long unsigned chunks_sent, const char *buf, size_t len)
135 {
136 struct dccp_client *dc, *tmp;
137 int ret;
138 char *header_buf;
139 unsigned header_len;
140
141 if (listen_fd < 0 || !len)
142 return;
143
144 list_for_each_entry_safe(dc, tmp, &clients, node) {
145 ret = write_ok(dc->fd);
146 if (ret < 0) {
147 dccp_shutdown_client(dc);
148 continue;
149 }
150 if (!ret)
151 continue;
152 if (!dc->header_sent && current_chunk) {
153 header_buf = vss_get_header(&header_len);
154 if (header_buf && header_len > 0) {
155 ret = dccp_write(dc->fd, header_buf, header_len);
156 if (ret != header_len) {
157 int err = errno;
158 PARA_ERROR_LOG("header write: %d/%u (%s)\n",
159 ret, header_len, ret < 0?
160 strerror(err) : "");
161 dccp_shutdown_client(dc);
162 continue;
163 }
164 dc->header_sent = 1;
165 ret = write_ok(dc->fd);
166 if (ret < 0) {
167 dccp_shutdown_client(dc);
168 continue;
169 }
170 if (!ret)
171 continue;
172 }
173 }
174 // PARA_DEBUG_LOG("writing %d bytes to fd %d\n", len, dc->fd);
175 ret = dccp_write(dc->fd, buf, len);
176 if (ret != len)
177 dccp_shutdown_client(dc);
178 }
179 }
180
181 static void dccp_shutdown_clients(void)
182 {
183 struct dccp_client *dc, *tmp;
184
185 list_for_each_entry_safe(dc, tmp, &clients, node)
186 dccp_shutdown_client(dc);
187 }
188
189 static char *dccp_info(void)
190 {
191 static char *buf;
192 int num_clients = 0;
193 struct dccp_client *dc, *tmp;
194
195 free(buf);
196 list_for_each_entry_safe(dc, tmp, &clients, node)
197 num_clients++;
198 buf = make_message("dccp connected clients: %d\n", num_clients);
199 return buf;
200 }
201
202 static char *dccp_help(void)
203 {
204 return make_message("no help available\n");
205 }
206
207 /**
208 * the init function of the dccp sender
209 *
210 * \param s pointer to the dccp sender struct
211 *
212 * It initializes all function pointers of \a s and starts
213 * listening on the given port.
214 */
215 void dccp_send_init(struct sender *s)
216 {
217 int ret;
218
219 INIT_LIST_HEAD(&clients);
220 s->info = dccp_info;
221 s->send = dccp_send;
222 s->pre_select = dccp_pre_select;
223 s->post_select = dccp_post_select;
224 s->shutdown_clients = dccp_shutdown_clients;
225 s->help = dccp_help;
226 s->client_cmds[SENDER_ON] = NULL;
227 s->client_cmds[SENDER_OFF] = NULL;
228 s->client_cmds[SENDER_DENY] = NULL;
229 s->client_cmds[SENDER_ALLOW] = NULL;
230 s->client_cmds[SENDER_ADD] = NULL;
231 s->client_cmds[SENDER_DELETE] = NULL;
232 self = s;
233 ret = dccp_open();
234 if (ret < 0) {
235 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
236 s->status = SENDER_OFF;
237 } else
238 s->status = SENDER_ON;
239 }