Add GPL headers for dccp.c dccp.h, dccp_recv.c, and dccp_send.c
[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 #include "server.h"
20 #include "net.h"
21 #include "list.h"
22 #include "afs.h"
23 #include "send.h"
24 #include "dccp.h"
25 #include "error.h"
26 #include "string.h"
27 #include "server.cmdline.h"
28 extern struct gengetopt_args_info conf;
29 /** the list of connected clients **/
30 static struct list_head clients;
31 static int listen_fd = -1;
32 static struct sender *self;
33
34 /** describes one connected client */
35 struct dccp_client {
36 /** the dccp socket */
37 int fd;
38 /** address information about the client */
39 struct sockaddr_in addr;
40 /** the position of this client in the client list */
41 struct list_head node;
42 int header_sent; /* non-zero if audio file header has been sent */
43 };
44
45 static void dccp_pre_select(__unused struct audio_format *af, int *max_fileno, fd_set *rfds,
46 __unused fd_set *wfds)
47 {
48 if (listen_fd < 0)
49 return;
50 FD_SET(listen_fd, rfds);
51 *max_fileno = MAX(*max_fileno, listen_fd);
52 }
53
54 static void dccp_post_select(__unused struct audio_format *af, fd_set *rfds,
55 __unused fd_set *wfds)
56 {
57 struct dccp_client *dc;
58 int ret;
59
60 if (!FD_ISSET(listen_fd, rfds))
61 return;
62 PARA_NOTICE_LOG("%s", "accepting...\n");
63 dc = para_calloc(sizeof(struct dccp_client));
64 ret = para_accept(listen_fd, &dc->addr, sizeof(struct sockaddr_in));
65 if (ret < 0) {
66 PARA_ERROR_LOG("%s", PARA_STRERROR(-ret));
67 return;
68 }
69 PARA_NOTICE_LOG("%s", "connection\n");
70 dc->fd = ret;
71 list_add(&dc->node, &clients);
72 }
73
74 static int dccp_open(void)
75 {
76 struct sockaddr_in servaddr;
77 int ret;
78
79 ret = dccp_get_socket();
80 if (ret < 0)
81 return ret;
82 listen_fd = ret;
83
84 bzero(&servaddr, sizeof(servaddr));
85 servaddr.sin_family = AF_INET;
86 servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
87 servaddr.sin_port = htons(conf.dccp_port_arg);
88 ret = bind(listen_fd, (struct sockaddr *)&servaddr, sizeof(servaddr));
89 if (ret < 0)
90 return -E_DCCP_BIND;
91 ret = dccp_set_socket(listen_fd);
92 if (ret < 0)
93 return ret;
94 ret = listen(listen_fd, 0);
95 if (ret < 0)
96 return -E_DCCP_LISTEN;
97 PARA_DEBUG_LOG("listening on fd %d\n", listen_fd);
98 return 1;
99 }
100
101 static void dccp_shutdown_client(struct dccp_client *dc)
102 {
103 close(dc->fd);
104 list_del(&dc->node);
105 free(dc);
106 }
107
108 static void dccp_send(__unused struct audio_format *af,
109 long unsigned current_chunk,
110 __unused long unsigned chunks_sent, const char *buf, size_t len)
111 {
112 struct dccp_client *dc, *tmp;
113 int ret, header_len;
114 char *header_buf;
115
116 if (listen_fd < 0 || !len)
117 return;
118
119 list_for_each_entry_safe(dc, tmp, &clients, node) {
120 if (!_write_ok(dc->fd))
121 continue;
122 if (!dc->header_sent && af->get_header_info && current_chunk) {
123 header_buf = af->get_header_info(&header_len);
124 if (!header_buf || header_len <= 0)
125 continue; /* header not yet available */
126 ret = write(dc->fd, header_buf, header_len);
127 if (ret != header_len) {
128 dccp_shutdown_client(dc);
129 continue;
130 }
131 if (!_write_ok(dc->fd))
132 continue;
133 }
134 PARA_DEBUG_LOG("writing %d bytes to fd %d\n", len, dc->fd);
135 ret = write(dc->fd, buf, len);
136 if (ret != len)
137 dccp_shutdown_client(dc);
138 }
139 }
140
141 static void dccp_shutdown_clients(void)
142 {
143 struct dccp_client *dc, *tmp;
144
145 list_for_each_entry_safe(dc, tmp, &clients, node)
146 dccp_shutdown_client(dc);
147 }
148
149 static char *dccp_info(void)
150 {
151 static char *buf;
152 int num_clients = 0;
153 struct dccp_client *dc, *tmp;
154
155 free(buf);
156 list_for_each_entry_safe(dc, tmp, &clients, node)
157 num_clients++;
158 buf = make_message("%d connected clients\n", num_clients);
159 return buf;
160 }
161
162 static char *dccp_help(void)
163 {
164 return make_message("no help available\n");
165 }
166
167 void dccp_send_init(struct sender *s)
168 {
169 int ret;
170
171 INIT_LIST_HEAD(&clients);
172 s->info = dccp_info;
173 s->send = dccp_send;
174 s->pre_select = dccp_pre_select;
175 s->post_select = dccp_post_select;
176 s->shutdown_clients = dccp_shutdown_clients;
177 s->help = dccp_help;
178 s->client_cmds[SENDER_ON] = NULL;
179 s->client_cmds[SENDER_OFF] = NULL;
180 s->client_cmds[SENDER_DENY] = NULL;
181 s->client_cmds[SENDER_ALLOW] = NULL;
182 s->client_cmds[SENDER_ADD] = NULL;
183 s->client_cmds[SENDER_DELETE] = NULL;
184 self = s;
185 ret = dccp_open();
186 if (ret < 0)
187 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
188 else
189 s->status = SENDER_ON;
190 }