]> git.sesse.net Git - ffmpeg/blob - libavformat/udp.c
Merge commit '5b35b290dd76db64f079b3940f6148c8f273fc17'
[ffmpeg] / libavformat / udp.c
1 /*
2  * UDP prototype streaming system
3  * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * UDP protocol
25  */
26
27 #define _DEFAULT_SOURCE
28 #define _BSD_SOURCE     /* Needed for using struct ip_mreq with recent glibc */
29
30 #include "avformat.h"
31 #include "avio_internal.h"
32 #include "libavutil/avassert.h"
33 #include "libavutil/parseutils.h"
34 #include "libavutil/fifo.h"
35 #include "libavutil/intreadwrite.h"
36 #include "libavutil/avstring.h"
37 #include "libavutil/opt.h"
38 #include "libavutil/log.h"
39 #include "libavutil/time.h"
40 #include "internal.h"
41 #include "network.h"
42 #include "os_support.h"
43 #include "url.h"
44
45 #if HAVE_UDPLITE_H
46 #include "udplite.h"
47 #else
48 /* On many Linux systems, udplite.h is missing but the kernel supports UDP-Lite.
49  * So, we provide a fallback here.
50  */
51 #define UDPLITE_SEND_CSCOV                               10
52 #define UDPLITE_RECV_CSCOV                               11
53 #endif
54
55 #ifndef IPPROTO_UDPLITE
56 #define IPPROTO_UDPLITE                                  136
57 #endif
58
59 #if HAVE_PTHREAD_CANCEL
60 #include <pthread.h>
61 #endif
62
63 #ifndef HAVE_PTHREAD_CANCEL
64 #define HAVE_PTHREAD_CANCEL 0
65 #endif
66
67 #ifndef IPV6_ADD_MEMBERSHIP
68 #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
69 #define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
70 #endif
71
72 #define UDP_TX_BUF_SIZE 32768
73 #define UDP_MAX_PKT_SIZE 65536
74 #define UDP_HEADER_SIZE 8
75
76 typedef struct UDPContext {
77     const AVClass *class;
78     int udp_fd;
79     int ttl;
80     int udplite_coverage;
81     int buffer_size;
82     int pkt_size;
83     int is_multicast;
84     int is_broadcast;
85     int local_port;
86     int reuse_socket;
87     int overrun_nonfatal;
88     struct sockaddr_storage dest_addr;
89     int dest_addr_len;
90     int is_connected;
91
92     /* Circular Buffer variables for use in UDP receive code */
93     int circular_buffer_size;
94     AVFifoBuffer *fifo;
95     int circular_buffer_error;
96     int64_t packet_gap; /* delay between transmitted packets */
97     int close_req;
98 #if HAVE_PTHREAD_CANCEL
99     pthread_t circular_buffer_thread;
100     pthread_mutex_t mutex;
101     pthread_cond_t cond;
102     int thread_started;
103 #endif
104     uint8_t tmp[UDP_MAX_PKT_SIZE+4];
105     int remaining_in_dg;
106     char *localaddr;
107     int timeout;
108     struct sockaddr_storage local_addr_storage;
109     char *sources;
110     char *block;
111 } UDPContext;
112
113 #define OFFSET(x) offsetof(UDPContext, x)
114 #define D AV_OPT_FLAG_DECODING_PARAM
115 #define E AV_OPT_FLAG_ENCODING_PARAM
116 static const AVOption options[] = {
117     { "buffer_size",    "System data size (in bytes)",                     OFFSET(buffer_size),    AV_OPT_TYPE_INT,    { .i64 = -1 },    -1, INT_MAX, .flags = D|E },
118     { "packet_gap",     "Delay between packets",                           OFFSET(packet_gap),     AV_OPT_TYPE_DURATION,    { .i64 = 0  },     0, INT_MAX, .flags = E },
119     { "localport",      "Local port",                                      OFFSET(local_port),     AV_OPT_TYPE_INT,    { .i64 = -1 },    -1, INT_MAX, D|E },
120     { "local_port",     "Local port",                                      OFFSET(local_port),     AV_OPT_TYPE_INT,    { .i64 = -1 },    -1, INT_MAX, .flags = D|E },
121     { "localaddr",      "Local address",                                   OFFSET(localaddr),      AV_OPT_TYPE_STRING, { .str = NULL },               .flags = D|E },
122     { "udplite_coverage", "choose UDPLite head size which should be validated by checksum", OFFSET(udplite_coverage), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, D|E },
123     { "pkt_size",       "Maximum UDP packet size",                         OFFSET(pkt_size),       AV_OPT_TYPE_INT,    { .i64 = 1472 },  -1, INT_MAX, .flags = D|E },
124     { "reuse",          "explicitly allow reusing UDP sockets",            OFFSET(reuse_socket),   AV_OPT_TYPE_BOOL,   { .i64 = -1 },    -1, 1,       D|E },
125     { "reuse_socket",   "explicitly allow reusing UDP sockets",            OFFSET(reuse_socket),   AV_OPT_TYPE_BOOL,   { .i64 = -1 },    -1, 1,       .flags = D|E },
126     { "broadcast", "explicitly allow or disallow broadcast destination",   OFFSET(is_broadcast),   AV_OPT_TYPE_BOOL,   { .i64 = 0  },     0, 1,       E },
127     { "ttl",            "Time to live (multicast only)",                   OFFSET(ttl),            AV_OPT_TYPE_INT,    { .i64 = 16 },     0, INT_MAX, E },
128     { "connect",        "set if connect() should be called on socket",     OFFSET(is_connected),   AV_OPT_TYPE_BOOL,   { .i64 =  0 },     0, 1,       .flags = D|E },
129     { "fifo_size",      "set the UDP receiving circular buffer size, expressed as a number of packets with size of 188 bytes", OFFSET(circular_buffer_size), AV_OPT_TYPE_INT, {.i64 = 7*4096}, 0, INT_MAX, D },
130     { "overrun_nonfatal", "survive in case of UDP receiving circular buffer overrun", OFFSET(overrun_nonfatal), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1,    D },
131     { "timeout",        "set raise error timeout (only in read mode)",     OFFSET(timeout),        AV_OPT_TYPE_INT,    { .i64 = 0 },      0, INT_MAX, D },
132     { "sources",        "Source list",                                     OFFSET(sources),        AV_OPT_TYPE_STRING, { .str = NULL },               .flags = D|E },
133     { "block",          "Block list",                                      OFFSET(block),          AV_OPT_TYPE_STRING, { .str = NULL },               .flags = D|E },
134     { NULL }
135 };
136
137 static const AVClass udp_class = {
138     .class_name = "udp",
139     .item_name  = av_default_item_name,
140     .option     = options,
141     .version    = LIBAVUTIL_VERSION_INT,
142 };
143
144 static const AVClass udplite_context_class = {
145     .class_name     = "udplite",
146     .item_name      = av_default_item_name,
147     .option         = options,
148     .version        = LIBAVUTIL_VERSION_INT,
149 };
150
151 static void log_net_error(void *ctx, int level, const char* prefix)
152 {
153     char errbuf[100];
154     av_strerror(ff_neterrno(), errbuf, sizeof(errbuf));
155     av_log(ctx, level, "%s: %s\n", prefix, errbuf);
156 }
157
158 static int udp_set_multicast_ttl(int sockfd, int mcastTTL,
159                                  struct sockaddr *addr)
160 {
161 #ifdef IP_MULTICAST_TTL
162     if (addr->sa_family == AF_INET) {
163         if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_TTL, &mcastTTL, sizeof(mcastTTL)) < 0) {
164             log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IP_MULTICAST_TTL)");
165             return -1;
166         }
167     }
168 #endif
169 #if defined(IPPROTO_IPV6) && defined(IPV6_MULTICAST_HOPS)
170     if (addr->sa_family == AF_INET6) {
171         if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &mcastTTL, sizeof(mcastTTL)) < 0) {
172             log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IPV6_MULTICAST_HOPS)");
173             return -1;
174         }
175     }
176 #endif
177     return 0;
178 }
179
180 static int udp_join_multicast_group(int sockfd, struct sockaddr *addr,struct sockaddr *local_addr)
181 {
182 #ifdef IP_ADD_MEMBERSHIP
183     if (addr->sa_family == AF_INET) {
184         struct ip_mreq mreq;
185
186         mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
187         if (local_addr)
188             mreq.imr_interface= ((struct sockaddr_in *)local_addr)->sin_addr;
189         else
190             mreq.imr_interface.s_addr= INADDR_ANY;
191         if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
192             log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IP_ADD_MEMBERSHIP)");
193             return -1;
194         }
195     }
196 #endif
197 #if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6)
198     if (addr->sa_family == AF_INET6) {
199         struct ipv6_mreq mreq6;
200
201         memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
202         mreq6.ipv6mr_interface= 0;
203         if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
204             log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IPV6_ADD_MEMBERSHIP)");
205             return -1;
206         }
207     }
208 #endif
209     return 0;
210 }
211
212 static int udp_leave_multicast_group(int sockfd, struct sockaddr *addr,struct sockaddr *local_addr)
213 {
214 #ifdef IP_DROP_MEMBERSHIP
215     if (addr->sa_family == AF_INET) {
216         struct ip_mreq mreq;
217
218         mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
219         if (local_addr)
220             mreq.imr_interface= ((struct sockaddr_in *)local_addr)->sin_addr;
221         else
222             mreq.imr_interface.s_addr= INADDR_ANY;
223         if (setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
224             log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IP_DROP_MEMBERSHIP)");
225             return -1;
226         }
227     }
228 #endif
229 #if HAVE_STRUCT_IPV6_MREQ && defined(IPPROTO_IPV6)
230     if (addr->sa_family == AF_INET6) {
231         struct ipv6_mreq mreq6;
232
233         memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
234         mreq6.ipv6mr_interface= 0;
235         if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
236             log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IPV6_DROP_MEMBERSHIP)");
237             return -1;
238         }
239     }
240 #endif
241     return 0;
242 }
243
244 static struct addrinfo *udp_resolve_host(URLContext *h,
245                                          const char *hostname, int port,
246                                          int type, int family, int flags)
247 {
248     struct addrinfo hints = { 0 }, *res = 0;
249     int error;
250     char sport[16];
251     const char *node = 0, *service = "0";
252
253     if (port > 0) {
254         snprintf(sport, sizeof(sport), "%d", port);
255         service = sport;
256     }
257     if ((hostname) && (hostname[0] != '\0') && (hostname[0] != '?')) {
258         node = hostname;
259     }
260     hints.ai_socktype = type;
261     hints.ai_family   = family;
262     hints.ai_flags = flags;
263     if ((error = getaddrinfo(node, service, &hints, &res))) {
264         res = NULL;
265         av_log(h, AV_LOG_ERROR, "getaddrinfo(%s, %s): %s\n",
266                node ? node : "unknown",
267                service,
268                gai_strerror(error));
269     }
270
271     return res;
272 }
273
274 static int udp_set_multicast_sources(URLContext *h,
275                                      int sockfd, struct sockaddr *addr,
276                                      int addr_len, char **sources,
277                                      int nb_sources, int include)
278 {
279 #if HAVE_STRUCT_GROUP_SOURCE_REQ && defined(MCAST_BLOCK_SOURCE) && !defined(_WIN32)
280     /* These ones are available in the microsoft SDK, but don't seem to work
281      * as on linux, so just prefer the v4-only approach there for now. */
282     int i;
283     for (i = 0; i < nb_sources; i++) {
284         struct group_source_req mreqs;
285         int level = addr->sa_family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6;
286         struct addrinfo *sourceaddr = udp_resolve_host(h, sources[i], 0,
287                                                        SOCK_DGRAM, AF_UNSPEC,
288                                                        0);
289         if (!sourceaddr)
290             return AVERROR(ENOENT);
291
292         mreqs.gsr_interface = 0;
293         memcpy(&mreqs.gsr_group, addr, addr_len);
294         memcpy(&mreqs.gsr_source, sourceaddr->ai_addr, sourceaddr->ai_addrlen);
295         freeaddrinfo(sourceaddr);
296
297         if (setsockopt(sockfd, level,
298                        include ? MCAST_JOIN_SOURCE_GROUP : MCAST_BLOCK_SOURCE,
299                        (const void *)&mreqs, sizeof(mreqs)) < 0) {
300             if (include)
301                 log_net_error(NULL, AV_LOG_ERROR, "setsockopt(MCAST_JOIN_SOURCE_GROUP)");
302             else
303                 log_net_error(NULL, AV_LOG_ERROR, "setsockopt(MCAST_BLOCK_SOURCE)");
304             return ff_neterrno();
305         }
306     }
307 #elif HAVE_STRUCT_IP_MREQ_SOURCE && defined(IP_BLOCK_SOURCE)
308     int i;
309     if (addr->sa_family != AF_INET) {
310         av_log(NULL, AV_LOG_ERROR,
311                "Setting multicast sources only supported for IPv4\n");
312         return AVERROR(EINVAL);
313     }
314     for (i = 0; i < nb_sources; i++) {
315         struct ip_mreq_source mreqs;
316         struct addrinfo *sourceaddr = udp_resolve_host(h, sources[i], 0,
317                                                        SOCK_DGRAM, AF_UNSPEC,
318                                                        0);
319         if (!sourceaddr)
320             return AVERROR(ENOENT);
321         if (sourceaddr->ai_addr->sa_family != AF_INET) {
322             freeaddrinfo(sourceaddr);
323             av_log(NULL, AV_LOG_ERROR, "%s is of incorrect protocol family\n",
324                    sources[i]);
325             return AVERROR(EINVAL);
326         }
327
328         mreqs.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
329         mreqs.imr_interface.s_addr = INADDR_ANY;
330         mreqs.imr_sourceaddr.s_addr = ((struct sockaddr_in *)sourceaddr->ai_addr)->sin_addr.s_addr;
331         freeaddrinfo(sourceaddr);
332
333         if (setsockopt(sockfd, IPPROTO_IP,
334                        include ? IP_ADD_SOURCE_MEMBERSHIP : IP_BLOCK_SOURCE,
335                        (const void *)&mreqs, sizeof(mreqs)) < 0) {
336             if (include)
337                 log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IP_ADD_SOURCE_MEMBERSHIP)");
338             else
339                 log_net_error(NULL, AV_LOG_ERROR, "setsockopt(IP_BLOCK_SOURCE)");
340             return ff_neterrno();
341         }
342     }
343 #else
344     return AVERROR(ENOSYS);
345 #endif
346     return 0;
347 }
348 static int udp_set_url(URLContext *h,
349                        struct sockaddr_storage *addr,
350                        const char *hostname, int port)
351 {
352     struct addrinfo *res0;
353     int addr_len;
354
355     res0 = udp_resolve_host(h, hostname, port, SOCK_DGRAM, AF_UNSPEC, 0);
356     if (!res0) return AVERROR(EIO);
357     memcpy(addr, res0->ai_addr, res0->ai_addrlen);
358     addr_len = res0->ai_addrlen;
359     freeaddrinfo(res0);
360
361     return addr_len;
362 }
363
364 static int udp_socket_create(URLContext *h, struct sockaddr_storage *addr,
365                              socklen_t *addr_len, const char *localaddr)
366 {
367     UDPContext *s = h->priv_data;
368     int udp_fd = -1;
369     struct addrinfo *res0, *res;
370     int family = AF_UNSPEC;
371
372     if (((struct sockaddr *) &s->dest_addr)->sa_family)
373         family = ((struct sockaddr *) &s->dest_addr)->sa_family;
374     res0 = udp_resolve_host(h, (localaddr && localaddr[0]) ? localaddr : NULL,
375                             s->local_port,
376                             SOCK_DGRAM, family, AI_PASSIVE);
377     if (!res0)
378         goto fail;
379     for (res = res0; res; res=res->ai_next) {
380         if (s->udplite_coverage)
381             udp_fd = ff_socket(res->ai_family, SOCK_DGRAM, IPPROTO_UDPLITE);
382         else
383             udp_fd = ff_socket(res->ai_family, SOCK_DGRAM, 0);
384         if (udp_fd != -1) break;
385         log_net_error(NULL, AV_LOG_ERROR, "socket");
386     }
387
388     if (udp_fd < 0)
389         goto fail;
390
391     memcpy(addr, res->ai_addr, res->ai_addrlen);
392     *addr_len = res->ai_addrlen;
393
394     freeaddrinfo(res0);
395
396     return udp_fd;
397
398  fail:
399     if (udp_fd >= 0)
400         closesocket(udp_fd);
401     if(res0)
402         freeaddrinfo(res0);
403     return -1;
404 }
405
406 static int udp_port(struct sockaddr_storage *addr, int addr_len)
407 {
408     char sbuf[sizeof(int)*3+1];
409     int error;
410
411     if ((error = getnameinfo((struct sockaddr *)addr, addr_len, NULL, 0,  sbuf, sizeof(sbuf), NI_NUMERICSERV)) != 0) {
412         av_log(NULL, AV_LOG_ERROR, "getnameinfo: %s\n", gai_strerror(error));
413         return -1;
414     }
415
416     return strtol(sbuf, NULL, 10);
417 }
418
419
420 /**
421  * If no filename is given to av_open_input_file because you want to
422  * get the local port first, then you must call this function to set
423  * the remote server address.
424  *
425  * url syntax: udp://host:port[?option=val...]
426  * option: 'ttl=n'       : set the ttl value (for multicast only)
427  *         'localport=n' : set the local port
428  *         'pkt_size=n'  : set max packet size
429  *         'reuse=1'     : enable reusing the socket
430  *         'overrun_nonfatal=1': survive in case of circular buffer overrun
431  *
432  * @param h media file context
433  * @param uri of the remote server
434  * @return zero if no error.
435  */
436 int ff_udp_set_remote_url(URLContext *h, const char *uri)
437 {
438     UDPContext *s = h->priv_data;
439     char hostname[256], buf[10];
440     int port;
441     const char *p;
442
443     av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
444
445     /* set the destination address */
446     s->dest_addr_len = udp_set_url(h, &s->dest_addr, hostname, port);
447     if (s->dest_addr_len < 0) {
448         return AVERROR(EIO);
449     }
450     s->is_multicast = ff_is_multicast_address((struct sockaddr*) &s->dest_addr);
451     p = strchr(uri, '?');
452     if (p) {
453         if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
454             int was_connected = s->is_connected;
455             s->is_connected = strtol(buf, NULL, 10);
456             if (s->is_connected && !was_connected) {
457                 if (connect(s->udp_fd, (struct sockaddr *) &s->dest_addr,
458                             s->dest_addr_len)) {
459                     s->is_connected = 0;
460                     log_net_error(h, AV_LOG_ERROR, "connect");
461                     return AVERROR(EIO);
462                 }
463             }
464         }
465     }
466
467     return 0;
468 }
469
470 /**
471  * Return the local port used by the UDP connection
472  * @param h media file context
473  * @return the local port number
474  */
475 int ff_udp_get_local_port(URLContext *h)
476 {
477     UDPContext *s = h->priv_data;
478     return s->local_port;
479 }
480
481 /**
482  * Return the udp file handle for select() usage to wait for several RTP
483  * streams at the same time.
484  * @param h media file context
485  */
486 static int udp_get_file_handle(URLContext *h)
487 {
488     UDPContext *s = h->priv_data;
489     return s->udp_fd;
490 }
491
492 #if HAVE_PTHREAD_CANCEL
493 static void *circular_buffer_task_rx( void *_URLContext)
494 {
495     URLContext *h = _URLContext;
496     UDPContext *s = h->priv_data;
497     int old_cancelstate;
498
499     pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_cancelstate);
500     pthread_mutex_lock(&s->mutex);
501     if (ff_socket_nonblock(s->udp_fd, 0) < 0) {
502         av_log(h, AV_LOG_ERROR, "Failed to set blocking mode");
503         s->circular_buffer_error = AVERROR(EIO);
504         goto end;
505     }
506     while(1) {
507         int len;
508
509         pthread_mutex_unlock(&s->mutex);
510         /* Blocking operations are always cancellation points;
511            see "General Information" / "Thread Cancelation Overview"
512            in Single Unix. */
513         pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old_cancelstate);
514         len = recv(s->udp_fd, s->tmp+4, sizeof(s->tmp)-4, 0);
515         pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_cancelstate);
516         pthread_mutex_lock(&s->mutex);
517         if (len < 0) {
518             if (ff_neterrno() != AVERROR(EAGAIN) && ff_neterrno() != AVERROR(EINTR)) {
519                 s->circular_buffer_error = ff_neterrno();
520                 goto end;
521             }
522             continue;
523         }
524         AV_WL32(s->tmp, len);
525
526         if(av_fifo_space(s->fifo) < len + 4) {
527             /* No Space left */
528             if (s->overrun_nonfatal) {
529                 av_log(h, AV_LOG_WARNING, "Circular buffer overrun. "
530                         "Surviving due to overrun_nonfatal option\n");
531                 continue;
532             } else {
533                 av_log(h, AV_LOG_ERROR, "Circular buffer overrun. "
534                         "To avoid, increase fifo_size URL option. "
535                         "To survive in such case, use overrun_nonfatal option\n");
536                 s->circular_buffer_error = AVERROR(EIO);
537                 goto end;
538             }
539         }
540         av_fifo_generic_write(s->fifo, s->tmp, len+4, NULL);
541         pthread_cond_signal(&s->cond);
542     }
543
544 end:
545     pthread_cond_signal(&s->cond);
546     pthread_mutex_unlock(&s->mutex);
547     return NULL;
548 }
549
550 static void *circular_buffer_task_tx( void *_URLContext)
551 {
552     URLContext *h = _URLContext;
553     UDPContext *s = h->priv_data;
554     int old_cancelstate;
555
556     pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_cancelstate);
557     pthread_mutex_lock(&s->mutex);
558
559     if (ff_socket_nonblock(s->udp_fd, 0) < 0) {
560         av_log(h, AV_LOG_ERROR, "Failed to set blocking mode");
561         s->circular_buffer_error = AVERROR(EIO);
562         goto end;
563     }
564
565     for(;;) {
566         int len;
567         const uint8_t *p;
568         uint8_t tmp[4];
569
570         len=av_fifo_size(s->fifo);
571
572         while (len<4) {
573             if (s->close_req)
574                 goto end;
575             if (pthread_cond_wait(&s->cond, &s->mutex) < 0) {
576                 goto end;
577             }
578             len=av_fifo_size(s->fifo);
579         }
580
581         av_fifo_generic_read(s->fifo, tmp, 4, NULL);
582         len=AV_RL32(tmp);
583
584         av_assert0(len >= 0);
585         av_assert0(len <= sizeof(s->tmp));
586
587         av_fifo_generic_read(s->fifo, s->tmp, len, NULL);
588
589         pthread_mutex_unlock(&s->mutex);
590         pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old_cancelstate);
591
592         p = s->tmp;
593         while (len) {
594             int ret;
595             av_assert0(len > 0);
596             if (!s->is_connected) {
597                 ret = sendto (s->udp_fd, p, len, 0,
598                             (struct sockaddr *) &s->dest_addr,
599                             s->dest_addr_len);
600             } else
601                 ret = send(s->udp_fd, p, len, 0);
602             if (ret >= 0) {
603                 len -= ret;
604                 p   += ret;
605             } else {
606                 ret = ff_neterrno();
607                 if (ret != AVERROR(EAGAIN) && ret != AVERROR(EINTR)) {
608                     pthread_mutex_lock(&s->mutex);
609                     s->circular_buffer_error = ret;
610                     pthread_mutex_unlock(&s->mutex);
611                     return NULL;
612                 }
613             }
614         }
615
616         av_usleep(s->packet_gap);
617
618         pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_cancelstate);
619         pthread_mutex_lock(&s->mutex);
620     }
621
622 end:
623     pthread_mutex_unlock(&s->mutex);
624     return NULL;
625 }
626
627
628 #endif
629
630 static int parse_source_list(char *buf, char **sources, int *num_sources,
631                              int max_sources)
632 {
633     char *source_start;
634
635     source_start = buf;
636     while (1) {
637         char *next = strchr(source_start, ',');
638         if (next)
639             *next = '\0';
640         sources[*num_sources] = av_strdup(source_start);
641         if (!sources[*num_sources])
642             return AVERROR(ENOMEM);
643         source_start = next + 1;
644         (*num_sources)++;
645         if (*num_sources >= max_sources || !next)
646             break;
647     }
648     return 0;
649 }
650
651 /* put it in UDP context */
652 /* return non zero if error */
653 static int udp_open(URLContext *h, const char *uri, int flags)
654 {
655     char hostname[1024], localaddr[1024] = "";
656     int port, udp_fd = -1, tmp, bind_ret = -1, dscp = -1;
657     UDPContext *s = h->priv_data;
658     int is_output;
659     const char *p;
660     char buf[256];
661     struct sockaddr_storage my_addr;
662     socklen_t len;
663     int i, num_include_sources = 0, num_exclude_sources = 0;
664     char *include_sources[32], *exclude_sources[32];
665
666     h->is_streamed = 1;
667
668     is_output = !(flags & AVIO_FLAG_READ);
669     if (s->buffer_size < 0)
670         s->buffer_size = is_output ? UDP_TX_BUF_SIZE : UDP_MAX_PKT_SIZE;
671
672     if (s->sources) {
673         if (parse_source_list(s->sources, include_sources,
674                               &num_include_sources,
675                               FF_ARRAY_ELEMS(include_sources)))
676             goto fail;
677     }
678
679     if (s->block) {
680         if (parse_source_list(s->block, exclude_sources, &num_exclude_sources,
681                               FF_ARRAY_ELEMS(exclude_sources)))
682             goto fail;
683     }
684
685     if (s->pkt_size > 0)
686         h->max_packet_size = s->pkt_size;
687
688     p = strchr(uri, '?');
689     if (p) {
690         if (av_find_info_tag(buf, sizeof(buf), "reuse", p)) {
691             char *endptr = NULL;
692             s->reuse_socket = strtol(buf, &endptr, 10);
693             /* assume if no digits were found it is a request to enable it */
694             if (buf == endptr)
695                 s->reuse_socket = 1;
696         }
697         if (av_find_info_tag(buf, sizeof(buf), "overrun_nonfatal", p)) {
698             char *endptr = NULL;
699             s->overrun_nonfatal = strtol(buf, &endptr, 10);
700             /* assume if no digits were found it is a request to enable it */
701             if (buf == endptr)
702                 s->overrun_nonfatal = 1;
703             if (!HAVE_PTHREAD_CANCEL)
704                 av_log(h, AV_LOG_WARNING,
705                        "'overrun_nonfatal' option was set but it is not supported "
706                        "on this build (pthread support is required)\n");
707         }
708         if (av_find_info_tag(buf, sizeof(buf), "ttl", p)) {
709             s->ttl = strtol(buf, NULL, 10);
710         }
711         if (av_find_info_tag(buf, sizeof(buf), "udplite_coverage", p)) {
712             s->udplite_coverage = strtol(buf, NULL, 10);
713         }
714         if (av_find_info_tag(buf, sizeof(buf), "localport", p)) {
715             s->local_port = strtol(buf, NULL, 10);
716         }
717         if (av_find_info_tag(buf, sizeof(buf), "pkt_size", p)) {
718             s->pkt_size = strtol(buf, NULL, 10);
719         }
720         if (av_find_info_tag(buf, sizeof(buf), "buffer_size", p)) {
721             s->buffer_size = strtol(buf, NULL, 10);
722         }
723         if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
724             s->is_connected = strtol(buf, NULL, 10);
725         }
726         if (av_find_info_tag(buf, sizeof(buf), "dscp", p)) {
727             dscp = strtol(buf, NULL, 10);
728         }
729         if (av_find_info_tag(buf, sizeof(buf), "fifo_size", p)) {
730             s->circular_buffer_size = strtol(buf, NULL, 10);
731             if (!HAVE_PTHREAD_CANCEL)
732                 av_log(h, AV_LOG_WARNING,
733                        "'circular_buffer_size' option was set but it is not supported "
734                        "on this build (pthread support is required)\n");
735         }
736         if (av_find_info_tag(buf, sizeof(buf), "packet_gap", p)) {
737             if (av_parse_time(&s->packet_gap, buf, 1)<0) {
738                 av_log(h, AV_LOG_ERROR, "Can't parse 'packet_gap'");
739                 goto fail;
740             }
741             if (!HAVE_PTHREAD_CANCEL)
742                 av_log(h, AV_LOG_WARNING,
743                        "'packet_gap' option was set but it is not supported "
744                        "on this build (pthread support is required)\n");
745         }
746         if (av_find_info_tag(buf, sizeof(buf), "localaddr", p)) {
747             av_strlcpy(localaddr, buf, sizeof(localaddr));
748         }
749         if (av_find_info_tag(buf, sizeof(buf), "sources", p)) {
750             if (parse_source_list(buf, include_sources, &num_include_sources,
751                                   FF_ARRAY_ELEMS(include_sources)))
752                 goto fail;
753         }
754         if (av_find_info_tag(buf, sizeof(buf), "block", p)) {
755             if (parse_source_list(buf, exclude_sources, &num_exclude_sources,
756                                   FF_ARRAY_ELEMS(exclude_sources)))
757                 goto fail;
758         }
759         if (!is_output && av_find_info_tag(buf, sizeof(buf), "timeout", p))
760             s->timeout = strtol(buf, NULL, 10);
761         if (is_output && av_find_info_tag(buf, sizeof(buf), "broadcast", p))
762             s->is_broadcast = strtol(buf, NULL, 10);
763     }
764     /* handling needed to support options picking from both AVOption and URL */
765     s->circular_buffer_size *= 188;
766     if (flags & AVIO_FLAG_WRITE) {
767         h->max_packet_size = s->pkt_size;
768     } else {
769         h->max_packet_size = UDP_MAX_PKT_SIZE;
770     }
771     h->rw_timeout = s->timeout;
772
773     /* fill the dest addr */
774     av_url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri);
775
776     /* XXX: fix av_url_split */
777     if (hostname[0] == '\0' || hostname[0] == '?') {
778         /* only accepts null hostname if input */
779         if (!(flags & AVIO_FLAG_READ))
780             goto fail;
781     } else {
782         if (ff_udp_set_remote_url(h, uri) < 0)
783             goto fail;
784     }
785
786     if ((s->is_multicast || s->local_port <= 0) && (h->flags & AVIO_FLAG_READ))
787         s->local_port = port;
788
789     if (localaddr[0])
790         udp_fd = udp_socket_create(h, &my_addr, &len, localaddr);
791     else
792         udp_fd = udp_socket_create(h, &my_addr, &len, s->localaddr);
793     if (udp_fd < 0)
794         goto fail;
795
796     s->local_addr_storage=my_addr; //store for future multicast join
797
798     /* Follow the requested reuse option, unless it's multicast in which
799      * case enable reuse unless explicitly disabled.
800      */
801     if (s->reuse_socket > 0 || (s->is_multicast && s->reuse_socket < 0)) {
802         s->reuse_socket = 1;
803         if (setsockopt (udp_fd, SOL_SOCKET, SO_REUSEADDR, &(s->reuse_socket), sizeof(s->reuse_socket)) != 0)
804             goto fail;
805     }
806
807     if (s->is_broadcast) {
808 #ifdef SO_BROADCAST
809         if (setsockopt (udp_fd, SOL_SOCKET, SO_BROADCAST, &(s->is_broadcast), sizeof(s->is_broadcast)) != 0)
810 #endif
811            goto fail;
812     }
813
814     /* Set the checksum coverage for UDP-Lite (RFC 3828) for sending and receiving.
815      * The receiver coverage has to be less than or equal to the sender coverage.
816      * Otherwise, the receiver will drop all packets.
817      */
818     if (s->udplite_coverage) {
819         if (setsockopt (udp_fd, IPPROTO_UDPLITE, UDPLITE_SEND_CSCOV, &(s->udplite_coverage), sizeof(s->udplite_coverage)) != 0)
820             av_log(h, AV_LOG_WARNING, "socket option UDPLITE_SEND_CSCOV not available");
821
822         if (setsockopt (udp_fd, IPPROTO_UDPLITE, UDPLITE_RECV_CSCOV, &(s->udplite_coverage), sizeof(s->udplite_coverage)) != 0)
823             av_log(h, AV_LOG_WARNING, "socket option UDPLITE_RECV_CSCOV not available");
824     }
825
826     if (dscp >= 0) {
827         dscp <<= 2;
828         if (setsockopt (udp_fd, IPPROTO_IP, IP_TOS, &dscp, sizeof(dscp)) != 0)
829             goto fail;
830     }
831
832     /* If multicast, try binding the multicast address first, to avoid
833      * receiving UDP packets from other sources aimed at the same UDP
834      * port. This fails on windows. This makes sending to the same address
835      * using sendto() fail, so only do it if we're opened in read-only mode. */
836     if (s->is_multicast && !(h->flags & AVIO_FLAG_WRITE)) {
837         bind_ret = bind(udp_fd,(struct sockaddr *)&s->dest_addr, len);
838     }
839     /* bind to the local address if not multicast or if the multicast
840      * bind failed */
841     /* the bind is needed to give a port to the socket now */
842     if (bind_ret < 0 && bind(udp_fd,(struct sockaddr *)&my_addr, len) < 0) {
843         log_net_error(h, AV_LOG_ERROR, "bind failed");
844         goto fail;
845     }
846
847     len = sizeof(my_addr);
848     getsockname(udp_fd, (struct sockaddr *)&my_addr, &len);
849     s->local_port = udp_port(&my_addr, len);
850
851     if (s->is_multicast) {
852         if (h->flags & AVIO_FLAG_WRITE) {
853             /* output */
854             if (udp_set_multicast_ttl(udp_fd, s->ttl, (struct sockaddr *)&s->dest_addr) < 0)
855                 goto fail;
856         }
857         if (h->flags & AVIO_FLAG_READ) {
858             /* input */
859             if (num_include_sources && num_exclude_sources) {
860                 av_log(h, AV_LOG_ERROR, "Simultaneously including and excluding multicast sources is not supported\n");
861                 goto fail;
862             }
863             if (num_include_sources) {
864                 if (udp_set_multicast_sources(h, udp_fd,
865                                               (struct sockaddr *)&s->dest_addr,
866                                               s->dest_addr_len,
867                                               include_sources,
868                                               num_include_sources, 1) < 0)
869                     goto fail;
870             } else {
871                 if (udp_join_multicast_group(udp_fd, (struct sockaddr *)&s->dest_addr,(struct sockaddr *)&s->local_addr_storage) < 0)
872                     goto fail;
873             }
874             if (num_exclude_sources) {
875                 if (udp_set_multicast_sources(h, udp_fd,
876                                               (struct sockaddr *)&s->dest_addr,
877                                               s->dest_addr_len,
878                                               exclude_sources,
879                                               num_exclude_sources, 0) < 0)
880                     goto fail;
881             }
882         }
883     }
884
885     if (is_output) {
886         /* limit the tx buf size to limit latency */
887         tmp = s->buffer_size;
888         if (setsockopt(udp_fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp)) < 0) {
889             log_net_error(h, AV_LOG_ERROR, "setsockopt(SO_SNDBUF)");
890             goto fail;
891         }
892     } else {
893         /* set udp recv buffer size to the requested value (default 64K) */
894         tmp = s->buffer_size;
895         if (setsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &tmp, sizeof(tmp)) < 0) {
896             log_net_error(h, AV_LOG_WARNING, "setsockopt(SO_RECVBUF)");
897         }
898         len = sizeof(tmp);
899         if (getsockopt(udp_fd, SOL_SOCKET, SO_RCVBUF, &tmp, &len) < 0) {
900             log_net_error(h, AV_LOG_WARNING, "getsockopt(SO_RCVBUF)");
901         } else {
902             av_log(h, AV_LOG_DEBUG, "end receive buffer size reported is %d\n", tmp);
903             if(tmp < s->buffer_size)
904                 av_log(h, AV_LOG_WARNING, "attempted to set receive buffer to size %d but it only ended up set as %d", s->buffer_size, tmp);
905         }
906
907         /* make the socket non-blocking */
908         ff_socket_nonblock(udp_fd, 1);
909     }
910     if (s->is_connected) {
911         if (connect(udp_fd, (struct sockaddr *) &s->dest_addr, s->dest_addr_len)) {
912             log_net_error(h, AV_LOG_ERROR, "connect");
913             goto fail;
914         }
915     }
916
917     for (i = 0; i < num_include_sources; i++)
918         av_freep(&include_sources[i]);
919     for (i = 0; i < num_exclude_sources; i++)
920         av_freep(&exclude_sources[i]);
921
922     s->udp_fd = udp_fd;
923
924 #if HAVE_PTHREAD_CANCEL
925     /*
926       Create thread in case of:
927       1. Input and circular_buffer_size is set
928       2. Output and packet_gap and circular_buffer_size is set
929     */
930
931     if (is_output && s->packet_gap && !s->circular_buffer_size) {
932         /* Warn user in case of 'circular_buffer_size' is not set */
933         av_log(h, AV_LOG_WARNING,"'packet_gap' option was set but 'circular_buffer_size' is not, but required\n");
934     }
935
936     if ((!is_output && s->circular_buffer_size) || (is_output && s->packet_gap && s->circular_buffer_size)) {
937         int ret;
938
939         /* start the task going */
940         s->fifo = av_fifo_alloc(s->circular_buffer_size);
941         ret = pthread_mutex_init(&s->mutex, NULL);
942         if (ret != 0) {
943             av_log(h, AV_LOG_ERROR, "pthread_mutex_init failed : %s\n", strerror(ret));
944             goto fail;
945         }
946         ret = pthread_cond_init(&s->cond, NULL);
947         if (ret != 0) {
948             av_log(h, AV_LOG_ERROR, "pthread_cond_init failed : %s\n", strerror(ret));
949             goto cond_fail;
950         }
951         ret = pthread_create(&s->circular_buffer_thread, NULL, is_output?circular_buffer_task_tx:circular_buffer_task_rx, h);
952         if (ret != 0) {
953             av_log(h, AV_LOG_ERROR, "pthread_create failed : %s\n", strerror(ret));
954             goto thread_fail;
955         }
956         s->thread_started = 1;
957     }
958 #endif
959
960     return 0;
961 #if HAVE_PTHREAD_CANCEL
962  thread_fail:
963     pthread_cond_destroy(&s->cond);
964  cond_fail:
965     pthread_mutex_destroy(&s->mutex);
966 #endif
967  fail:
968     if (udp_fd >= 0)
969         closesocket(udp_fd);
970     av_fifo_freep(&s->fifo);
971     for (i = 0; i < num_include_sources; i++)
972         av_freep(&include_sources[i]);
973     for (i = 0; i < num_exclude_sources; i++)
974         av_freep(&exclude_sources[i]);
975     return AVERROR(EIO);
976 }
977
978 static int udplite_open(URLContext *h, const char *uri, int flags)
979 {
980     UDPContext *s = h->priv_data;
981
982     // set default checksum coverage
983     s->udplite_coverage = UDP_HEADER_SIZE;
984
985     return udp_open(h, uri, flags);
986 }
987
988 static int udp_read(URLContext *h, uint8_t *buf, int size)
989 {
990     UDPContext *s = h->priv_data;
991     int ret;
992 #if HAVE_PTHREAD_CANCEL
993     int avail, nonblock = h->flags & AVIO_FLAG_NONBLOCK;
994
995     if (s->fifo) {
996         pthread_mutex_lock(&s->mutex);
997         do {
998             avail = av_fifo_size(s->fifo);
999             if (avail) { // >=size) {
1000                 uint8_t tmp[4];
1001
1002                 av_fifo_generic_read(s->fifo, tmp, 4, NULL);
1003                 avail= AV_RL32(tmp);
1004                 if(avail > size){
1005                     av_log(h, AV_LOG_WARNING, "Part of datagram lost due to insufficient buffer size\n");
1006                     avail= size;
1007                 }
1008
1009                 av_fifo_generic_read(s->fifo, buf, avail, NULL);
1010                 av_fifo_drain(s->fifo, AV_RL32(tmp) - avail);
1011                 pthread_mutex_unlock(&s->mutex);
1012                 return avail;
1013             } else if(s->circular_buffer_error){
1014                 int err = s->circular_buffer_error;
1015                 pthread_mutex_unlock(&s->mutex);
1016                 return err;
1017             } else if(nonblock) {
1018                 pthread_mutex_unlock(&s->mutex);
1019                 return AVERROR(EAGAIN);
1020             }
1021             else {
1022                 /* FIXME: using the monotonic clock would be better,
1023                    but it does not exist on all supported platforms. */
1024                 int64_t t = av_gettime() + 100000;
1025                 struct timespec tv = { .tv_sec  =  t / 1000000,
1026                                        .tv_nsec = (t % 1000000) * 1000 };
1027                 if (pthread_cond_timedwait(&s->cond, &s->mutex, &tv) < 0) {
1028                     pthread_mutex_unlock(&s->mutex);
1029                     return AVERROR(errno == ETIMEDOUT ? EAGAIN : errno);
1030                 }
1031                 nonblock = 1;
1032             }
1033         } while( 1);
1034     }
1035 #endif
1036
1037     if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
1038         ret = ff_network_wait_fd(s->udp_fd, 0);
1039         if (ret < 0)
1040             return ret;
1041     }
1042     ret = recv(s->udp_fd, buf, size, 0);
1043
1044     return ret < 0 ? ff_neterrno() : ret;
1045 }
1046
1047 static int udp_write(URLContext *h, const uint8_t *buf, int size)
1048 {
1049     UDPContext *s = h->priv_data;
1050     int ret;
1051
1052 #if HAVE_PTHREAD_CANCEL
1053     if (s->fifo) {
1054         uint8_t tmp[4];
1055
1056         pthread_mutex_lock(&s->mutex);
1057
1058         /*
1059           Return error if last tx failed.
1060           Here we can't know on which packet error was, but it needs to know that error exists.
1061         */
1062         if (s->circular_buffer_error<0) {
1063             int err=s->circular_buffer_error;
1064             pthread_mutex_unlock(&s->mutex);
1065             return err;
1066         }
1067
1068         if(av_fifo_space(s->fifo) < size + 4) {
1069             /* What about a partial packet tx ? */
1070             pthread_mutex_unlock(&s->mutex);
1071             return AVERROR(ENOMEM);
1072         }
1073         AV_WL32(tmp, size);
1074         av_fifo_generic_write(s->fifo, tmp, 4, NULL); /* size of packet */
1075         av_fifo_generic_write(s->fifo, (uint8_t *)buf, size, NULL); /* the data */
1076         pthread_cond_signal(&s->cond);
1077         pthread_mutex_unlock(&s->mutex);
1078         return size;
1079     }
1080 #endif
1081     if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
1082         ret = ff_network_wait_fd(s->udp_fd, 1);
1083         if (ret < 0)
1084             return ret;
1085     }
1086
1087     if (!s->is_connected) {
1088         ret = sendto (s->udp_fd, buf, size, 0,
1089                       (struct sockaddr *) &s->dest_addr,
1090                       s->dest_addr_len);
1091     } else
1092         ret = send(s->udp_fd, buf, size, 0);
1093
1094     return ret < 0 ? ff_neterrno() : ret;
1095 }
1096
1097 static int udp_close(URLContext *h)
1098 {
1099     UDPContext *s = h->priv_data;
1100
1101 #if HAVE_PTHREAD_CANCEL
1102     // Request close once writing is finished
1103     if (s->thread_started && !(h->flags & AVIO_FLAG_READ)) {
1104         pthread_mutex_lock(&s->mutex);
1105         s->close_req = 1;
1106         pthread_cond_signal(&s->cond);
1107         pthread_mutex_unlock(&s->mutex);
1108     }
1109 #endif
1110
1111     if (s->is_multicast && (h->flags & AVIO_FLAG_READ))
1112         udp_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr,(struct sockaddr *)&s->local_addr_storage);
1113 #if HAVE_PTHREAD_CANCEL
1114     if (s->thread_started) {
1115         int ret;
1116         // Cancel only read, as write has been signaled as success to the user
1117         if (h->flags & AVIO_FLAG_READ)
1118             pthread_cancel(s->circular_buffer_thread);
1119         ret = pthread_join(s->circular_buffer_thread, NULL);
1120         if (ret != 0)
1121             av_log(h, AV_LOG_ERROR, "pthread_join(): %s\n", strerror(ret));
1122         pthread_mutex_destroy(&s->mutex);
1123         pthread_cond_destroy(&s->cond);
1124     }
1125 #endif
1126     closesocket(s->udp_fd);
1127     av_fifo_freep(&s->fifo);
1128     return 0;
1129 }
1130
1131 const URLProtocol ff_udp_protocol = {
1132     .name                = "udp",
1133     .url_open            = udp_open,
1134     .url_read            = udp_read,
1135     .url_write           = udp_write,
1136     .url_close           = udp_close,
1137     .url_get_file_handle = udp_get_file_handle,
1138     .priv_data_size      = sizeof(UDPContext),
1139     .priv_data_class     = &udp_class,
1140     .flags               = URL_PROTOCOL_FLAG_NETWORK,
1141 };
1142
1143 const URLProtocol ff_udplite_protocol = {
1144     .name                = "udplite",
1145     .url_open            = udplite_open,
1146     .url_read            = udp_read,
1147     .url_write           = udp_write,
1148     .url_close           = udp_close,
1149     .url_get_file_handle = udp_get_file_handle,
1150     .priv_data_size      = sizeof(UDPContext),
1151     .priv_data_class     = &udplite_context_class,
1152     .flags               = URL_PROTOCOL_FLAG_NETWORK,
1153 };