| 1 |
/* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */ |
| 2 |
/* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */ |
| 3 |
|
| 4 |
/* |
| 5 |
* tcp.d - stream interface to TCP |
| 6 |
* |
| 7 |
* Copyright (c) 1990 Giuseppe Attardi |
| 8 |
* Copyright (c) 2001 Juan Jose Garcia Ripoll |
| 9 |
* |
| 10 |
* See file 'LICENSE' for the copyright details. |
| 11 |
* |
| 12 |
*/ |
| 13 |
|
| 14 |
#include <ecl/ecl.h> |
| 15 |
#include <stdio.h> |
| 16 |
#include <sys/types.h> |
| 17 |
#include <errno.h> |
| 18 |
|
| 19 |
#if defined(ECL_MS_WINDOWS_HOST) |
| 20 |
#include <winsock.h> |
| 21 |
#else |
| 22 |
#ifdef __ANDROID__ |
| 23 |
#include <errno.h> |
| 24 |
#else |
| 25 |
extern int errno; |
| 26 |
#endif |
| 27 |
#include <sys/types.h> |
| 28 |
#include <sys/socket.h> |
| 29 |
#include <sys/un.h> |
| 30 |
#include <netinet/in.h> |
| 31 |
#include <netinet/tcp.h> |
| 32 |
#include <arpa/inet.h> |
| 33 |
#include <netdb.h> |
| 34 |
#include <unistd.h> |
| 35 |
#include <stdlib.h> |
| 36 |
#endif |
| 37 |
#include <string.h> |
| 38 |
|
| 39 |
#if defined(ECL_MS_WINDOWS_HOST) |
| 40 |
#include <io.h> |
| 41 |
#else |
| 42 |
#include <sys/ioctl.h> |
| 43 |
#endif |
| 44 |
|
| 45 |
/* Maximum length for a unix socket pathname */ |
| 46 |
#define UNIX_MAX_PATH 107 |
| 47 |
|
| 48 |
#if defined(ECL_MS_WINDOWS_HOST) |
| 49 |
WSADATA wsadata; |
| 50 |
int wsock_initialized = 0; |
| 51 |
#define INIT_TCP \ |
| 52 |
if ( !wsock_initialized ) \ |
| 53 |
{ \ |
| 54 |
if ( WSAStartup( MAKEWORD( 2, 2 ), &wsadata ) != NO_ERROR ) \ |
| 55 |
FEerror( "Unable to initialize Windows socket library.", 0 ); \ |
| 56 |
else \ |
| 57 |
wsock_initialized = 1; \ |
| 58 |
} |
| 59 |
#else |
| 60 |
#define INIT_TCP |
| 61 |
#endif |
| 62 |
|
| 63 |
void |
| 64 |
ecl_tcp_close_all(void) |
| 65 |
{ |
| 66 |
#if defined(ECL_MS_WINDOWS_HOST) |
| 67 |
if ( wsock_initialized ) |
| 68 |
{ |
| 69 |
WSACleanup(); |
| 70 |
wsock_initialized = 0; |
| 71 |
} |
| 72 |
#endif |
| 73 |
} |
| 74 |
|
| 75 |
/*********************************************************************** |
| 76 |
* Client side |
| 77 |
**********************************************************************/ |
| 78 |
|
| 79 |
/* |
| 80 |
* Attempts to connect to server, given host and port. Returns file |
| 81 |
* descriptor (network socket) or 0 if connection fails. |
| 82 |
*/ |
| 83 |
static |
| 84 |
int connect_to_server(char *host, int port) |
| 85 |
{ |
| 86 |
struct sockaddr_in inaddr; /* INET socket address. */ |
| 87 |
struct sockaddr *addr; /* address to connect to */ |
| 88 |
struct hostent *host_ptr; |
| 89 |
int addrlen; /* length of address */ |
| 90 |
int fd; /* Network socket */ |
| 91 |
|
| 92 |
INIT_TCP |
| 93 |
|
| 94 |
/* Get the statistics on the specified host. */ |
| 95 |
if ((inaddr.sin_addr.s_addr = inet_addr(host)) == -1) { |
| 96 |
if ((host_ptr = gethostbyname(host)) == NULL) { |
| 97 |
/* No such host! */ |
| 98 |
errno = EINVAL; |
| 99 |
return(0); |
| 100 |
} |
| 101 |
/* Check the address type for an internet host. */ |
| 102 |
if (host_ptr->h_addrtype != AF_INET) { |
| 103 |
/* Not an Internet host! */ |
| 104 |
#if defined(ECL_MS_WINDOWS_HOST) |
| 105 |
errno = WSAEPROTOTYPE; |
| 106 |
#else |
| 107 |
errno = EPROTOTYPE; |
| 108 |
#endif |
| 109 |
return(0); |
| 110 |
} |
| 111 |
/* Set up the socket data. */ |
| 112 |
inaddr.sin_family = host_ptr->h_addrtype; |
| 113 |
memcpy((char *)&inaddr.sin_addr, (char *)host_ptr->h_addr, |
| 114 |
sizeof(inaddr.sin_addr)); |
| 115 |
} |
| 116 |
else |
| 117 |
inaddr.sin_family = AF_INET; |
| 118 |
|
| 119 |
addr = (struct sockaddr *) &inaddr; |
| 120 |
addrlen = sizeof (struct sockaddr_in); |
| 121 |
inaddr.sin_port = port; |
| 122 |
inaddr.sin_port = htons(inaddr.sin_port); |
| 123 |
/* |
| 124 |
* Open the network connection. |
| 125 |
*/ |
| 126 |
if ((fd = socket((int) addr->sa_family, SOCK_STREAM, 0)) < 0) |
| 127 |
return(0); /* errno set by system call. */ |
| 128 |
|
| 129 |
ecl_disable_interrupts(); |
| 130 |
#ifdef TCP_NODELAY |
| 131 |
/* make sure to turn off TCP coalescence */ |
| 132 |
#if defined(ECL_MS_WINDOWS_HOST) |
| 133 |
{ char mi = 1; |
| 134 |
setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, &mi, sizeof (char)); |
| 135 |
} |
| 136 |
#else |
| 137 |
{ int mi = 1; |
| 138 |
setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, &mi, sizeof (int)); |
| 139 |
} |
| 140 |
#endif |
| 141 |
#endif |
| 142 |
if (connect(fd, addr, addrlen) == -1) { |
| 143 |
#if defined(ECL_MS_WINDOWS_HOST) |
| 144 |
closesocket(fd); |
| 145 |
#else |
| 146 |
(void) close (fd); |
| 147 |
#endif |
| 148 |
fd = 0; |
| 149 |
} |
| 150 |
ecl_enable_interrupts(); |
| 151 |
return(fd); |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
/*********************************************************************** |
| 156 |
* Server side |
| 157 |
**********************************************************************/ |
| 158 |
/* |
| 159 |
* Creates a server port. Returns file |
| 160 |
* descriptor (network socket) or 0 if connection fails. |
| 161 |
*/ |
| 162 |
|
| 163 |
int |
| 164 |
create_server_port(int port) |
| 165 |
{ |
| 166 |
struct sockaddr_in inaddr; /* INET socket address. */ |
| 167 |
int addrlen; /* length of address */ |
| 168 |
int request, conn; /* Network socket */ |
| 169 |
|
| 170 |
INIT_TCP |
| 171 |
|
| 172 |
/* |
| 173 |
* Open the network connection. |
| 174 |
*/ |
| 175 |
if ((request = socket(AF_INET, SOCK_STREAM, 0)) < 0) { |
| 176 |
return(0); /* errno set by system call. */ |
| 177 |
} |
| 178 |
|
| 179 |
#ifdef SO_REUSEADDR |
| 180 |
/* Necesary to restart the server without a reboot */ |
| 181 |
#if defined(ECL_MS_WINDOWS_HOST) |
| 182 |
{ |
| 183 |
char one = 1; |
| 184 |
setsockopt(request, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(char)); |
| 185 |
} |
| 186 |
#else |
| 187 |
{ |
| 188 |
int one = 1; |
| 189 |
setsockopt(request, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(int)); |
| 190 |
} |
| 191 |
#endif |
| 192 |
#endif /* SO_REUSEADDR */ |
| 193 |
#ifdef TCP_NODELAY |
| 194 |
/* make sure to turn off TCP coalescence */ |
| 195 |
#if defined(ECL_MS_WINDOWS_HOST) |
| 196 |
{ char mi = 1; |
| 197 |
setsockopt(request, IPPROTO_TCP, TCP_NODELAY, &mi, sizeof (char)); |
| 198 |
} |
| 199 |
#else |
| 200 |
{ int mi = 1; |
| 201 |
setsockopt(request, IPPROTO_TCP, TCP_NODELAY, &mi, sizeof (int)); |
| 202 |
} |
| 203 |
#endif |
| 204 |
#endif |
| 205 |
|
| 206 |
/* Set up the socket data. */ |
| 207 |
addrlen = sizeof (struct sockaddr_in); |
| 208 |
memset((char *)&inaddr, 0, addrlen); |
| 209 |
inaddr.sin_family = AF_INET; |
| 210 |
#if defined(ECL_MS_WINDOWS_HOST) |
| 211 |
inaddr.sin_port = htons((unsigned short)port); |
| 212 |
#else |
| 213 |
inaddr.sin_port = htons(port); |
| 214 |
#endif |
| 215 |
inaddr.sin_addr.s_addr = htonl(INADDR_ANY); |
| 216 |
|
| 217 |
if (bind(request, (struct sockaddr *)&inaddr, addrlen)) |
| 218 |
FElibc_error("Binding TCP socket", 0); |
| 219 |
if (listen(request, 1)) |
| 220 |
FElibc_error("TCP listening", 0); |
| 221 |
#if 0 && defined(ECL_THREADS) |
| 222 |
/* Don't make this file-descriptor non-blocking |
| 223 |
* just block on it before we attempt to accept from it |
| 224 |
* Think _hard_ about moving this out of here, into somewhere sane |
| 225 |
* and creating an 'accepting' stream type, which is bound to a port |
| 226 |
* on reading returns streams |
| 227 |
*/ |
| 228 |
{ |
| 229 |
FILE *fp; /* need to use FILE *'s rather than fd... *sigh* */ |
| 230 |
if ((fp = fdopen(request, "r")) == (FILE *)0) |
| 231 |
printf("fdopen didn't work on accept fd!\n"); fflush(stdout); |
| 232 |
fcntl(request, F_SETFL, O_NONBLOCK); |
| 233 |
clearerr(fp); |
| 234 |
|
| 235 |
loop: errno = 0; |
| 236 |
if ((conn = accept(request, (struct sockaddr *)NULL, (int *)NULL)) < 0) |
| 237 |
if (errno) { |
| 238 |
lwpblockon(active, fp, PD_INPUT); |
| 239 |
clearerr(fp); |
| 240 |
goto loop; |
| 241 |
} else { |
| 242 |
fclose(fp); |
| 243 |
FElibc_error("Accepting requests", 0); |
| 244 |
} |
| 245 |
fclose(fp); |
| 246 |
} |
| 247 |
#else |
| 248 |
if ((conn = accept(request, (struct sockaddr *)NULL, NULL)) < 0) |
| 249 |
FElibc_error("Accepting requests", 0); |
| 250 |
#endif /* THREADS */ |
| 251 |
|
| 252 |
close(request); |
| 253 |
return(conn); |
| 254 |
} |
| 255 |
|
| 256 |
/*********************************************************************** |
| 257 |
* Public interface to lisp environment |
| 258 |
**********************************************************************/ |
| 259 |
|
| 260 |
/* |
| 261 |
@open-client-stream -- |
| 262 |
|
| 263 |
To test this function, try: |
| 264 |
(setq s (si:open-client-stream "host" 13)) |
| 265 |
(read-line s) |
| 266 |
"Wed Jun 22 19:44:36 METDST 1994" |
| 267 |
*/ |
| 268 |
cl_object |
| 269 |
si_open_client_stream(cl_object host, cl_object port) |
| 270 |
{ |
| 271 |
int fd; /* file descriptor */ |
| 272 |
cl_object stream; |
| 273 |
|
| 274 |
/* Ensure "host" is a string that we can pass to a C function */ |
| 275 |
host = si_copy_to_simple_base_string(host); |
| 276 |
|
| 277 |
if (ecl_unlikely(!ECL_FIXNUMP(port) || |
| 278 |
ecl_fixnum_minusp(port) || |
| 279 |
ecl_fixnum_greater(port,ecl_make_fixnum(65535)))) { |
| 280 |
FEwrong_type_nth_arg(@[si::open-client-stream], 2, port, |
| 281 |
ecl_read_from_cstring("(INTEGER 0 65535)")); |
| 282 |
} |
| 283 |
|
| 284 |
if (host->base_string.fillp > BUFSIZ - 1) |
| 285 |
FEerror("~S is a too long file name.", 1, host); |
| 286 |
|
| 287 |
ecl_disable_interrupts(); |
| 288 |
fd = connect_to_server((char*)host->base_string.self, ecl_fixnum(port)); |
| 289 |
ecl_enable_interrupts(); |
| 290 |
|
| 291 |
if (fd == 0) { |
| 292 |
@(return ECL_NIL); |
| 293 |
} |
| 294 |
|
| 295 |
#if defined(ECL_MS_WINDOWS_HOST) |
| 296 |
stream = ecl_make_stream_from_fd(host, fd, ecl_smm_io_wsock, 8, 0, ECL_NIL); |
| 297 |
#else |
| 298 |
stream = ecl_make_stream_from_fd(host, fd, ecl_smm_io, 8, 0, ECL_NIL); |
| 299 |
#endif |
| 300 |
|
| 301 |
@(return stream); |
| 302 |
} |
| 303 |
|
| 304 |
cl_object |
| 305 |
si_open_server_stream(cl_object port) |
| 306 |
{ |
| 307 |
int fd; /* file descriptor */ |
| 308 |
cl_index p; |
| 309 |
|
| 310 |
if (ecl_unlikely(!ECL_FIXNUMP(port) || |
| 311 |
ecl_fixnum_minusp(port) || |
| 312 |
ecl_fixnum_greater(port,ecl_make_fixnum(65535)))) { |
| 313 |
FEwrong_type_only_arg(@[si::open-server-stream], port, |
| 314 |
ecl_read_from_cstring("(INTEGER 0 65535)")); |
| 315 |
} |
| 316 |
p = ecl_fixnum(port); |
| 317 |
ecl_disable_interrupts(); |
| 318 |
fd = create_server_port(p); |
| 319 |
ecl_enable_interrupts(); |
| 320 |
|
| 321 |
@(return ((fd == 0)? ECL_NIL : ecl_make_stream_from_fd(ECL_NIL, fd, ecl_smm_io, 8, 0, ECL_NIL))); |
| 322 |
} |
| 323 |
|
| 324 |
/************************************************************ |
| 325 |
* Unix sockets * |
| 326 |
************************************************************/ |
| 327 |
|
| 328 |
cl_object |
| 329 |
si_open_unix_socket_stream(cl_object path) |
| 330 |
{ |
| 331 |
#if defined(ECL_MS_WINDOWS_HOST) |
| 332 |
FEerror("UNIX socket not supported under Win32 platform", 0); |
| 333 |
#else |
| 334 |
int fd; /* file descriptor */ |
| 335 |
struct sockaddr_un addr; |
| 336 |
|
| 337 |
if (ecl_unlikely(!ECL_STRINGP(path))) |
| 338 |
FEwrong_type_only_arg(@[si::open-unix-socket-stream], path, @[string]); |
| 339 |
|
| 340 |
path = si_coerce_to_base_string(path); |
| 341 |
if (path->base_string.fillp > UNIX_MAX_PATH-1) |
| 342 |
FEerror("~S is a too long file name.", 1, path); |
| 343 |
|
| 344 |
fd = socket(PF_UNIX, SOCK_STREAM, 0); |
| 345 |
if (fd < 0) { |
| 346 |
FElibc_error("Unable to create unix socket", 0); |
| 347 |
@(return ECL_NIL); |
| 348 |
} |
| 349 |
|
| 350 |
memcpy(addr.sun_path, path->base_string.self, path->base_string.fillp); |
| 351 |
addr.sun_path[path->base_string.fillp] = 0; |
| 352 |
addr.sun_family = AF_UNIX; |
| 353 |
|
| 354 |
if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) { |
| 355 |
close(fd); |
| 356 |
FElibc_error("Unable to connect to unix socket ~A", 1, path); |
| 357 |
@(return ECL_NIL); |
| 358 |
} |
| 359 |
|
| 360 |
@(return ecl_make_stream_from_fd(path, fd, ecl_smm_io, 8, 0, ECL_NIL)); |
| 361 |
#endif |
| 362 |
} |
| 363 |
|
| 364 |
/************************************************************ |
| 365 |
* Hostname resolution * |
| 366 |
************************************************************/ |
| 367 |
cl_object |
| 368 |
si_lookup_host_entry(cl_object host_or_address) |
| 369 |
{ |
| 370 |
struct hostent *he; |
| 371 |
unsigned long l; |
| 372 |
char address[4]; |
| 373 |
cl_object name, aliases, addresses; |
| 374 |
int i; |
| 375 |
|
| 376 |
INIT_TCP |
| 377 |
|
| 378 |
switch (ecl_t_of(host_or_address)) { |
| 379 |
#ifdef ECL_UNICODE |
| 380 |
case t_string: |
| 381 |
#endif |
| 382 |
case t_base_string: |
| 383 |
host_or_address = si_copy_to_simple_base_string(host_or_address); |
| 384 |
he = gethostbyname((char*)host_or_address->base_string.self); |
| 385 |
break; |
| 386 |
case t_fixnum: |
| 387 |
l = ecl_fixnum(host_or_address); |
| 388 |
goto addr; |
| 389 |
case t_bignum: |
| 390 |
l = _ecl_big_to_ulong(host_or_address); |
| 391 |
addr: address[0] = l & 0xFF; |
| 392 |
address[1] = (l >> 8) & 0xFF; |
| 393 |
address[2] = (l >> 16) & 0xFF; |
| 394 |
address[3] = (l >> 24) & 0xFF; |
| 395 |
he = gethostbyaddr((char*)&address, 4, AF_INET); |
| 396 |
break; |
| 397 |
default: |
| 398 |
FEerror("LOOKUP-HOST-ENTRY: Number or string expected, got ~S", |
| 399 |
1, host_or_address); |
| 400 |
} |
| 401 |
if (he == NULL) { |
| 402 |
@(return ECL_NIL ECL_NIL ECL_NIL); |
| 403 |
} |
| 404 |
name = ecl_make_simple_base_string(he->h_name,-1); |
| 405 |
aliases = ECL_NIL; |
| 406 |
for (i = 0; he->h_aliases[i] != 0; i++) |
| 407 |
aliases = CONS(ecl_make_simple_base_string(he->h_aliases[i],-1), aliases); |
| 408 |
addresses = ECL_NIL; |
| 409 |
for (i = 0; he->h_addr_list[i]; i++) { |
| 410 |
unsigned long *s = (unsigned long*)(he->h_addr_list[i]); |
| 411 |
l = *s; |
| 412 |
addresses = CONS(ecl_make_integer(l), addresses); |
| 413 |
} |
| 414 |
@(return name aliases addresses); |
| 415 |
} |