27343fbc0874e96865880eaa5866d1e65a1fa385
2 * Copyright (C) 2006-2007 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file dccp_send.c paraslash's dccp sender */
10 * based on server.c of dccp-cs-0.01.tar.bz2,
11 * (C) 2005 Ian McDonald <imcdnzl@gmail.com>
23 #include "close_on_fork.h"
24 #include "server.cmdline.h"
26 /** the list of connected clients **/
27 static struct list_head clients
;
28 static int listen_fd
= -1;
29 static struct sender
*self
;
31 /** describes one connected client */
33 /** the dccp socket */
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 */
43 static void dccp_pre_select( int *max_fileno
, fd_set
*rfds
,
44 __a_unused fd_set
*wfds
)
48 FD_SET(listen_fd
, rfds
);
49 *max_fileno
= PARA_MAX(*max_fileno
, listen_fd
);
52 static void dccp_post_select(fd_set
*rfds
, __a_unused fd_set
*wfds
)
54 struct dccp_client
*dc
;
57 if (!FD_ISSET(listen_fd
, rfds
))
59 dc
= para_calloc(sizeof(struct dccp_client
));
60 ret
= para_accept(listen_fd
, &dc
->addr
, sizeof(struct sockaddr_in
));
62 PARA_ERROR_LOG("%s", PARA_STRERROR(-ret
));
65 PARA_NOTICE_LOG("connection from %s\n", inet_ntoa(dc
->addr
.sin_addr
));
67 para_list_add(&dc
->node
, &clients
);
68 add_close_on_fork_list(dc
->fd
);
69 mark_fd_nonblock(dc
->fd
);
72 static int dccp_open(void)
74 struct sockaddr_in servaddr
;
77 ret
= dccp_get_socket();
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
));
89 ret
= dccp_set_socket(listen_fd
);
92 ret
= listen(listen_fd
, 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
);
101 static void dccp_shutdown_client(struct dccp_client
*dc
)
103 PARA_DEBUG_LOG("shutting down %s (fd %d)\n", inet_ntoa(dc
->addr
.sin_addr
),
106 del_close_on_fork_list(dc
->fd
);
111 /** give up if write would block that many times */
112 #define DCCP_WRITE_RETRIES 100
114 static int dccp_write(int fd
, const char *buf
, size_t len
)
116 size_t size
, written
= 0;
117 int ret
, retries
= 0;
119 size
= PARA_MIN(1024, len
- written
);
120 ret
= write(fd
, buf
+ written
, size
);
122 if (errno
!= EAGAIN
|| retries
++ > DCCP_WRITE_RETRIES
)
124 PARA_DEBUG_LOG("EAGAIN #%d@%zd/%zd\n", retries
, written
, len
);
135 return -E_DCCP_WRITE
;
138 static void dccp_send(long unsigned current_chunk
,
139 __a_unused
long unsigned chunks_sent
, const char *buf
, size_t len
)
141 struct dccp_client
*dc
, *tmp
;
146 if (listen_fd
< 0 || !len
)
149 list_for_each_entry_safe(dc
, tmp
, &clients
, node
) {
150 ret
= write_ok(dc
->fd
);
152 dccp_shutdown_client(dc
);
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
) {
163 PARA_ERROR_LOG("header write: %d/%u (%s)\n",
164 ret
, header_len
, ret
< 0?
166 dccp_shutdown_client(dc
);
170 ret
= write_ok(dc
->fd
);
172 dccp_shutdown_client(dc
);
179 // PARA_DEBUG_LOG("writing %d bytes to fd %d\n", len, dc->fd);
180 ret
= dccp_write(dc
->fd
, buf
, len
);
182 dccp_shutdown_client(dc
);
186 static void dccp_shutdown_clients(void)
188 struct dccp_client
*dc
, *tmp
;
190 list_for_each_entry_safe(dc
, tmp
, &clients
, node
)
191 dccp_shutdown_client(dc
);
194 static char *dccp_info(void)
198 struct dccp_client
*dc
, *tmp
;
201 list_for_each_entry_safe(dc
, tmp
, &clients
, node
)
203 buf
= make_message("dccp connected clients: %d\n", num_clients
);
207 static char *dccp_help(void)
209 return make_message("no help available\n");
213 * the init function of the dccp sender
215 * \param s pointer to the dccp sender struct
217 * It initializes all function pointers of \a s and starts
218 * listening on the given port.
220 void dccp_send_init(struct sender
*s
)
224 INIT_LIST_HEAD(&clients
);
227 s
->pre_select
= dccp_pre_select
;
228 s
->post_select
= dccp_post_select
;
229 s
->shutdown_clients
= dccp_shutdown_clients
;
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
;
240 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret
));
241 s
->status
= SENDER_OFF
;
243 s
->status
= SENDER_ON
;