| 1 |
;; -*- Mode: Lisp; Syntax: Common-Lisp -*- |
| 2 |
;; $Id$ |
| 3 |
|
| 4 |
;; This file is based on SBCL's SB-BSD-SOCKET module and has been |
| 5 |
;; heavily modified to work with ECL by Julian Stecklina. |
| 6 |
;; Port to Windows Sockets contributed by M. Goffioul. |
| 7 |
|
| 8 |
;; You may do whatever you want with this file. (PUBLIC DOMAIN) |
| 9 |
|
| 10 |
;; Trivial stuff is copied from SBCL's SB-BSD-SOCKETS, which is also |
| 11 |
;; in the public domain. |
| 12 |
|
| 13 |
(in-package "SB-BSD-SOCKETS") |
| 14 |
|
| 15 |
;; Obviously this requires the one or other form of BSD compatible |
| 16 |
;; socket interface. |
| 17 |
|
| 18 |
#+(or :win32 :mingw32) |
| 19 |
(eval-when (:compile-toplevel :load-toplevel :execute) |
| 20 |
(pushnew :wsock *features*)) |
| 21 |
|
| 22 |
;; Include the neccessary headers |
| 23 |
#-:wsock |
| 24 |
(clines |
| 25 |
"#include <sys/types.h>" |
| 26 |
"#include <sys/socket.h>" |
| 27 |
"#include <sys/un.h>" |
| 28 |
"#define wincoerce(t,x) (x)" |
| 29 |
#-:win32 |
| 30 |
"#include <sys/time.h>" |
| 31 |
"#include <netdb.h>" |
| 32 |
"#include <string.h>" |
| 33 |
"#include <unistd.h>" |
| 34 |
"#include <netinet/in.h>" |
| 35 |
"#include <netinet/tcp.h>" |
| 36 |
"#include <errno.h>" |
| 37 |
"#include <fcntl.h>" |
| 38 |
#+:cygwin |
| 39 |
"#ifndef MSG_WAITALL |
| 40 |
#define MSG_WAITALL 0 |
| 41 |
#endif" |
| 42 |
"#ifndef MSG_CONFIRM" |
| 43 |
"#define MSG_CONFIRM 0" |
| 44 |
"#endif" |
| 45 |
"#ifndef MSG_NOSIGNAL" |
| 46 |
"#define MSG_NOSIGNAL 0" |
| 47 |
"#endif" |
| 48 |
"#ifndef MSG_DONTWAIT" |
| 49 |
"#define MSG_DONTWAIT 0" |
| 50 |
"#endif" |
| 51 |
"#ifndef MSG_EOR" |
| 52 |
"#define MSG_EOR 0" |
| 53 |
"#endif") |
| 54 |
#+:wsock |
| 55 |
(clines |
| 56 |
"#include <winsock2.h>" |
| 57 |
"typedef unsigned int uint32_t;" |
| 58 |
"#define wincoerce(t,x) ((t)(x))" |
| 59 |
#-:mingw32 |
| 60 |
"typedef int ssize_t;" |
| 61 |
"typedef int socklen_t;" |
| 62 |
"#define MSG_WAITALL 0" |
| 63 |
"#define MSG_EOR 0" |
| 64 |
"#define MSG_DONTWAIT 0" |
| 65 |
"#define MSG_NOSIGNAL 0" |
| 66 |
"#define MSG_CONFIRM 0" |
| 67 |
"#define MSG_TRUNC 0" |
| 68 |
"#include <errno.h>" |
| 69 |
"#include <fcntl.h>" |
| 70 |
"#include <stdio.h>") |
| 71 |
|
| 72 |
#+windows |
| 73 |
(clines |
| 74 |
"#include <ws2tcpip.h>") |
| 75 |
|
| 76 |
#+:wsock |
| 77 |
(progn |
| 78 |
(defvar +wsock-initialized+ nil) |
| 79 |
(defun wsock-initialize () |
| 80 |
(unless +wsock-initialized+ |
| 81 |
(if (c-inline () () :object |
| 82 |
" |
| 83 |
{ |
| 84 |
WSADATA wsadata; |
| 85 |
cl_object output; |
| 86 |
ecl_disable_interrupts(); |
| 87 |
if (WSAStartup(MAKEWORD(2,2), &wsadata) == NO_ERROR) |
| 88 |
output = ECL_T; |
| 89 |
else |
| 90 |
output = ECL_NIL; |
| 91 |
ecl_enable_interrupts(); |
| 92 |
@(return) = output; |
| 93 |
}") |
| 94 |
(setf +wsock-initialized+ t) |
| 95 |
(error "Unable to initialize Windows Socket library")))) |
| 96 |
(wsock-initialize) |
| 97 |
); #+:wsock |
| 98 |
|
| 99 |
(eval-when (:compile-toplevel :execute) |
| 100 |
(defmacro c-constant (c-name) |
| 101 |
`(c-inline () () :int ,c-name :one-liner t)) |
| 102 |
(defmacro define-c-constants (&rest args) |
| 103 |
`(let () ; Prevents evaluation of constant value form |
| 104 |
,@(loop |
| 105 |
for (lisp-name c-name) on args by #'cddr |
| 106 |
collect `(defconstant ,lisp-name (c-constant ,c-name)))))) |
| 107 |
|
| 108 |
#+:wsock |
| 109 |
(Clines |
| 110 |
"#define AF_LOCAL -1" |
| 111 |
) |
| 112 |
|
| 113 |
(define-c-constants |
| 114 |
+af-inet+ "AF_INET" |
| 115 |
+af-local+ #-(or sun4sol2 aix) "AF_LOCAL" #+(or sun4sol2 aix) "AF_UNIX" |
| 116 |
+eagain+ "EAGAIN" |
| 117 |
+eintr+ "EINTR") |
| 118 |
|
| 119 |
#+:android |
| 120 |
(define-c-constants |
| 121 |
+ipproto_ip+ "IPPROTO_IP" |
| 122 |
+ipproto_icmp+ "IPPROTO_ICMP" |
| 123 |
+ipproto_igmp+ "IPPROTO_IGMP" |
| 124 |
+ipproto_ipip+ "IPPROTO_IPIP" |
| 125 |
+ipproto_tcp+ "IPPROTO_TCP" |
| 126 |
+ipproto_egp+ "IPPROTO_EGP" |
| 127 |
+ipproto_pup+ "IPPROTO_PUP" |
| 128 |
+ipproto_udp+ "IPPROTO_UDP" |
| 129 |
+ipproto_idp+ "IPPROTO_IDP" |
| 130 |
+ipproto_dccp+ "IPPROTO_DCCP" |
| 131 |
+ipproto_rsvp+ "IPPROTO_RSVP" |
| 132 |
+ipproto_gre+ "IPPROTO_GRE" |
| 133 |
+ipproto_ipv6+ "IPPROTO_IPV6" |
| 134 |
+ipproto_esp+ "IPPROTO_ESP" |
| 135 |
+ipproto_ah+ "IPPROTO_AH" |
| 136 |
+ipproto_pim+ "IPPROTO_PIM" |
| 137 |
+ipproto_comp+ "IPPROTO_COMP" |
| 138 |
+ipproto_sctp+ "IPPROTO_SCTP" |
| 139 |
+ipproto_raw+ "IPPROTO_RAW") |
| 140 |
|
| 141 |
#+:wsock |
| 142 |
(defconstant +af-named-pipe+ -2) |
| 143 |
|
| 144 |
(Clines |
| 145 |
" |
| 146 |
static void fill_inet_sockaddr(struct sockaddr_in *sockaddr, int port, |
| 147 |
int a1, int a2, int a3, int a4) |
| 148 |
{ |
| 149 |
#if defined(_MSC_VER) || defined(mingw32) |
| 150 |
memset(sockaddr,0,sizeof(struct sockaddr_in)); |
| 151 |
#else |
| 152 |
bzero(sockaddr,sizeof(struct sockaddr_in)); |
| 153 |
#endif |
| 154 |
sockaddr->sin_family = AF_INET; |
| 155 |
sockaddr->sin_port = htons(port); |
| 156 |
sockaddr->sin_addr.s_addr= htonl((uint32_t)a1<<24 | (uint32_t)a2<<16 | (uint32_t)a3<<8 | (uint32_t)a4) ; |
| 157 |
|
| 158 |
} |
| 159 |
") |
| 160 |
|
| 161 |
;; Foreign functions |
| 162 |
|
| 163 |
(defentry ff-socket (:int :int :int) (:int "socket") :no-interrupts t) |
| 164 |
(defentry ff-listen (:int :int) (:int "listen") :no-interrupts t) |
| 165 |
(defentry ff-close (:int) (:int "close") :no-interrupts t) |
| 166 |
#+:wsock (defentry ff-closesocket (:int) (:int "closesocket") :no-interrupts t) |
| 167 |
|
| 168 |
;;; This courtesy of Pierre Mai in comp.lang.lisp 08 Jan 1999 00:51:44 +0100 |
| 169 |
;;; Message-ID: <[email protected]> |
| 170 |
|
| 171 |
(defun split (string &optional max (ws '(#\Space #\Tab))) |
| 172 |
"Split `string' along whitespace as defined by the sequence `ws'. |
| 173 |
The whitespace is elided from the result. The whole string will be |
| 174 |
split, unless `max' is a non-negative integer, in which case the |
| 175 |
string will be split into `max' tokens at most, the last one |
| 176 |
containing the whole rest of the given `string', if any." |
| 177 |
(flet ((is-ws (char) (find char ws))) |
| 178 |
(loop for start = (position-if-not #'is-ws string) |
| 179 |
then (position-if-not #'is-ws string :start index) |
| 180 |
for index = (and start |
| 181 |
(if (and max (= (1+ word-count) max)) |
| 182 |
nil |
| 183 |
(position-if #'is-ws string :start start))) |
| 184 |
while start |
| 185 |
collect (subseq string start index) |
| 186 |
count 1 into word-count |
| 187 |
while index))) |
| 188 |
|
| 189 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 190 |
;;; |
| 191 |
;;; NAME RESOLVING |
| 192 |
;;; |
| 193 |
|
| 194 |
(defclass host-ent () |
| 195 |
((name :initarg :name :accessor host-ent-name) |
| 196 |
(aliases :initarg :aliases :accessor host-ent-aliases) |
| 197 |
(address-type :initarg :type :accessor host-ent-address-type) |
| 198 |
; presently always AF_INET |
| 199 |
(addresses :initarg :addresses :accessor host-ent-addresses)) |
| 200 |
(:documentation "")) |
| 201 |
|
| 202 |
(defgeneric host-ent-address (host-ent) |
| 203 |
(:documentation "")) |
| 204 |
|
| 205 |
(defmethod host-ent-address ((host-ent host-ent)) |
| 206 |
(car (host-ent-addresses host-ent))) |
| 207 |
|
| 208 |
(defun get-host-by-name (host-name) |
| 209 |
"Returns a HOST-ENT instance for HOST-NAME or throws some kind of condition. |
| 210 |
HOST-NAME may also be an IP address in dotted quad notation or some |
| 211 |
other weird stuff - see getaddrinfo(3) for details." |
| 212 |
(multiple-value-bind (errno canonical-name addresses aliases) |
| 213 |
(c-inline (host-name :test #'equalp) (:cstring :object :object) |
| 214 |
(values :int :object :object :object) |
| 215 |
" |
| 216 |
{ |
| 217 |
struct addrinfo hints; |
| 218 |
struct addrinfo *result; |
| 219 |
cl_object host_name = ECL_NIL; |
| 220 |
cl_object aliases = ECL_NIL; |
| 221 |
cl_object addresses = ECL_NIL; |
| 222 |
int err; |
| 223 |
|
| 224 |
memset(&hints, 0, sizeof(hints)); |
| 225 |
hints.ai_family = AF_INET; /* IPv4 */ |
| 226 |
hints.ai_socktype = 0; /* Any type */ |
| 227 |
hints.ai_protocol = 0; /* Any protocol */ |
| 228 |
hints.ai_flags = (AI_CANONNAME); /* Get cannonname */ |
| 229 |
hints.ai_addr = NULL; |
| 230 |
hints.ai_next = NULL; |
| 231 |
|
| 232 |
ecl_disable_interrupts(); |
| 233 |
err = getaddrinfo(#0, NULL, &hints, &result); |
| 234 |
ecl_enable_interrupts(); |
| 235 |
|
| 236 |
if (err == 0) { |
| 237 |
struct addrinfo *rp; |
| 238 |
|
| 239 |
for (rp = result; rp != NULL; rp = rp->ai_next) { |
| 240 |
if ( (rp == result) && (rp->ai_canonname != 0) ) { /* first one may hold cannonname */ |
| 241 |
host_name = ecl_make_simple_base_string( rp->ai_canonname, -1 ); |
| 242 |
} |
| 243 |
struct sockaddr_in *ipv4 = (struct sockaddr_in *)rp->ai_addr; |
| 244 |
uint32_t ip = ntohl( ipv4->sin_addr.s_addr ); |
| 245 |
cl_object vector = cl_make_array(1,ecl_make_fixnum(4)); |
| 246 |
ecl_aset(vector,0, ecl_make_fixnum( ip>>24 )); |
| 247 |
ecl_aset(vector,1, ecl_make_fixnum( (ip>>16) & 0xFF)); |
| 248 |
ecl_aset(vector,2, ecl_make_fixnum( (ip>>8) & 0xFF)); |
| 249 |
ecl_aset(vector,3, ecl_make_fixnum( ip & 0xFF )); |
| 250 |
addresses = cl_adjoin(4, vector, addresses, #1, #2); |
| 251 |
if ( rp->ai_canonname != 0 ) { |
| 252 |
cl_object alias = ecl_make_simple_base_string( rp->ai_canonname, -1 ); |
| 253 |
aliases = CONS(alias, aliases); |
| 254 |
} |
| 255 |
} |
| 256 |
freeaddrinfo(result); |
| 257 |
} |
| 258 |
@(return 0) = err; |
| 259 |
@(return 1) = host_name; |
| 260 |
@(return 2) = addresses; |
| 261 |
@(return 3) = aliases; |
| 262 |
}" |
| 263 |
:side-effects t) |
| 264 |
(if (= errno 0) |
| 265 |
(make-instance 'host-ent |
| 266 |
:name (or canonical-name host-name) |
| 267 |
:aliases aliases |
| 268 |
:type +af-inet+ |
| 269 |
:addresses addresses) |
| 270 |
(name-service-error "get-host-by-name" errno)))) |
| 271 |
|
| 272 |
;;; Use values from getnameinfo man page if NI_MAXHOST is not declared |
| 273 |
(clines |
| 274 |
"#ifndef NI_MAXHOST |
| 275 |
#define NI_MAXHOST 1025 |
| 276 |
#endif") |
| 277 |
|
| 278 |
(defun get-host-by-address (address) |
| 279 |
(assert (and (typep address 'vector) |
| 280 |
(= (length address) 4))) |
| 281 |
(multiple-value-bind (errno name) |
| 282 |
(c-inline ((aref address 0) (aref address 1) (aref address 2) (aref address 3)) |
| 283 |
(:int :int :int :int) (values :int t) |
| 284 |
" |
| 285 |
{ |
| 286 |
struct sockaddr_in addr; |
| 287 |
socklen_t addr_len = (socklen_t)sizeof(struct sockaddr_in); |
| 288 |
char host[NI_MAXHOST]; |
| 289 |
int err; |
| 290 |
|
| 291 |
fill_inet_sockaddr(&addr, 0, #0, #1, #2, #3); |
| 292 |
|
| 293 |
ecl_disable_interrupts(); |
| 294 |
err = getnameinfo((struct sockaddr *) &addr, addr_len, host, NI_MAXHOST, NULL, 0, NI_NAMEREQD); |
| 295 |
ecl_enable_interrupts(); |
| 296 |
|
| 297 |
@(return 0) = err; |
| 298 |
@(return 1) = err ? ECL_NIL : ecl_make_simple_base_string(host,-1); |
| 299 |
}" |
| 300 |
:side-effects t) |
| 301 |
(if (= errno 0) |
| 302 |
(make-instance 'host-ent |
| 303 |
:name name |
| 304 |
:aliases nil |
| 305 |
:type +af-inet+ |
| 306 |
:addresses (list address)) |
| 307 |
(name-service-error "get-host-by-address" errno)))) |
| 308 |
|
| 309 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 310 |
;;; |
| 311 |
;;; SOCKET BASE CLASS AND GENERIC FUNCTIONS |
| 312 |
;;; |
| 313 |
|
| 314 |
(defclass socket () |
| 315 |
((file-descriptor :initarg :descriptor |
| 316 |
:reader socket-file-descriptor) |
| 317 |
(family :initform (error "No socket family") |
| 318 |
:reader socket-family) |
| 319 |
(protocol :initarg :protocol |
| 320 |
:reader socket-protocol |
| 321 |
:documentation "Protocol used by the socket. If a |
| 322 |
keyword, the symbol-name of the keyword will be passed to |
| 323 |
GET-PROTOCOL-BY-NAME downcased, and the returned value used as |
| 324 |
protocol. Other values are used as-is.") |
| 325 |
(type :initarg :type |
| 326 |
:reader socket-type |
| 327 |
:initform :stream |
| 328 |
:documentation "Type of the socket: :STREAM or :DATAGRAM.") |
| 329 |
(stream) |
| 330 |
#+:wsock |
| 331 |
(non-blocking-p :initform nil)) |
| 332 |
(:documentation "Common base class of all sockets, not meant to be |
| 333 |
directly instantiated.")) |
| 334 |
|
| 335 |
(defmethod print-object ((object socket) stream) |
| 336 |
(print-unreadable-object (object stream :type t :identity t) |
| 337 |
(if (slot-boundp object 'file-descriptor) |
| 338 |
(progn |
| 339 |
(princ "descriptor " stream) |
| 340 |
(princ (slot-value object 'file-descriptor) stream)) |
| 341 |
(princ "(unbound descriptor)")))) |
| 342 |
|
| 343 |
(defmethod shared-initialize :after ((socket socket) slot-names |
| 344 |
&key protocol type |
| 345 |
&allow-other-keys) |
| 346 |
(let* ((proto-num |
| 347 |
(cond ((and protocol (keywordp protocol)) |
| 348 |
(get-protocol-by-name (string-downcase (symbol-name protocol)))) |
| 349 |
(protocol protocol) |
| 350 |
(t 0))) |
| 351 |
(fd (or (and (slot-boundp socket 'file-descriptor) |
| 352 |
(socket-file-descriptor socket)) |
| 353 |
#+:wsock |
| 354 |
(and (member (socket-family socket) (list +af-named-pipe+ +af-local+)) 0) |
| 355 |
(ff-socket (socket-family socket) |
| 356 |
(ecase (or type |
| 357 |
(socket-type socket)) |
| 358 |
((:datagram) (c-constant "SOCK_DGRAM")) |
| 359 |
((:stream) (c-constant "SOCK_STREAM"))) |
| 360 |
proto-num)))) |
| 361 |
(if (= fd -1) (socket-error "socket")) |
| 362 |
(setf (slot-value socket 'file-descriptor) fd |
| 363 |
(slot-value socket 'protocol) proto-num) |
| 364 |
#+ ignore |
| 365 |
(sb-ext:finalize socket (lambda () (sockint::close fd))))) |
| 366 |
|
| 367 |
;; Generics |
| 368 |
|
| 369 |
(defgeneric socket-bind (socket &rest address) |
| 370 |
(:documentation "Bind SOCKET to ADDRESS, which may vary according to |
| 371 |
socket family. For the INET family, pass ADDRESS and PORT as two |
| 372 |
arguments; for FILE address family sockets, pass the filename string. |
| 373 |
See also bind(2)")) |
| 374 |
|
| 375 |
(defgeneric socket-accept (socket) |
| 376 |
(:documentation "Perform the accept(2) call, returning a |
| 377 |
newly-created connected socket and the peer address as multiple |
| 378 |
values")) |
| 379 |
|
| 380 |
(defgeneric socket-connect (socket &rest address) |
| 381 |
(:documentation "Perform the connect(2) call to connect SOCKET to a |
| 382 |
remote PEER. No useful return value.")) |
| 383 |
|
| 384 |
(defgeneric socket-peername (socket) |
| 385 |
(:documentation "Return the socket's peer; depending on the address |
| 386 |
family this may return multiple values")) |
| 387 |
|
| 388 |
(defgeneric socket-name (socket) |
| 389 |
(:documentation "Return the address (as vector of bytes) and port |
| 390 |
that the socket is bound to, as multiple values.")) |
| 391 |
|
| 392 |
(defgeneric socket-listen (socket backlog) |
| 393 |
(:documentation "Mark SOCKET as willing to accept incoming connections. BACKLOG |
| 394 |
defines the maximum length that the queue of pending connections may |
| 395 |
grow to before new connection attempts are refused. See also listen(2)")) |
| 396 |
|
| 397 |
(defgeneric socket-receive (socket buffer length |
| 398 |
&key |
| 399 |
oob peek waitall element-type) |
| 400 |
(:documentation "Read LENGTH octets from SOCKET into BUFFER (or a freshly-consed buffer if |
| 401 |
NIL), using recvfrom(2). If LENGTH is NIL, the length of BUFFER is |
| 402 |
used, so at least one of these two arguments must be non-NIL. If |
| 403 |
BUFFER is supplied, it had better be of an element type one octet wide. |
| 404 |
Returns the buffer, its length, and the address of the peer |
| 405 |
that sent it, as multiple values. On datagram sockets, sets MSG_TRUNC |
| 406 |
so that the actual packet length is returned even if the buffer was too |
| 407 |
small")) |
| 408 |
|
| 409 |
(defgeneric socket-send (socket buffer length |
| 410 |
&key |
| 411 |
address external-format oob eor dontroute dontwait |
| 412 |
nosignal confirm more) |
| 413 |
(:documentation "Send length octets from buffer into socket, using sendto(2). |
| 414 |
If buffer is a string, it will converted to octets according to external-format& |
| 415 |
If length is nil, the length of the octet buffer is used. The format of address |
| 416 |
depends on the socket type (for example for inet domain sockets it would be a |
| 417 |
list of an ip address and a port). If no socket address is provided, send(2) |
| 418 |
will be called instead. Returns the number of octets written.")) |
| 419 |
|
| 420 |
|
| 421 |
(defgeneric socket-close (socket &key abort) |
| 422 |
(:documentation "Close SOCKET. May throw any kind of error that write(2) would have |
| 423 |
thrown. If SOCKET-MAKE-STREAM has been called, calls CLOSE using ABORT on that |
| 424 |
stream instead")) |
| 425 |
|
| 426 |
(defgeneric socket-make-stream (socket &rest args) |
| 427 |
(:documentation "Find or create a STREAM that can be used for IO |
| 428 |
on SOCKET (which must be connected). ARGS are passed onto |
| 429 |
SB-SYS:MAKE-FD-STREAM.")) |
| 430 |
|
| 431 |
(defgeneric non-blocking-mode (socket) |
| 432 |
(:documentation "Is SOCKET in non-blocking mode?")) |
| 433 |
|
| 434 |
(defgeneric (setf non-blocking-mode) (non-blocking-p socket) |
| 435 |
(:documentation "Put SOCKET in non-blocking mode - or not, according to NON-BLOCKING-P")) |
| 436 |
|
| 437 |
(defgeneric socket-close-low-level (socket) |
| 438 |
(:documentation "Close SOCKET at low level. Do not use directly.")) |
| 439 |
|
| 440 |
;; Methods |
| 441 |
|
| 442 |
(defmethod socket-listen ((socket socket) backlog) |
| 443 |
(let ((r (ff-listen (socket-file-descriptor socket) backlog))) |
| 444 |
(if (= r -1) |
| 445 |
(socket-error "listen")))) |
| 446 |
|
| 447 |
(defmethod socket-close-low-level ((socket socket)) |
| 448 |
(ff-close (socket-file-descriptor socket))) |
| 449 |
|
| 450 |
(defmethod socket-close ((socket socket) &key abort) |
| 451 |
;; the close(2) manual page has all kinds of warning about not |
| 452 |
;; checking the return value of close, on the grounds that an |
| 453 |
;; earlier write(2) might have returned successfully w/o actually |
| 454 |
;; writing the stuff to disk. It then goes on to define the only |
| 455 |
;; possible error return as EBADF (fd isn't a valid open file |
| 456 |
;; descriptor). Presumably this is an oversight and we could also |
| 457 |
;; get anything that write(2) would have given us. |
| 458 |
|
| 459 |
;; note that if you have a socket _and_ a stream on the same fd, |
| 460 |
;; the socket will avoid doing anything to close the fd in case |
| 461 |
;; the stream has done it already - if so, it may have been |
| 462 |
;; reassigned to some other file, and closing it would be bad |
| 463 |
|
| 464 |
(let ((fd (socket-file-descriptor socket))) |
| 465 |
(unless (eql fd -1) ; already closed |
| 466 |
(cond ((slot-boundp socket 'stream) |
| 467 |
(let ((stream (slot-value socket 'stream))) |
| 468 |
#+threads |
| 469 |
(close (two-way-stream-input-stream stream) :abort abort) |
| 470 |
#+threads |
| 471 |
(close (two-way-stream-output-stream stream) :abort abort) |
| 472 |
#-threads |
| 473 |
(close stream :abort abort)) ;; closes fd indirectly |
| 474 |
(slot-makunbound socket 'stream)) |
| 475 |
((= (socket-close-low-level socket) -1) |
| 476 |
(socket-error "close"))) |
| 477 |
(setf (slot-value socket 'file-descriptor) -1)))) |
| 478 |
|
| 479 |
(ffi::clines " |
| 480 |
static void * |
| 481 |
safe_buffer_pointer(cl_object x, cl_index size) |
| 482 |
{ |
| 483 |
cl_type t = ecl_t_of(x); |
| 484 |
int ok = 0; |
| 485 |
if (t == t_base_string) { |
| 486 |
ok = (size <= x->base_string.dim); |
| 487 |
} else if (t == t_vector) { |
| 488 |
cl_elttype aet = (cl_elttype)x->vector.elttype; |
| 489 |
if (aet == ecl_aet_b8 || aet == ecl_aet_i8 || aet == ecl_aet_bc) { |
| 490 |
ok = (size <= x->vector.dim); |
| 491 |
} else if (aet == ecl_aet_fix || aet == ecl_aet_index) { |
| 492 |
cl_index divisor = sizeof(cl_index); |
| 493 |
size = (size + divisor - 1) / divisor; |
| 494 |
ok = (size <= x->vector.dim); |
| 495 |
} |
| 496 |
} |
| 497 |
if (!ok) { |
| 498 |
FEerror(\"Lisp object is not a valid socket buffer: ~A\", 1, x); |
| 499 |
} |
| 500 |
return (void *)x->vector.self.t; |
| 501 |
} |
| 502 |
") |
| 503 |
|
| 504 |
;; FIXME: How bad is manipulating fillp directly? |
| 505 |
(defmethod socket-receive ((socket socket) buffer length |
| 506 |
&key oob peek waitall (element-type 'ext:byte8)) |
| 507 |
(unless (or buffer length) (error "You have to supply either buffer or length!")) |
| 508 |
(let ((buffer (or buffer (make-array length :element-type element-type))) |
| 509 |
(length (or length (length buffer))) |
| 510 |
(fd (socket-file-descriptor socket)) |
| 511 |
(trunc (if (eql (socket-type socket) :datagram) t nil))) |
| 512 |
|
| 513 |
(multiple-value-bind (len-recv errno vector port) |
| 514 |
(c-inline (fd buffer length oob peek waitall trunc) |
| 515 |
(:int :object :int :bool :bool :bool :bool) |
| 516 |
(values :long :int :object :int) |
| 517 |
" |
| 518 |
{ |
| 519 |
int flags = ( #3 ? MSG_OOB : 0 ) | |
| 520 |
( #4 ? MSG_PEEK : 0 ) | |
| 521 |
( #5 ? MSG_WAITALL : 0 ) | |
| 522 |
( #6 ? MSG_TRUNC : 0 ); |
| 523 |
cl_type type = ecl_t_of(#1); |
| 524 |
ssize_t len; |
| 525 |
struct sockaddr_in sender; |
| 526 |
socklen_t addr_len = (socklen_t)sizeof(struct sockaddr_in); |
| 527 |
|
| 528 |
ecl_disable_interrupts(); |
| 529 |
len = recvfrom(#0, wincoerce(char*, safe_buffer_pointer(#1, #2)), |
| 530 |
#2, flags, (struct sockaddr *)&sender, &addr_len); |
| 531 |
ecl_enable_interrupts(); |
| 532 |
if (len >= 0) { |
| 533 |
if (type == t_vector) { #1->vector.fillp = len; } |
| 534 |
else if (type == t_base_string) { #1->base_string.fillp = len; } |
| 535 |
} |
| 536 |
@(return 0) = len; |
| 537 |
@(return 1) = errno; |
| 538 |
@(return 2) = ECL_NIL; |
| 539 |
@(return 3) = 0; |
| 540 |
|
| 541 |
if (len >= 0) { |
| 542 |
uint32_t ip = ntohl(sender.sin_addr.s_addr); |
| 543 |
uint16_t port = ntohs(sender.sin_port); |
| 544 |
cl_object vector = cl_make_array(1,ecl_make_fixnum(4)); |
| 545 |
|
| 546 |
ecl_aset(vector,0, ecl_make_fixnum( ip>>24 )); |
| 547 |
ecl_aset(vector,1, ecl_make_fixnum( (ip>>16) & 0xFF)); |
| 548 |
ecl_aset(vector,2, ecl_make_fixnum( (ip>>8) & 0xFF)); |
| 549 |
ecl_aset(vector,3, ecl_make_fixnum( ip & 0xFF )); |
| 550 |
|
| 551 |
@(return 2) = vector; |
| 552 |
@(return 3) = port; |
| 553 |
} |
| 554 |
} |
| 555 |
" |
| 556 |
:one-liner nil) |
| 557 |
(cond ((and (= len-recv -1) |
| 558 |
(member errno (list +eagain+ +eintr+))) |
| 559 |
nil) |
| 560 |
((= len-recv -1) |
| 561 |
(socket-error "receive")) |
| 562 |
(t |
| 563 |
(values buffer len-recv vector port)))))) |
| 564 |
|
| 565 |
|
| 566 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 567 |
;;; |
| 568 |
;;; INET SOCKETS |
| 569 |
;;; |
| 570 |
|
| 571 |
;; We could refactor a lot here, if we pass sockaddr_foo structs around in Lisp. But |
| 572 |
;; I do not feel comfortable with that. |
| 573 |
|
| 574 |
(define-condition unknown-protocol (error) |
| 575 |
((name :initarg :name |
| 576 |
:reader unknown-protocol-name)) |
| 577 |
(:report (lambda (condition stream) |
| 578 |
(format stream "Protocol not found: ~A" |
| 579 |
(prin1-to-string (unknown-protocol-name condition)))))) |
| 580 |
|
| 581 |
(defun get-protocol-by-name (string-or-symbol) |
| 582 |
"Calls getprotobyname" |
| 583 |
#-:android |
| 584 |
(let* ((string (string string-or-symbol)) |
| 585 |
(proto-num (c-inline (string) (:cstring) :int |
| 586 |
"{ |
| 587 |
struct protoent *pe; |
| 588 |
pe = getprotobyname(#0); |
| 589 |
@(return 0) = pe ? pe->p_proto : -1; |
| 590 |
} |
| 591 |
"))) |
| 592 |
(if (= proto-num -1) |
| 593 |
(error 'unknown-protocol :name string) |
| 594 |
proto-num)) |
| 595 |
;; getprotobyname is not yet implemented on bionic |
| 596 |
#+:android |
| 597 |
(let ((proto (string-downcase (if (symbolp string-or-symbol) |
| 598 |
(symbol-name string-or-symbol) |
| 599 |
string-or-symbol)))) |
| 600 |
(cond |
| 601 |
((string= proto "ip") +ipproto_ip+) |
| 602 |
((string= proto "icmp") +ipproto_icmp+) |
| 603 |
((string= proto "igmp") +ipproto_igmp+) |
| 604 |
((string= proto "ipip") +ipproto_ipip+) |
| 605 |
((string= proto "tcp") +ipproto_tcp+) |
| 606 |
((string= proto "egp") +ipproto_egp+) |
| 607 |
((string= proto "pup") +ipproto_pup+) |
| 608 |
((string= proto "udp") +ipproto_udp+) |
| 609 |
((string= proto "idp") +ipproto_idp+) |
| 610 |
((string= proto "dccp") +ipproto_dccp+) |
| 611 |
((string= proto "rsvp") +ipproto_rsvp+) |
| 612 |
((string= proto "gre") +ipproto_gre+) |
| 613 |
((string= proto "ipv6") +ipproto_ipv6+) |
| 614 |
((string= proto "esp") +ipproto_esp+) |
| 615 |
((string= proto "ah") +ipproto_ah+) |
| 616 |
((string= proto "pim") +ipproto_pim+) |
| 617 |
((string= proto "comp") +ipproto_comp+) |
| 618 |
((string= proto "sctp") +ipproto_sctp+) |
| 619 |
((string= proto "raw") +ipproto_raw+)))) |
| 620 |
|
| 621 |
(defun make-inet-address (dotted-quads) |
| 622 |
"Return a vector of octets given a string DOTTED-QUADS in the format |
| 623 |
\"127.0.0.1\"" |
| 624 |
(map 'vector |
| 625 |
#'parse-integer |
| 626 |
(split dotted-quads nil '(#\.)))) |
| 627 |
|
| 628 |
(defclass inet-socket (socket) |
| 629 |
((family :initform +af-inet+)) |
| 630 |
(:documentation "Class representing TCP and UDP sockets. |
| 631 |
|
| 632 |
Examples: |
| 633 |
|
| 634 |
(make-instance 'inet-socket :type :stream :protocol :tcp) |
| 635 |
|
| 636 |
(make-instance 'inet-socket :type :datagram :protocol :udp) |
| 637 |
")) |
| 638 |
|
| 639 |
(defun make-inet-socket (type protocol) |
| 640 |
"Make an INET socket. Deprecated in favour of make-instance" |
| 641 |
(make-instance 'inet-socket :type type :protocol protocol)) |
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
(defmethod socket-bind ((socket inet-socket) &rest address) |
| 646 |
(assert (= 2 (length address)) (address) "Socket-bind needs three parameters for inet sockets.") |
| 647 |
(let ((ip (first address)) |
| 648 |
(port (second address))) |
| 649 |
(if (= -1 |
| 650 |
(c-inline (port (aref ip 0) (aref ip 1) (aref ip 2) (aref ip 3) |
| 651 |
(socket-file-descriptor socket)) |
| 652 |
(:int :int :int :int :int :int) |
| 653 |
:int |
| 654 |
" |
| 655 |
{ |
| 656 |
struct sockaddr_in sockaddr; |
| 657 |
int output; |
| 658 |
ecl_disable_interrupts(); |
| 659 |
fill_inet_sockaddr(&sockaddr, #0, #1, #2, #3, #4); |
| 660 |
output = bind(#5,(struct sockaddr*)&sockaddr, sizeof(struct sockaddr_in)); |
| 661 |
ecl_enable_interrupts(); |
| 662 |
@(return) = output; |
| 663 |
}" |
| 664 |
:side-effects t)) |
| 665 |
(socket-error "bind")))) |
| 666 |
|
| 667 |
(defmethod socket-accept ((socket inet-socket)) |
| 668 |
(let ((sfd (socket-file-descriptor socket))) |
| 669 |
(multiple-value-bind (fd vector port) |
| 670 |
(c-inline (sfd) (:int) (values :int :object :int) |
| 671 |
"{ |
| 672 |
struct sockaddr_in sockaddr; |
| 673 |
socklen_t addr_len = (socklen_t)sizeof(struct sockaddr_in); |
| 674 |
int new_fd; |
| 675 |
|
| 676 |
ecl_disable_interrupts(); |
| 677 |
new_fd = accept(#0, (struct sockaddr*)&sockaddr, &addr_len); |
| 678 |
ecl_enable_interrupts(); |
| 679 |
|
| 680 |
@(return 0) = new_fd; |
| 681 |
@(return 1) = ECL_NIL; |
| 682 |
@(return 2) = 0; |
| 683 |
if (new_fd != -1) { |
| 684 |
uint32_t ip = ntohl(sockaddr.sin_addr.s_addr); |
| 685 |
uint16_t port = ntohs(sockaddr.sin_port); |
| 686 |
cl_object vector = cl_make_array(1,ecl_make_fixnum(4)); |
| 687 |
|
| 688 |
ecl_aset(vector,0, ecl_make_fixnum( ip>>24 )); |
| 689 |
ecl_aset(vector,1, ecl_make_fixnum( (ip>>16) & 0xFF)); |
| 690 |
ecl_aset(vector,2, ecl_make_fixnum( (ip>>8) & 0xFF)); |
| 691 |
ecl_aset(vector,3, ecl_make_fixnum( ip & 0xFF )); |
| 692 |
|
| 693 |
@(return 1) = vector; |
| 694 |
@(return 2) = port; |
| 695 |
} |
| 696 |
}") |
| 697 |
(cond |
| 698 |
((= fd -1) |
| 699 |
(socket-error "accept")) |
| 700 |
(t |
| 701 |
(values |
| 702 |
(make-instance (class-of socket) |
| 703 |
:type (socket-type socket) |
| 704 |
:protocol (socket-protocol socket) |
| 705 |
:descriptor fd) |
| 706 |
vector |
| 707 |
port)))))) |
| 708 |
|
| 709 |
(defmethod socket-connect ((socket inet-socket) &rest address) |
| 710 |
(let ((ip (first address)) |
| 711 |
(port (second address))) |
| 712 |
(if (= -1 |
| 713 |
(c-inline (port (aref ip 0) (aref ip 1) (aref ip 2) (aref ip 3) |
| 714 |
(socket-file-descriptor socket)) |
| 715 |
(:int :int :int :int :int :int) |
| 716 |
:int |
| 717 |
" |
| 718 |
{ |
| 719 |
struct sockaddr_in sockaddr; |
| 720 |
int output; |
| 721 |
|
| 722 |
ecl_disable_interrupts(); |
| 723 |
fill_inet_sockaddr(&sockaddr, #0, #1, #2, #3, #4); |
| 724 |
output = connect(#5,(struct sockaddr*)&sockaddr, sizeof(struct sockaddr_in)); |
| 725 |
ecl_enable_interrupts(); |
| 726 |
|
| 727 |
@(return) = output; |
| 728 |
}")) |
| 729 |
(socket-error "connect")))) |
| 730 |
|
| 731 |
(defmethod socket-peername ((socket inet-socket)) |
| 732 |
(let* ((vector (make-array 4)) |
| 733 |
(fd (socket-file-descriptor socket)) |
| 734 |
(port (c-inline (fd vector) (:int t) :int |
| 735 |
"@01;{ |
| 736 |
struct sockaddr_in name; |
| 737 |
socklen_t len = sizeof(struct sockaddr_in); |
| 738 |
int ret; |
| 739 |
|
| 740 |
ecl_disable_interrupts(); |
| 741 |
ret = getpeername(#0,(struct sockaddr*)&name,&len); |
| 742 |
ecl_enable_interrupts(); |
| 743 |
|
| 744 |
if (ret == 0) { |
| 745 |
uint32_t ip = ntohl(name.sin_addr.s_addr); |
| 746 |
uint16_t port = ntohs(name.sin_port); |
| 747 |
|
| 748 |
ecl_aset(#1,0, ecl_make_fixnum( ip>>24 )); |
| 749 |
ecl_aset(#1,1, ecl_make_fixnum( (ip>>16) & 0xFF)); |
| 750 |
ecl_aset(#1,2, ecl_make_fixnum( (ip>>8) & 0xFF)); |
| 751 |
ecl_aset(#1,3, ecl_make_fixnum( ip & 0xFF )); |
| 752 |
|
| 753 |
@(return) = port; |
| 754 |
} else { |
| 755 |
@(return) = -1; |
| 756 |
} |
| 757 |
}"))) |
| 758 |
(if (>= port 0) |
| 759 |
(values vector port) |
| 760 |
(socket-error "getpeername")))) |
| 761 |
|
| 762 |
(defmethod socket-name ((socket inet-socket)) |
| 763 |
(let* ((vector (make-array 4)) |
| 764 |
(fd (socket-file-descriptor socket)) |
| 765 |
(port (c-inline (fd vector) (:int t) :int |
| 766 |
"@01;{ |
| 767 |
struct sockaddr_in name; |
| 768 |
socklen_t len = sizeof(struct sockaddr_in); |
| 769 |
int ret; |
| 770 |
|
| 771 |
ecl_disable_interrupts(); |
| 772 |
ret = getsockname(#0,(struct sockaddr*)&name,&len); |
| 773 |
ecl_enable_interrupts(); |
| 774 |
|
| 775 |
if (ret == 0) { |
| 776 |
uint32_t ip = ntohl(name.sin_addr.s_addr); |
| 777 |
uint16_t port = ntohs(name.sin_port); |
| 778 |
|
| 779 |
ecl_aset(#1,0, ecl_make_fixnum( ip>>24 )); |
| 780 |
ecl_aset(#1,1, ecl_make_fixnum( (ip>>16) & 0xFF)); |
| 781 |
ecl_aset(#1,2, ecl_make_fixnum( (ip>>8) & 0xFF)); |
| 782 |
ecl_aset(#1,3, ecl_make_fixnum( ip & 0xFF )); |
| 783 |
|
| 784 |
@(return) = port; |
| 785 |
} else { |
| 786 |
@(return) = -1; |
| 787 |
} |
| 788 |
}"))) |
| 789 |
(if (>= port 0) |
| 790 |
(values vector port) |
| 791 |
(socket-error "getsockname")))) |
| 792 |
|
| 793 |
#+:wsock |
| 794 |
(defmethod socket-close-low-level ((socket inet-socket)) |
| 795 |
(ff-closesocket (socket-file-descriptor socket))) |
| 796 |
|
| 797 |
(defmethod socket-send ((socket socket) buffer length |
| 798 |
&key address external-format oob eor dontroute dontwait nosignal confirm more) |
| 799 |
(declare (ignore external-format more)) |
| 800 |
(assert (or (stringp buffer) |
| 801 |
(typep buffer 'vector))) |
| 802 |
(let (;eh, here goes string->octet convertion... |
| 803 |
;When will ecl support Unicode? |
| 804 |
(length (or length (length buffer))) |
| 805 |
(fd (socket-file-descriptor socket))) |
| 806 |
(let ((len-sent |
| 807 |
(if address |
| 808 |
(progn |
| 809 |
(assert (= 2 (length address))) |
| 810 |
(c-inline (fd buffer length |
| 811 |
(second address) |
| 812 |
(aref (first address) 0) |
| 813 |
(aref (first address) 1) |
| 814 |
(aref (first address) 2) |
| 815 |
(aref (first address) 3) |
| 816 |
oob eor dontroute dontwait nosignal confirm) |
| 817 |
(:int :object :int |
| 818 |
:int :int :int :int :int |
| 819 |
:bool :bool :bool :bool :bool :bool) |
| 820 |
:long |
| 821 |
" |
| 822 |
{ |
| 823 |
int sock = #0; |
| 824 |
int length = #2; |
| 825 |
void *buffer = safe_buffer_pointer(#1, length); |
| 826 |
int flags = ( #8 ? MSG_OOB : 0 ) | |
| 827 |
( #9 ? MSG_EOR : 0 ) | |
| 828 |
( #a ? MSG_DONTROUTE : 0 ) | |
| 829 |
( #b ? MSG_DONTWAIT : 0 ) | |
| 830 |
( #c ? MSG_NOSIGNAL : 0 ) | |
| 831 |
( #d ? MSG_CONFIRM : 0 ); |
| 832 |
cl_type type = ecl_t_of(#1); |
| 833 |
struct sockaddr_in sockaddr; |
| 834 |
ssize_t len; |
| 835 |
|
| 836 |
ecl_disable_interrupts(); |
| 837 |
fill_inet_sockaddr(&sockaddr, #3, #4, #5, #6, #7); |
| 838 |
##if (MSG_NOSIGNAL == 0) && defined(SO_NOSIGPIPE) |
| 839 |
{ |
| 840 |
int sockopt = #c; |
| 841 |
setsockopt(#0,SOL_SOCKET,SO_NOSIGPIPE, |
| 842 |
wincoerce(char *,&sockopt), |
| 843 |
sizeof(int)); |
| 844 |
} |
| 845 |
##endif |
| 846 |
len = sendto(sock, wincoerce(char *,buffer), |
| 847 |
length, flags,(struct sockaddr*)&sockaddr, |
| 848 |
sizeof(struct sockaddr_in)); |
| 849 |
ecl_enable_interrupts(); |
| 850 |
@(return) = len; |
| 851 |
} |
| 852 |
" |
| 853 |
:one-liner nil)) |
| 854 |
(c-inline (fd buffer length |
| 855 |
oob eor dontroute dontwait nosignal confirm) |
| 856 |
(:int :object :int |
| 857 |
:bool :bool :bool :bool :bool :bool) |
| 858 |
:long |
| 859 |
" |
| 860 |
{ |
| 861 |
int sock = #0; |
| 862 |
int length = #2; |
| 863 |
void *buffer = safe_buffer_pointer(#1, length); |
| 864 |
int flags = ( #3 ? MSG_OOB : 0 ) | |
| 865 |
( #4 ? MSG_EOR : 0 ) | |
| 866 |
( #5 ? MSG_DONTROUTE : 0 ) | |
| 867 |
( #6 ? MSG_DONTWAIT : 0 ) | |
| 868 |
( #7 ? MSG_NOSIGNAL : 0 ) | |
| 869 |
( #8 ? MSG_CONFIRM : 0 ); |
| 870 |
cl_type type = ecl_t_of(#1); |
| 871 |
ssize_t len; |
| 872 |
ecl_disable_interrupts(); |
| 873 |
##if (MSG_NOSIGNAL == 0) && defined(SO_NOSIGPIPE) |
| 874 |
{ |
| 875 |
int sockopt = #7; |
| 876 |
setsockopt(#0,SOL_SOCKET,SO_NOSIGPIPE, |
| 877 |
wincoerce(char *,&sockopt), |
| 878 |
sizeof(int)); |
| 879 |
} |
| 880 |
##endif |
| 881 |
len = send(sock, wincoerce(char *, buffer), length, flags); |
| 882 |
ecl_enable_interrupts(); |
| 883 |
@(return) = len; |
| 884 |
} |
| 885 |
" |
| 886 |
:one-liner nil)))) |
| 887 |
(if (= len-sent -1) |
| 888 |
(socket-error "send") |
| 889 |
len-sent)))) |
| 890 |
|
| 891 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 892 |
;;; |
| 893 |
;;; UNIX SOCKETS |
| 894 |
;;; |
| 895 |
|
| 896 |
#-:wsock |
| 897 |
(progn |
| 898 |
|
| 899 |
(defclass local-socket (socket) |
| 900 |
((family :initform +af-local+)) |
| 901 |
(:documentation "Class representing local domain (AF_LOCAL) sockets, |
| 902 |
also known as unix-domain sockets.")) |
| 903 |
|
| 904 |
|
| 905 |
(defmethod socket-bind ((socket local-socket) &rest address) |
| 906 |
(assert (= 1 (length address)) (address) "Socket-bind needs two parameters for local sockets.") |
| 907 |
(let ((name (first address)) |
| 908 |
(fd (socket-file-descriptor socket)) |
| 909 |
(family (socket-family socket))) |
| 910 |
(if (= -1 |
| 911 |
(c-inline (fd name family) (:int :cstring :int) :int |
| 912 |
" |
| 913 |
{ |
| 914 |
struct sockaddr_un sockaddr; |
| 915 |
size_t size; |
| 916 |
int output; |
| 917 |
##ifdef BSD |
| 918 |
sockaddr.sun_len = sizeof(struct sockaddr_un); |
| 919 |
##endif |
| 920 |
sockaddr.sun_family = #2; |
| 921 |
strncpy(sockaddr.sun_path,#1,sizeof(sockaddr.sun_path)); |
| 922 |
sockaddr.sun_path[sizeof(sockaddr.sun_path)-1] = 0; |
| 923 |
|
| 924 |
ecl_disable_interrupts(); |
| 925 |
output = bind(#0,(struct sockaddr*)&sockaddr, sizeof(struct sockaddr_un)); |
| 926 |
ecl_enable_interrupts(); |
| 927 |
|
| 928 |
@(return) = output; |
| 929 |
}")) |
| 930 |
(socket-error "bind")))) |
| 931 |
|
| 932 |
(defmethod socket-accept ((socket local-socket)) |
| 933 |
(multiple-value-bind (fd name) |
| 934 |
(c-inline ((socket-file-descriptor socket)) (:int) (values :int :object) |
| 935 |
"{ |
| 936 |
struct sockaddr_un sockaddr; |
| 937 |
socklen_t addr_len = (socklen_t)sizeof(struct sockaddr_un); |
| 938 |
int new_fd; |
| 939 |
ecl_disable_interrupts(); |
| 940 |
new_fd = accept(#0, (struct sockaddr *)&sockaddr, &addr_len); |
| 941 |
ecl_enable_interrupts(); |
| 942 |
@(return 0) = new_fd; |
| 943 |
@(return 1) = (new_fd == -1) ? ECL_NIL : ecl_make_simple_base_string(sockaddr.sun_path,-1); |
| 944 |
}") |
| 945 |
(cond |
| 946 |
((= fd -1) |
| 947 |
(socket-error "accept")) |
| 948 |
(t |
| 949 |
(values |
| 950 |
(make-instance (class-of socket) |
| 951 |
:type (socket-type socket) |
| 952 |
:protocol (socket-protocol socket) |
| 953 |
:descriptor fd) |
| 954 |
name))))) |
| 955 |
|
| 956 |
(defmethod socket-connect ((socket local-socket) &rest address) |
| 957 |
(assert (= 1 (length address)) (address) "Socket-connect needs two parameters for local sockets.") |
| 958 |
(let ((path (first address)) |
| 959 |
(fd (socket-file-descriptor socket)) |
| 960 |
(family (socket-family socket))) |
| 961 |
(if (= -1 |
| 962 |
(c-inline (fd family path) (:int :int :cstring) :int |
| 963 |
" |
| 964 |
{ |
| 965 |
struct sockaddr_un sockaddr; |
| 966 |
int output; |
| 967 |
##ifdef BSD |
| 968 |
sockaddr.sun_len = sizeof(struct sockaddr_un); |
| 969 |
##endif |
| 970 |
sockaddr.sun_family = #1; |
| 971 |
strncpy(sockaddr.sun_path,#2,sizeof(sockaddr.sun_path)); |
| 972 |
sockaddr.sun_path[sizeof(sockaddr.sun_path)-1] = 0; |
| 973 |
|
| 974 |
ecl_disable_interrupts(); |
| 975 |
output = connect(#0,(struct sockaddr*)&sockaddr, sizeof(struct sockaddr_un)); |
| 976 |
ecl_enable_interrupts(); |
| 977 |
|
| 978 |
@(return) = output; |
| 979 |
}")) |
| 980 |
(socket-error "connect")))) |
| 981 |
|
| 982 |
(defmethod socket-peername ((socket local-socket)) |
| 983 |
(let* ((fd (socket-file-descriptor socket)) |
| 984 |
(peer (c-inline (fd) (:int) t |
| 985 |
" |
| 986 |
{ |
| 987 |
struct sockaddr_un name; |
| 988 |
socklen_t len = sizeof(struct sockaddr_un); |
| 989 |
int ret; |
| 990 |
|
| 991 |
ecl_disable_interrupts(); |
| 992 |
ret = getpeername(#0,(struct sockaddr*)&name,&len); |
| 993 |
ecl_enable_interrupts(); |
| 994 |
|
| 995 |
if (ret == 0) { |
| 996 |
@(return) = ecl_make_simple_base_string(name.sun_path,-1); |
| 997 |
} else { |
| 998 |
@(return) = ECL_NIL; |
| 999 |
} |
| 1000 |
}"))) |
| 1001 |
(if peer |
| 1002 |
peer |
| 1003 |
(socket-error "getpeername")))) |
| 1004 |
|
| 1005 |
) ;#-:wsock |
| 1006 |
|
| 1007 |
|
| 1008 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 1009 |
;;; |
| 1010 |
;;; UNIX SOCKETS [WIN32, using the cygwin way] |
| 1011 |
;;; |
| 1012 |
|
| 1013 |
#+:wsock |
| 1014 |
(progn |
| 1015 |
|
| 1016 |
(defclass local-socket (socket) |
| 1017 |
((family :initform +af-local+) |
| 1018 |
proxy-socket local-path) |
| 1019 |
(:documentation "Class representing local domain (AF_LOCAL) sockets, |
| 1020 |
also known as unix-domain sockets.")) |
| 1021 |
|
| 1022 |
(defmethod initialize-instance :after ((socket local-socket) &rest args) |
| 1023 |
(declare (ignore args)) |
| 1024 |
(with-slots (protocol type) socket |
| 1025 |
(setf (slot-value socket 'proxy-socket) |
| 1026 |
(make-instance 'inet-socket :protocol protocol :type type)))) |
| 1027 |
|
| 1028 |
(defmethod socket-bind ((socket local-socket) &rest address) |
| 1029 |
(assert (= 1 (length address)) (address) "Socket-bind needs two parameters for local sockets.") |
| 1030 |
(with-slots (proxy-socket local-path) socket |
| 1031 |
(socket-bind proxy-socket #(127 0 0 1) 0) |
| 1032 |
(multiple-value-bind (ip port) (socket-peername proxy-socket) |
| 1033 |
(handler-case |
| 1034 |
(with-open-file (fd (first address) :if-exists :error :if-does-not-exist :create :direction :output) |
| 1035 |
(format fd "!<socket >~D 00000000-00000000-00000000-00000000" port)) |
| 1036 |
(file-error () |
| 1037 |
(socket-close proxy-socket) |
| 1038 |
(c-inline () () nil "WSASetLastError(WSAEADDRINUSE)" :one-liner t) |
| 1039 |
(socket-error "socket-bind"))) |
| 1040 |
(setf local-path (first address)) |
| 1041 |
socket))) |
| 1042 |
|
| 1043 |
(defmethod socket-accept ((socket local-socket)) |
| 1044 |
(multiple-value-bind (new-socket addr) (socket-accept (slot-value socket 'proxy-socket)) |
| 1045 |
(values socket (slot-value socket 'local-path)))) |
| 1046 |
|
| 1047 |
(defmethod socket-connect ((socket local-socket) &rest address) |
| 1048 |
(assert (= 1 (length address)) (address) "Socket-connect needs two parameters for local sockets.") |
| 1049 |
(with-slots (proxy-socket local-path) socket |
| 1050 |
(handler-case |
| 1051 |
(with-open-file (fd (first address) :if-does-not-exist :error :direction :input) |
| 1052 |
(let ((buf (make-string 128)) port) |
| 1053 |
(read-sequence buf fd) |
| 1054 |
(unless (and (string-equal "!<socket >" (subseq buf 0 10)) |
| 1055 |
(typep (setq port (read-from-string (subseq buf 10) nil 'eof)) '(integer 0 65535))) |
| 1056 |
(c-inline () () nil "WSASetLastError(WSAEFAULT)" :one-liner t) |
| 1057 |
(socket-error "connect")) |
| 1058 |
(prog1 |
| 1059 |
(socket-connect proxy-socket #(127 0 0 1) port) |
| 1060 |
(setf local-path (first address))))) |
| 1061 |
(file-error () |
| 1062 |
(socket-error "connect"))))) |
| 1063 |
|
| 1064 |
(defmethod socket-peername ((socket local-socket)) |
| 1065 |
(unless (slot-boundp socket 'local-path) |
| 1066 |
(c-inline () () nil "WSASetLastError(WSAENOTCONN)" :one-liner t) |
| 1067 |
(socket-error "socket-peername")) |
| 1068 |
(slot-value socket 'local-path)) |
| 1069 |
|
| 1070 |
(defmethod socket-close ((socket local-socket) &key abort) |
| 1071 |
(socket-close (slot-value socket 'proxy-socket) :abort abort) |
| 1072 |
(slot-makunbound socket 'local-path)) |
| 1073 |
|
| 1074 |
(defmethod socket-make-stream ((socket local-socket) &rest args) |
| 1075 |
(apply #'socket-make-stream (cons (slot-value socket 'proxy-socket) args))) |
| 1076 |
|
| 1077 |
(defmethod non-blocking-mode ((socket local-socket)) |
| 1078 |
(non-blocking-mode (slot-value socket 'proxy-socket))) |
| 1079 |
|
| 1080 |
(defmethod (setf non-blocking-mode) (non-blocking-p (socket local-socket)) |
| 1081 |
(setf (non-blocking-mode (slot-value socket 'proxy-socket)) non-blocking-p)) |
| 1082 |
|
| 1083 |
) ;#+:wsock |
| 1084 |
|
| 1085 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 1086 |
;;; |
| 1087 |
;;; NAMED PIPE SOCKETS [WIN32] |
| 1088 |
;;; |
| 1089 |
|
| 1090 |
#+:wsock |
| 1091 |
(progn |
| 1092 |
|
| 1093 |
(defclass named-pipe-socket (socket) |
| 1094 |
((family :initform +af-named-pipe+) |
| 1095 |
(pipe-name :initarg :pipe-name)) |
| 1096 |
(:documentation "Class representing Win32 named pipe, using a socket-like interface.")) |
| 1097 |
|
| 1098 |
(defmethod socket-bind ((socket named-pipe-socket) &rest address) |
| 1099 |
(assert (= 1 (length address)) (address) "Socket-bind needs two parameters for local sockets.") |
| 1100 |
(let* ((pipe-name (concatenate 'string "\\\\.\\pipe\\" (first address))) |
| 1101 |
(hnd (c-inline (pipe-name) (:cstring) :int |
| 1102 |
" |
| 1103 |
{ |
| 1104 |
HANDLE hnd; |
| 1105 |
ecl_disable_interrupts(); |
| 1106 |
hnd = CreateNamedPipe( |
| 1107 |
#0, |
| 1108 |
PIPE_ACCESS_DUPLEX, |
| 1109 |
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT, |
| 1110 |
PIPE_UNLIMITED_INSTANCES, |
| 1111 |
4096, |
| 1112 |
4096, |
| 1113 |
NMPWAIT_USE_DEFAULT_WAIT, |
| 1114 |
NULL); |
| 1115 |
ecl_enable_interrupts(); |
| 1116 |
if (hnd == INVALID_HANDLE_VALUE) |
| 1117 |
@(return) = -1; |
| 1118 |
else |
| 1119 |
@(return) = _open_osfhandle((intptr_t)hnd, O_RDWR); |
| 1120 |
}"))) |
| 1121 |
(when (= hnd -1) |
| 1122 |
(socket-error "CreateNamedPipe")) |
| 1123 |
(setf (slot-value socket 'pipe-name) pipe-name) |
| 1124 |
(setf (slot-value socket 'file-descriptor) hnd))) |
| 1125 |
|
| 1126 |
(defmethod socket-accept ((socket named-pipe-socket)) |
| 1127 |
(let* ((fd (socket-file-descriptor socket)) |
| 1128 |
(afd (c-inline (fd) (:int) :int |
| 1129 |
" |
| 1130 |
{ |
| 1131 |
HANDLE hnd = (HANDLE)_get_osfhandle(#0), dupHnd; |
| 1132 |
ecl_disable_interrupts(); |
| 1133 |
if (ConnectNamedPipe(hnd, NULL) != 0 || GetLastError() == ERROR_PIPE_CONNECTED) { |
| 1134 |
@(return) = #0; |
| 1135 |
} else |
| 1136 |
@(return) = -1; |
| 1137 |
ecl_enable_interrupts(); |
| 1138 |
}" |
| 1139 |
:one-liner nil))) |
| 1140 |
(cond |
| 1141 |
((= afd -1) |
| 1142 |
(socket-error "accept")) |
| 1143 |
(t |
| 1144 |
;; rebind the socket to create a new named pipe instance in the server |
| 1145 |
(socket-bind socket (subseq (slot-value socket 'pipe-name) 9)) |
| 1146 |
(values |
| 1147 |
(make-instance (class-of socket) |
| 1148 |
:type (socket-type socket) |
| 1149 |
:protocol (socket-protocol socket) |
| 1150 |
:descriptor afd |
| 1151 |
:pipe-name (slot-value socket 'pipe-name)) |
| 1152 |
(slot-value socket 'pipe-name)))))) |
| 1153 |
|
| 1154 |
(defmethod socket-connect ((socket named-pipe-socket) &rest address) |
| 1155 |
(assert (= 1 (length address)) (address) "Socket-connect needs two parameters for local sockets.") |
| 1156 |
(let* ((path (first address)) |
| 1157 |
(pipe-name (concatenate 'string "\\\\.\\pipe\\" path))) |
| 1158 |
(if (= -1 |
| 1159 |
(setf (slot-value socket 'file-descriptor) |
| 1160 |
(c-inline (pipe-name) (:cstring) :int |
| 1161 |
" |
| 1162 |
{ |
| 1163 |
HANDLE hnd; |
| 1164 |
ecl_disable_interrupts(); |
| 1165 |
hnd = CreateFile( |
| 1166 |
#0, |
| 1167 |
GENERIC_READ | GENERIC_WRITE, |
| 1168 |
0, |
| 1169 |
NULL, |
| 1170 |
OPEN_EXISTING, |
| 1171 |
0, |
| 1172 |
NULL); |
| 1173 |
if (hnd == INVALID_HANDLE_VALUE) |
| 1174 |
@(return) = -1; |
| 1175 |
else |
| 1176 |
@(return) = _open_osfhandle((intptr_t)hnd, O_RDWR); |
| 1177 |
ecl_enable_interrupts(); |
| 1178 |
}"))) |
| 1179 |
(socket-error "connect") |
| 1180 |
(setf (slot-value socket 'pipe-name) pipe-name)))) |
| 1181 |
|
| 1182 |
(defmethod socket-peername ((socket named-pipe-socket)) |
| 1183 |
(slot-value socket 'pipe-name)) |
| 1184 |
|
| 1185 |
(defmethod (setf non-blocking-mode) (non-blocking-p (socket named-pipe-socket)) |
| 1186 |
(let ((fd (socket-file-descriptor socket))) |
| 1187 |
(if (= 0 |
| 1188 |
(c-inline (fd non-blocking-p) (:int t) :int |
| 1189 |
" |
| 1190 |
{ |
| 1191 |
DWORD mode = PIPE_READMODE_BYTE | (#1 == ECL_T ? PIPE_NOWAIT : PIPE_WAIT); |
| 1192 |
HANDLE h = (HANDLE)_get_osfhandle(#0); |
| 1193 |
ecl_disable_interrupts(); |
| 1194 |
@(return) = SetNamedPipeHandleState(h, &mode, NULL, NULL); |
| 1195 |
ecl_enable_interrupts(); |
| 1196 |
}" |
| 1197 |
:one-liner nil)) |
| 1198 |
(socket-error "SetNamedPipeHandleState") |
| 1199 |
(setf (slot-value socket 'non-blocking-p) non-blocking-p)))) |
| 1200 |
|
| 1201 |
(defmethod socket-close ((socket named-pipe-socket) &key abort) |
| 1202 |
(declare (ignore abort)) |
| 1203 |
(let ((fd (socket-file-descriptor socket))) |
| 1204 |
(unless (c-inline (fd) (:int) t |
| 1205 |
" |
| 1206 |
{ |
| 1207 |
DWORD flags; |
| 1208 |
HANDLE h = (HANDLE)_get_osfhandle(#0); |
| 1209 |
ecl_disable_interrupts(); |
| 1210 |
if (!GetNamedPipeInfo(h, &flags, NULL, NULL, NULL)) |
| 1211 |
@(return) = ECL_NIL; |
| 1212 |
if (flags == PIPE_CLIENT_END || DisconnectNamedPipe(h)) |
| 1213 |
@(return) = ECL_T; |
| 1214 |
else |
| 1215 |
@(return) = ECL_NIL; |
| 1216 |
ecl_enable_interrupts(); |
| 1217 |
}" |
| 1218 |
:one-liner nil) |
| 1219 |
(socket-error "DisconnectNamedPipe")) |
| 1220 |
(call-next-method))) |
| 1221 |
|
| 1222 |
) ;#+:wsock |
| 1223 |
|
| 1224 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 1225 |
;;; |
| 1226 |
;;; NON-BLOCKING MODE |
| 1227 |
;;; |
| 1228 |
|
| 1229 |
(defmethod non-blocking-mode ((socket socket)) |
| 1230 |
#-:wsock |
| 1231 |
(let ((fd (socket-file-descriptor socket))) |
| 1232 |
(not (zerop (c-inline (fd) (:int) :int "fcntl(#0,F_GETFL,NULL)&O_NONBLOCK" :one-liner t)))) |
| 1233 |
#+:wsock |
| 1234 |
(slot-value socket 'non-blocking-p) |
| 1235 |
) |
| 1236 |
|
| 1237 |
(defmethod (setf non-blocking-mode) (non-blocking-p (socket socket)) |
| 1238 |
(let ((fd (socket-file-descriptor socket)) |
| 1239 |
(nblock (if non-blocking-p 1 0))) |
| 1240 |
(if (= -1 (c-inline (fd nblock) (:int :int) :int |
| 1241 |
#+:wsock |
| 1242 |
" |
| 1243 |
{ |
| 1244 |
int blocking_flag = (#1 ? 1 : 0); |
| 1245 |
ecl_disable_interrupts(); |
| 1246 |
@(return) = ioctlsocket(#0, FIONBIO, (u_long*)&blocking_flag); |
| 1247 |
ecl_enable_interrupts(); |
| 1248 |
}" |
| 1249 |
#-:wsock |
| 1250 |
" |
| 1251 |
{ |
| 1252 |
int oldflags = fcntl(#0,F_GETFL,NULL); |
| 1253 |
int newflags = (oldflags & ~O_NONBLOCK) | |
| 1254 |
(#1 ? O_NONBLOCK : 0); |
| 1255 |
ecl_disable_interrupts(); |
| 1256 |
@(return) = fcntl(#0,F_SETFL,newflags); |
| 1257 |
ecl_enable_interrupts(); |
| 1258 |
}")) |
| 1259 |
(socket-error #-:wsock "fcntl" #+:wsock "ioctlsocket") |
| 1260 |
#-:wsock non-blocking-p |
| 1261 |
#+:wsock (setf (slot-value socket 'non-blocking-p) non-blocking-p)))) |
| 1262 |
|
| 1263 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 1264 |
;;; |
| 1265 |
;;; STREAMS |
| 1266 |
;;; |
| 1267 |
;;; To actually read/write to/from the sockets, we use Lisp streams. The |
| 1268 |
;;; following functions take care of building the streams. Fortunately |
| 1269 |
;;; we do not have to care about things like buffering, binary streams, |
| 1270 |
;;; etc, but we rather reuse the code from the C core. (For instance |
| 1271 |
;;; the sockets will be closed upon garbage collection) |
| 1272 |
;;; |
| 1273 |
|
| 1274 |
(defun dup (fd) |
| 1275 |
(ffi:c-inline (fd) (:int) :int "dup(#0)" :one-liner t)) |
| 1276 |
|
| 1277 |
(defun auto-close-two-way-stream (stream) |
| 1278 |
(declare (si::c-local)) |
| 1279 |
(ffi:c-inline (stream) (t) :void |
| 1280 |
"(#0)->stream.flags |= ECL_STREAM_CLOSE_COMPONENTS" |
| 1281 |
:one-liner t)) |
| 1282 |
|
| 1283 |
(defun socket-make-stream-inner (fd input output buffering element-type external-format) |
| 1284 |
;; In Unix we have to create one stream per channel. The reason is |
| 1285 |
;; that buffered I/O is done using ANSI C FILEs which do not support |
| 1286 |
;; concurrent reads and writes -- if one thread is listening to the |
| 1287 |
;; FILE it blocks all output. The solution is to create a |
| 1288 |
;; two-way-stream when both input and output are T, and force that |
| 1289 |
;; stream to close its components (small hack in ECL). In Windows we |
| 1290 |
;; do not have this problem because we do not know how to wrap a |
| 1291 |
;; FILE around a socket. |
| 1292 |
(cond ((and input output) |
| 1293 |
#+wsock |
| 1294 |
(make-stream-from-fd fd :input-output-wsock |
| 1295 |
:buffering buffering |
| 1296 |
:element-type element-type |
| 1297 |
:external-format external-format) |
| 1298 |
#-wsock |
| 1299 |
(let* ((in (socket-make-stream-inner (dup fd) t nil buffering |
| 1300 |
element-type external-format)) |
| 1301 |
(out (socket-make-stream-inner fd nil t buffering |
| 1302 |
element-type external-format)) |
| 1303 |
(stream (make-two-way-stream in out))) |
| 1304 |
(auto-close-two-way-stream stream) |
| 1305 |
stream)) |
| 1306 |
(input |
| 1307 |
(make-stream-from-fd fd #-wsock :input #+wsock :input-wsock |
| 1308 |
:buffering buffering |
| 1309 |
:element-type element-type |
| 1310 |
:external-format external-format)) |
| 1311 |
(output |
| 1312 |
(make-stream-from-fd fd #-wsock :output #+wsock :output-wsock |
| 1313 |
:buffering buffering |
| 1314 |
:element-type element-type |
| 1315 |
:external-format external-format)) |
| 1316 |
(t |
| 1317 |
(error "SOCKET-MAKE-STREAM: at least one of :INPUT or :OUTPUT has to be true.")))) |
| 1318 |
|
| 1319 |
(defmethod socket-make-stream ((socket socket) |
| 1320 |
&key (input nil input-p) |
| 1321 |
(output nil output-p) |
| 1322 |
(buffering :full) |
| 1323 |
(element-type 'base-char) |
| 1324 |
(external-format :default)) |
| 1325 |
(let ((stream (and (slot-boundp socket 'stream) |
| 1326 |
(slot-value socket 'stream)))) |
| 1327 |
(unless stream |
| 1328 |
;; Complicated default logic for compatibility with previous releases |
| 1329 |
;; should disappear soon. (FIXME!) |
| 1330 |
(unless (or input-p output-p) |
| 1331 |
(setf input t output t)) |
| 1332 |
(setf stream (socket-make-stream-inner (socket-file-descriptor socket) |
| 1333 |
input output buffering element-type |
| 1334 |
external-format)) |
| 1335 |
(setf (slot-value socket 'stream) stream) |
| 1336 |
#+ ignore |
| 1337 |
(sb-ext:cancel-finalization socket)) |
| 1338 |
stream)) |
| 1339 |
|
| 1340 |
(defmethod ext::stream-fd ((socket socket)) |
| 1341 |
(socket-file-descriptor socket)) |
| 1342 |
|
| 1343 |
#+:wsock |
| 1344 |
(defmethod socket-make-stream ((socket named-pipe-socket) |
| 1345 |
&key input output |
| 1346 |
(buffering :full) (external-format :default)) |
| 1347 |
(let ((stream (and (slot-boundp socket 'stream) |
| 1348 |
(slot-value socket 'stream)))) |
| 1349 |
(unless stream |
| 1350 |
(setf stream |
| 1351 |
(let* ((fd (socket-file-descriptor socket)) |
| 1352 |
(in (make-stream-from-fd fd :smm-input buffering external-format)) |
| 1353 |
(out (make-stream-from-fd fd :smm-output buffering external-format))) |
| 1354 |
(make-two-way-stream in out))) |
| 1355 |
(setf (slot-value socket 'stream) stream)) |
| 1356 |
stream)) |
| 1357 |
|
| 1358 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 1359 |
;;; |
| 1360 |
;;; ERROR HANDLING |
| 1361 |
;;; |
| 1362 |
;;; A plethora of conditions are defined below, almost one for each |
| 1363 |
;;; possible error produced by the socket or DNS interface. |
| 1364 |
;;; |
| 1365 |
|
| 1366 |
#+:wsock |
| 1367 |
(defun get-win32-error-string (num) |
| 1368 |
(c-inline (num) (:int) t |
| 1369 |
"{char *lpMsgBuf; |
| 1370 |
cl_object msg; |
| 1371 |
ecl_disable_interrupts(); |
| 1372 |
FormatMessage( |
| 1373 |
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, |
| 1374 |
NULL, |
| 1375 |
#0, |
| 1376 |
0, |
| 1377 |
(LPTSTR)&lpMsgBuf, |
| 1378 |
0, |
| 1379 |
NULL); |
| 1380 |
msg = ecl_make_simple_base_string(lpMsgBuf,-1); |
| 1381 |
LocalFree(lpMsgBuf); |
| 1382 |
ecl_enable_interrupts(); |
| 1383 |
@(return) = msg;}" |
| 1384 |
:one-liner nil)) |
| 1385 |
|
| 1386 |
;;; |
| 1387 |
;;; 1) SOCKET ERRORS |
| 1388 |
;;; |
| 1389 |
|
| 1390 |
(define-condition socket-error (error) |
| 1391 |
((errno :initform nil |
| 1392 |
:initarg :errno |
| 1393 |
:reader socket-error-errno) |
| 1394 |
(symbol :initform nil :initarg :symbol :reader socket-error-symbol) |
| 1395 |
(syscall :initform "outer space" :initarg :syscall :reader socket-error-syscall)) |
| 1396 |
(:report (lambda (c s) |
| 1397 |
(let ((num (socket-error-errno c))) |
| 1398 |
(format s "Socket error in \"~A\": ~A (~A)" |
| 1399 |
(socket-error-syscall c) |
| 1400 |
(or (socket-error-symbol c) (socket-error-errno c)) |
| 1401 |
#+:wsock |
| 1402 |
(get-win32-error-string num) |
| 1403 |
#-:wsock |
| 1404 |
(c-inline (num) (:int) :cstring |
| 1405 |
"strerror(#0)" :one-liner t))))) |
| 1406 |
(:documentation "Common base class of socket related conditions.")) |
| 1407 |
|
| 1408 |
(defmacro define-socket-condition (symbol name) |
| 1409 |
`(let () ; Prevents evaluation of constant value at compilation time |
| 1410 |
(defconstant ,symbol (c-constant ,(symbol-name symbol))) |
| 1411 |
(define-condition ,name (socket-error) |
| 1412 |
((symbol :reader socket-error-symbol :initform (quote ,symbol)))) |
| 1413 |
(export ',name) |
| 1414 |
(push (cons ,symbol (quote ,name)) *conditions-for-errno*))) |
| 1415 |
|
| 1416 |
(defparameter *conditions-for-errno* nil) |
| 1417 |
;;; this needs the rest of the list adding to it, really. They also |
| 1418 |
;;; need symbols to be added to constants.ccon |
| 1419 |
;;; I haven't yet thought of a non-kludgey way of keeping all this in |
| 1420 |
;;; the same place |
| 1421 |
#+:wsock |
| 1422 |
(Clines |
| 1423 |
"#define EADDRINUSE WSAEADDRINUSE" |
| 1424 |
"#define ECONNREFUSED WSAECONNREFUSED" |
| 1425 |
"#define ETIMEDOUT WSAETIMEDOUT" |
| 1426 |
"#define ENOBUFS WSAENOBUFS" |
| 1427 |
"#define EOPNOTSUPP WSAEOPNOTSUPP" |
| 1428 |
"#define EPROTONOSUPPORT WSAEPROTONOSUPPORT" |
| 1429 |
"#define ESOCKTNOSUPPORT WSAESOCKTNOSUPPORT" |
| 1430 |
"#define ENETUNREACH WSAENETUNREACH" |
| 1431 |
) |
| 1432 |
|
| 1433 |
#+:haiku |
| 1434 |
(clines |
| 1435 |
"#define ESOCKTNOSUPPORT ENOTSUP") |
| 1436 |
|
| 1437 |
(define-socket-condition EADDRINUSE address-in-use-error) |
| 1438 |
(define-socket-condition EAGAIN interrupted-error) |
| 1439 |
(define-socket-condition EBADF bad-file-descriptor-error) |
| 1440 |
(define-socket-condition ECONNREFUSED connection-refused-error) |
| 1441 |
(define-socket-condition ETIMEDOUT operation-timeout-error) |
| 1442 |
(define-socket-condition EINTR interrupted-error) |
| 1443 |
(define-socket-condition EINVAL invalid-argument-error) |
| 1444 |
(define-socket-condition ENOBUFS no-buffers-error) |
| 1445 |
(define-socket-condition ENOMEM out-of-memory-error) |
| 1446 |
(define-socket-condition EOPNOTSUPP operation-not-supported-error) |
| 1447 |
(define-socket-condition EPERM operation-not-permitted-error) |
| 1448 |
(define-socket-condition EPROTONOSUPPORT protocol-not-supported-error) |
| 1449 |
(define-socket-condition ESOCKTNOSUPPORT socket-type-not-supported-error) |
| 1450 |
(define-socket-condition ENETUNREACH network-unreachable-error) |
| 1451 |
|
| 1452 |
|
| 1453 |
(defun condition-for-errno (err) |
| 1454 |
(or (cdr (assoc err *conditions-for-errno* :test #'eql)) 'socket-error)) |
| 1455 |
|
| 1456 |
(defun socket-error (where) |
| 1457 |
(let* ((errno (c-constant #-:wsock "errno" #+:wsock "WSAGetLastError()")) |
| 1458 |
(condition (condition-for-errno errno))) |
| 1459 |
(error condition :errno errno :syscall where))) |
| 1460 |
|
| 1461 |
;;; |
| 1462 |
;;; 2) DNS ERRORS |
| 1463 |
;;; |
| 1464 |
|
| 1465 |
(clines |
| 1466 |
"#ifndef EAI_SYSTEM |
| 1467 |
#define EAI_SYSTEM 0 |
| 1468 |
#endif") |
| 1469 |
|
| 1470 |
(defun name-service-error (where &optional (errno #+:wsock (c-constant "WSAGetLastError()"))) |
| 1471 |
(if (= errno (c-constant "EAI_SYSTEM")) |
| 1472 |
(socket-error where) |
| 1473 |
(let ((condition |
| 1474 |
(condition-for-name-service-errno errno))) |
| 1475 |
(error condition :errno errno :syscall where)))) |
| 1476 |
|
| 1477 |
(define-condition name-service-error (condition) |
| 1478 |
((errno :initform nil |
| 1479 |
:initarg :errno |
| 1480 |
:reader name-service-error-errno) |
| 1481 |
(symbol :initform nil :initarg :symbol :reader name-service-error-symbol) |
| 1482 |
(syscall :initform "an unknown location" :initarg :syscall :reader name-service-error-syscall)) |
| 1483 |
(:report (lambda (c s) |
| 1484 |
(let ((num (name-service-error-errno c))) |
| 1485 |
(format s "Name service error in \"~A\": ~A (~A)" |
| 1486 |
(name-service-error-syscall c) |
| 1487 |
(or (name-service-error-symbol c) |
| 1488 |
(name-service-error-errno c)) |
| 1489 |
(get-name-service-error-message num)))))) |
| 1490 |
|
| 1491 |
(defmacro define-name-service-condition (symbol name) |
| 1492 |
`(let () |
| 1493 |
(defconstant ,symbol (c-constant ,(symbol-name symbol))) |
| 1494 |
(define-condition ,name (name-service-error) |
| 1495 |
((symbol :reader name-service-error-symbol :initform (quote ,symbol)))) |
| 1496 |
(push (cons ,symbol (quote ,name)) *conditions-for-name-service-errno*) |
| 1497 |
(export (quote ,symbol)))) |
| 1498 |
|
| 1499 |
(defparameter *conditions-for-name-service-errno* nil) |
| 1500 |
|
| 1501 |
;;; getaddrinfo/getnameinfo have more failure codes, but for |
| 1502 |
;;; compability with gethostbyname/gethostbyaddr, we only need the |
| 1503 |
;;; following |
| 1504 |
(define-name-service-condition EAI_NONAME host-not-found-error) |
| 1505 |
(define-name-service-condition EAI_AGAIN try-again-error) |
| 1506 |
(define-name-service-condition EAI_FAIL no-recovery-error) |
| 1507 |
|
| 1508 |
(defun condition-for-name-service-errno (err) |
| 1509 |
(or (cdr (assoc err *conditions-for-name-service-errno* :test #'eql)) |
| 1510 |
'name-service)) |
| 1511 |
|
| 1512 |
(defun get-name-service-error-message (num) |
| 1513 |
#+:wsock |
| 1514 |
(get-win32-error-string num) |
| 1515 |
#-:wsock |
| 1516 |
(c-inline (num) (:int) :cstring "gai_strerror(#0)" :one-liner t)) |
| 1517 |
|
| 1518 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 1519 |
;;; |
| 1520 |
;;; SOCKET OPTIONS |
| 1521 |
;;; |
| 1522 |
|
| 1523 |
(defun get-sockopt-int (fd level const) |
| 1524 |
(let ((ret (c-inline (fd level const) (:int :int :int) t |
| 1525 |
"{ |
| 1526 |
int sockopt, ret; |
| 1527 |
socklen_t socklen = sizeof(int); |
| 1528 |
|
| 1529 |
ecl_disable_interrupts(); |
| 1530 |
ret = getsockopt(#0,#1,#2,wincoerce(char*,&sockopt),&socklen); |
| 1531 |
ecl_enable_interrupts(); |
| 1532 |
|
| 1533 |
@(return) = (ret == 0) ? ecl_make_integer(sockopt) : ECL_NIL; |
| 1534 |
}"))) |
| 1535 |
(if ret |
| 1536 |
ret |
| 1537 |
(error "Sockopt error: ~A" (c-inline () () :cstring "strerror(errno)" :one-liner t))))) |
| 1538 |
|
| 1539 |
(defun get-sockopt-bool (fd level const) |
| 1540 |
(let ((ret (c-inline (fd level const) (:int :int :int) t |
| 1541 |
"{ |
| 1542 |
int sockopt, ret; |
| 1543 |
socklen_t socklen = sizeof(int); |
| 1544 |
|
| 1545 |
ecl_disable_interrupts(); |
| 1546 |
ret = getsockopt(#0,#1,#2,wincoerce(char*,&sockopt),&socklen); |
| 1547 |
ecl_enable_interrupts(); |
| 1548 |
|
| 1549 |
@(return) = (ret == 0) ? ecl_make_integer(sockopt) : ECL_NIL; |
| 1550 |
}"))) |
| 1551 |
(if ret |
| 1552 |
(/= ret 0) |
| 1553 |
(error "Sockopt error: ~A" (c-inline () () :cstring "strerror(errno)" :one-liner t))))) |
| 1554 |
|
| 1555 |
#+wsock |
| 1556 |
(defun get-sockopt-timeval (fd level const) |
| 1557 |
(* 1000 (get-sockopt-int fd level const))) |
| 1558 |
|
| 1559 |
#-wsock |
| 1560 |
(defun get-sockopt-timeval (fd level const) |
| 1561 |
(let ((ret (c-inline (fd level const) (:int :int :int) t |
| 1562 |
"{ |
| 1563 |
struct timeval tv; |
| 1564 |
socklen_t socklen = sizeof(struct timeval); |
| 1565 |
int ret; |
| 1566 |
|
| 1567 |
ecl_disable_interrupts(); |
| 1568 |
ret = getsockopt(#0,#1,#2,wincoerce(char*,&tv),&socklen); |
| 1569 |
ecl_enable_interrupts(); |
| 1570 |
|
| 1571 |
@(return) = (ret == 0) ? ecl_make_double_float((double)tv.tv_sec |
| 1572 |
+ ((double)tv.tv_usec) / 1000000.0) : ECL_NIL; |
| 1573 |
}"))) |
| 1574 |
(if ret |
| 1575 |
ret |
| 1576 |
(error "Sockopt error: ~A" (c-inline () () :cstring "strerror(errno)" :one-liner t))))) |
| 1577 |
|
| 1578 |
(defun get-sockopt-linger (fd level const) |
| 1579 |
(let ((ret (c-inline (fd level const) (:int :int :int) t |
| 1580 |
"{ |
| 1581 |
struct linger sockopt; |
| 1582 |
socklen_t socklen = sizeof(struct linger); |
| 1583 |
int ret; |
| 1584 |
|
| 1585 |
ecl_disable_interrupts(); |
| 1586 |
ret = getsockopt(#0,#1,#2,wincoerce(char*,&sockopt),&socklen); |
| 1587 |
ecl_enable_interrupts(); |
| 1588 |
|
| 1589 |
@(return) = (ret == 0) ? ecl_make_integer((sockopt.l_onoff != 0) ? sockopt.l_linger : 0) : ECL_NIL; |
| 1590 |
}"))) |
| 1591 |
(if ret |
| 1592 |
ret |
| 1593 |
(error "Sockopt error: ~A" (c-inline () () :cstring "strerror(errno)" :one-liner t))))) |
| 1594 |
|
| 1595 |
(defun set-sockopt-int (fd level const value) |
| 1596 |
(let ((ret (c-inline (fd level const value) (:int :int :int :int) t |
| 1597 |
"{ |
| 1598 |
int sockopt = #3; |
| 1599 |
int ret; |
| 1600 |
|
| 1601 |
ecl_disable_interrupts(); |
| 1602 |
ret = setsockopt(#0,#1,#2,wincoerce(char *,&sockopt),sizeof(int)); |
| 1603 |
ecl_enable_interrupts(); |
| 1604 |
|
| 1605 |
@(return) = (ret == 0) ? ECL_T : ECL_NIL; |
| 1606 |
}"))) |
| 1607 |
(if ret |
| 1608 |
value |
| 1609 |
(error "Sockopt error: ~A" (c-inline () () :cstring "strerror(errno)" :one-liner t))))) |
| 1610 |
|
| 1611 |
(defun set-sockopt-bool (fd level const value) |
| 1612 |
(let ((ret (c-inline (fd level const value) (:int :int :int :object) t |
| 1613 |
"{ |
| 1614 |
int sockopt = (#3 == ECL_NIL) ? 0 : 1; |
| 1615 |
int ret; |
| 1616 |
|
| 1617 |
ecl_disable_interrupts(); |
| 1618 |
ret = setsockopt(#0,#1,#2,wincoerce(char *,&sockopt),sizeof(int)); |
| 1619 |
ecl_enable_interrupts(); |
| 1620 |
|
| 1621 |
@(return) = (ret == 0) ? ECL_T : ECL_NIL; |
| 1622 |
}"))) |
| 1623 |
(if ret |
| 1624 |
value |
| 1625 |
(error "Sockopt error: ~A" (c-inline () () :cstring "strerror(errno)" :one-liner t))))) |
| 1626 |
|
| 1627 |
#-wsock |
| 1628 |
(defun set-sockopt-timeval (fd level const value) |
| 1629 |
(let ((ret (c-inline (fd level const value) (:int :int :int :double) t |
| 1630 |
"{ |
| 1631 |
struct timeval tv; |
| 1632 |
double tmp = #3; |
| 1633 |
int ret; |
| 1634 |
|
| 1635 |
ecl_disable_interrupts(); |
| 1636 |
tv.tv_sec = (int)tmp; |
| 1637 |
tv.tv_usec = (int)((tmp-floor(tmp))*1000000.0); |
| 1638 |
ret = setsockopt(#0,#1,#2,&tv,sizeof(struct timeval)); |
| 1639 |
ecl_enable_interrupts(); |
| 1640 |
|
| 1641 |
@(return) = (ret == 0) ? ECL_T : ECL_NIL; |
| 1642 |
}"))) |
| 1643 |
(if ret |
| 1644 |
value |
| 1645 |
(error "Sockopt error: ~A" (c-inline () () :cstring "strerror(errno)" :one-liner t))))) |
| 1646 |
|
| 1647 |
#+wsock |
| 1648 |
(defun set-sockopt-timeval (fd level const value) |
| 1649 |
(set-sockopt-int fd level const (* 1000 value))) |
| 1650 |
|
| 1651 |
(defun set-sockopt-linger (fd level const value) |
| 1652 |
(let ((ret (c-inline (fd level const value) (:int :int :int :int) t |
| 1653 |
"{ |
| 1654 |
struct linger sockopt = {0, 0}; |
| 1655 |
int value = #3; |
| 1656 |
int ret; |
| 1657 |
|
| 1658 |
if (value > 0) { |
| 1659 |
sockopt.l_onoff = 1; |
| 1660 |
sockopt.l_linger = value; |
| 1661 |
} |
| 1662 |
|
| 1663 |
ecl_disable_interrupts(); |
| 1664 |
ret = setsockopt(#0,#1,#2,wincoerce(char *,&sockopt), |
| 1665 |
sizeof(struct linger)); |
| 1666 |
ecl_enable_interrupts(); |
| 1667 |
|
| 1668 |
@(return) = (ret == 0) ? ECL_T : ECL_NIL; |
| 1669 |
}"))) |
| 1670 |
(if ret |
| 1671 |
value |
| 1672 |
(error "Sockopt error: ~A" (c-inline () () :cstring "strerror(errno)" :one-liner t))))) |
| 1673 |
|
| 1674 |
(eval-when (:compile-toplevel :load-toplevel) |
| 1675 |
(defmacro define-sockopt (name c-level c-const type &optional (read-only nil)) |
| 1676 |
`(progn |
| 1677 |
(export ',name) |
| 1678 |
(defun ,name (socket) |
| 1679 |
(,(intern (format nil "GET-SOCKOPT-~A" type)) |
| 1680 |
(socket-file-descriptor socket) |
| 1681 |
(c-constant ,c-level) |
| 1682 |
(c-constant ,c-const))) |
| 1683 |
,@(unless read-only |
| 1684 |
`((defun (setf ,name) (value socket) |
| 1685 |
(,(intern (format nil "SET-SOCKOPT-~A" type)) |
| 1686 |
(socket-file-descriptor socket) |
| 1687 |
(c-constant ,c-level) |
| 1688 |
(c-constant ,c-const) |
| 1689 |
value))))))) |
| 1690 |
|
| 1691 |
(define-sockopt sockopt-type "SOL_SOCKET" "SO_TYPE" int t) |
| 1692 |
(define-sockopt sockopt-receive-buffer "SOL_SOCKET" "SO_RCVBUF" int) |
| 1693 |
(define-sockopt sockopt-receive-timeout "SOL_SOCKET" "SO_RCVTIMEO" timeval) |
| 1694 |
(define-sockopt sockopt-send-timeout "SOL_SOCKET" "SO_SNDTIMEO" timeval) |
| 1695 |
(define-sockopt sockopt-reuse-address "SOL_SOCKET" "SO_REUSEADDR" bool) |
| 1696 |
(define-sockopt sockopt-keep-alive "SOL_SOCKET" "SO_KEEPALIVE" bool) |
| 1697 |
(define-sockopt sockopt-dont-route "SOL_SOCKET" "SO_DONTROUTE" bool) |
| 1698 |
(define-sockopt sockopt-linger "SOL_SOCKET" "SO_LINGER" linger) |
| 1699 |
|
| 1700 |
#-(or :sun4sol2 :linux :android :wsock :cygwin) |
| 1701 |
(define-sockopt sockopt-reuse-port "SOL_SOCKET" "SO_REUSEPORT" bool) |
| 1702 |
|
| 1703 |
(define-sockopt sockopt-tcp-nodelay "IPPROTO_TCP" "TCP_NODELAY" bool) |
| 1704 |
|
| 1705 |
;; Add sockopts here as you need them... |
| 1706 |
|
| 1707 |
;; Finished loading |
| 1708 |
(provide '#:sockets) |
| 1709 |
(provide '#:sb-bsd-sockets) |