Revision aa519c47

View differences:

libavformat/udp.c
158 158
    return addr_len;
159 159
}
160 160

  
161
static int udp_ipv6_set_local(URLContext *h) {
162
    UDPContext *s = h->priv_data;
161
static int udp_socket_create(UDPContext *s, struct sockaddr_storage *addr, int *addr_len)
162
{
163 163
    int udp_fd = -1;
164
    struct sockaddr_storage clientaddr;
165
    socklen_t addrlen;
166
    char sbuf[NI_MAXSERV];
167
    char hbuf[NI_MAXHOST];
168 164
    struct addrinfo *res0 = NULL, *res = NULL;
169 165
    int family = AF_UNSPEC;
170 166

  
......
182 178
    if (udp_fd < 0)
183 179
        goto fail;
184 180

  
185
    if (bind(udp_fd, res0->ai_addr, res0->ai_addrlen) < 0) {
186
        perror("bind");
187
        goto fail;
188
    }
189
    freeaddrinfo(res0);
190
        res0 = NULL;
191

  
192
    addrlen = sizeof(clientaddr);
193
    if (getsockname(udp_fd, (struct sockaddr *)&clientaddr, &addrlen) < 0) {
194
        perror("getsockname");
195
        goto fail;
196
    }
197

  
198
    if (getnameinfo((struct sockaddr *)&clientaddr, addrlen, hbuf, sizeof(hbuf),  sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
199
        perror("getnameinfo");
200
        goto fail;
201
    }
181
    memcpy(addr, res->ai_addr, res->ai_addrlen);
182
    *addr_len = res->ai_addrlen;
202 183

  
203
    s->local_port = strtol(sbuf, NULL, 10);
184
    freeaddrinfo(res0);
204 185

  
205 186
    return udp_fd;
206 187

  
......
212 193
    return -1;
213 194
}
214 195

  
196
static int udp_port(struct sockaddr_storage *addr, int addr_len)
197
{
198
    char sbuf[NI_MAXSERV];
199
    char hbuf[NI_MAXHOST];
200

  
201
    if (getnameinfo((struct sockaddr *)addr, addr_len, hbuf, sizeof(hbuf),  sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
202
        perror("getnameinfo");
203
        return -1;
204
    }
205

  
206
    return strtol(sbuf, NULL, 10);
207
}
208

  
215 209
#else
216 210

  
217 211
static int udp_set_url(struct sockaddr_in *addr, const char *hostname, int port)
......
225 219
    return sizeof(struct sockaddr_in);
226 220
}
227 221

  
222
static int udp_socket_create(UDPContext *s, struct sockaddr_in *addr, int *addr_len)
223
{
224
    int fd;
225

  
226
    fd = socket(AF_INET, SOCK_DGRAM, 0);
227
    if (fd < 0)
228
        return -1;
229

  
230
    addr->sin_family = AF_INET;
231
    addr->sin_addr.s_addr = htonl (INADDR_ANY);
232
    addr->sin_port = htons(s->local_port);
233
    *addr_len = sizeof(struct sockaddr_in);
234

  
235
    return fd;
236
}
237

  
238
static int udp_port(struct sockaddr_in *addr, int len)
239
{
240
    return ntohs(addr->sin_port);
241
}
228 242
#endif /* CONFIG_IPV6 */
229 243

  
230 244

  
......
295 309
    char buf[256];
296 310
#ifndef CONFIG_IPV6
297 311
    struct sockaddr_in my_addr;
298
    int len;
312
#else
313
    struct sockaddr_storage my_addr;
299 314
#endif
315
    int len;
300 316

  
301 317
    h->is_streamed = 1;
302 318
    h->max_packet_size = 1472;
......
341 357

  
342 358
    if (s->is_multicast && !(h->flags & URL_WRONLY))
343 359
        s->local_port = port;
344
#ifndef CONFIG_IPV6
345
    udp_fd = socket(AF_INET, SOCK_DGRAM, 0);
360
    udp_fd = udp_socket_create(s, &my_addr, &len);
346 361
    if (udp_fd < 0)
347 362
        goto fail;
348 363

  
349
    my_addr.sin_family = AF_INET;
350
    my_addr.sin_addr.s_addr = htonl (INADDR_ANY);
351
    my_addr.sin_port = htons(s->local_port);
352

  
353 364
    if (s->reuse_socket)
354 365
        if (setsockopt (udp_fd, SOL_SOCKET, SO_REUSEADDR, &(s->reuse_socket), sizeof(s->reuse_socket)) != 0)
355 366
            goto fail;
356 367

  
357 368
    /* the bind is needed to give a port to the socket now */
358
    if (bind(udp_fd,(struct sockaddr *)&my_addr, sizeof(my_addr)) < 0)
369
    if (bind(udp_fd,(struct sockaddr *)&my_addr, len) < 0)
359 370
        goto fail;
360 371

  
361 372
    len = sizeof(my_addr);
362 373
    getsockname(udp_fd, (struct sockaddr *)&my_addr, &len);
363
    s->local_port = ntohs(my_addr.sin_port);
364
#else
365
    udp_fd = udp_ipv6_set_local(h);
366
    if (udp_fd < 0)
367
        goto fail;
368
#endif /* CONFIG_IPV6 */
374
    s->local_port = udp_port(&my_addr, len);
375

  
369 376
    if (s->is_multicast) {
370 377
        if (h->flags & URL_WRONLY) {
371 378
            /* output */

Also available in: Unified diff