Commit a840cdda authored by Martin Storsjö's avatar Martin Storsjö

network: Use av_strerror for getting error messages

Also use ff_neterrno() instead of errno directly (which doesn't work
on windows), for getting the error code.
Signed-off-by: 's avatarMartin Storsjö <martin@martin.st>
parent 860b40f1
...@@ -141,10 +141,12 @@ static int tcp_open(URLContext *h, const char *uri, int flags) ...@@ -141,10 +141,12 @@ static int tcp_open(URLContext *h, const char *uri, int flags)
optlen = sizeof(ret); optlen = sizeof(ret);
getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen); getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen);
if (ret != 0) { if (ret != 0) {
char errbuf[100];
ret = AVERROR(ret);
av_strerror(ret, errbuf, sizeof(errbuf));
av_log(h, AV_LOG_ERROR, av_log(h, AV_LOG_ERROR,
"TCP connection to %s:%d failed: %s\n", "TCP connection to %s:%d failed: %s\n",
hostname, port, strerror(ret)); hostname, port, errbuf);
ret = AVERROR(ret);
goto fail; goto fail;
} }
} }
......
...@@ -57,13 +57,20 @@ typedef struct { ...@@ -57,13 +57,20 @@ typedef struct {
#define UDP_TX_BUF_SIZE 32768 #define UDP_TX_BUF_SIZE 32768
#define UDP_MAX_PKT_SIZE 65536 #define UDP_MAX_PKT_SIZE 65536
static void log_net_error(void *ctx, int level, const char* prefix)
{
char errbuf[100];
av_strerror(ff_neterrno(), errbuf, sizeof(errbuf));
av_log(ctx, level, "%s: %s\n", prefix, errbuf);
}
static int udp_set_multicast_ttl(int sockfd, int mcastTTL, static int udp_set_multicast_ttl(int sockfd, int mcastTTL,
struct sockaddr *addr) struct sockaddr *addr)
{ {
#ifdef IP_MULTICAST_TTL #ifdef IP_MULTICAST_TTL
if (addr->sa_family == AF_INET) { if (addr->sa_family == AF_INET) {
if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_TTL, &mcastTTL, sizeof(mcastTTL)) < 0) { if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_TTL, &mcastTTL, sizeof(mcastTTL)) < 0) {
av_log(NULL, AV_LOG_ERROR, "setsockopt(IP_MULTICAST_TTL): %s\n", strerror(errno)); log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IP_MULTICAST_TTL)");
return -1; return -1;
} }
} }
...@@ -71,7 +78,7 @@ static int udp_set_multicast_ttl(int sockfd, int mcastTTL, ...@@ -71,7 +78,7 @@ static int udp_set_multicast_ttl(int sockfd, int mcastTTL,
#if defined(IPPROTO_IPV6) && defined(IPV6_MULTICAST_HOPS) #if defined(IPPROTO_IPV6) && defined(IPV6_MULTICAST_HOPS)
if (addr->sa_family == AF_INET6) { if (addr->sa_family == AF_INET6) {
if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &mcastTTL, sizeof(mcastTTL)) < 0) { if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &mcastTTL, sizeof(mcastTTL)) < 0) {
av_log(NULL, AV_LOG_ERROR, "setsockopt(IPV6_MULTICAST_HOPS): %s\n", strerror(errno)); log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IPV6_MULTICAST_HOPS)");
return -1; return -1;
} }
} }
...@@ -88,7 +95,7 @@ static int udp_join_multicast_group(int sockfd, struct sockaddr *addr) ...@@ -88,7 +95,7 @@ static int udp_join_multicast_group(int sockfd, struct sockaddr *addr)
mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr; mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
mreq.imr_interface.s_addr= INADDR_ANY; mreq.imr_interface.s_addr= INADDR_ANY;
if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) { if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
av_log(NULL, AV_LOG_ERROR, "setsockopt(IP_ADD_MEMBERSHIP): %s\n", strerror(errno)); log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IP_ADD_MEMBERSHIP)");
return -1; return -1;
} }
} }
...@@ -100,7 +107,7 @@ static int udp_join_multicast_group(int sockfd, struct sockaddr *addr) ...@@ -100,7 +107,7 @@ static int udp_join_multicast_group(int sockfd, struct sockaddr *addr)
memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr)); memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
mreq6.ipv6mr_interface= 0; mreq6.ipv6mr_interface= 0;
if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) { if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
av_log(NULL, AV_LOG_ERROR, "setsockopt(IPV6_ADD_MEMBERSHIP): %s\n", strerror(errno)); log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IPV6_ADD_MEMBERSHIP)");
return -1; return -1;
} }
} }
...@@ -117,7 +124,7 @@ static int udp_leave_multicast_group(int sockfd, struct sockaddr *addr) ...@@ -117,7 +124,7 @@ static int udp_leave_multicast_group(int sockfd, struct sockaddr *addr)
mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr; mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
mreq.imr_interface.s_addr= INADDR_ANY; mreq.imr_interface.s_addr= INADDR_ANY;
if (setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) { if (setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
av_log(NULL, AV_LOG_ERROR, "setsockopt(IP_DROP_MEMBERSHIP): %s\n", strerror(errno)); log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IP_DROP_MEMBERSHIP)");
return -1; return -1;
} }
} }
...@@ -129,7 +136,7 @@ static int udp_leave_multicast_group(int sockfd, struct sockaddr *addr) ...@@ -129,7 +136,7 @@ static int udp_leave_multicast_group(int sockfd, struct sockaddr *addr)
memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr)); memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
mreq6.ipv6mr_interface= 0; mreq6.ipv6mr_interface= 0;
if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) { if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
av_log(NULL, AV_LOG_ERROR, "setsockopt(IPV6_DROP_MEMBERSHIP): %s\n", strerror(errno)); log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IPV6_DROP_MEMBERSHIP)");
return -1; return -1;
} }
} }
...@@ -194,7 +201,7 @@ static int udp_socket_create(UDPContext *s, struct sockaddr_storage *addr, ...@@ -194,7 +201,7 @@ static int udp_socket_create(UDPContext *s, struct sockaddr_storage *addr,
for (res = res0; res; res=res->ai_next) { for (res = res0; res; res=res->ai_next) {
udp_fd = socket(res->ai_family, SOCK_DGRAM, 0); udp_fd = socket(res->ai_family, SOCK_DGRAM, 0);
if (udp_fd > 0) break; if (udp_fd > 0) break;
av_log(NULL, AV_LOG_ERROR, "socket: %s\n", strerror(errno)); log_net_error(NULL, AV_LOG_ERROR, "socket");
} }
if (udp_fd < 0) if (udp_fd < 0)
...@@ -268,7 +275,7 @@ int ff_udp_set_remote_url(URLContext *h, const char *uri) ...@@ -268,7 +275,7 @@ int ff_udp_set_remote_url(URLContext *h, const char *uri)
if (connect(s->udp_fd, (struct sockaddr *) &s->dest_addr, if (connect(s->udp_fd, (struct sockaddr *) &s->dest_addr,
s->dest_addr_len)) { s->dest_addr_len)) {
s->is_connected = 0; s->is_connected = 0;
av_log(h, AV_LOG_ERROR, "connect: %s\n", strerror(errno)); log_net_error(h, AV_LOG_ERROR, "connect");
return AVERROR(EIO); return AVERROR(EIO);
} }
} }
...@@ -391,7 +398,7 @@ static int udp_open(URLContext *h, const char *uri, int flags) ...@@ -391,7 +398,7 @@ static int udp_open(URLContext *h, const char *uri, int flags)
* bind failed */ * bind failed */
/* the bind is needed to give a port to the socket now */ /* the bind is needed to give a port to the socket now */
if (bind_ret < 0 && bind(udp_fd,(struct sockaddr *)&my_addr, len) < 0) { if (bind_ret < 0 && bind(udp_fd,(struct sockaddr *)&my_addr, len) < 0) {
av_log(h, AV_LOG_ERROR, "bind failed: %s\n", strerror(errno)); log_net_error(h, AV_LOG_ERROR, "bind failed");
goto fail; goto fail;
} }
...@@ -416,7 +423,7 @@ static int udp_open(URLContext *h, const char *uri, int flags) ...@@ -416,7 +423,7 @@ static int udp_open(URLContext *h, const char *uri, int flags)
/* limit the tx buf size to limit latency */ /* limit the tx buf size to limit latency */
tmp = s->buffer_size; tmp = s->buffer_size;
if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) { if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
av_log(h, AV_LOG_ERROR, "setsockopt(SO_SNDBUF): %s\n", strerror(errno)); log_net_error(h, AV_LOG_ERROR, "setsockopt(SO_SNDBUF)");
goto fail; goto fail;
} }
} else { } else {
...@@ -424,14 +431,14 @@ static int udp_open(URLContext *h, const char *uri, int flags) ...@@ -424,14 +431,14 @@ static int udp_open(URLContext *h, const char *uri, int flags)
* avoid losing data on OSes that set this too low by default. */ * avoid losing data on OSes that set this too low by default. */
tmp = s->buffer_size; tmp = s->buffer_size;
if (setsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &tmp, sizeof(tmp)) < 0) { if (setsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &tmp, sizeof(tmp)) < 0) {
av_log(h, AV_LOG_WARNING, "setsockopt(SO_RECVBUF): %s\n", strerror(errno)); log_net_error(h, AV_LOG_WARNING, "setsockopt(SO_RECVBUF)");
} }
/* make the socket non-blocking */ /* make the socket non-blocking */
ff_socket_nonblock(udp_fd, 1); ff_socket_nonblock(udp_fd, 1);
} }
if (s->is_connected) { if (s->is_connected) {
if (connect(udp_fd, (struct sockaddr *) &s->dest_addr, s->dest_addr_len)) { if (connect(udp_fd, (struct sockaddr *) &s->dest_addr, s->dest_addr_len)) {
av_log(h, AV_LOG_ERROR, "connect: %s\n", strerror(errno)); log_net_error(h, AV_LOG_ERROR, "connect");
goto fail; goto fail;
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment