udp: Add an option for connecting the udp socket

This allows us to find out the local sending address via getsockname,
otherwise we just get INADDR_ANY (or similar for v6).

This also makes writes return ECONNREFUSED if nobody is listening on the
receiving port.

Originally committed as revision 25405 to svn://svn.ffmpeg.org/ffmpeg/trunk
This commit is contained in:
Martin Storsjö 2010-10-08 08:42:30 +00:00
parent b7952ed184
commit 6766a354f7
1 changed files with 13 additions and 0 deletions

View File

@ -50,6 +50,7 @@ typedef struct {
int reuse_socket;
struct sockaddr_storage dest_addr;
int dest_addr_len;
int is_connected;
} UDPContext;
#define UDP_TX_BUF_SIZE 32768
@ -325,6 +326,9 @@ static int udp_open(URLContext *h, const char *uri, int flags)
if (find_info_tag(buf, sizeof(buf), "buffer_size", p)) {
s->buffer_size = strtol(buf, NULL, 10);
}
if (find_info_tag(buf, sizeof(buf), "connect", p)) {
s->is_connected = strtol(buf, NULL, 10);
}
}
/* fill the dest addr */
@ -393,6 +397,12 @@ static int udp_open(URLContext *h, const char *uri, int flags)
/* make the socket non-blocking */
ff_socket_nonblock(udp_fd, 1);
}
if (s->is_connected) {
if (connect(udp_fd, (struct sockaddr *) &s->dest_addr, s->dest_addr_len)) {
av_log(NULL, AV_LOG_ERROR, "connect: %s\n", strerror(errno));
goto fail;
}
}
s->udp_fd = udp_fd;
return 0;
@ -444,9 +454,12 @@ static int udp_write(URLContext *h, const uint8_t *buf, int size)
int ret;
for(;;) {
if (!s->is_connected) {
ret = sendto (s->udp_fd, buf, size, 0,
(struct sockaddr *) &s->dest_addr,
s->dest_addr_len);
} else
ret = send(s->udp_fd, buf, size, 0);
if (ret < 0) {
if (ff_neterrno() != FF_NETERROR(EINTR) &&
ff_neterrno() != FF_NETERROR(EAGAIN))