2 * Network layer for MPlayer
4 * Copyright (C) 2001 Bertrand BAUDET <bertrand_baudet@yahoo.com>
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 #if !defined(WIN32) && !defined(WIN64)
26 #endif /* Def WIN32 or Def WIN64 */
35 #include <sys/types.h>
44 #include <netinet/in.h>
45 #include <sys/socket.h>
46 #include <arpa/inet.h>
58 int network_prefer_ipv4
= 0;
60 // Converts an address family constant to a string
62 static const char *af2String(int af
) {
64 case AF_INET
: return "AF_INET";
67 case AF_INET6
: return "AF_INET6";
69 default: return "Unknown address family!";
75 // Connect to a server using a TCP connection, with specified address family
76 // return -2 for fatal error, like unable to resolve name, connection timeout...
77 // return -1 is unable to connect to a particular port
80 connect2Server_with_af(char *host
, int port
, int af
,int verb
) {
88 struct sockaddr_in four
;
90 struct sockaddr_in6 six
;
93 size_t server_address_size
;
94 void *our_s_addr
; // Pointer to sin_addr or sin6_addr
95 struct hostent
*hp
=NULL
;
105 #if HAVE_WINSOCK2_H && defined(HAVE_AF_INET6)
106 // our winsock name resolution code can not handle IPv6
107 if (af
== AF_INET6
) {
108 mp_msg(MSGT_NETWORK
, MSGL_WARN
, "IPv6 not supported for winsock2\n");
109 return TCP_ERROR_FATAL
;
113 socket_server_fd
= socket(af
, SOCK_STREAM
, 0);
116 if( socket_server_fd
==-1 ) {
117 // mp_msg(MSGT_NETWORK,MSGL_ERR,"Failed to create %s socket:\n", af2String(af));
118 return TCP_ERROR_FATAL
;
121 #if defined(SO_RCVTIMEO) && defined(SO_SNDTIMEO)
123 /* timeout in milliseconds */
129 setsockopt(socket_server_fd
, SOL_SOCKET
, SO_RCVTIMEO
, &to
, sizeof(to
));
130 setsockopt(socket_server_fd
, SOL_SOCKET
, SO_SNDTIMEO
, &to
, sizeof(to
));
134 case AF_INET
: our_s_addr
= (void *) &server_address
.four
.sin_addr
; break;
136 case AF_INET6
: our_s_addr
= (void *) &server_address
.six
.sin6_addr
; break;
139 mp_msg(MSGT_NETWORK
,MSGL_ERR
, MSGTR_MPDEMUX_NW_UnknownAF
, af
);
140 return TCP_ERROR_FATAL
;
144 memset(&server_address
, 0, sizeof(server_address
));
147 if (inet_pton(af
, host
, our_s_addr
)!=1)
149 if (inet_aton(host
, our_s_addr
)!=1)
150 #elif HAVE_WINSOCK2_H
151 if ( inet_addr(host
)==INADDR_NONE
)
154 if(verb
) mp_msg(MSGT_NETWORK
,MSGL_STATUS
,MSGTR_MPDEMUX_NW_ResolvingHostForAF
, host
, af2String(af
));
156 #ifdef HAVE_GETHOSTBYNAME2
157 hp
=(struct hostent
*)gethostbyname2( host
, af
);
159 hp
=(struct hostent
*)gethostbyname( host
);
162 if(verb
) mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_NW_CantResolv
, af2String(af
), host
);
163 return TCP_ERROR_FATAL
;
166 memcpy( our_s_addr
, (void*)hp
->h_addr_list
[0], hp
->h_length
);
170 unsigned long addr
= inet_addr(host
);
171 memcpy( our_s_addr
, (void*)&addr
, sizeof(addr
) );
177 server_address
.four
.sin_family
=af
;
178 server_address
.four
.sin_port
=htons(port
);
179 server_address_size
= sizeof(server_address
.four
);
183 server_address
.six
.sin6_family
=af
;
184 server_address
.six
.sin6_port
=htons(port
);
185 server_address_size
= sizeof(server_address
.six
);
189 mp_msg(MSGT_NETWORK
,MSGL_ERR
, MSGTR_MPDEMUX_NW_UnknownAF
, af
);
190 return TCP_ERROR_FATAL
;
193 #if HAVE_INET_ATON || defined(HAVE_WINSOCK2_H)
194 av_strlcpy( buf
, inet_ntoa( *((struct in_addr
*)our_s_addr
) ), 255);
196 inet_ntop(af
, our_s_addr
, buf
, 255);
198 if(verb
) mp_msg(MSGT_NETWORK
,MSGL_STATUS
,MSGTR_MPDEMUX_NW_ConnectingToServer
, host
, buf
, port
);
200 // Turn the socket as non blocking so we can timeout on the connection
202 fcntl( socket_server_fd
, F_SETFL
, fcntl(socket_server_fd
, F_GETFL
) | O_NONBLOCK
);
205 ioctlsocket( socket_server_fd
, FIONBIO
, &val
);
207 if( connect( socket_server_fd
, (struct sockaddr
*)&server_address
, server_address_size
)==-1 ) {
209 if( errno
!=EINPROGRESS
) {
211 if( (WSAGetLastError() != WSAEINPROGRESS
) && (WSAGetLastError() != WSAEWOULDBLOCK
) ) {
213 if(verb
) mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_NW_CantConnect2Server
, af2String(af
));
214 closesocket(socket_server_fd
);
215 return TCP_ERROR_PORT
;
221 FD_SET( socket_server_fd
, &set
);
222 // When the connection will be made, we will have a writeable fd
223 while((ret
= select(socket_server_fd
+1, NULL
, &set
, NULL
, &tv
)) == 0) {
224 if(count
> 30 || stream_check_interrupt(500)) {
226 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_NW_ConnTimeout
);
228 mp_msg(MSGT_NETWORK
,MSGL_V
,"Connection interrupted by user\n");
229 return TCP_ERROR_TIMEOUT
;
233 FD_SET( socket_server_fd
, &set
);
237 if (ret
< 0) mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_NW_SelectFailed
);
239 // Turn back the socket as blocking
241 fcntl( socket_server_fd
, F_SETFL
, fcntl(socket_server_fd
, F_GETFL
) & ~O_NONBLOCK
);
244 ioctlsocket( socket_server_fd
, FIONBIO
, &val
);
246 // Check if there were any errors
247 err_len
= sizeof(int);
248 ret
= getsockopt(socket_server_fd
,SOL_SOCKET
,SO_ERROR
,&err
,&err_len
);
250 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_NW_GetSockOptFailed
,strerror(errno
));
251 return TCP_ERROR_FATAL
;
254 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_NW_ConnectError
,strerror(err
));
255 return TCP_ERROR_PORT
;
258 return socket_server_fd
;
261 // Connect to a server using a TCP connection
262 // return -2 for fatal error, like unable to resolve name, connection timeout...
263 // return -1 is unable to connect to a particular port
267 connect2Server(char *host
, int port
, int verb
) {
270 int s
= TCP_ERROR_FATAL
;
272 r
= connect2Server_with_af(host
, port
, network_prefer_ipv4
? AF_INET
:AF_INET6
,verb
);
273 if (r
>= 0) return r
;
275 s
= connect2Server_with_af(host
, port
, network_prefer_ipv4
? AF_INET6
:AF_INET
,verb
);
276 if (s
== TCP_ERROR_FATAL
) return r
;
279 return connect2Server_with_af(host
, port
, AF_INET
,verb
);