2 * Copyright (C) 2006-2010 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>
15 #include <sys/types.h>
30 #include "close_on_fork.h"
31 #include "chunk_queue.h"
32 #include "server.cmdline.h"
35 /** Do not write more than that many bytes at once. */
36 #define DCCP_MAX_BYTES_PER_WRITE 1024
38 static struct sender_status dccp_sender_status
, *dss
= &dccp_sender_status
;
40 static void dccp_pre_select(int *max_fileno
, fd_set
*rfds
,
41 __a_unused fd_set
*wfds
)
43 if (dss
->listen_fd
>= 0)
44 para_fd_set(dss
->listen_fd
, rfds
, max_fileno
);
48 * Query the TX CCID used on the sender-client half connection.
49 * \param sockfd Server socket descriptor to query (after accept(2)).
50 * \return CCID number > 0, -1 on error/incompatibility.
52 * NB: This feature is only available on Linux > 2.6.30; on older kernels
53 * ENOPROTOOPT ("Protocol not available") will be returned.
55 static int dccp_get_tx_ccid(int sockfd
)
58 socklen_t len
= sizeof(tx_ccid
);
60 if (getsockopt(sockfd
, SOL_DCCP
,
61 DCCP_SOCKOPT_TX_CCID
, &tx_ccid
, &len
) < 0) {
62 PARA_WARNING_LOG("DCCP_SOCKOPT_TX_CCID: %s\n", strerror(errno
));
68 static void dccp_post_select(fd_set
*rfds
, __a_unused fd_set
*wfds
)
70 struct sender_client
*sc
;
73 sc
= accept_sender_client(dss
, rfds
);
77 /* If CCID identifiable, present client as <host>#<port>~<ccid> */
78 tx_ccid
= dccp_get_tx_ccid(sc
->fd
);
80 char *tmp
= make_message("%s~%d", sc
->name
, tx_ccid
);
86 * Bypass unused CCID paths: the sender does not receive application data
87 * from the client; by shutting down this unused communication path we can
88 * reduce processing costs a bit. See analogous comment in dccp_recv.c.
90 if (shutdown(sc
->fd
, SHUT_RD
) >= 0)
92 PARA_WARNING_LOG("%s\n", strerror(errno
));
93 shutdown_client(sc
, dss
);
96 static void dccp_send(long unsigned current_chunk
,
97 __a_unused
long unsigned chunks_sent
, const char *buf
,
98 size_t len
, const char *header_buf
, size_t header_len
)
100 struct sender_client
*sc
, *tmp
;
102 list_for_each_entry_safe(sc
, tmp
, &dss
->client_list
, node
)
103 send_chunk(sc
, dss
, DCCP_MAX_BYTES_PER_WRITE
, current_chunk
, buf
,
104 len
, header_buf
, header_len
);
107 static void dccp_shutdown_clients(void)
109 shutdown_clients(dss
);
112 static int dccp_com_on(__a_unused
struct sender_command_data
*scd
)
114 return generic_com_on(dss
, IPPROTO_DCCP
);
117 static int dccp_com_off(__a_unused
struct sender_command_data
*scd
)
119 generic_com_off(dss
);
124 static int dccp_com_deny(struct sender_command_data
*scd
)
126 generic_com_deny(scd
, dss
);
130 static int dccp_com_allow(struct sender_command_data
*scd
)
132 generic_com_allow(scd
, dss
);
137 * Return list of available CCIDs or warning, in static buffer.
139 static const char *dccp_list_available_ccids(void)
141 /* Worst case length: n * 3 digits + n-1 spaces + '\0' */
142 static char list
[DCCP_MAX_HOST_CCIDS
* 4];
146 nccids
= dccp_available_ccids(&ccids
);
148 snprintf(list
, sizeof(list
), "Unable to query available CCIDs");
150 for (i
= len
= 0; i
< nccids
; i
++)
151 len
+= snprintf(list
+ len
, sizeof(list
) - len
,
152 "%s%d", i
? " " : "", ccids
[i
]);
157 static char *dccp_info(void)
159 char *info
= get_sender_info(dss
, "dccp");
160 char *ret
= make_message("%s" "\tsupported ccids: %s\n",
161 info
, dccp_list_available_ccids());
167 * The init function of the dccp sender.
169 * \param s pointer to the dccp sender struct.
171 * It initializes all function pointers of \a s and starts
172 * listening on the given port.
174 void dccp_send_init(struct sender
*s
)
180 s
->pre_select
= dccp_pre_select
;
181 s
->post_select
= dccp_post_select
;
182 s
->shutdown_clients
= dccp_shutdown_clients
;
183 s
->help
= generic_sender_help
;
184 s
->client_cmds
[SENDER_ON
] = dccp_com_on
;
185 s
->client_cmds
[SENDER_OFF
] = dccp_com_off
;
186 s
->client_cmds
[SENDER_DENY
] = dccp_com_deny
;
187 s
->client_cmds
[SENDER_ALLOW
] = dccp_com_allow
;
188 s
->client_cmds
[SENDER_ADD
] = NULL
;
189 s
->client_cmds
[SENDER_DELETE
] = NULL
;
191 init_sender_status(dss
, conf
.dccp_access_arg
, conf
.dccp_access_given
,
192 conf
.dccp_port_arg
, conf
.dccp_max_clients_arg
,
193 conf
.dccp_default_deny_given
);
194 ret
= generic_com_on(dss
, IPPROTO_DCCP
);
196 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));