Mercurial > libavformat.hg
comparison udp.c @ 885:da1d5db0ce5c libavformat
COSMETICS: Remove all trailing whitespace.
author | diego |
---|---|
date | Sat, 17 Dec 2005 18:14:38 +0000 |
parents | 2ad855f5a156 |
children | edbe5c3717f9 |
comparison
equal
deleted
inserted
replaced
884:2ece9c9dd94c | 885:da1d5db0ce5c |
---|---|
51 | 51 |
52 #ifdef CONFIG_IPV6 | 52 #ifdef CONFIG_IPV6 |
53 | 53 |
54 int udp_ipv6_is_multicast_address(const struct sockaddr *addr) { | 54 int udp_ipv6_is_multicast_address(const struct sockaddr *addr) { |
55 if (addr->sa_family == AF_INET) | 55 if (addr->sa_family == AF_INET) |
56 return IN_MULTICAST(ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr)); | 56 return IN_MULTICAST(ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr)); |
57 if (addr->sa_family == AF_INET6) | 57 if (addr->sa_family == AF_INET6) |
58 return IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)addr)->sin6_addr); | 58 return IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)addr)->sin6_addr); |
59 return -1; | 59 return -1; |
60 } | 60 } |
61 | 61 |
62 int udp_ipv6_set_multicast_ttl(int sockfd, int mcastTTL, struct sockaddr *addr) { | 62 int udp_ipv6_set_multicast_ttl(int sockfd, int mcastTTL, struct sockaddr *addr) { |
63 if (addr->sa_family == AF_INET) { | 63 if (addr->sa_family == AF_INET) { |
75 return 0; | 75 return 0; |
76 } | 76 } |
77 | 77 |
78 int udp_ipv6_join_multicast_group(int sockfd, struct sockaddr *addr) { | 78 int udp_ipv6_join_multicast_group(int sockfd, struct sockaddr *addr) { |
79 struct ip_mreq mreq; | 79 struct ip_mreq mreq; |
80 struct ipv6_mreq mreq6; | 80 struct ipv6_mreq mreq6; |
81 if (addr->sa_family == AF_INET) { | 81 if (addr->sa_family == AF_INET) { |
82 mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr; | 82 mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr; |
83 mreq.imr_interface.s_addr= INADDR_ANY; | 83 mreq.imr_interface.s_addr= INADDR_ANY; |
84 if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) { | 84 if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) { |
85 perror("setsockopt(IP_ADD_MEMBERSHIP)"); | 85 perror("setsockopt(IP_ADD_MEMBERSHIP)"); |
97 return 0; | 97 return 0; |
98 } | 98 } |
99 | 99 |
100 int udp_ipv6_leave_multicast_group(int sockfd, struct sockaddr *addr) { | 100 int udp_ipv6_leave_multicast_group(int sockfd, struct sockaddr *addr) { |
101 struct ip_mreq mreq; | 101 struct ip_mreq mreq; |
102 struct ipv6_mreq mreq6; | 102 struct ipv6_mreq mreq6; |
103 if (addr->sa_family == AF_INET) { | 103 if (addr->sa_family == AF_INET) { |
104 mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr; | 104 mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr; |
105 mreq.imr_interface.s_addr= INADDR_ANY; | 105 mreq.imr_interface.s_addr= INADDR_ANY; |
106 if (setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) { | 106 if (setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) { |
107 perror("setsockopt(IP_DROP_MEMBERSHIP)"); | 107 perror("setsockopt(IP_DROP_MEMBERSHIP)"); |
134 } | 134 } |
135 if ((node) || (service)) { | 135 if ((node) || (service)) { |
136 memset(&hints, 0, sizeof(hints)); | 136 memset(&hints, 0, sizeof(hints)); |
137 hints.ai_socktype = type; | 137 hints.ai_socktype = type; |
138 hints.ai_family = family; | 138 hints.ai_family = family; |
139 hints.ai_flags = flags; | 139 hints.ai_flags = flags; |
140 if ((error = getaddrinfo(node, service, &hints, &res))) { | 140 if ((error = getaddrinfo(node, service, &hints, &res))) { |
141 av_log(NULL, AV_LOG_ERROR, "udp_ipv6_resolve_host: %s\n", gai_strerror(error)); | 141 av_log(NULL, AV_LOG_ERROR, "udp_ipv6_resolve_host: %s\n", gai_strerror(error)); |
142 } | 142 } |
143 } | 143 } |
144 return res; | 144 return res; |
164 struct sockaddr_storage clientaddr; | 164 struct sockaddr_storage clientaddr; |
165 socklen_t addrlen; | 165 socklen_t addrlen; |
166 char sbuf[NI_MAXSERV]; | 166 char sbuf[NI_MAXSERV]; |
167 char hbuf[NI_MAXHOST]; | 167 char hbuf[NI_MAXHOST]; |
168 struct addrinfo *res0 = NULL, *res = NULL; | 168 struct addrinfo *res0 = NULL, *res = NULL; |
169 | 169 |
170 if (s->local_port != 0) { | 170 if (s->local_port != 0) { |
171 res0 = udp_ipv6_resolve_host(0, s->local_port, SOCK_DGRAM, AF_UNSPEC, AI_PASSIVE); | 171 res0 = udp_ipv6_resolve_host(0, s->local_port, SOCK_DGRAM, AF_UNSPEC, AI_PASSIVE); |
172 if (res0 == 0) | 172 if (res0 == 0) |
173 goto fail; | 173 goto fail; |
174 for (res = res0; res; res=res->ai_next) { | 174 for (res = res0; res; res=res->ai_next) { |
175 udp_fd = socket(res->ai_family, SOCK_DGRAM, 0); | 175 udp_fd = socket(res->ai_family, SOCK_DGRAM, 0); |
176 if (udp_fd > 0) break; | 176 if (udp_fd > 0) break; |
177 perror("socket"); | 177 perror("socket"); |
178 } | 178 } |
179 } else { | 179 } else { |
180 udp_fd = socket(s->dest_addr.ss_family, SOCK_DGRAM, 0); | 180 udp_fd = socket(s->dest_addr.ss_family, SOCK_DGRAM, 0); |
181 if (udp_fd < 0) | 181 if (udp_fd < 0) |
182 perror("socket"); | 182 perror("socket"); |
183 } | 183 } |
184 | 184 |
185 if (udp_fd < 0) | 185 if (udp_fd < 0) |
186 goto fail; | 186 goto fail; |
187 | 187 |
188 if (s->local_port != 0) { | 188 if (s->local_port != 0) { |
189 if (bind(udp_fd, res0->ai_addr, res0->ai_addrlen) < 0) { | 189 if (bind(udp_fd, res0->ai_addr, res0->ai_addrlen) < 0) { |
190 perror("bind"); | 190 perror("bind"); |
191 goto fail; | 191 goto fail; |
192 } | 192 } |
193 freeaddrinfo(res0); | 193 freeaddrinfo(res0); |
194 res0 = NULL; | 194 res0 = NULL; |
195 } | 195 } |
196 | 196 |
197 addrlen = sizeof(clientaddr); | 197 addrlen = sizeof(clientaddr); |
198 if (getsockname(udp_fd, (struct sockaddr *)&clientaddr, &addrlen) < 0) { | 198 if (getsockname(udp_fd, (struct sockaddr *)&clientaddr, &addrlen) < 0) { |
199 perror("getsockname"); | 199 perror("getsockname"); |
200 goto fail; | 200 goto fail; |
204 perror("getnameinfo"); | 204 perror("getnameinfo"); |
205 goto fail; | 205 goto fail; |
206 } | 206 } |
207 | 207 |
208 s->local_port = strtol(sbuf, NULL, 10); | 208 s->local_port = strtol(sbuf, NULL, 10); |
209 | 209 |
210 return udp_fd; | 210 return udp_fd; |
211 | 211 |
212 fail: | 212 fail: |
213 if (udp_fd >= 0) | 213 if (udp_fd >= 0) |
214 #ifdef CONFIG_BEOS_NETSERVER | 214 #ifdef CONFIG_BEOS_NETSERVER |
215 closesocket(udp_fd); | 215 closesocket(udp_fd); |
216 #else | 216 #else |
228 * If no filename is given to av_open_input_file because you want to | 228 * If no filename is given to av_open_input_file because you want to |
229 * get the local port first, then you must call this function to set | 229 * get the local port first, then you must call this function to set |
230 * the remote server address. | 230 * the remote server address. |
231 * | 231 * |
232 * url syntax: udp://host:port[?option=val...] | 232 * url syntax: udp://host:port[?option=val...] |
233 * option: 'multicast=1' : enable multicast | 233 * option: 'multicast=1' : enable multicast |
234 * 'ttl=n' : set the ttl value (for multicast only) | 234 * 'ttl=n' : set the ttl value (for multicast only) |
235 * 'localport=n' : set the local port | 235 * 'localport=n' : set the local port |
236 * 'pkt_size=n' : set max packet size | 236 * 'pkt_size=n' : set max packet size |
237 * | 237 * |
238 * @param s1 media file context | 238 * @param s1 media file context |
245 return udp_ipv6_set_remote_url(h, uri); | 245 return udp_ipv6_set_remote_url(h, uri); |
246 #else | 246 #else |
247 UDPContext *s = h->priv_data; | 247 UDPContext *s = h->priv_data; |
248 char hostname[256]; | 248 char hostname[256]; |
249 int port; | 249 int port; |
250 | 250 |
251 url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri); | 251 url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri); |
252 | 252 |
253 /* set the destination address */ | 253 /* set the destination address */ |
254 if (resolve_host(&s->dest_addr.sin_addr, hostname) < 0) | 254 if (resolve_host(&s->dest_addr.sin_addr, hostname) < 0) |
255 return AVERROR_IO; | 255 return AVERROR_IO; |
298 | 298 |
299 h->is_streamed = 1; | 299 h->is_streamed = 1; |
300 h->max_packet_size = 1472; | 300 h->max_packet_size = 1472; |
301 | 301 |
302 is_output = (flags & URL_WRONLY); | 302 is_output = (flags & URL_WRONLY); |
303 | 303 |
304 s = av_malloc(sizeof(UDPContext)); | 304 s = av_malloc(sizeof(UDPContext)); |
305 if (!s) | 305 if (!s) |
306 return -ENOMEM; | 306 return -ENOMEM; |
307 | 307 |
308 h->priv_data = s; | 308 h->priv_data = s; |
323 } | 323 } |
324 } | 324 } |
325 | 325 |
326 /* fill the dest addr */ | 326 /* fill the dest addr */ |
327 url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri); | 327 url_split(NULL, 0, NULL, 0, hostname, sizeof(hostname), &port, NULL, 0, uri); |
328 | 328 |
329 /* XXX: fix url_split */ | 329 /* XXX: fix url_split */ |
330 if (hostname[0] == '\0' || hostname[0] == '?') { | 330 if (hostname[0] == '\0' || hostname[0] == '?') { |
331 /* only accepts null hostname if input */ | 331 /* only accepts null hostname if input */ |
332 if (s->is_multicast || (flags & URL_WRONLY)) | 332 if (s->is_multicast || (flags & URL_WRONLY)) |
333 goto fail; | 333 goto fail; |
348 } else { | 348 } else { |
349 my_addr.sin_port = htons(s->local_port); | 349 my_addr.sin_port = htons(s->local_port); |
350 } | 350 } |
351 | 351 |
352 /* the bind is needed to give a port to the socket now */ | 352 /* the bind is needed to give a port to the socket now */ |
353 if (bind(udp_fd,(struct sockaddr *)&my_addr, sizeof(my_addr)) < 0) | 353 if (bind(udp_fd,(struct sockaddr *)&my_addr, sizeof(my_addr)) < 0) |
354 goto fail; | 354 goto fail; |
355 | 355 |
356 len = sizeof(my_addr1); | 356 len = sizeof(my_addr1); |
357 getsockname(udp_fd, (struct sockaddr *)&my_addr1, &len); | 357 getsockname(udp_fd, (struct sockaddr *)&my_addr1, &len); |
358 s->local_port = ntohs(my_addr1.sin_port); | 358 s->local_port = ntohs(my_addr1.sin_port); |
359 | 359 |
360 #ifndef CONFIG_BEOS_NETSERVER | 360 #ifndef CONFIG_BEOS_NETSERVER |
361 if (s->is_multicast) { | 361 if (s->is_multicast) { |
362 if (h->flags & URL_WRONLY) { | 362 if (h->flags & URL_WRONLY) { |
363 /* output */ | 363 /* output */ |
364 if (setsockopt(udp_fd, IPPROTO_IP, IP_MULTICAST_TTL, | 364 if (setsockopt(udp_fd, IPPROTO_IP, IP_MULTICAST_TTL, |
365 &s->ttl, sizeof(s->ttl)) < 0) { | 365 &s->ttl, sizeof(s->ttl)) < 0) { |
366 perror("IP_MULTICAST_TTL"); | 366 perror("IP_MULTICAST_TTL"); |
367 goto fail; | 367 goto fail; |
368 } | 368 } |
369 } else { | 369 } else { |
370 /* input */ | 370 /* input */ |
371 memset(&s->mreq, 0, sizeof(s->mreq)); | 371 memset(&s->mreq, 0, sizeof(s->mreq)); |
372 s->mreq.imr_multiaddr = s->dest_addr.sin_addr; | 372 s->mreq.imr_multiaddr = s->dest_addr.sin_addr; |
373 s->mreq.imr_interface.s_addr = htonl (INADDR_ANY); | 373 s->mreq.imr_interface.s_addr = htonl (INADDR_ANY); |
374 if (setsockopt(udp_fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, | 374 if (setsockopt(udp_fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, |
375 &s->mreq, sizeof(s->mreq)) < 0) { | 375 &s->mreq, sizeof(s->mreq)) < 0) { |
376 perror("rtp: IP_ADD_MEMBERSHIP"); | 376 perror("rtp: IP_ADD_MEMBERSHIP"); |
377 goto fail; | 377 goto fail; |
378 } | 378 } |
379 } | 379 } |
393 } else { | 393 } else { |
394 if (udp_ipv6_join_multicast_group(udp_fd, (struct sockaddr *)&s->dest_addr) < 0) | 394 if (udp_ipv6_join_multicast_group(udp_fd, (struct sockaddr *)&s->dest_addr) < 0) |
395 goto fail; | 395 goto fail; |
396 } | 396 } |
397 } | 397 } |
398 #endif | 398 #endif |
399 #endif | 399 #endif |
400 | 400 |
401 if (is_output) { | 401 if (is_output) { |
402 /* limit the tx buf size to limit latency */ | 402 /* limit the tx buf size to limit latency */ |
403 tmp = UDP_TX_BUF_SIZE; | 403 tmp = UDP_TX_BUF_SIZE; |
448 { | 448 { |
449 UDPContext *s = h->priv_data; | 449 UDPContext *s = h->priv_data; |
450 int ret; | 450 int ret; |
451 | 451 |
452 for(;;) { | 452 for(;;) { |
453 ret = sendto (s->udp_fd, buf, size, 0, | 453 ret = sendto (s->udp_fd, buf, size, 0, |
454 (struct sockaddr *) &s->dest_addr, | 454 (struct sockaddr *) &s->dest_addr, |
455 #ifndef CONFIG_IPV6 | 455 #ifndef CONFIG_IPV6 |
456 sizeof (s->dest_addr)); | 456 sizeof (s->dest_addr)); |
457 #else | 457 #else |
458 s->dest_addr_len); | 458 s->dest_addr_len); |
472 UDPContext *s = h->priv_data; | 472 UDPContext *s = h->priv_data; |
473 | 473 |
474 #ifndef CONFIG_BEOS_NETSERVER | 474 #ifndef CONFIG_BEOS_NETSERVER |
475 #ifndef CONFIG_IPV6 | 475 #ifndef CONFIG_IPV6 |
476 if (s->is_multicast && !(h->flags & URL_WRONLY)) { | 476 if (s->is_multicast && !(h->flags & URL_WRONLY)) { |
477 if (setsockopt(s->udp_fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, | 477 if (setsockopt(s->udp_fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, |
478 &s->mreq, sizeof(s->mreq)) < 0) { | 478 &s->mreq, sizeof(s->mreq)) < 0) { |
479 perror("IP_DROP_MEMBERSHIP"); | 479 perror("IP_DROP_MEMBERSHIP"); |
480 } | 480 } |
481 } | 481 } |
482 #else | 482 #else |