Give better name to Inverse_Table_6_9
[mplayer/glamo.git] / stream / udp.c
blob84769ce403d63f46fc0517991f1db1da14819894
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 if (isalpha (url->hostname[0]))
77 #if !HAVE_WINSOCK2_H
78 hp = (struct hostent *) gethostbyname (url->hostname);
79 if (!hp)
81 mp_msg (MSGT_NETWORK, MSGL_ERR,
82 "Counldn't resolve name: %s\n", url->hostname);
83 closesocket (socket_server_fd);
84 return -1;
86 memcpy ((void *) &server_address.sin_addr.s_addr,
87 (void *) hp->h_addr_list[0], hp->h_length);
88 #else
89 server_address.sin_addr.s_addr = htonl (INADDR_ANY);
90 #endif /* HAVE_WINSOCK2_H */
92 else
94 #if HAVE_INET_PTON
95 inet_pton (AF_INET, url->hostname, &server_address.sin_addr);
96 #elif HAVE_INET_ATON
97 inet_aton (url->hostname, &server_address.sin_addr);
98 #elif !HAVE_WINSOCK2_H
99 server_address.sin_addr.s_addr = htonl(INADDR_ANY);
100 #endif
102 server_address.sin_family = AF_INET;
103 server_address.sin_port = htons (url->port);
105 if(reuse_socket && setsockopt(socket_server_fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)))
106 mp_msg(MSGT_NETWORK, MSGL_ERR, "SO_REUSEADDR failed! ignore.\n");
108 if (bind (socket_server_fd, (struct sockaddr *) &server_address,
109 sizeof (server_address)) == -1)
111 #if !HAVE_WINSOCK2_H
112 if (errno != EINPROGRESS)
113 #else
114 if (WSAGetLastError () != WSAEINPROGRESS)
115 #endif /* HAVE_WINSOCK2_H */
117 mp_msg (MSGT_NETWORK, MSGL_ERR, "Failed to connect to server\n");
118 closesocket (socket_server_fd);
119 return -1;
123 #if HAVE_WINSOCK2_H
124 if (isalpha (url->hostname[0]))
126 hp = (struct hostent *) gethostbyname (url->hostname);
127 if (!hp)
129 mp_msg (MSGT_NETWORK, MSGL_ERR,
130 "Could not resolve name: %s\n", url->hostname);
131 closesocket (socket_server_fd);
132 return -1;
134 memcpy ((void *) &server_address.sin_addr.s_addr,
135 (void *) hp->h_addr, hp->h_length);
137 else
139 unsigned int addr = inet_addr (url->hostname);
140 memcpy ((void *) &server_address.sin_addr, (void *) &addr, sizeof (addr));
142 #endif /* HAVE_WINSOCK2_H */
144 /* Increase the socket rx buffer size to maximum -- this is UDP */
145 rxsockbufsz = 240 * 1024;
146 if (setsockopt (socket_server_fd, SOL_SOCKET, SO_RCVBUF,
147 &rxsockbufsz, sizeof (rxsockbufsz)))
149 mp_msg (MSGT_NETWORK, MSGL_ERR,
150 "Couldn't set receive socket buffer size\n");
153 if ((ntohl (server_address.sin_addr.s_addr) >> 28) == 0xe)
155 mcast.imr_multiaddr.s_addr = server_address.sin_addr.s_addr;
156 mcast.imr_interface.s_addr = 0;
158 if (setsockopt (socket_server_fd, IPPROTO_IP,
159 IP_ADD_MEMBERSHIP, &mcast, sizeof (mcast)))
161 mp_msg (MSGT_NETWORK, MSGL_ERR, "IP_ADD_MEMBERSHIP failed (do you have multicasting enabled in your kernel?)\n");
162 closesocket (socket_server_fd);
163 return -1;
167 tv.tv_sec = 1; /* 1 second timeout */
168 tv.tv_usec = 0;
170 FD_ZERO (&set);
171 FD_SET (socket_server_fd, &set);
173 err = select (socket_server_fd + 1, &set, NULL, NULL, &tv);
174 if (err < 0)
176 mp_msg (MSGT_NETWORK, MSGL_FATAL,
177 "Select failed: %s\n", strerror (errno));
178 closesocket (socket_server_fd);
179 return -1;
182 if (err == 0)
184 mp_msg (MSGT_NETWORK, MSGL_ERR,
185 "Timeout! No data from host %s\n", url->hostname);
186 closesocket (socket_server_fd);
187 return -1;
190 err_len = sizeof (err);
191 getsockopt (socket_server_fd, SOL_SOCKET, SO_ERROR, &err, &err_len);
192 if (err)
194 mp_msg (MSGT_NETWORK, MSGL_DBG2, "Socket error: %d\n", err);
195 closesocket (socket_server_fd);
196 return -1;
199 return socket_server_fd;