9e03b402fe18d2a9d0584c2653a8d7baeb78af35
2 * Copyright (C) 2005-2009 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file udp_send.c Para_server's udp sender. */
13 #include "server.cmdline.h"
23 #include "portable_io.h"
24 #include "udp_header.h"
30 /** Convert in_addr to ascii. */
31 #define TARGET_ADDR(oc) inet_ntoa((oc)->addr)
33 /** Describes one entry in the list of targets for the udp sender. */
37 /** The position of this target in the list of targets. */
38 struct list_head node
;
45 static struct list_head targets
;
46 static int sender_status
;
48 static void udp_delete_target(struct udp_target
*ut
, const char *msg
)
50 PARA_NOTICE_LOG("deleting %s:%d (%s) from list\n", TARGET_ADDR(ut
),
58 static void udp_send_buf(char *buf
, size_t len
)
60 struct udp_target
*ut
, *tmp
;
63 list_for_each_entry_safe(ut
, tmp
, &targets
, node
) {
67 ret
= write_all(ut
->fd
, buf
, &written
);
68 if (ret
< 0) /* TODO: Use chunk queueing */
69 return udp_delete_target(ut
, "send error");
71 PARA_WARNING_LOG("short write %zu/%zu\n", written
, len
);
75 static int udp_init_session(struct udp_target
*ut
)
79 if (ut
->fd
>= 0) /* nothing to do */
81 PARA_NOTICE_LOG("sending to udp %s:%d\n", TARGET_ADDR(ut
), ut
->port
);
82 ret
= create_udp_send_socket(TARGET_ADDR(ut
), ut
->port
,
87 return mark_fd_nonblocking(ut
->fd
);
90 static void udp_shutdown_targets(void)
92 char buf
[UDP_AUDIO_HEADER_LEN
];
93 struct udp_target
*ut
, *tmp
;
95 udp_write_packet_type(buf
, UDP_EOF_PACKET
);
97 list_for_each_entry_safe(ut
, tmp
, &targets
, node
) {
100 write(ut
->fd
, buf
, UDP_AUDIO_HEADER_LEN
);
106 static int need_extra_header(long unsigned current_chunk
)
108 static struct timeval last_header
;
113 tv_diff(now
, &last_header
, &diff
);
114 if (tv2ms(&diff
) < conf
.udp_header_interval_arg
)
120 static void udp_send(long unsigned current_chunk
, __a_unused
long unsigned chunks_sent
,
121 const char *buf
, size_t len
, const char *header_buf
,
124 struct udp_target
*ut
, *tmp
;
126 uint8_t packet_type
= UDP_DATA_PACKET
;
129 struct timeval
*chunk_tv
;
130 uint8_t stream_type
= header_len
? UDP_HEADER_STREAM
: UDP_PLAIN_STREAM
;
132 // PARA_NOTICE_LOG("header_len: %zd, header_buf: %p\n", header_len,
134 if (sender_status
!= SENDER_ON
)
137 /* we might not yet know the chunk time */
138 chunk_tv
= vss_chunk_time();
141 if (list_empty(&targets
))
143 list_for_each_entry_safe(ut
, tmp
, &targets
, node
) {
144 ret
= udp_init_session(ut
);
146 udp_delete_target(ut
, para_strerror(-ret
));
148 if (!need_extra_header(current_chunk
))
151 packet_type
= UDP_BOF_PACKET
;
153 packet_type
= UDP_HEADER_PACKET
;
154 sendbuf_len
= UDP_AUDIO_HEADER_LEN
+ header_len
+ len
;
155 sendbuf
= para_malloc(sendbuf_len
);
156 udp_write_magic(sendbuf
);
157 udp_write_stream_type(sendbuf
, stream_type
);
158 udp_write_packet_type(sendbuf
, packet_type
);
159 udp_write_header_len(sendbuf
, header_len
);
161 memcpy(sendbuf
+ UDP_AUDIO_HEADER_LEN
, header_buf
,
163 memcpy(sendbuf
+ UDP_AUDIO_HEADER_LEN
+ header_len
, buf
, len
);
164 udp_send_buf(sendbuf
, sendbuf_len
);
168 static int udp_com_on(__a_unused
struct sender_command_data
*scd
)
170 sender_status
= SENDER_ON
;
174 static int udp_com_off(__a_unused
struct sender_command_data
*scd
)
176 udp_shutdown_targets();
177 sender_status
= SENDER_OFF
;
181 static int udp_com_delete(struct sender_command_data
*scd
)
183 char *a
= para_strdup(inet_ntoa(scd
->addr
));
184 struct udp_target
*ut
, *tmp
;
185 list_for_each_entry_safe(ut
, tmp
, &targets
, node
) {
186 if (scd
->port
!= ut
->port
)
188 if (strcmp(TARGET_ADDR(ut
), a
))
190 udp_delete_target(ut
, "com_delete");
195 static void udp_add_target(int port
, struct in_addr
*addr
)
197 struct udp_target
*ut
= para_calloc(sizeof(struct udp_target
));
200 ut
->fd
= -1; /* not yet connected */
201 PARA_INFO_LOG("adding to target list (%s:%d)\n",
202 TARGET_ADDR(ut
), ut
->port
);
203 para_list_add(&ut
->node
, &targets
);
206 static int udp_com_add(struct sender_command_data
*scd
)
208 int port
= (scd
->port
> 0)? scd
->port
: conf
.udp_default_port_arg
;
209 udp_add_target(port
, &scd
->addr
);
213 static char *udp_info(void)
215 struct udp_target
*ut
;
216 char *ret
, *tgts
= NULL
;
218 list_for_each_entry(ut
, &targets
, node
) {
219 char *tmp
= make_message("%s%s:%d ", tgts
? tgts
: "",
220 TARGET_ADDR(ut
), ut
->port
);
229 (sender_status
== SENDER_ON
)? "on" : "off",
230 conf
.udp_default_port_arg
,
231 tgts
? tgts
: "(none)"
237 static void udp_init_target_list(void)
241 INIT_LIST_HEAD(&targets
);
242 for (i
= 0; i
< conf
.udp_target_given
; i
++) {
243 char *arg
= para_strdup(conf
.udp_target_arg
[i
]);
244 char *p
= strchr(arg
, ':');
251 if (!inet_pton(AF_INET
, arg
, &addr
))
254 if (port
< 0 || port
> 65535)
255 port
= conf
.udp_default_port_arg
;
256 udp_add_target(port
, &addr
);
259 PARA_CRIT_LOG("syntax error for udp target option "
260 "#%d, ignoring\n", i
);
267 static char *udp_help(void)
271 "usage: {add|delete} IP port\n"
272 "example: add 224.0.1.38 8000 (multicast streaming)\n"
277 * The init function of para_server's udp sender.
279 * \param s Pointer to the http sender struct.
281 * It initializes all function pointers of \a s and the list of udp targets.
283 void udp_send_init(struct sender
*s
)
285 INIT_LIST_HEAD(&targets
);
289 s
->pre_select
= NULL
;
290 s
->post_select
= NULL
;
291 s
->shutdown_clients
= udp_shutdown_targets
;
292 s
->client_cmds
[SENDER_ON
] = udp_com_on
;
293 s
->client_cmds
[SENDER_OFF
] = udp_com_off
;
294 s
->client_cmds
[SENDER_DENY
] = NULL
;
295 s
->client_cmds
[SENDER_ALLOW
] = NULL
;
296 s
->client_cmds
[SENDER_ADD
] = udp_com_add
;
297 s
->client_cmds
[SENDER_DELETE
] = udp_com_delete
;
298 sender_status
= SENDER_OFF
;
299 udp_init_target_list();
300 if (!conf
.udp_no_autostart_given
)
301 sender_status
= SENDER_ON
;
302 PARA_DEBUG_LOG("udp sender init complete\n");