fecdec_filter: Avoid potentially expensive pointer subtraction.
[paraslash.git] / dccp_send.c
1 /*
2 * Copyright (C) 2006-2009 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 <sys/types.h>
15 #include <dirent.h>
16
17 #include "para.h"
18 #include "error.h"
19 #include "string.h"
20 #include "afh.h"
21 #include "afs.h"
22 #include "server.h"
23 #include "net.h"
24 #include "list.h"
25 #include "vss.h"
26 #include "send.h"
27 #include "fd.h"
28 #include "close_on_fork.h"
29 #include "chunk_queue.h"
30 #include "server.cmdline.h"
31 #include "acl.h"
32
33 /** Do not write more than that many bytes at once. */
34 #define DCCP_MAX_BYTES_PER_WRITE 1024
35
36 static struct sender_status dccp_sender_status, *dss = &dccp_sender_status;
37
38 static void dccp_pre_select(int *max_fileno, fd_set *rfds,
39 __a_unused fd_set *wfds)
40 {
41 if (dss->listen_fd >= 0)
42 para_fd_set(dss->listen_fd, rfds, max_fileno);
43 }
44
45 static void dccp_post_select(fd_set *rfds, __a_unused fd_set *wfds)
46 {
47 struct sender_client *sc;
48
49 if (dss->listen_fd < 0 || !FD_ISSET(dss->listen_fd, rfds))
50 return;
51 sc = accept_sender_client(dss);
52 if (!sc)
53 return;
54 /*
55 * Bypass unused CCID paths: the sender does not receive application data
56 * from the client; by shutting down this unused communication path we can
57 * reduce processing costs a bit. See analogous comment in dccp_recv.c.
58 */
59 if (shutdown(sc->fd, SHUT_RD) >= 0)
60 return;
61 PARA_WARNING_LOG("%s\n", strerror(errno));
62 shutdown_client(sc, dss);
63 }
64
65 static void dccp_send(long unsigned current_chunk,
66 __a_unused long unsigned chunks_sent, const char *buf,
67 size_t len, const char *header_buf, size_t header_len)
68 {
69 struct sender_client *sc, *tmp;
70
71 list_for_each_entry_safe(sc, tmp, &dss->client_list, node)
72 send_chunk(sc, dss, DCCP_MAX_BYTES_PER_WRITE, current_chunk, buf,
73 len, header_buf, header_len);
74 }
75
76 static void dccp_shutdown_clients(void)
77 {
78 shutdown_clients(dss);
79 }
80
81 static int dccp_com_on(__a_unused struct sender_command_data *scd)
82 {
83 return generic_com_on(dss, IPPROTO_DCCP);
84 }
85
86 static int dccp_com_off(__a_unused struct sender_command_data *scd)
87 {
88 generic_com_off(dss);
89 return 1;
90 }
91
92
93 static int dccp_com_deny(struct sender_command_data *scd)
94 {
95 generic_com_deny(scd, dss);
96 return 1;
97 }
98
99 static int dccp_com_allow(struct sender_command_data *scd)
100 {
101 generic_com_allow(scd, dss);
102 return 1;
103 }
104
105 static char *dccp_info(void)
106 {
107 return get_sender_info(dss, "dccp");
108 }
109
110 /**
111 * The init function of the dccp sender.
112 *
113 * \param s pointer to the dccp sender struct.
114 *
115 * It initializes all function pointers of \a s and starts
116 * listening on the given port.
117 */
118 void dccp_send_init(struct sender *s)
119 {
120 int ret;
121
122 s->info = dccp_info;
123 s->send = dccp_send;
124 s->pre_select = dccp_pre_select;
125 s->post_select = dccp_post_select;
126 s->shutdown_clients = dccp_shutdown_clients;
127 s->help = generic_sender_help;
128 s->client_cmds[SENDER_ON] = dccp_com_on;
129 s->client_cmds[SENDER_OFF] = dccp_com_off;
130 s->client_cmds[SENDER_DENY] = dccp_com_deny;
131 s->client_cmds[SENDER_ALLOW] = dccp_com_allow;
132 s->client_cmds[SENDER_ADD] = NULL;
133 s->client_cmds[SENDER_DELETE] = NULL;
134
135 init_sender_status(dss, conf.dccp_access_arg, conf.dccp_access_given,
136 conf.dccp_port_arg, conf.dccp_max_clients_arg,
137 conf.dccp_default_deny_given);
138 ret = generic_com_on(dss, IPPROTO_DCCP);
139 if (ret < 0)
140 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
141 }