2 * Copyright (C) 2006-2008 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>
14 #include <sys/types.h>
28 #include "close_on_fork.h"
29 #include "chunk_queue.h"
30 #include "server.cmdline.h"
32 /** the list of connected clients **/
33 static struct list_head clients
;
34 static int listen_fd
= -1;
36 /** Maximal number of bytes in a chunk queue. */
37 #define DCCP_MAX_PENDING_BYTES 40000
39 /** Do not write more than that many bytes at once. */
40 #define DCCP_MAX_BYTES_PER_WRITE 1024
42 static void dccp_pre_select(int *max_fileno
, fd_set
*rfds
,
43 __a_unused fd_set
*wfds
)
46 para_fd_set(listen_fd
, rfds
, max_fileno
);
49 static void dccp_post_select(fd_set
*rfds
, __a_unused fd_set
*wfds
)
51 struct sender_client
*sc
;
54 if (listen_fd
< 0 || !FD_ISSET(listen_fd
, rfds
))
56 ret
= para_accept(listen_fd
, NULL
, 0);
58 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));
63 * Bypass unused CCID paths: the sender does not receive application data
64 * from the client; by shutting down this unused communication path we can
65 * reduce processing costs a bit. See analogous comment in dccp_recv.c.
67 if (shutdown(fd
, SHUT_RD
) < 0) {
68 ret
= -ERRNO_TO_PARA_ERROR(errno
);
71 ret
= mark_fd_nonblocking(fd
);
74 sc
= para_calloc(sizeof(*sc
));
76 sc
->name
= make_message("%s", remote_name(sc
->fd
));
77 PARA_NOTICE_LOG("connection from %s\n", sc
->name
);
78 para_list_add(&sc
->node
, &clients
);
79 add_close_on_fork_list(sc
->fd
);
80 sc
->cq
= cq_new(DCCP_MAX_PENDING_BYTES
);
83 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));
87 static void dccp_send(long unsigned current_chunk
,
88 __a_unused
long unsigned chunks_sent
, const char *buf
, size_t len
)
90 struct sender_client
*sc
, *tmp
;
92 list_for_each_entry_safe(sc
, tmp
, &clients
, node
)
93 send_chunk(sc
, DCCP_MAX_BYTES_PER_WRITE
, current_chunk
, buf
,
97 static void dccp_shutdown_clients(void)
99 struct sender_client
*sc
, *tmp
;
101 list_for_each_entry_safe(sc
, tmp
, &clients
, node
)
105 static char *dccp_info(void)
108 struct sender_client
*sc
, *tmp
;
110 list_for_each_entry_safe(sc
, tmp
, &clients
, node
)
112 return make_message("dccp connected clients: %d\n", num_clients
);
115 static char *dccp_help(void)
117 return make_message("no help available\n");
121 * The init function of the dccp sender.
123 * \param s pointer to the dccp sender struct.
125 * It initializes all function pointers of \a s and starts
126 * listening on the given port.
128 void dccp_send_init(struct sender
*s
)
132 INIT_LIST_HEAD(&clients
);
135 s
->pre_select
= dccp_pre_select
;
136 s
->post_select
= dccp_post_select
;
137 s
->shutdown_clients
= dccp_shutdown_clients
;
139 s
->client_cmds
[SENDER_ON
] = NULL
;
140 s
->client_cmds
[SENDER_OFF
] = NULL
;
141 s
->client_cmds
[SENDER_DENY
] = NULL
;
142 s
->client_cmds
[SENDER_ALLOW
] = NULL
;
143 s
->client_cmds
[SENDER_ADD
] = NULL
;
144 s
->client_cmds
[SENDER_DELETE
] = NULL
;
145 ret
= open_sender(IPPROTO_DCCP
, conf
.dccp_port_arg
);
147 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));