Windows: ao_pcm: add io.h include for MinGW64
[mplayer.git] / stream / udp.c
blobe27808b4c20c69ba6e1e71eb2cc3a398b7e76431
1 /*
2 * network helpers for UDP connections (originally borrowed from rtp.c)
4 * Copyright (C) 2006 Benjamin Zores
6 * This file is part of MPlayer.
8 * MPlayer is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * MPlayer is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "config.h"
25 #include <stdlib.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <unistd.h>
29 #include <stdlib.h>
30 #include <sys/types.h>
31 #include <sys/time.h>
32 #include <ctype.h>
34 #if !HAVE_WINSOCK2_H
35 #include <netdb.h>
36 #include <netinet/in.h>
37 #include <sys/socket.h>
38 #include <arpa/inet.h>
39 #else
40 #include <winsock2.h>
41 #include <ws2tcpip.h>
42 #endif
44 #include "mp_msg.h"
45 #include "network.h"
46 #include "url.h"
47 #include "udp.h"
49 int reuse_socket=0;
51 /* Start listening on a UDP port. If multicast, join the group. */
52 int
53 udp_open_socket (URL_t *url)
55 int socket_server_fd, rxsockbufsz;
56 int err;
57 socklen_t err_len;
58 fd_set set;
59 struct sockaddr_in server_address;
60 struct ip_mreq mcast;
61 struct timeval tv;
62 struct hostent *hp;
63 int reuse=reuse_socket;
65 mp_msg (MSGT_NETWORK, MSGL_V,
66 "Listening for traffic on %s:%d ...\n", url->hostname, url->port);
68 socket_server_fd = socket (AF_INET, SOCK_DGRAM, 0);
69 if (socket_server_fd == -1)
71 mp_msg (MSGT_NETWORK, MSGL_ERR, "Failed to create socket\n");
72 return -1;
75 memset(&server_address, 0, sizeof(server_address));
76 if (isalpha (url->hostname[0]))
78 #if !HAVE_WINSOCK2_H
79 hp = (struct hostent *) gethostbyname (url->hostname);
80 if (!hp)
82 mp_msg (MSGT_NETWORK, MSGL_ERR,
83 "Counldn't resolve name: %s\n", url->hostname);
84 closesocket (socket_server_fd);
85 return -1;
87 memcpy ((void *) &server_address.sin_addr.s_addr,
88 (void *) hp->h_addr_list[0], hp->h_length);
89 #else
90 server_address.sin_addr.s_addr = htonl (INADDR_ANY);
91 #endif /* HAVE_WINSOCK2_H */
93 else
95 #if HAVE_INET_PTON
96 inet_pton (AF_INET, url->hostname, &server_address.sin_addr);
97 #elif HAVE_INET_ATON
98 inet_aton (url->hostname, &server_address.sin_addr);
99 #elif !HAVE_WINSOCK2_H
100 server_address.sin_addr.s_addr = htonl(INADDR_ANY);
101 #endif
103 server_address.sin_family = AF_INET;
104 server_address.sin_port = htons (url->port);
106 if(reuse_socket && setsockopt(socket_server_fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)))
107 mp_msg(MSGT_NETWORK, MSGL_ERR, "SO_REUSEADDR failed! ignore.\n");
109 if (bind (socket_server_fd, (struct sockaddr *) &server_address,
110 sizeof (server_address)) == -1)
112 #if !HAVE_WINSOCK2_H
113 if (errno != EINPROGRESS)
114 #else
115 if (WSAGetLastError () != WSAEINPROGRESS)
116 #endif /* HAVE_WINSOCK2_H */
118 mp_msg (MSGT_NETWORK, MSGL_ERR, "Failed to connect to server\n");
119 closesocket (socket_server_fd);
120 return -1;
124 #if HAVE_WINSOCK2_H
125 if (isalpha (url->hostname[0]))
127 hp = (struct hostent *) gethostbyname (url->hostname);
128 if (!hp)
130 mp_msg (MSGT_NETWORK, MSGL_ERR,
131 "Could not resolve name: %s\n", url->hostname);
132 closesocket (socket_server_fd);
133 return -1;
135 memcpy ((void *) &server_address.sin_addr.s_addr,
136 (void *) hp->h_addr, hp->h_length);
138 else
140 unsigned int addr = inet_addr (url->hostname);
141 memcpy ((void *) &server_address.sin_addr, (void *) &addr, sizeof (addr));
143 #endif /* HAVE_WINSOCK2_H */
145 /* Increase the socket rx buffer size to maximum -- this is UDP */
146 rxsockbufsz = 240 * 1024;
147 if (setsockopt (socket_server_fd, SOL_SOCKET, SO_RCVBUF,
148 &rxsockbufsz, sizeof (rxsockbufsz)))
150 mp_msg (MSGT_NETWORK, MSGL_ERR,
151 "Couldn't set receive socket buffer size\n");
154 if ((ntohl (server_address.sin_addr.s_addr) >> 28) == 0xe)
156 mcast.imr_multiaddr.s_addr = server_address.sin_addr.s_addr;
157 mcast.imr_interface.s_addr = 0;
159 if (setsockopt (socket_server_fd, IPPROTO_IP,
160 IP_ADD_MEMBERSHIP, &mcast, sizeof (mcast)))
162 mp_msg (MSGT_NETWORK, MSGL_ERR, "IP_ADD_MEMBERSHIP failed (do you have multicasting enabled in your kernel?)\n");
163 closesocket (socket_server_fd);
164 return -1;
168 tv.tv_sec = 1; /* 1 second timeout */
169 tv.tv_usec = 0;
171 FD_ZERO (&set);
172 FD_SET (socket_server_fd, &set);
174 err = select (socket_server_fd + 1, &set, NULL, NULL, &tv);
175 if (err < 0)
177 mp_msg (MSGT_NETWORK, MSGL_FATAL,
178 "Select failed: %s\n", strerror (errno));
179 closesocket (socket_server_fd);
180 return -1;
183 if (err == 0)
185 mp_msg (MSGT_NETWORK, MSGL_ERR,
186 "Timeout! No data from host %s\n", url->hostname);
187 closesocket (socket_server_fd);
188 return -1;
191 err_len = sizeof (err);
192 getsockopt (socket_server_fd, SOL_SOCKET, SO_ERROR, &err, &err_len);
193 if (err)
195 mp_msg (MSGT_NETWORK, MSGL_DBG2, "Socket error: %d\n", err);
196 closesocket (socket_server_fd);
197 return -1;
200 return socket_server_fd;