19 |
|
|
20 |
*/ |
*/ |
21 |
|
|
|
|
|
22 |
#define IN_SOCKETS |
#define IN_SOCKETS |
23 |
|
|
24 |
#include "include.h" |
#include "include.h" |
28 |
#include "sheader.h" |
#include "sheader.h" |
29 |
|
|
30 |
#include <sys/types.h> |
#include <sys/types.h> |
31 |
#include <sys/socket.h> |
#ifndef __MINGW32__ |
32 |
#include <netinet/in.h> |
# include <sys/socket.h> |
33 |
#include <arpa/inet.h> |
# include <netinet/in.h> |
34 |
|
# include <arpa/inet.h> |
35 |
|
#else |
36 |
|
# include <windows.h> |
37 |
|
# include <winsock2.h> |
38 |
|
#endif |
39 |
|
|
40 |
#ifdef __STDC__ |
#ifdef __STDC__ |
41 |
#include <string.h> |
#include <string.h> |
42 |
#endif |
#endif |
43 |
|
|
44 |
|
#ifndef __MINGW32__ |
45 |
# include <netdb.h> |
# include <netdb.h> |
46 |
|
#endif |
47 |
|
|
48 |
#include <sys/time.h> |
#include <sys/time.h> |
49 |
#ifndef NO_UNISTD_H |
#ifndef NO_UNISTD_H |
57 |
void write_timeout_error(); |
void write_timeout_error(); |
58 |
void connection_failure(); |
void connection_failure(); |
59 |
|
|
60 |
|
#ifdef __MINGW32__ |
61 |
|
/* Keep track of socket initialisations */ |
62 |
|
int w32_socket_initialisations = 0; |
63 |
|
WSADATA WSAData; |
64 |
|
|
65 |
|
int w32_socket_init(void) |
66 |
|
{ |
67 |
|
int rv = 0; |
68 |
|
if (w32_socket_initialisations++) { |
69 |
|
rv = 0; |
70 |
|
} else { |
71 |
|
if (WSAStartup(0x0101, &WSAData)) { |
72 |
|
w32_socket_initialisations = 0; |
73 |
|
fprintf ( stderr, "WSAStartup failed\n" ); |
74 |
|
WSACleanup(); |
75 |
|
rv = -1; |
76 |
|
} |
77 |
|
} |
78 |
|
|
79 |
|
return rv; |
80 |
|
} |
81 |
|
|
82 |
|
int w32_socket_exit(void) |
83 |
|
{ |
84 |
|
int rv = 0; |
85 |
|
|
86 |
|
if ( w32_socket_initialisations == 0 || |
87 |
|
--w32_socket_initialisations > 0 ) { |
88 |
|
rv = 0; |
89 |
|
} else { |
90 |
|
rv = WSACleanup(); |
91 |
|
} |
92 |
|
|
93 |
|
return rv; |
94 |
|
} |
95 |
|
|
96 |
|
#endif |
97 |
|
|
98 |
#define BIND_MAX_RETRY 128 |
#define BIND_MAX_RETRY 128 |
99 |
#define BIND_ADDRESS_INCREMENT 16 |
#define BIND_ADDRESS_INCREMENT 16 |
110 |
{ int s, n, rc; struct |
{ int s, n, rc; struct |
111 |
sockaddr_in addr; |
sockaddr_in addr; |
112 |
|
|
113 |
|
#ifdef __MINGW32__ |
114 |
|
if ( w32_socket_init() < 0 ) { |
115 |
|
perror("ERROR !!! Windows socket DLL initialisation failed in sock_connect_to_name\n"); |
116 |
|
return Cnil; |
117 |
|
} |
118 |
|
#endif |
119 |
|
|
120 |
/* Using TCP layer */ |
/* Using TCP layer */ |
121 |
s = socket(PF_INET, SOCK_STREAM, 0); |
s = socket(PF_INET, SOCK_STREAM, 0); |
122 |
if (s < 0) |
if (s < 0) |
142 |
iLastAddressUsed += BIND_ADDRESS_INCREMENT; |
iLastAddressUsed += BIND_ADDRESS_INCREMENT; |
143 |
if (iLastAddressUsed > BIND_LAST_ADDRESS) |
if (iLastAddressUsed > BIND_LAST_ADDRESS) |
144 |
iLastAddressUsed = BIND_INITIAL_ADDRESS; |
iLastAddressUsed = BIND_INITIAL_ADDRESS; |
145 |
} while ((rc < 0) && (errno == EADDRINUSE) && (cRetry < BIND_MAX_RETRY)); |
} while ((rc < 0) && |
146 |
|
#ifdef __MINGW32__ |
147 |
|
(errno == WSAEADDRINUSE) && |
148 |
|
#else |
149 |
|
(errno == EADDRINUSE) && |
150 |
|
#endif |
151 |
|
(cRetry < BIND_MAX_RETRY)); |
152 |
if (0) |
if (0) |
153 |
{ |
{ |
154 |
fprintf(stderr, |
fprintf(stderr, |
192 |
free(OBJ_TO_CONNECTION_STATE(sfd)->read_buffer); |
free(OBJ_TO_CONNECTION_STATE(sfd)->read_buffer); |
193 |
res = close(OBJ_TO_CONNECTION_STATE(sfd)->fd); |
res = close(OBJ_TO_CONNECTION_STATE(sfd)->fd); |
194 |
free (OBJ_TO_CONNECTION_STATE(sfd)); |
free (OBJ_TO_CONNECTION_STATE(sfd)); |
195 |
|
#ifdef __MINGW32__ |
196 |
|
w32_socket_exit(); |
197 |
|
#endif |
198 |
RETURN1(res ? Ct : Cnil); |
RETURN1(res ? Ct : Cnil); |
199 |
} |
} |
200 |
|
|