* Licensed under the GPL v2. For licencing details see COPYING.
*/
-/** \file dccp_send.c paraslash's dccp sender */
+/** \file dccp_send.c Paraslash's dccp sender. */
/*
* based on server.c of dccp-cs-0.01.tar.bz2,
#include "server.cmdline.h"
#include "acl.h"
-/** the list of connected clients **/
-static struct list_head clients;
-/** The whitelist/blacklist. */
-static struct list_head dccp_acl;
-static int listen_fd = -1;
-
-/** Maximal number of bytes in a chunk queue. */
-#define DCCP_MAX_PENDING_BYTES 40000
-
/** Do not write more than that many bytes at once. */
#define DCCP_MAX_BYTES_PER_WRITE 1024
+static struct sender_status dccp_sender_status, *dss = &dccp_sender_status;
+
static void dccp_pre_select(int *max_fileno, fd_set *rfds,
__a_unused fd_set *wfds)
{
- if (listen_fd >= 0)
- para_fd_set(listen_fd, rfds, max_fileno);
+ if (dss->listen_fd >= 0)
+ para_fd_set(dss->listen_fd, rfds, max_fileno);
}
static void dccp_post_select(fd_set *rfds, __a_unused fd_set *wfds)
{
struct sender_client *sc;
- int ret, fd;
- if (listen_fd < 0 || !FD_ISSET(listen_fd, rfds))
+ if (dss->listen_fd < 0 || !FD_ISSET(dss->listen_fd, rfds))
return;
- ret = para_accept(listen_fd, NULL, 0);
- if (ret < 0) {
- PARA_ERROR_LOG("%s\n", para_strerror(-ret));
+ sc = accept_sender_client(dss);
+ if (!sc)
return;
- }
- fd = ret;
/*
* Bypass unused CCID paths: the sender does not receive application data
* from the client; by shutting down this unused communication path we can
* reduce processing costs a bit. See analogous comment in dccp_recv.c.
*/
- if (shutdown(fd, SHUT_RD) < 0) {
- ret = -ERRNO_TO_PARA_ERROR(errno);
- goto err;
- }
- ret = mark_fd_nonblocking(fd);
- if (ret < 0)
- goto err;
- ret = acl_check_access(fd, &dccp_acl, conf.dccp_default_deny_given);
- if (ret < 0)
- goto err;
- sc = para_calloc(sizeof(*sc));
- sc->fd = fd;
- sc->name = make_message("%s", remote_name(sc->fd));
- PARA_NOTICE_LOG("connection from %s\n", sc->name);
- para_list_add(&sc->node, &clients);
- add_close_on_fork_list(sc->fd);
- sc->cq = cq_new(DCCP_MAX_PENDING_BYTES);
- return;
-err:
- PARA_ERROR_LOG("%s\n", para_strerror(-ret));
- close(fd);
+ if (shutdown(sc->fd, SHUT_RD) >= 0)
+ return;
+ PARA_WARNING_LOG("%s\n", strerror(errno));
+ shutdown_client(sc, dss);
}
static void dccp_send(long unsigned current_chunk,
{
struct sender_client *sc, *tmp;
- list_for_each_entry_safe(sc, tmp, &clients, node)
- send_chunk(sc, DCCP_MAX_BYTES_PER_WRITE, current_chunk, buf,
+ list_for_each_entry_safe(sc, tmp, &dss->client_list, node)
+ send_chunk(sc, dss, DCCP_MAX_BYTES_PER_WRITE, current_chunk, buf,
len);
}
static void dccp_shutdown_clients(void)
{
- struct sender_client *sc, *tmp;
+ shutdown_clients(dss);
+}
- list_for_each_entry_safe(sc, tmp, &clients, node)
- shutdown_client(sc);
+static int dccp_com_on(__a_unused struct sender_command_data *scd)
+{
+ return generic_com_on(dss, IPPROTO_DCCP);
}
-static int dccp_com_deny(struct sender_command_data *scd)
+static int dccp_com_off(__a_unused struct sender_command_data *scd)
{
- acl_deny(scd->addr, scd->netmask, &dccp_acl,
- conf.dccp_default_deny_given);
+ generic_com_off(dss);
return 1;
}
-static int dccp_com_allow(struct sender_command_data *scd)
+
+static int dccp_com_deny(struct sender_command_data *scd)
{
- acl_allow(scd->addr, scd->netmask, &dccp_acl,
- conf.dccp_default_deny_given);
+ generic_com_deny(scd, dss);
return 1;
}
-static char *dccp_info(void)
+static int dccp_com_allow(struct sender_command_data *scd)
{
- int num_clients = 0;
- struct sender_client *sc, *tmp;
-
- list_for_each_entry_safe(sc, tmp, &clients, node)
- num_clients++;
- return make_message("dccp connected clients: %d\n", num_clients);
+ generic_com_allow(scd, dss);
+ return 1;
}
-static char *dccp_help(void)
+static char *dccp_info(void)
{
- return make_message("no help available\n");
+ return get_sender_info(dss, "dccp");
}
/**
{
int ret;
- INIT_LIST_HEAD(&clients);
s->info = dccp_info;
s->send = dccp_send;
s->pre_select = dccp_pre_select;
s->post_select = dccp_post_select;
s->shutdown_clients = dccp_shutdown_clients;
- s->help = dccp_help;
- s->client_cmds[SENDER_ON] = NULL;
- s->client_cmds[SENDER_OFF] = NULL;
+ s->help = generic_sender_help;
+ s->client_cmds[SENDER_ON] = dccp_com_on;
+ s->client_cmds[SENDER_OFF] = dccp_com_off;
s->client_cmds[SENDER_DENY] = dccp_com_deny;
s->client_cmds[SENDER_ALLOW] = dccp_com_allow;
s->client_cmds[SENDER_ADD] = NULL;
s->client_cmds[SENDER_DELETE] = NULL;
- acl_init(&dccp_acl, conf.dccp_access_arg, conf.dccp_access_given);
- ret = open_sender(IPPROTO_DCCP, conf.dccp_port_arg);
+
+ init_sender_status(dss, conf.dccp_access_arg, conf.dccp_access_given,
+ conf.dccp_port_arg, conf.dccp_max_clients_arg,
+ conf.dccp_default_deny_given);
+ ret = generic_com_on(dss, IPPROTO_DCCP);
if (ret < 0)
PARA_ERROR_LOG("%s\n", para_strerror(-ret));
- else
- listen_fd = ret;
}
/** Message sent to clients that do not send a valid get request. */
#define HTTP_ERR_MSG "HTTP/1.0 400 Bad Request\n"
-
/** The possible states of a client from the server's POV. */
-enum http_status {
+enum http_client_status {
/** We accepted the connection on the tcp socket. */
HTTP_CONNECTED,
/** Successfully received the get request. */
HTTP_INVALID_GET_REQUEST
};
-/** Clients will be kicked if there are more than that many bytes pending. */
-#define MAX_BACKLOG 400000
-/** The list of connected clients. */
-static struct list_head clients;
-/** The whitelist/blacklist. */
-static struct list_head http_acl;
-
-static int listen_fd = -1, numclients;
-
struct private_http_sender_data {
- enum http_status status;
+ enum http_client_status status;
};
+static struct sender_status http_sender_status, *hss = &http_sender_status;
+
static int http_send_msg(struct sender_client *sc, const char *msg)
{
int ret = send_buffer(sc->fd, msg);
if (ret < 0)
- shutdown_client(sc);
+ shutdown_client(sc, hss);
return ret;
}
static void http_shutdown_clients(void)
{
- struct sender_client *sc, *tmp;
- list_for_each_entry_safe(sc, tmp, &clients, node)
- shutdown_client(sc);
+ shutdown_clients(hss);
}
static void http_send(long unsigned current_chunk,
{
struct sender_client *sc, *tmp;
- list_for_each_entry_safe(sc, tmp, &clients, node) {
+ list_for_each_entry_safe(sc, tmp, &hss->client_list, node) {
struct private_http_sender_data *phsd = sc->private_data;
if (phsd->status != HTTP_STREAMING)
continue;
- send_chunk(sc, 0, current_chunk, buf, len);
+ send_chunk(sc, hss, 0, current_chunk, buf, len);
}
}
static void http_post_select(fd_set *rfds, __a_unused fd_set *wfds)
{
- int ret, fd;
struct sender_client *sc, *tmp;
struct private_http_sender_data *phsd;
- if (listen_fd < 0)
+ if (hss->listen_fd < 0)
return;
- list_for_each_entry_safe(sc, tmp, &clients, node) {
+ list_for_each_entry_safe(sc, tmp, &hss->client_list, node) {
phsd = sc->private_data;
switch (phsd->status) {
case HTTP_STREAMING: /* nothing to do */
break;
case HTTP_INVALID_GET_REQUEST: /* need to send err msg */
if (http_send_err_msg(sc) >= 0)
- shutdown_client(sc);
+ shutdown_client(sc, hss);
break;
}
}
- if (!FD_ISSET(listen_fd, rfds))
+ if (!FD_ISSET(hss->listen_fd, rfds))
return;
- ret = para_accept(listen_fd, NULL, 0);
- if (ret < 0) {
- PARA_ERROR_LOG("%s\n", para_strerror(-ret));
+ sc = accept_sender_client(hss);
+ if (!sc)
return;
- }
- fd = ret;
- ret = -E_MAX_CLIENTS;
- if (conf.http_max_clients_arg > 0 && numclients >=
- conf.http_max_clients_arg) {
- goto err_out;
- }
- ret = mark_fd_nonblocking(fd);
- if (ret < 0)
- goto err_out;
- ret = acl_check_access(fd, &http_acl, conf.http_default_deny_given);
- if (ret < 0)
- goto err_out;
- numclients++;
- sc = para_calloc(sizeof(*sc));
- sc->fd = fd;
- sc->name = make_message("%s", remote_name(fd));
- PARA_NOTICE_LOG("connection from %s (fd %d)\n", sc->name, fd);
phsd = para_malloc(sizeof(*phsd));
sc->private_data = phsd;
phsd->status = HTTP_CONNECTED;
- sc->cq = cq_new(MAX_BACKLOG);
- PARA_INFO_LOG("accepted client #%d: %s (fd %d)\n", numclients,
- sc->name, fd);
- para_list_add(&sc->node, &clients);
- add_close_on_fork_list(fd);
- return;
-err_out:
- PARA_WARNING_LOG("%s\n", para_strerror(-ret));
- close(fd);
}
static void http_pre_select(int *max_fileno, fd_set *rfds, __a_unused fd_set *wfds)
{
struct sender_client *sc, *tmp;
- if (listen_fd < 0)
+ if (hss->listen_fd < 0)
return;
- para_fd_set(listen_fd, rfds, max_fileno);
- list_for_each_entry_safe(sc, tmp, &clients, node) {
+ para_fd_set(hss->listen_fd, rfds, max_fileno);
+ list_for_each_entry_safe(sc, tmp, &hss->client_list, node) {
struct private_http_sender_data *phsd = sc->private_data;
if (phsd->status == HTTP_CONNECTED) /* need to recv get request */
para_fd_set(sc->fd, rfds, max_fileno);
static int http_com_on(__a_unused struct sender_command_data *scd)
{
- if (listen_fd >= 0)
- return 1;
- return open_sender(IPPROTO_TCP, conf.http_port_arg);
+ return generic_com_on(hss, IPPROTO_TCP);
}
static int http_com_off(__a_unused struct sender_command_data *scd)
{
- if (listen_fd < 0)
- return 1;
- PARA_NOTICE_LOG("closing http port %d\n", conf.http_port_arg);
- close(listen_fd);
- del_close_on_fork_list(listen_fd);
- http_shutdown_clients();
- listen_fd = -1;
+ generic_com_off(hss);
return 1;
}
static int http_com_deny(struct sender_command_data *scd)
{
- acl_deny(scd->addr, scd->netmask, &http_acl,
- conf.http_default_deny_given);
+ generic_com_deny(scd, hss);
return 1;
}
static int http_com_allow(struct sender_command_data *scd)
{
- acl_allow(scd->addr, scd->netmask, &http_acl,
- conf.http_default_deny_given);
+ generic_com_allow(scd, hss);
return 1;
}
static char *http_info(void)
{
- char *clnts = NULL, *ret;
- struct sender_client *sc, *tmp_sc;
-
- char *acl_contents = acl_get_contents(&http_acl);
- list_for_each_entry_safe(sc, tmp_sc, &clients, node) {
- char *tmp = make_message("%s%s ", clnts? clnts : "", sc->name);
- free(clnts);
- clnts = tmp;
- }
- ret = make_message(
- "http status: %s\n"
- "http tcp port: %d\n"
- "http clients: %d\n"
- "http maximal number of clients: %d%s\n"
- "http connected clients: %s\n"
- "http access %s list: %s\n",
- (listen_fd >= 0)? "on" : "off",
- conf.http_port_arg,
- numclients,
- conf.http_max_clients_arg,
- conf.http_max_clients_arg > 0? "" : " (unlimited)",
- clnts? clnts : "(none)",
- conf.http_default_deny_given? "allow" : "deny",
- acl_contents? acl_contents : "(none)"
- );
- free(acl_contents);
- free(clnts);
- return ret;
-}
-
-static char *http_help(void)
-{
- return make_message(
- "usage: {on|off}\n"
- "usage: {allow|deny} IP mask\n"
- "example: allow 127.0.0.1 32\n"
- );
+ return get_sender_info(hss, "http");
}
/**
void http_send_init(struct sender *s)
{
int ret;
- INIT_LIST_HEAD(&clients);
s->info = http_info;
s->send = http_send;
s->pre_select = http_pre_select;
s->post_select = http_post_select;
s->shutdown_clients = http_shutdown_clients;
- s->help = http_help;
+ s->help = generic_sender_help;
s->client_cmds[SENDER_ON] = http_com_on;
s->client_cmds[SENDER_OFF] = http_com_off;
s->client_cmds[SENDER_DENY] = http_com_deny;
s->client_cmds[SENDER_ALLOW] = http_com_allow;
s->client_cmds[SENDER_ADD] = NULL;
s->client_cmds[SENDER_DELETE] = NULL;
- acl_init(&http_acl, conf.http_access_arg, conf.http_access_given);
+
+ init_sender_status(hss, conf.http_access_arg, conf.http_access_given,
+ conf.http_port_arg, conf.http_max_clients_arg,
+ conf.http_default_deny_given);
if (conf.http_no_autostart_given)
return;
- ret = open_sender(IPPROTO_TCP, conf.http_port_arg);
+ ret = generic_com_on(hss, IPPROTO_TCP);
if (ret < 0)
PARA_ERROR_LOG("%s\n", para_strerror(-ret));
- else
- listen_fd = ret;
}
tgts = tmp;
}
ret = make_message(
- "ortp status: %s\n"
- "ortp default port: udp %d\n"
- "ortp targets: %s\n",
+ "ortp sender:\n"
+ "\tstatus: %s\n"
+ "\tport: udp %d\n"
+ "\ttargets: %s\n",
(sender_status == SENDER_ON)? "on" : "off",
conf.ortp_default_port_arg,
tgts? tgts : "(none)"
void *private_data;
};
-int open_sender(unsigned l4type, int port);
-void shutdown_client(struct sender_client *sc);
-void send_chunk(struct sender_client *sc, size_t max_bytes_per_write,
- long unsigned current_chunk, const char *buf, size_t len);
+struct sender_status {
+ int listen_fd;
+ int port;
+ int num_clients;
+ int max_clients;
+ int default_deny;
+ /** The list of connected clients. */
+ struct list_head client_list;
+ /** The whitelist/blacklist. */
+ struct list_head acl;
+};
+
+void shutdown_client(struct sender_client *sc, struct sender_status *ss);
+void shutdown_clients(struct sender_status *ss);
+void send_chunk(struct sender_client *sc, struct sender_status *ss,
+ size_t max_bytes_per_write, long unsigned current_chunk,
+ const char *buf, size_t len);
+void init_sender_status(struct sender_status *ss, char **access_arg, int num_access_args,
+ int port, int max_clients, int default_deny);
+char *get_sender_info(struct sender_status *ss, char *name);
+
+void generic_com_allow(struct sender_command_data *scd,
+ struct sender_status *ss);
+void generic_com_deny(struct sender_command_data *scd,
+ struct sender_status *ss);
+int generic_com_on(struct sender_status *ss, unsigned protocol);
+void generic_com_off(struct sender_status *ss);
+char *generic_sender_help(void);
+struct sender_client *accept_sender_client(struct sender_status *ss);
#include "afh.h"
#include "afs.h"
#include "server.h"
+#include "acl.h"
#include "send.h"
#include "close_on_fork.h"
#include "chunk_queue.h"
#include "vss.h"
+/** Clients will be kicked if there are more than that many bytes pending. */
+#define MAX_CQ_BYTES 40000
/**
* Open a passive socket of given layer4 type.
*
* \return The listening fd on success, negative on errors.
*/
-int open_sender(unsigned l4type, int port)
+static int open_sender(unsigned l4type, int port)
{
int fd, ret = para_listen(AF_UNSPEC, l4type, port);
* list, destroy the chunk queue of this client, delete the client from the
* list of connected clients and free the sender_client struct.
*/
-void shutdown_client(struct sender_client *sc)
+void shutdown_client(struct sender_client *sc, struct sender_status *ss)
{
PARA_INFO_LOG("shutting down %s on fd %d\n", sc->name, sc->fd);
free(sc->name);
list_del(&sc->node);
free(sc->private_data);
free(sc);
+ ss->num_clients--;
+}
+
+void shutdown_clients(struct sender_status *ss)
+{
+ struct sender_client *sc, *tmp;
+ list_for_each_entry_safe(sc, tmp, &ss->client_list, node)
+ shutdown_client(sc, ss);
}
/**
}
static int queue_chunk_or_shutdown(struct sender_client *sc,
- long unsigned chunk_num, size_t sent)
+ struct sender_status *ss, long unsigned chunk_num,
+ size_t sent)
{
int ret = cq_enqueue(sc->cq, chunk_num, sent);
if (ret < 0)
- shutdown_client(sc);
+ shutdown_client(sc, ss);
return ret;
}
* On errors, the client is shut down. If only a part of the buffer could be
* written, the remainder is put into the chunk queue for that client.
*/
-void send_chunk(struct sender_client *sc, size_t max_bytes_per_write,
- long unsigned current_chunk, const char *buf, size_t len)
+void send_chunk(struct sender_client *sc, struct sender_status *ss,
+ size_t max_bytes_per_write, long unsigned current_chunk,
+ const char *buf, size_t len)
{
int ret;
size_t header_len;
char *header_buf = vss_get_header(&header_len);
if (header_buf && header_len > 0) {
- ret = queue_chunk_or_shutdown(sc, -1U, 0);
+ ret = queue_chunk_or_shutdown(sc, ss, -1U, 0);
if (ret < 0)
goto out;
}
}
ret = send_queued_chunks(sc, max_bytes_per_write);
if (ret < 0) {
- shutdown_client(sc);
+ shutdown_client(sc, ss);
goto out;
}
if (!len)
goto out;
if (!ret) { /* still data left in the queue */
- ret = queue_chunk_or_shutdown(sc, current_chunk, 0);
+ ret = queue_chunk_or_shutdown(sc, ss, current_chunk, 0);
goto out;
}
ret = write_nonblock(sc->fd, buf, len, max_bytes_per_write);
if (ret < 0) {
- shutdown_client(sc);
+ shutdown_client(sc, ss);
goto out;
}
if (ret != len)
- ret = queue_chunk_or_shutdown(sc, current_chunk, ret);
+ ret = queue_chunk_or_shutdown(sc, ss, current_chunk, ret);
out:
if (ret < 0)
PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
}
+
+void init_sender_status(struct sender_status *ss, char **access_arg,
+ int num_access_args, int port, int max_clients, int default_deny)
+{
+ ss->listen_fd = -1;
+ INIT_LIST_HEAD(&ss->client_list);
+ ss->port = port;
+ acl_init(&ss->acl, access_arg, num_access_args);
+ ss->num_clients = 0;
+ ss->max_clients = max_clients;
+ ss->default_deny = default_deny;
+}
+
+char *get_sender_info(struct sender_status *ss, char *name)
+{
+ char *clnts = NULL, *ret;
+ struct sender_client *sc, *tmp_sc;
+
+ char *acl_contents = acl_get_contents(&ss->acl);
+ list_for_each_entry_safe(sc, tmp_sc, &ss->client_list, node) {
+ char *tmp = make_message("%s%s ", clnts? clnts : "", sc->name);
+ free(clnts);
+ clnts = tmp;
+ }
+ ret = make_message(
+ "%s sender:\n"
+ "\tstatus: %s\n"
+ "\tport: %d\n"
+ "\tnumber of connected clients: %d\n"
+ "\tmaximal number of clients: %d%s\n"
+ "\tconnected clients: %s\n"
+ "\taccess %s list: %s\n",
+ name,
+ (ss->listen_fd >= 0)? "on" : "off",
+ ss->port,
+ ss->num_clients,
+ ss->max_clients,
+ ss->max_clients > 0? "" : " (unlimited)",
+ clnts? clnts : "(none)",
+ ss->default_deny? "allow" : "deny",
+ acl_contents? acl_contents : "(empty)"
+ );
+ free(acl_contents);
+ free(clnts);
+ return ret;
+}
+
+void generic_com_allow(struct sender_command_data *scd,
+ struct sender_status *ss)
+{
+ acl_allow(scd->addr, scd->netmask, &ss->acl, ss->default_deny);
+}
+
+void generic_com_deny(struct sender_command_data *scd,
+ struct sender_status *ss)
+{
+ acl_deny(scd->addr, scd->netmask, &ss->acl, ss->default_deny);
+}
+
+int generic_com_on(struct sender_status *ss, unsigned protocol)
+{
+ int ret;
+
+ if (ss->listen_fd >= 0)
+ return 1;
+ ret = open_sender(protocol, ss->port);
+ if (ret < 0)
+ return ret;
+ ss->listen_fd = ret;
+ return 1;
+}
+
+void generic_com_off(struct sender_status *ss)
+{
+ if (ss->listen_fd < 0)
+ return;
+ PARA_NOTICE_LOG("closing port %d\n", ss->port);
+ close(ss->listen_fd);
+ del_close_on_fork_list(ss->listen_fd);
+ shutdown_clients(ss);
+ ss->listen_fd = -1;
+}
+
+struct sender_client *accept_sender_client(struct sender_status *ss)
+{
+ struct sender_client *sc;
+ int fd, ret = para_accept(ss->listen_fd, NULL, 0);
+ if (ret < 0) {
+ PARA_ERROR_LOG("%s\n", para_strerror(-ret));
+ return NULL;
+ }
+ fd = ret;
+ ret = -E_MAX_CLIENTS;
+ if (ss->max_clients > 0 && ss->num_clients >= ss->max_clients)
+ goto err_out;
+ ret = mark_fd_nonblocking(fd);
+ if (ret < 0)
+ goto err_out;
+ ret = acl_check_access(fd, &ss->acl, ss->default_deny);
+ if (ret < 0)
+ goto err_out;
+ ss->num_clients++;
+ sc = para_calloc(sizeof(*sc));
+ sc->fd = fd;
+ sc->name = make_message("%s", remote_name(fd));
+ sc->cq = cq_new(MAX_CQ_BYTES);
+ para_list_add(&sc->node, &ss->client_list);
+ add_close_on_fork_list(fd);
+ PARA_INFO_LOG("accepted client #%d: %s (fd %d)\n", ss->num_clients,
+ sc->name, fd);
+ return sc;
+err_out:
+ PARA_WARNING_LOG("%s\n", para_strerror(-ret));
+ close(fd);
+ return NULL;
+}
+
+char *generic_sender_help(void)
+{
+ return make_message(
+ "usage: {on|off}\n"
+ "usage: {allow|deny} IP mask\n"
+ "example: allow 127.0.0.1 32\n"
+ );
+}
optional
multiple
+option "dccp_max_clients" -
+#~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+"see http_max_clients"
+
+ int typestr="number"
+ default="-1"
+ optional
+
+
section "ortp sender"
#~~~~~~~~~~~~~~~~~~~~