network: factor out bind-listening code
authorLuca Barbato <lu_zero@gentoo.org>
Wed, 29 May 2013 23:08:51 +0000 (01:08 +0200)
committerLuca Barbato <lu_zero@gentoo.org>
Sat, 1 Jun 2013 13:29:53 +0000 (15:29 +0200)
Introduce ff_listen_bind, to be shared with the other non-tcp
network protocols.

libavformat/network.c
libavformat/network.h
libavformat/tcp.c

index f493c29..55d55af 100644 (file)
@@ -187,3 +187,32 @@ int ff_is_multicast_address(struct sockaddr *addr)
 
     return 0;
 }
+
+int ff_listen_bind(int fd, const struct sockaddr *addr,
+                   socklen_t addrlen, int timeout)
+{
+    int ret;
+    int reuse = 1;
+    struct pollfd lp = { fd, POLLIN, 0 };
+    setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
+    ret = bind(fd, addr, addrlen);
+    if (ret)
+        return ff_neterrno();
+
+    ret = listen(fd, 1);
+    if (ret)
+        return ff_neterrno();
+
+    ret = poll(&lp, 1, timeout >= 0 ? timeout : -1);
+    if (ret <= 0)
+        return AVERROR(ETIMEDOUT);
+
+    ret = accept(fd, NULL, NULL);
+    if (ret < 0)
+        return ff_neterrno();
+
+    closesocket(fd);
+
+    ff_socket_nonblock(ret, 1);
+    return ret;
+}
index 5160767..db1b09a 100644 (file)
@@ -209,4 +209,7 @@ const char *ff_gai_strerror(int ecode);
 
 int ff_is_multicast_address(struct sockaddr *addr);
 
+int ff_listen_bind(int fd, const struct sockaddr *addr,
+                   socklen_t addrlen, int timeout);
+
 #endif /* AVFORMAT_NETWORK_H */
index bdaab7f..6e4de0d 100644 (file)
@@ -85,39 +85,18 @@ static int tcp_open(URLContext *h, const char *uri, int flags)
     cur_ai = ai;
 
  restart:
-    ret = AVERROR(EIO);
     fd = socket(cur_ai->ai_family, cur_ai->ai_socktype, cur_ai->ai_protocol);
-    if (fd < 0)
+    if (fd < 0) {
+        ret = ff_neterrno();
         goto fail;
+    }
 
     if (listen_socket) {
-        int fd1;
-        int reuse = 1;
-        struct pollfd lp = { fd, POLLIN, 0 };
-        setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
-        ret = bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
-        if (ret) {
-            ret = ff_neterrno();
-            goto fail1;
-        }
-        ret = listen(fd, 1);
-        if (ret) {
-            ret = ff_neterrno();
-            goto fail1;
-        }
-        ret = poll(&lp, 1, listen_timeout >= 0 ? listen_timeout : -1);
-        if (ret <= 0) {
-            ret = AVERROR(ETIMEDOUT);
-            goto fail1;
-        }
-        fd1 = accept(fd, NULL, NULL);
-        if (fd1 < 0) {
-            ret = ff_neterrno();
+        if ((fd = ff_listen_bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen,
+                                 listen_timeout)) < 0) {
+            ret = fd;
             goto fail1;
         }
-        closesocket(fd);
-        fd = fd1;
-        ff_socket_nonblock(fd, 1);
     } else {
  redo:
         ff_socket_nonblock(fd, 1);
@@ -177,6 +156,7 @@ static int tcp_open(URLContext *h, const char *uri, int flags)
         cur_ai = cur_ai->ai_next;
         if (fd >= 0)
             closesocket(fd);
+        ret = 0;
         goto restart;
     }
  fail1: