Mercurial > mplayer.hg
annotate stream/tcp.c @ 19489:87c339558bc1
fix menu toggle/hide issues
author | ben |
---|---|
date | Tue, 22 Aug 2006 19:40:50 +0000 |
parents | 3f19764369ac |
children |
rev | line source |
---|---|
19335
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
1 /* |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
2 * Copyright (C) 2001 Bertrand BAUDET, 2006 Benjamin Zores |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
3 * Network helpers for TCP connections |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
4 * (originally borrowed from network.c, |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
5 * by Bertrand BAUDET <bertrand_baudet@yahoo.com>). |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
6 * |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
7 * This program is free software; you can redistribute it and/or modify |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
8 * it under the terms of the GNU General Public License as published by |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
9 * the Free Software Foundation; either version 2 of the License, or |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
10 * (at your option) any later version. |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
11 * |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
12 * This program is distributed in the hope that it will be useful, |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
15 * GNU General Public License for more details. |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
16 * |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
17 * You should have received a copy of the GNU General Public License |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
18 * along with this program; if not, write to the Free Software Foundation, |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
19 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
20 */ |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
21 |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
22 #include "config.h" |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
23 |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
24 #include <stdlib.h> |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
25 #include <string.h> |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
26 #include <errno.h> |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
27 #include <unistd.h> |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
28 #include <stdlib.h> |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
29 #include <sys/types.h> |
19464 | 30 #include <sys/time.h> |
19335
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
31 #include <ctype.h> |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
32 #include <netdb.h> |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
33 #include <unistd.h> |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
34 #include <fcntl.h> |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
35 |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
36 #ifndef HAVE_WINSOCK2 |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
37 #include <netinet/in.h> |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
38 #include <sys/socket.h> |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
39 #include <arpa/inet.h> |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
40 #define closesocket close |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
41 #else |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
42 #include <winsock2.h> |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
43 #include <ws2tcpip.h> |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
44 #endif |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
45 |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
46 #include "mp_msg.h" |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
47 #include "help_mp.h" |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
48 #include "tcp.h" |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
49 |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
50 /* IPv6 options */ |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
51 int network_prefer_ipv4 = 0; |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
52 |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
53 /* Converts an address family constant to a string */ |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
54 static const char * |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
55 af2String (int af) |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
56 { |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
57 switch (af) |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
58 { |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
59 case AF_INET: |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
60 return "AF_INET"; |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
61 #ifdef HAVE_AF_INET6 |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
62 case AF_INET6: |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
63 return "AF_INET6"; |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
64 #endif |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
65 default: |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
66 return "Unknown address family!"; |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
67 } |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
68 } |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
69 |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
70 static int |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
71 connect2Server_with_af (char *host, int port, int af ,int verb) |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
72 { |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
73 int socket_server_fd; |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
74 int err; |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
75 socklen_t err_len; |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
76 int ret, count = 0; |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
77 fd_set set; |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
78 struct timeval tv; |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
79 |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
80 union { |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
81 struct sockaddr_in four; |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
82 #ifdef HAVE_AF_INET6 |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
83 struct sockaddr_in6 six; |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
84 #endif |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
85 } server_address; |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
86 |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
87 size_t server_address_size; |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
88 void *our_s_addr; /* Pointer to sin_addr or sin6_addr */ |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
89 struct hostent *hp = NULL; |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
90 char buf[255]; |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
91 |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
92 #ifdef HAVE_WINSOCK2 |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
93 u_long val; |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
94 #endif |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
95 |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
96 socket_server_fd = socket (af, SOCK_STREAM, 0); |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
97 if (socket_server_fd == -1) |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
98 return TCP_ERROR_FATAL; |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
99 |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
100 switch (af) |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
101 { |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
102 case AF_INET: |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
103 our_s_addr = (void *) &server_address.four.sin_addr; |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
104 break; |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
105 #ifdef HAVE_AF_INET6 |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
106 case AF_INET6: |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
107 our_s_addr = (void *) &server_address.six.sin6_addr; |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
108 break; |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
109 #endif |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
110 default: |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
111 mp_msg (MSGT_NETWORK, MSGL_ERR, MSGTR_MPDEMUX_NW_UnknownAF, af); |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
112 return TCP_ERROR_FATAL; |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
113 } |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
114 |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
115 memset (&server_address, 0, sizeof (server_address)); |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
116 |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
117 #ifndef HAVE_WINSOCK2 |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
118 #ifdef USE_ATON |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
119 if (inet_aton (host, our_s_addr) !=1) |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
120 #else |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
121 if (inet_pton (af, host, our_s_addr) !=1) |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
122 #endif /* USE_ATON */ |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
123 #else |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
124 if (inet_addr (host) == INADDR_NONE) |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
125 #endif /* HAVE_WINSOCK2 */ |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
126 { |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
127 if (verb) |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
128 mp_msg (MSGT_NETWORK, MSGL_STATUS, |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
129 MSGTR_MPDEMUX_NW_ResolvingHostForAF, host, af2String (af)); |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
130 |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
131 #ifdef HAVE_GETHOSTBYNAME2 |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
132 hp = (struct hostent *) gethostbyname2 (host, af); |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
133 #else |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
134 hp = (struct hostent *) gethostbyname (host); |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
135 #endif /* HAVE_GETHOSTBYNAME2 */ |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
136 |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
137 if (!hp) |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
138 { |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
139 if (verb) |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
140 mp_msg (MSGT_NETWORK, MSGL_ERR, |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
141 MSGTR_MPDEMUX_NW_CantResolv, af2String (af), host); |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
142 return TCP_ERROR_FATAL; |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
143 } |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
144 |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
145 memcpy (our_s_addr, (void *) hp->h_addr_list[0], hp->h_length); |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
146 } |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
147 #ifdef HAVE_WINSOCK2 |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
148 else |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
149 { |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
150 unsigned long addr = inet_addr (host); |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
151 memcpy (our_s_addr, (void *) &addr, sizeof (addr)); |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
152 } |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
153 #endif /* HAVE_WINSOCK2 */ |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
154 |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
155 switch (af) |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
156 { |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
157 case AF_INET: |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
158 server_address.four.sin_family = af; |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
159 server_address.four.sin_port = htons (port); |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
160 server_address_size = sizeof (server_address.four); |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
161 break; |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
162 #ifdef HAVE_AF_INET6 |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
163 case AF_INET6: |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
164 server_address.six.sin6_family = af; |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
165 server_address.six.sin6_port = htons (port); |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
166 server_address_size = sizeof (server_address.six); |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
167 break; |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
168 #endif /* HAVE_AF_INET6 */ |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
169 default: |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
170 mp_msg (MSGT_NETWORK, MSGL_ERR, MSGTR_MPDEMUX_NW_UnknownAF, af); |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
171 return TCP_ERROR_FATAL; |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
172 } |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
173 |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
174 #if defined(USE_ATON) || defined(HAVE_WINSOCK2) |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
175 strncpy (buf, inet_ntoa (*((struct in_addr *) our_s_addr)), 255); |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
176 #else |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
177 inet_ntop (af, our_s_addr, buf, 255); |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
178 #endif /* USE_ATON || HAVE_WINSOCK2 */ |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
179 |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
180 if (verb) |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
181 mp_msg (MSGT_NETWORK, MSGL_STATUS, |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
182 MSGTR_MPDEMUX_NW_ConnectingToServer, host, buf, port); |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
183 |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
184 /* Turn the socket as non blocking so we can timeout on the connection */ |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
185 #ifndef HAVE_WINSOCK2 |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
186 fcntl (socket_server_fd, F_SETFL, |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
187 fcntl (socket_server_fd, F_GETFL) | O_NONBLOCK); |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
188 #else |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
189 val = 1; |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
190 ioctlsocket (socket_server_fd, FIONBIO, &val); |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
191 #endif /* HAVE_WINSOCK2 */ |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
192 |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
193 if (connect (socket_server_fd, (struct sockaddr *) &server_address, |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
194 server_address_size) == -1) |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
195 { |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
196 #ifndef HAVE_WINSOCK2 |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
197 if (errno != EINPROGRESS) |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
198 #else |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
199 if ((WSAGetLastError () != WSAEINPROGRESS) |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
200 && (WSAGetLastError () != WSAEWOULDBLOCK)) |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
201 #endif /* HAVE_WINSOCK2 */ |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
202 { |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
203 if (verb) |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
204 mp_msg (MSGT_NETWORK, MSGL_ERR, |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
205 MSGTR_MPDEMUX_NW_CantConnect2Server, af2String (af)); |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
206 |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
207 closesocket (socket_server_fd); |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
208 return TCP_ERROR_PORT; |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
209 } |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
210 } |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
211 |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
212 tv.tv_sec = 0; |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
213 tv.tv_usec = 500000; |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
214 |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
215 FD_ZERO (&set); |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
216 FD_SET (socket_server_fd, &set); |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
217 |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
218 /* When the connection will be made, we will have a writable fd */ |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
219 while ((ret = select (socket_server_fd + 1, NULL, &set, NULL, &tv)) == 0) |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
220 { |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
221 if (ret < 0) |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
222 mp_msg (MSGT_NETWORK, MSGL_ERR, MSGTR_MPDEMUX_NW_SelectFailed); |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
223 else if (ret > 0) |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
224 break; |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
225 else if (count > 30 || mp_input_check_interrupt (500)) |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
226 { |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
227 if (count > 30) |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
228 mp_msg (MSGT_NETWORK, MSGL_ERR, MSGTR_MPDEMUX_NW_ConnTimeout); |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
229 else |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
230 mp_msg (MSGT_NETWORK, MSGL_V, "Connection interuppted by user\n"); |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
231 return TCP_ERROR_TIMEOUT; |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
232 } |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
233 |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
234 count++; |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
235 FD_ZERO (&set); |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
236 FD_SET (socket_server_fd, &set); |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
237 tv.tv_sec = 0; |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
238 tv.tv_usec = 500000; |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
239 } |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
240 |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
241 /* Turn back the socket as blocking */ |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
242 #ifndef HAVE_WINSOCK2 |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
243 fcntl (socket_server_fd, F_SETFL, |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
244 fcntl (socket_server_fd, F_GETFL) & ~O_NONBLOCK); |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
245 #else |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
246 val = 0; |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
247 ioctlsocket (socket_server_fd, FIONBIO, &val); |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
248 #endif /* HAVE_WINSOCK2 */ |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
249 |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
250 /* Check if there were any error */ |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
251 err_len = sizeof (int); |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
252 ret = getsockopt (socket_server_fd, SOL_SOCKET, SO_ERROR, &err, &err_len); |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
253 if (ret < 0) |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
254 { |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
255 mp_msg (MSGT_NETWORK, MSGL_ERR, |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
256 MSGTR_MPDEMUX_NW_GetSockOptFailed, strerror (errno)); |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
257 return TCP_ERROR_FATAL; |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
258 } |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
259 |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
260 if (err > 0) |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
261 { |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
262 mp_msg (MSGT_NETWORK, MSGL_ERR, |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
263 MSGTR_MPDEMUX_NW_ConnectError, strerror (err)); |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
264 return TCP_ERROR_PORT; |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
265 } |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
266 |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
267 return socket_server_fd; |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
268 } |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
269 |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
270 int |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
271 connect2Server (char *host, int port, int verb) |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
272 { |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
273 #ifdef HAVE_AF_INET6 |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
274 int r; |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
275 int s = TCP_ERROR_FATAL; |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
276 |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
277 r = connect2Server_with_af (host, port, |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
278 network_prefer_ipv4 ? AF_INET:AF_INET6, verb); |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
279 if (r > TCP_ERROR_PORT) |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
280 return r; |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
281 |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
282 s = connect2Server_with_af (host, port, |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
283 network_prefer_ipv4 ? AF_INET6:AF_INET, verb); |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
284 if (s == TCP_ERROR_FATAL) |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
285 return r; |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
286 |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
287 return s; |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
288 #else |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
289 return connect2Server_with_af (host, port, AF_INET,verb); |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
290 #endif /* HAVE_AF_INET6 */ |
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
diff
changeset
|
291 } |