Browse code

Merge commit '28306e6d620c109ddd672f7243adfbc2bbb3b18f'

* commit '28306e6d620c109ddd672f7243adfbc2bbb3b18f':
network: factor out bind-listening code
use my full first name instead of short one in copyrights

Conflicts:
libavformat/tcp.c

Merged-by: Michael Niedermayer <michaelni@gmx.at>

Michael Niedermayer authored on 2013/06/02 17:30:35
Showing 10 changed files
... ...
@@ -1,6 +1,6 @@
1 1
 /*
2 2
  * Bink video decoder
3
- * Copyright (C) 2009 Kostya Shishkov
3
+ * Copyright (C) 2009 Konstantin Shishkov
4 4
  *
5 5
  * This file is part of FFmpeg.
6 6
  *
... ...
@@ -1,6 +1,6 @@
1 1
 /*
2 2
  * Bink DSP routines
3
- * Copyright (c) 2009 Kostya Shishkov
3
+ * Copyright (c) 2009 Konstantin Shishkov
4 4
  *
5 5
  * This file is part of FFmpeg.
6 6
  *
... ...
@@ -1,6 +1,6 @@
1 1
 /*
2 2
  * Bink DSP routines
3
- * Copyright (c) 2009 Kostya Shishkov
3
+ * Copyright (c) 2009 Konstantin Shishkov
4 4
  *
5 5
  * This file is part of Libav.
6 6
  *
... ...
@@ -212,3 +212,32 @@ int ff_is_multicast_address(struct sockaddr *addr)
212 212
 
213 213
     return 0;
214 214
 }
215
+
216
+int ff_listen_bind(int fd, const struct sockaddr *addr,
217
+                   socklen_t addrlen, int timeout)
218
+{
219
+    int ret;
220
+    int reuse = 1;
221
+    struct pollfd lp = { fd, POLLIN, 0 };
222
+    setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
223
+    ret = bind(fd, addr, addrlen);
224
+    if (ret)
225
+        return ff_neterrno();
226
+
227
+    ret = listen(fd, 1);
228
+    if (ret)
229
+        return ff_neterrno();
230
+
231
+    ret = poll(&lp, 1, timeout >= 0 ? timeout : -1);
232
+    if (ret <= 0)
233
+        return AVERROR(ETIMEDOUT);
234
+
235
+    ret = accept(fd, NULL, NULL);
236
+    if (ret < 0)
237
+        return ff_neterrno();
238
+
239
+    closesocket(fd);
240
+
241
+    ff_socket_nonblock(ret, 1);
242
+    return ret;
243
+}
... ...
@@ -222,4 +222,7 @@ const char *ff_gai_strerror(int ecode);
222 222
 
223 223
 int ff_is_multicast_address(struct sockaddr *addr);
224 224
 
225
+int ff_listen_bind(int fd, const struct sockaddr *addr,
226
+                   socklen_t addrlen, int timeout);
227
+
225 228
 #endif /* AVFORMAT_NETWORK_H */
... ...
@@ -1,6 +1,6 @@
1 1
 /*
2 2
  * RTMP definitions
3
- * Copyright (c) 2009 Kostya Shishkov
3
+ * Copyright (c) 2009 Konstantin Shishkov
4 4
  *
5 5
  * This file is part of FFmpeg.
6 6
  *
... ...
@@ -1,6 +1,6 @@
1 1
 /*
2 2
  * RTMP input format
3
- * Copyright (c) 2009 Kostya Shishkov
3
+ * Copyright (c) 2009 Konstantin Shishkov
4 4
  *
5 5
  * This file is part of FFmpeg.
6 6
  *
... ...
@@ -1,6 +1,6 @@
1 1
 /*
2 2
  * RTMP packet utilities
3
- * Copyright (c) 2009 Kostya Shishkov
3
+ * Copyright (c) 2009 Konstantin Shishkov
4 4
  *
5 5
  * This file is part of FFmpeg.
6 6
  *
... ...
@@ -1,6 +1,6 @@
1 1
 /*
2 2
  * RTMP network protocol
3
- * Copyright (c) 2009 Kostya Shishkov
3
+ * Copyright (c) 2009 Konstantin Shishkov
4 4
  *
5 5
  * This file is part of FFmpeg.
6 6
  *
... ...
@@ -108,39 +108,18 @@ static int tcp_open(URLContext *h, const char *uri, int flags)
108 108
     cur_ai = ai;
109 109
 
110 110
  restart:
111
-    ret = AVERROR(EIO);
112 111
     fd = socket(cur_ai->ai_family, cur_ai->ai_socktype, cur_ai->ai_protocol);
113
-    if (fd < 0)
112
+    if (fd < 0) {
113
+        ret = ff_neterrno();
114 114
         goto fail;
115
+    }
115 116
 
116 117
     if (s->listen) {
117
-        int fd1;
118
-        int reuse = 1;
119
-        struct pollfd lp = { fd, POLLIN, 0 };
120
-        setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
121
-        ret = bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
122
-        if (ret) {
123
-            ret = ff_neterrno();
124
-            goto fail1;
125
-        }
126
-        ret = listen(fd, 1);
127
-        if (ret) {
128
-            ret = ff_neterrno();
129
-            goto fail1;
130
-        }
131
-        ret = poll(&lp, 1, s->listen_timeout >= 0 ? s->listen_timeout : -1);
132
-        if (ret <= 0) {
133
-            ret = AVERROR(ETIMEDOUT);
134
-            goto fail1;
135
-        }
136
-        fd1 = accept(fd, NULL, NULL);
137
-        if (fd1 < 0) {
138
-            ret = ff_neterrno();
118
+        if ((fd = ff_listen_bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen,
119
+                                 s->listen_timeout)) < 0) {
120
+            ret = fd;
139 121
             goto fail1;
140 122
         }
141
-        closesocket(fd);
142
-        fd = fd1;
143
-        ff_socket_nonblock(fd, 1);
144 123
     } else {
145 124
  redo:
146 125
         ff_socket_nonblock(fd, 1);
... ...
@@ -202,6 +181,7 @@ static int tcp_open(URLContext *h, const char *uri, int flags)
202 202
         cur_ai = cur_ai->ai_next;
203 203
         if (fd >= 0)
204 204
             closesocket(fd);
205
+        ret = 0;
205 206
         goto restart;
206 207
     }
207 208
  fail1: