Commit | Line | Data |
---|---|---|
09270afd | 1 | /* Copyright (C) 1996,1997,1998,2000,2001, 2002, 2003, 2004 Free Software Foundation, Inc. |
86667910 | 2 | * |
73be1d9e MV |
3 | * This library is free software; you can redistribute it and/or |
4 | * modify it under the terms of the GNU Lesser General Public | |
5 | * License as published by the Free Software Foundation; either | |
6 | * version 2.1 of the License, or (at your option) any later version. | |
86667910 | 7 | * |
73be1d9e MV |
8 | * This library is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
11 | * Lesser General Public License for more details. | |
86667910 | 12 | * |
73be1d9e MV |
13 | * You should have received a copy of the GNU Lesser General Public |
14 | * License along with this library; if not, write to the Free Software | |
15 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
16 | */ | |
1bbd0b84 | 17 | |
1bbd0b84 | 18 | |
0f2d19dd JB |
19 | \f |
20 | ||
eccde741 RB |
21 | #if HAVE_CONFIG_H |
22 | # include <config.h> | |
23 | #endif | |
24 | ||
e6e2e95a | 25 | #include <errno.h> |
69d49ac8 | 26 | #include <gmp.h> |
e6e2e95a | 27 | |
a0599745 MD |
28 | #include "libguile/_scm.h" |
29 | #include "libguile/unif.h" | |
30 | #include "libguile/feature.h" | |
31 | #include "libguile/fports.h" | |
32 | #include "libguile/strings.h" | |
33 | #include "libguile/vectors.h" | |
7f9994d9 | 34 | #include "libguile/dynwind.h" |
20e6290e | 35 | |
a0599745 MD |
36 | #include "libguile/validate.h" |
37 | #include "libguile/socket.h" | |
95b88819 | 38 | |
b4e15479 SJ |
39 | #ifdef __MINGW32__ |
40 | #include "win32-socket.h" | |
41 | #endif | |
42 | ||
af68e5e5 SJ |
43 | #ifdef HAVE_STDINT_H |
44 | #include <stdint.h> | |
45 | #endif | |
95b88819 GH |
46 | #ifdef HAVE_STRING_H |
47 | #include <string.h> | |
48 | #endif | |
370312ae GH |
49 | #ifdef HAVE_UNISTD_H |
50 | #include <unistd.h> | |
51 | #endif | |
0f2d19dd | 52 | #include <sys/types.h> |
f87c105a | 53 | #ifdef HAVE_WINSOCK2_H |
82893676 MG |
54 | #include <winsock2.h> |
55 | #else | |
0f2d19dd | 56 | #include <sys/socket.h> |
1ba8c23a | 57 | #ifdef HAVE_UNIX_DOMAIN_SOCKETS |
0f2d19dd | 58 | #include <sys/un.h> |
0e958795 | 59 | #endif |
0f2d19dd JB |
60 | #include <netinet/in.h> |
61 | #include <netdb.h> | |
62 | #include <arpa/inet.h> | |
82893676 | 63 | #endif |
0f2d19dd | 64 | |
97d0e20b GH |
65 | #if defined (HAVE_UNIX_DOMAIN_SOCKETS) && !defined (SUN_LEN) |
66 | #define SUN_LEN(ptr) ((size_t) (((struct sockaddr_un *) 0)->sun_path) \ | |
67 | + strlen ((ptr)->sun_path)) | |
68 | #endif | |
69 | ||
439006bf GH |
70 | /* we are not currently using socklen_t. it's not defined on all systems, |
71 | so would need to be checked by configure. in the meantime, plain | |
72 | int is the best alternative. */ | |
73 | ||
0f2d19dd JB |
74 | \f |
75 | ||
a1ec6916 | 76 | SCM_DEFINE (scm_htons, "htons", 1, 0, 0, |
eefae538 GH |
77 | (SCM value), |
78 | "Convert a 16 bit quantity from host to network byte ordering.\n" | |
79 | "@var{value} is packed into 2 bytes, which are then converted\n" | |
80 | "and returned as a new integer.") | |
1bbd0b84 | 81 | #define FUNC_NAME s_scm_htons |
5c11cc9d | 82 | { |
7cee5b31 | 83 | return scm_from_ushort (htons (scm_to_ushort (value))); |
5c11cc9d | 84 | } |
1bbd0b84 | 85 | #undef FUNC_NAME |
5c11cc9d | 86 | |
a1ec6916 | 87 | SCM_DEFINE (scm_ntohs, "ntohs", 1, 0, 0, |
eefae538 GH |
88 | (SCM value), |
89 | "Convert a 16 bit quantity from network to host byte ordering.\n" | |
90 | "@var{value} is packed into 2 bytes, which are then converted\n" | |
91 | "and returned as a new integer.") | |
1bbd0b84 | 92 | #define FUNC_NAME s_scm_ntohs |
5c11cc9d | 93 | { |
7cee5b31 | 94 | return scm_from_ushort (ntohs (scm_to_ushort (value))); |
5c11cc9d | 95 | } |
1bbd0b84 | 96 | #undef FUNC_NAME |
5c11cc9d | 97 | |
a1ec6916 | 98 | SCM_DEFINE (scm_htonl, "htonl", 1, 0, 0, |
eefae538 GH |
99 | (SCM value), |
100 | "Convert a 32 bit quantity from host to network byte ordering.\n" | |
101 | "@var{value} is packed into 4 bytes, which are then converted\n" | |
102 | "and returned as a new integer.") | |
1bbd0b84 | 103 | #define FUNC_NAME s_scm_htonl |
5c11cc9d | 104 | { |
eccde741 | 105 | scm_t_uint32 c_in = SCM_NUM2ULONG (1, value); |
66c73b76 | 106 | |
b9bd8526 | 107 | return scm_from_ulong (htonl (c_in)); |
5c11cc9d | 108 | } |
1bbd0b84 | 109 | #undef FUNC_NAME |
5c11cc9d | 110 | |
a1ec6916 | 111 | SCM_DEFINE (scm_ntohl, "ntohl", 1, 0, 0, |
eefae538 GH |
112 | (SCM value), |
113 | "Convert a 32 bit quantity from network to host byte ordering.\n" | |
114 | "@var{value} is packed into 4 bytes, which are then converted\n" | |
115 | "and returned as a new integer.") | |
1bbd0b84 | 116 | #define FUNC_NAME s_scm_ntohl |
5c11cc9d | 117 | { |
eccde741 | 118 | scm_t_uint32 c_in = SCM_NUM2ULONG (1, value); |
66c73b76 | 119 | |
b9bd8526 | 120 | return scm_from_ulong (ntohl (c_in)); |
5c11cc9d | 121 | } |
1bbd0b84 | 122 | #undef FUNC_NAME |
5c11cc9d | 123 | |
66c73b76 GH |
124 | #ifndef HAVE_INET_ATON |
125 | /* for our definition in inet_aton.c, not usually needed. */ | |
126 | extern int inet_aton (); | |
127 | #endif | |
128 | ||
129 | SCM_DEFINE (scm_inet_aton, "inet-aton", 1, 0, 0, | |
130 | (SCM address), | |
eefae538 GH |
131 | "Convert an IPv4 Internet address from printable string\n" |
132 | "(dotted decimal notation) to an integer. E.g.,\n\n" | |
66c73b76 GH |
133 | "@lisp\n" |
134 | "(inet-aton \"127.0.0.1\") @result{} 2130706433\n" | |
135 | "@end lisp") | |
136 | #define FUNC_NAME s_scm_inet_aton | |
137 | { | |
138 | struct in_addr soka; | |
396e5506 MV |
139 | char *c_address; |
140 | int rv; | |
66c73b76 | 141 | |
396e5506 MV |
142 | c_address = scm_to_locale_string (address); |
143 | rv = inet_aton (c_address, &soka); | |
144 | free (c_address); | |
145 | if (rv == 0) | |
66c73b76 | 146 | SCM_MISC_ERROR ("bad address", SCM_EOL); |
b9bd8526 | 147 | return scm_from_ulong (ntohl (soka.s_addr)); |
66c73b76 GH |
148 | } |
149 | #undef FUNC_NAME | |
150 | ||
151 | ||
152 | SCM_DEFINE (scm_inet_ntoa, "inet-ntoa", 1, 0, 0, | |
153 | (SCM inetid), | |
eefae538 GH |
154 | "Convert an IPv4 Internet address to a printable\n" |
155 | "(dotted decimal notation) string. E.g.,\n\n" | |
66c73b76 GH |
156 | "@lisp\n" |
157 | "(inet-ntoa 2130706433) @result{} \"127.0.0.1\"\n" | |
158 | "@end lisp") | |
159 | #define FUNC_NAME s_scm_inet_ntoa | |
160 | { | |
161 | struct in_addr addr; | |
162 | char *s; | |
163 | SCM answer; | |
164 | addr.s_addr = htonl (SCM_NUM2ULONG (1, inetid)); | |
165 | s = inet_ntoa (addr); | |
cc95e00a | 166 | answer = scm_from_locale_string (s); |
66c73b76 GH |
167 | return answer; |
168 | } | |
169 | #undef FUNC_NAME | |
170 | ||
171 | #ifdef HAVE_INET_NETOF | |
172 | SCM_DEFINE (scm_inet_netof, "inet-netof", 1, 0, 0, | |
173 | (SCM address), | |
eefae538 GH |
174 | "Return the network number part of the given IPv4\n" |
175 | "Internet address. E.g.,\n\n" | |
66c73b76 GH |
176 | "@lisp\n" |
177 | "(inet-netof 2130706433) @result{} 127\n" | |
178 | "@end lisp") | |
179 | #define FUNC_NAME s_scm_inet_netof | |
180 | { | |
181 | struct in_addr addr; | |
182 | addr.s_addr = htonl (SCM_NUM2ULONG (1, address)); | |
b9bd8526 | 183 | return scm_from_ulong (inet_netof (addr)); |
66c73b76 GH |
184 | } |
185 | #undef FUNC_NAME | |
186 | #endif | |
187 | ||
188 | #ifdef HAVE_INET_LNAOF | |
189 | SCM_DEFINE (scm_lnaof, "inet-lnaof", 1, 0, 0, | |
190 | (SCM address), | |
191 | "Return the local-address-with-network part of the given\n" | |
eefae538 GH |
192 | "IPv4 Internet address, using the obsolete class A/B/C system.\n" |
193 | "E.g.,\n\n" | |
66c73b76 GH |
194 | "@lisp\n" |
195 | "(inet-lnaof 2130706433) @result{} 1\n" | |
196 | "@end lisp") | |
197 | #define FUNC_NAME s_scm_lnaof | |
198 | { | |
199 | struct in_addr addr; | |
200 | addr.s_addr = htonl (SCM_NUM2ULONG (1, address)); | |
b9bd8526 | 201 | return scm_from_ulong (inet_lnaof (addr)); |
66c73b76 GH |
202 | } |
203 | #undef FUNC_NAME | |
204 | #endif | |
205 | ||
206 | #ifdef HAVE_INET_MAKEADDR | |
207 | SCM_DEFINE (scm_inet_makeaddr, "inet-makeaddr", 2, 0, 0, | |
208 | (SCM net, SCM lna), | |
eefae538 | 209 | "Make an IPv4 Internet address by combining the network number\n" |
66c73b76 | 210 | "@var{net} with the local-address-within-network number\n" |
eefae538 | 211 | "@var{lna}. E.g.,\n\n" |
66c73b76 GH |
212 | "@lisp\n" |
213 | "(inet-makeaddr 127 1) @result{} 2130706433\n" | |
214 | "@end lisp") | |
215 | #define FUNC_NAME s_scm_inet_makeaddr | |
216 | { | |
217 | struct in_addr addr; | |
218 | unsigned long netnum; | |
219 | unsigned long lnanum; | |
220 | ||
221 | netnum = SCM_NUM2ULONG (1, net); | |
222 | lnanum = SCM_NUM2ULONG (2, lna); | |
223 | addr = inet_makeaddr (netnum, lnanum); | |
b9bd8526 | 224 | return scm_from_ulong (ntohl (addr.s_addr)); |
66c73b76 GH |
225 | } |
226 | #undef FUNC_NAME | |
227 | #endif | |
228 | ||
a57a0b1e | 229 | #ifdef HAVE_IPV6 |
eefae538 | 230 | |
66c73b76 GH |
231 | /* flip a 128 bit IPv6 address between host and network order. */ |
232 | #ifdef WORDS_BIGENDIAN | |
233 | #define FLIP_NET_HOST_128(addr) | |
234 | #else | |
235 | #define FLIP_NET_HOST_128(addr)\ | |
236 | {\ | |
237 | int i;\ | |
238 | \ | |
239 | for (i = 0; i < 8; i++)\ | |
240 | {\ | |
2de4f939 | 241 | scm_t_uint8 c = (addr)[i];\ |
66c73b76 GH |
242 | \ |
243 | (addr)[i] = (addr)[15 - i];\ | |
244 | (addr)[15 - i] = c;\ | |
245 | }\ | |
246 | } | |
247 | #endif | |
248 | ||
2de4f939 RB |
249 | #ifdef WORDS_BIGENDIAN |
250 | #define FLIPCPY_NET_HOST_128(dest, src) memcpy (dest, src, 16) | |
251 | #else | |
252 | #define FLIPCPY_NET_HOST_128(dest, src) \ | |
253 | { \ | |
254 | const scm_t_uint8 *tmp_srcp = (src) + 15; \ | |
255 | scm_t_uint8 *tmp_destp = (dest); \ | |
256 | \ | |
257 | do { \ | |
258 | *tmp_destp++ = *tmp_srcp--; \ | |
259 | } while (tmp_srcp != (src)); \ | |
260 | } | |
261 | #endif | |
262 | ||
263 | ||
7310ad0c | 264 | #if (SIZEOF_SCM_T_BITS * SCM_CHAR_BIT) > 128 |
2de4f939 RB |
265 | #error "Assumption that scm_t_bits <= 128 bits has been violated." |
266 | #endif | |
267 | ||
7310ad0c | 268 | #if (SIZEOF_UNSIGNED_LONG * SCM_CHAR_BIT) > 128 |
2de4f939 RB |
269 | #error "Assumption that unsigned long <= 128 bits has been violated." |
270 | #endif | |
271 | ||
7310ad0c | 272 | #if (SIZEOF_UNSIGNED_LONG_LONG * SCM_CHAR_BIT) > 128 |
2de4f939 RB |
273 | #error "Assumption that unsigned long long <= 128 bits has been violated." |
274 | #endif | |
275 | ||
66c73b76 GH |
276 | /* convert a 128 bit IPv6 address in network order to a host ordered |
277 | SCM integer. */ | |
7cee5b31 MV |
278 | static SCM |
279 | scm_from_ipv6 (const scm_t_uint8 *src) | |
66c73b76 | 280 | { |
2b4d1547 KR |
281 | SCM result = scm_i_mkbig (); |
282 | mpz_import (SCM_I_BIG_MPZ (result), | |
283 | 1, /* chunk */ | |
284 | 1, /* big-endian chunk ordering */ | |
285 | 16, /* chunks are 16 bytes long */ | |
286 | 1, /* big-endian byte ordering */ | |
287 | 0, /* "nails" -- leading unused bits per chunk */ | |
288 | src); | |
289 | return scm_i_normbig (result); | |
290 | } | |
66c73b76 GH |
291 | |
292 | /* convert a host ordered SCM integer to a 128 bit IPv6 address in | |
293 | network order. */ | |
7cee5b31 MV |
294 | static void |
295 | scm_to_ipv6 (scm_t_uint8 dst[16], SCM src) | |
66c73b76 | 296 | { |
e11e83f3 | 297 | if (SCM_I_INUMP (src)) |
66c73b76 | 298 | { |
e11e83f3 | 299 | scm_t_signed_bits n = SCM_I_INUM (src); |
7cee5b31 MV |
300 | if (n < 0) |
301 | scm_out_of_range (NULL, src); | |
2de4f939 RB |
302 | #ifdef WORDS_BIGENDIAN |
303 | memset (dst, 0, 16 - sizeof (scm_t_signed_bits)); | |
304 | memcpy (dst + (16 - sizeof (scm_t_signed_bits)), | |
305 | &n, | |
306 | sizeof (scm_t_signed_bits)); | |
307 | #else | |
308 | memset (dst + sizeof (scm_t_signed_bits), | |
309 | 0, | |
310 | 16 - sizeof (scm_t_signed_bits)); | |
311 | /* FIXME: this pair of ops is kinda wasteful -- should rewrite as | |
312 | a single loop perhaps, similar to the handling of bignums. */ | |
313 | memcpy (dst, &n, sizeof (scm_t_signed_bits)); | |
314 | FLIP_NET_HOST_128 (dst); | |
315 | #endif | |
66c73b76 | 316 | } |
7cee5b31 | 317 | else if (SCM_BIGP (src)) |
66c73b76 | 318 | { |
2de4f939 | 319 | size_t count; |
7cee5b31 MV |
320 | |
321 | if ((mpz_sgn (SCM_I_BIG_MPZ (src)) < 0) | |
322 | || mpz_sizeinbase (SCM_I_BIG_MPZ (src), 2) > 128) | |
323 | scm_out_of_range (NULL, src); | |
324 | ||
66c73b76 | 325 | memset (dst, 0, 16); |
2de4f939 RB |
326 | mpz_export (dst, |
327 | &count, | |
328 | 1, /* big-endian chunk ordering */ | |
329 | 16, /* chunks are 16 bytes long */ | |
330 | 1, /* big-endian byte ordering */ | |
331 | 0, /* "nails" -- leading unused bits per chunk */ | |
332 | SCM_I_BIG_MPZ (src)); | |
333 | scm_remember_upto_here_1 (src); | |
66c73b76 | 334 | } |
2de4f939 | 335 | else |
7cee5b31 | 336 | scm_wrong_type_arg (NULL, 0, src); |
2de4f939 RB |
337 | } |
338 | ||
66c73b76 GH |
339 | #ifdef HAVE_INET_PTON |
340 | SCM_DEFINE (scm_inet_pton, "inet-pton", 2, 0, 0, | |
341 | (SCM family, SCM address), | |
eefae538 GH |
342 | "Convert a string containing a printable network address to\n" |
343 | "an integer address. Note that unlike the C version of this\n" | |
344 | "function,\n" | |
66c73b76 | 345 | "the result is an integer with normal host byte ordering.\n" |
eefae538 | 346 | "@var{family} can be @code{AF_INET} or @code{AF_INET6}. E.g.,\n\n" |
66c73b76 | 347 | "@lisp\n" |
dd85ce47 ML |
348 | "(inet-pton AF_INET \"127.0.0.1\") @result{} 2130706433\n" |
349 | "(inet-pton AF_INET6 \"::1\") @result{} 1\n" | |
66c73b76 GH |
350 | "@end lisp") |
351 | #define FUNC_NAME s_scm_inet_pton | |
352 | { | |
353 | int af; | |
354 | char *src; | |
355 | char dst[16]; | |
396e5506 | 356 | int rv, eno; |
66c73b76 | 357 | |
7cee5b31 | 358 | af = scm_to_int (family); |
66c73b76 | 359 | SCM_ASSERT_RANGE (1, family, af == AF_INET || af == AF_INET6); |
396e5506 | 360 | src = scm_to_locale_string (address); |
66c73b76 | 361 | rv = inet_pton (af, src, dst); |
396e5506 MV |
362 | eno = errno; |
363 | free (src); | |
364 | errno = eno; | |
66c73b76 GH |
365 | if (rv == -1) |
366 | SCM_SYSERROR; | |
367 | else if (rv == 0) | |
368 | SCM_MISC_ERROR ("Bad address", SCM_EOL); | |
369 | if (af == AF_INET) | |
b9bd8526 | 370 | return scm_from_ulong (ntohl (*(scm_t_uint32 *) dst)); |
66c73b76 | 371 | else |
7cee5b31 | 372 | return scm_from_ipv6 ((char *) dst); |
66c73b76 GH |
373 | } |
374 | #undef FUNC_NAME | |
375 | #endif | |
376 | ||
377 | #ifdef HAVE_INET_NTOP | |
378 | SCM_DEFINE (scm_inet_ntop, "inet-ntop", 2, 0, 0, | |
379 | (SCM family, SCM address), | |
eefae538 | 380 | "Convert a network address into a printable string.\n" |
66c73b76 GH |
381 | "Note that unlike the C version of this function,\n" |
382 | "the input is an integer with normal host byte ordering.\n" | |
eefae538 | 383 | "@var{family} can be @code{AF_INET} or @code{AF_INET6}. E.g.,\n\n" |
66c73b76 | 384 | "@lisp\n" |
dd85ce47 | 385 | "(inet-ntop AF_INET 2130706433) @result{} \"127.0.0.1\"\n" |
66c73b76 GH |
386 | "(inet-ntop AF_INET6 (- (expt 2 128) 1)) @result{}\n" |
387 | "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff\n" | |
388 | "@end lisp") | |
389 | #define FUNC_NAME s_scm_inet_ntop | |
390 | { | |
391 | int af; | |
392 | #ifdef INET6_ADDRSTRLEN | |
393 | char dst[INET6_ADDRSTRLEN]; | |
394 | #else | |
395 | char dst[46]; | |
396 | #endif | |
397 | char addr6[16]; | |
398 | ||
7cee5b31 | 399 | af = scm_to_int (family); |
66c73b76 GH |
400 | SCM_ASSERT_RANGE (1, family, af == AF_INET || af == AF_INET6); |
401 | if (af == AF_INET) | |
eccde741 | 402 | *(scm_t_uint32 *) addr6 = htonl (SCM_NUM2ULONG (2, address)); |
66c73b76 | 403 | else |
7cee5b31 | 404 | scm_to_ipv6 (addr6, address); |
66c73b76 GH |
405 | if (inet_ntop (af, &addr6, dst, sizeof dst) == NULL) |
406 | SCM_SYSERROR; | |
cc95e00a | 407 | return scm_from_locale_string (dst); |
66c73b76 GH |
408 | } |
409 | #undef FUNC_NAME | |
410 | #endif | |
411 | ||
a57a0b1e | 412 | #endif /* HAVE_IPV6 */ |
eefae538 | 413 | |
bc45012d | 414 | SCM_SYMBOL (sym_socket, "socket"); |
82ddea4e | 415 | |
439006bf | 416 | #define SCM_SOCK_FD_TO_PORT(fd) scm_fdes_to_port (fd, "r+0", sym_socket) |
1bbd0b84 | 417 | |
a1ec6916 | 418 | SCM_DEFINE (scm_socket, "socket", 3, 0, 0, |
1bbd0b84 | 419 | (SCM family, SCM style, SCM proto), |
1e6808ea | 420 | "Return a new socket port of the type specified by @var{family},\n" |
eefae538 | 421 | "@var{style} and @var{proto}. All three parameters are\n" |
3453619b GH |
422 | "integers. Supported values for @var{family} are\n" |
423 | "@code{AF_UNIX}, @code{AF_INET} and @code{AF_INET6}.\n" | |
424 | "Typical values for @var{style} are @code{SOCK_STREAM},\n" | |
eefae538 GH |
425 | "@code{SOCK_DGRAM} and @code{SOCK_RAW}.\n\n" |
426 | "@var{proto} can be obtained from a protocol name using\n" | |
1e6808ea | 427 | "@code{getprotobyname}. A value of zero specifies the default\n" |
eefae538 | 428 | "protocol, which is usually right.\n\n" |
1e6808ea MG |
429 | "A single socket port cannot by used for communication until it\n" |
430 | "has been connected to another socket.") | |
1bbd0b84 | 431 | #define FUNC_NAME s_scm_socket |
0f2d19dd | 432 | { |
370312ae | 433 | int fd; |
370312ae | 434 | |
7cee5b31 MV |
435 | fd = socket (scm_to_int (family), |
436 | scm_to_int (style), | |
437 | scm_to_int (proto)); | |
439006bf GH |
438 | if (fd == -1) |
439 | SCM_SYSERROR; | |
440 | return SCM_SOCK_FD_TO_PORT (fd); | |
0f2d19dd | 441 | } |
1bbd0b84 | 442 | #undef FUNC_NAME |
0f2d19dd | 443 | |
0e958795 | 444 | #ifdef HAVE_SOCKETPAIR |
a1ec6916 | 445 | SCM_DEFINE (scm_socketpair, "socketpair", 3, 0, 0, |
1bbd0b84 | 446 | (SCM family, SCM style, SCM proto), |
1e6808ea | 447 | "Return a pair of connected (but unnamed) socket ports of the\n" |
eefae538 | 448 | "type specified by @var{family}, @var{style} and @var{proto}.\n" |
1e6808ea MG |
449 | "Many systems support only socket pairs of the @code{AF_UNIX}\n" |
450 | "family. Zero is likely to be the only meaningful value for\n" | |
eefae538 | 451 | "@var{proto}.") |
1bbd0b84 | 452 | #define FUNC_NAME s_scm_socketpair |
0f2d19dd | 453 | { |
370312ae GH |
454 | int fam; |
455 | int fd[2]; | |
370312ae | 456 | |
7cee5b31 | 457 | fam = scm_to_int (family); |
370312ae | 458 | |
7cee5b31 | 459 | if (socketpair (fam, scm_to_int (style), scm_to_int (proto), fd) == -1) |
1bbd0b84 | 460 | SCM_SYSERROR; |
370312ae | 461 | |
439006bf | 462 | return scm_cons (SCM_SOCK_FD_TO_PORT (fd[0]), SCM_SOCK_FD_TO_PORT (fd[1])); |
0f2d19dd | 463 | } |
1bbd0b84 | 464 | #undef FUNC_NAME |
0e958795 | 465 | #endif |
0f2d19dd | 466 | |
a1ec6916 | 467 | SCM_DEFINE (scm_getsockopt, "getsockopt", 3, 0, 0, |
1bbd0b84 | 468 | (SCM sock, SCM level, SCM optname), |
1e6808ea | 469 | "Return the value of a particular socket option for the socket\n" |
eefae538 | 470 | "port @var{sock}. @var{level} is an integer code for type of\n" |
1e6808ea MG |
471 | "option being requested, e.g., @code{SOL_SOCKET} for\n" |
472 | "socket-level options. @var{optname} is an integer code for the\n" | |
473 | "option required and should be specified using one of the\n" | |
eefae538 | 474 | "symbols @code{SO_DEBUG}, @code{SO_REUSEADDR} etc.\n\n" |
1e6808ea MG |
475 | "The returned value is typically an integer but @code{SO_LINGER}\n" |
476 | "returns a pair of integers.") | |
1bbd0b84 | 477 | #define FUNC_NAME s_scm_getsockopt |
0f2d19dd | 478 | { |
370312ae | 479 | int fd; |
439006bf | 480 | /* size of optval is the largest supported option. */ |
370312ae GH |
481 | #ifdef HAVE_STRUCT_LINGER |
482 | char optval[sizeof (struct linger)]; | |
439006bf | 483 | int optlen = sizeof (struct linger); |
370312ae | 484 | #else |
1be6b49c ML |
485 | char optval[sizeof (size_t)]; |
486 | int optlen = sizeof (size_t); | |
370312ae GH |
487 | #endif |
488 | int ilevel; | |
489 | int ioptname; | |
0f2d19dd | 490 | |
78446828 | 491 | sock = SCM_COERCE_OUTPORT (sock); |
2cf6d014 | 492 | SCM_VALIDATE_OPFPORT (1, sock); |
7cee5b31 MV |
493 | ilevel = scm_to_int (level); |
494 | ioptname = scm_to_int (optname); | |
0f2d19dd | 495 | |
ee149d03 | 496 | fd = SCM_FPORT_FDES (sock); |
370312ae | 497 | if (getsockopt (fd, ilevel, ioptname, (void *) optval, &optlen) == -1) |
1bbd0b84 | 498 | SCM_SYSERROR; |
1cc91f1b | 499 | |
439006bf | 500 | if (ilevel == SOL_SOCKET) |
0f2d19dd | 501 | { |
439006bf GH |
502 | #ifdef SO_LINGER |
503 | if (ioptname == SO_LINGER) | |
504 | { | |
370312ae | 505 | #ifdef HAVE_STRUCT_LINGER |
439006bf GH |
506 | struct linger *ling = (struct linger *) optval; |
507 | ||
b9bd8526 MV |
508 | return scm_cons (scm_from_long (ling->l_onoff), |
509 | scm_from_long (ling->l_linger)); | |
370312ae | 510 | #else |
b9bd8526 | 511 | return scm_cons (scm_from_long (*(int *) optval), |
7cee5b31 | 512 | scm_from_int (0)); |
0f2d19dd | 513 | #endif |
439006bf GH |
514 | } |
515 | else | |
370312ae | 516 | #endif |
439006bf | 517 | if (0 |
370312ae | 518 | #ifdef SO_SNDBUF |
439006bf | 519 | || ioptname == SO_SNDBUF |
370312ae GH |
520 | #endif |
521 | #ifdef SO_RCVBUF | |
439006bf | 522 | || ioptname == SO_RCVBUF |
370312ae | 523 | #endif |
439006bf GH |
524 | ) |
525 | { | |
b9bd8526 | 526 | return scm_from_size_t (*(size_t *) optval); |
439006bf GH |
527 | } |
528 | } | |
b9bd8526 | 529 | return scm_from_int (*(int *) optval); |
0f2d19dd | 530 | } |
1bbd0b84 | 531 | #undef FUNC_NAME |
0f2d19dd | 532 | |
a1ec6916 | 533 | SCM_DEFINE (scm_setsockopt, "setsockopt", 4, 0, 0, |
1bbd0b84 | 534 | (SCM sock, SCM level, SCM optname, SCM value), |
eefae538 GH |
535 | "Set the value of a particular socket option for the socket\n" |
536 | "port @var{sock}. @var{level} is an integer code for type of option\n" | |
b380b885 MD |
537 | "being set, e.g., @code{SOL_SOCKET} for socket-level options.\n" |
538 | "@var{optname} is an\n" | |
539 | "integer code for the option to set and should be specified using one of\n" | |
540 | "the symbols @code{SO_DEBUG}, @code{SO_REUSEADDR} etc.\n" | |
541 | "@var{value} is the value to which the option should be set. For\n" | |
542 | "most options this must be an integer, but for @code{SO_LINGER} it must\n" | |
543 | "be a pair.\n\n" | |
544 | "The return value is unspecified.") | |
1bbd0b84 | 545 | #define FUNC_NAME s_scm_setsockopt |
0f2d19dd | 546 | { |
370312ae | 547 | int fd; |
439006bf GH |
548 | int optlen = -1; |
549 | /* size of optval is the largest supported option. */ | |
370312ae | 550 | #ifdef HAVE_STRUCT_LINGER |
439006bf | 551 | char optval[sizeof (struct linger)]; |
370312ae | 552 | #else |
1be6b49c | 553 | char optval[sizeof (size_t)]; |
370312ae GH |
554 | #endif |
555 | int ilevel, ioptname; | |
439006bf | 556 | |
78446828 | 557 | sock = SCM_COERCE_OUTPORT (sock); |
439006bf GH |
558 | |
559 | SCM_VALIDATE_OPFPORT (1, sock); | |
7cee5b31 MV |
560 | ilevel = scm_to_int (level); |
561 | ioptname = scm_to_int (optname); | |
439006bf | 562 | |
ee149d03 | 563 | fd = SCM_FPORT_FDES (sock); |
439006bf GH |
564 | |
565 | if (ilevel == SOL_SOCKET) | |
370312ae | 566 | { |
439006bf GH |
567 | #ifdef SO_LINGER |
568 | if (ioptname == SO_LINGER) | |
569 | { | |
370312ae | 570 | #ifdef HAVE_STRUCT_LINGER |
439006bf GH |
571 | struct linger ling; |
572 | long lv; | |
573 | ||
d2e53ed6 | 574 | SCM_ASSERT (scm_is_pair (value), value, SCM_ARG4, FUNC_NAME); |
439006bf GH |
575 | lv = SCM_NUM2LONG (4, SCM_CAR (value)); |
576 | ling.l_onoff = (int) lv; | |
577 | SCM_ASSERT_RANGE (SCM_ARG4, value, ling.l_onoff == lv); | |
578 | lv = SCM_NUM2LONG (4, SCM_CDR (value)); | |
579 | ling.l_linger = (int) lv; | |
580 | SCM_ASSERT_RANGE (SCM_ARG4, value, ling.l_linger == lv); | |
581 | optlen = (int) sizeof (struct linger); | |
582 | memcpy (optval, (void *) &ling, optlen); | |
370312ae | 583 | #else |
439006bf GH |
584 | int ling; |
585 | long lv; | |
586 | ||
d2e53ed6 | 587 | SCM_ASSERT (scm_is_pair (value), value, SCM_ARG4, FUNC_NAME); |
439006bf GH |
588 | /* timeout is ignored, but may as well validate it. */ |
589 | lv = SCM_NUM2LONG (4, SCM_CDR (value)); | |
590 | ling = (int) lv; | |
591 | SCM_ASSERT_RANGE (SCM_ARG4, value, ling == lv); | |
592 | lv = SCM_NUM2LONG (4, SCM_CAR (value)); | |
593 | ling = (int) lv; | |
594 | SCM_ASSERT_RANGE (SCM_ARG4, value, ling == lv); | |
595 | optlen = (int) sizeof (int); | |
596 | (*(int *) optval) = ling; | |
597 | #endif | |
598 | } | |
599 | else | |
600 | #endif | |
601 | if (0 | |
370312ae | 602 | #ifdef SO_SNDBUF |
439006bf | 603 | || ioptname == SO_SNDBUF |
370312ae GH |
604 | #endif |
605 | #ifdef SO_RCVBUF | |
439006bf | 606 | || ioptname == SO_RCVBUF |
370312ae | 607 | #endif |
439006bf GH |
608 | ) |
609 | { | |
610 | long lv = SCM_NUM2LONG (4, value); | |
611 | ||
1be6b49c ML |
612 | optlen = (int) sizeof (size_t); |
613 | (*(size_t *) optval) = (size_t) lv; | |
439006bf GH |
614 | } |
615 | } | |
616 | if (optlen == -1) | |
0f2d19dd | 617 | { |
439006bf GH |
618 | /* Most options take an int. */ |
619 | long lv = SCM_NUM2LONG (4, value); | |
620 | int val = (int) lv; | |
621 | ||
622 | SCM_ASSERT_RANGE (SCM_ARG4, value, val == lv); | |
370312ae | 623 | optlen = (int) sizeof (int); |
439006bf | 624 | (*(int *) optval) = val; |
0f2d19dd | 625 | } |
370312ae | 626 | if (setsockopt (fd, ilevel, ioptname, (void *) optval, optlen) == -1) |
1bbd0b84 | 627 | SCM_SYSERROR; |
370312ae | 628 | return SCM_UNSPECIFIED; |
0f2d19dd | 629 | } |
1bbd0b84 | 630 | #undef FUNC_NAME |
0f2d19dd | 631 | |
a1ec6916 | 632 | SCM_DEFINE (scm_shutdown, "shutdown", 2, 0, 0, |
1bbd0b84 | 633 | (SCM sock, SCM how), |
b380b885 | 634 | "Sockets can be closed simply by using @code{close-port}. The\n" |
bb2c02f2 | 635 | "@code{shutdown} procedure allows reception or transmission on a\n" |
b380b885 MD |
636 | "connection to be shut down individually, according to the parameter\n" |
637 | "@var{how}:\n\n" | |
638 | "@table @asis\n" | |
639 | "@item 0\n" | |
640 | "Stop receiving data for this socket. If further data arrives, reject it.\n" | |
641 | "@item 1\n" | |
642 | "Stop trying to transmit data from this socket. Discard any\n" | |
643 | "data waiting to be sent. Stop looking for acknowledgement of\n" | |
644 | "data already sent; don't retransmit it if it is lost.\n" | |
645 | "@item 2\n" | |
646 | "Stop both reception and transmission.\n" | |
647 | "@end table\n\n" | |
648 | "The return value is unspecified.") | |
1bbd0b84 | 649 | #define FUNC_NAME s_scm_shutdown |
0f2d19dd | 650 | { |
370312ae | 651 | int fd; |
78446828 | 652 | sock = SCM_COERCE_OUTPORT (sock); |
34d19ef6 | 653 | SCM_VALIDATE_OPFPORT (1, sock); |
ee149d03 | 654 | fd = SCM_FPORT_FDES (sock); |
7cee5b31 | 655 | if (shutdown (fd, scm_to_signed_integer (how, 0, 2)) == -1) |
1bbd0b84 | 656 | SCM_SYSERROR; |
370312ae GH |
657 | return SCM_UNSPECIFIED; |
658 | } | |
1bbd0b84 | 659 | #undef FUNC_NAME |
0f2d19dd | 660 | |
370312ae GH |
661 | /* convert fam/address/args into a sockaddr of the appropriate type. |
662 | args is modified by removing the arguments actually used. | |
663 | which_arg and proc are used when reporting errors: | |
664 | which_arg is the position of address in the original argument list. | |
665 | proc is the name of the original procedure. | |
666 | size returns the size of the structure allocated. */ | |
667 | ||
370312ae | 668 | static struct sockaddr * |
439006bf GH |
669 | scm_fill_sockaddr (int fam, SCM address, SCM *args, int which_arg, |
670 | const char *proc, int *size) | |
3453619b | 671 | #define FUNC_NAME proc |
370312ae GH |
672 | { |
673 | switch (fam) | |
0f2d19dd | 674 | { |
370312ae GH |
675 | case AF_INET: |
676 | { | |
370312ae | 677 | struct sockaddr_in *soka; |
3453619b GH |
678 | unsigned long addr; |
679 | int port; | |
370312ae | 680 | |
3453619b GH |
681 | SCM_VALIDATE_ULONG_COPY (which_arg, address, addr); |
682 | SCM_VALIDATE_CONS (which_arg + 1, *args); | |
7cee5b31 | 683 | port = scm_to_int (SCM_CAR (*args)); |
3453619b | 684 | *args = SCM_CDR (*args); |
67329a9e | 685 | soka = (struct sockaddr_in *) scm_malloc (sizeof (struct sockaddr_in)); |
439006bf GH |
686 | if (!soka) |
687 | scm_memory_error (proc); | |
93b047f4 | 688 | #if HAVE_STRUCT_SOCKADDR_SIN_LEN |
3453619b GH |
689 | soka->sin_len = sizeof (struct sockaddr_in); |
690 | #endif | |
370312ae | 691 | soka->sin_family = AF_INET; |
3453619b GH |
692 | soka->sin_addr.s_addr = htonl (addr); |
693 | soka->sin_port = htons (port); | |
370312ae GH |
694 | *size = sizeof (struct sockaddr_in); |
695 | return (struct sockaddr *) soka; | |
696 | } | |
a57a0b1e | 697 | #ifdef HAVE_IPV6 |
3453619b GH |
698 | case AF_INET6: |
699 | { | |
700 | /* see RFC2553. */ | |
701 | int port; | |
702 | struct sockaddr_in6 *soka; | |
703 | unsigned long flowinfo = 0; | |
704 | unsigned long scope_id = 0; | |
705 | ||
3453619b | 706 | SCM_VALIDATE_CONS (which_arg + 1, *args); |
7cee5b31 | 707 | port = scm_to_int (SCM_CAR (*args)); |
3453619b | 708 | *args = SCM_CDR (*args); |
d2e53ed6 | 709 | if (scm_is_pair (*args)) |
3453619b GH |
710 | { |
711 | SCM_VALIDATE_ULONG_COPY (which_arg + 2, SCM_CAR (*args), flowinfo); | |
712 | *args = SCM_CDR (*args); | |
d2e53ed6 | 713 | if (scm_is_pair (*args)) |
3453619b GH |
714 | { |
715 | SCM_VALIDATE_ULONG_COPY (which_arg + 3, SCM_CAR (*args), | |
716 | scope_id); | |
717 | *args = SCM_CDR (*args); | |
718 | } | |
719 | } | |
67329a9e | 720 | soka = (struct sockaddr_in6 *) scm_malloc (sizeof (struct sockaddr_in6)); |
3453619b GH |
721 | if (!soka) |
722 | scm_memory_error (proc); | |
93b047f4 | 723 | #if HAVE_STRUCT_SOCKADDR_IN6_SIN6_LEN |
3453619b GH |
724 | soka->sin6_len = sizeof (struct sockaddr_in6); |
725 | #endif | |
726 | soka->sin6_family = AF_INET6; | |
7cee5b31 | 727 | scm_to_ipv6 (soka->sin6_addr.s6_addr, address); |
5a525b84 | 728 | soka->sin6_port = htons (port); |
3453619b | 729 | soka->sin6_flowinfo = flowinfo; |
5a525b84 | 730 | #ifdef HAVE_SIN6_SCOPE_ID |
3453619b | 731 | soka->sin6_scope_id = scope_id; |
5a525b84 | 732 | #endif |
3453619b GH |
733 | *size = sizeof (struct sockaddr_in6); |
734 | return (struct sockaddr *) soka; | |
735 | } | |
736 | #endif | |
1ba8c23a | 737 | #ifdef HAVE_UNIX_DOMAIN_SOCKETS |
370312ae GH |
738 | case AF_UNIX: |
739 | { | |
740 | struct sockaddr_un *soka; | |
439006bf | 741 | int addr_size; |
7f9994d9 MV |
742 | char *c_address; |
743 | ||
744 | scm_frame_begin (0); | |
745 | ||
746 | c_address = scm_to_locale_string (address); | |
747 | scm_frame_free (c_address); | |
370312ae | 748 | |
439006bf GH |
749 | /* the static buffer size in sockaddr_un seems to be arbitrary |
750 | and not necessarily a hard limit. e.g., the glibc manual | |
751 | suggests it may be possible to declare it size 0. let's | |
752 | ignore it. if the O/S doesn't like the size it will cause | |
753 | connect/bind etc., to fail. sun_path is always the last | |
754 | member of the structure. */ | |
755 | addr_size = sizeof (struct sockaddr_un) | |
7f9994d9 | 756 | + max (0, strlen (c_address) + 1 - (sizeof soka->sun_path)); |
67329a9e | 757 | soka = (struct sockaddr_un *) scm_malloc (addr_size); |
439006bf GH |
758 | memset (soka, 0, addr_size); /* for sun_len: see sin_len above. */ |
759 | soka->sun_family = AF_UNIX; | |
7f9994d9 | 760 | strcpy (soka->sun_path, c_address); |
439006bf | 761 | *size = SUN_LEN (soka); |
7f9994d9 MV |
762 | |
763 | scm_frame_end (); | |
370312ae GH |
764 | return (struct sockaddr *) soka; |
765 | } | |
0e958795 | 766 | #endif |
370312ae | 767 | default: |
e11e83f3 | 768 | scm_out_of_range (proc, scm_from_int (fam)); |
0f2d19dd | 769 | } |
0f2d19dd | 770 | } |
3453619b | 771 | #undef FUNC_NAME |
370312ae | 772 | |
a1ec6916 | 773 | SCM_DEFINE (scm_connect, "connect", 3, 0, 1, |
1bbd0b84 | 774 | (SCM sock, SCM fam, SCM address, SCM args), |
eefae538 | 775 | "Initiate a connection from a socket using a specified address\n" |
3453619b GH |
776 | "family to the address\n" |
777 | "specified by @var{address} and possibly @var{args}.\n" | |
778 | "The format required for @var{address}\n" | |
779 | "and @var{args} depends on the family of the socket.\n\n" | |
b380b885 | 780 | "For a socket of family @code{AF_UNIX},\n" |
3453619b | 781 | "only @var{address} is specified and must be a string with the\n" |
b380b885 MD |
782 | "filename where the socket is to be created.\n\n" |
783 | "For a socket of family @code{AF_INET},\n" | |
3453619b GH |
784 | "@var{address} must be an integer IPv4 host address and\n" |
785 | "@var{args} must be a single integer port number.\n\n" | |
786 | "For a socket of family @code{AF_INET6},\n" | |
787 | "@var{address} must be an integer IPv6 host address and\n" | |
788 | "@var{args} may be up to three integers:\n" | |
789 | "port [flowinfo] [scope_id],\n" | |
790 | "where flowinfo and scope_id default to zero.\n\n" | |
b380b885 | 791 | "The return value is unspecified.") |
1bbd0b84 | 792 | #define FUNC_NAME s_scm_connect |
0f2d19dd | 793 | { |
370312ae GH |
794 | int fd; |
795 | struct sockaddr *soka; | |
439006bf | 796 | int size; |
0f2d19dd | 797 | |
78446828 | 798 | sock = SCM_COERCE_OUTPORT (sock); |
34d19ef6 | 799 | SCM_VALIDATE_OPFPORT (1, sock); |
ee149d03 | 800 | fd = SCM_FPORT_FDES (sock); |
7cee5b31 | 801 | soka = scm_fill_sockaddr (scm_to_int (fam), address, &args, 3, FUNC_NAME, |
439006bf | 802 | &size); |
370312ae | 803 | if (connect (fd, soka, size) == -1) |
439006bf GH |
804 | { |
805 | int save_errno = errno; | |
806 | ||
807 | free (soka); | |
808 | errno = save_errno; | |
809 | SCM_SYSERROR; | |
810 | } | |
811 | free (soka); | |
370312ae | 812 | return SCM_UNSPECIFIED; |
0f2d19dd | 813 | } |
1bbd0b84 | 814 | #undef FUNC_NAME |
0f2d19dd | 815 | |
a1ec6916 | 816 | SCM_DEFINE (scm_bind, "bind", 3, 0, 1, |
1bbd0b84 | 817 | (SCM sock, SCM fam, SCM address, SCM args), |
eefae538 | 818 | "Assign an address to the socket port @var{sock}.\n" |
b380b885 MD |
819 | "Generally this only needs to be done for server sockets,\n" |
820 | "so they know where to look for incoming connections. A socket\n" | |
821 | "without an address will be assigned one automatically when it\n" | |
822 | "starts communicating.\n\n" | |
eefae538 GH |
823 | "The format of @var{address} and @var{args} depends\n" |
824 | "on the family of the socket.\n\n" | |
b380b885 | 825 | "For a socket of family @code{AF_UNIX}, only @var{address}\n" |
eefae538 GH |
826 | "is specified and must be a string with the filename where\n" |
827 | "the socket is to be created.\n\n" | |
828 | "For a socket of family @code{AF_INET}, @var{address}\n" | |
829 | "must be an integer IPv4 address and @var{args}\n" | |
830 | "must be a single integer port number.\n\n" | |
831 | "The values of the following variables can also be used for\n" | |
832 | "@var{address}:\n\n" | |
b380b885 MD |
833 | "@defvar INADDR_ANY\n" |
834 | "Allow connections from any address.\n" | |
835 | "@end defvar\n\n" | |
836 | "@defvar INADDR_LOOPBACK\n" | |
837 | "The address of the local host using the loopback device.\n" | |
838 | "@end defvar\n\n" | |
839 | "@defvar INADDR_BROADCAST\n" | |
840 | "The broadcast address on the local network.\n" | |
841 | "@end defvar\n\n" | |
842 | "@defvar INADDR_NONE\n" | |
843 | "No address.\n" | |
844 | "@end defvar\n\n" | |
eefae538 GH |
845 | "For a socket of family @code{AF_INET6}, @var{address}\n" |
846 | "must be an integer IPv6 address and @var{args}\n" | |
847 | "may be up to three integers:\n" | |
848 | "port [flowinfo] [scope_id],\n" | |
849 | "where flowinfo and scope_id default to zero.\n\n" | |
b380b885 | 850 | "The return value is unspecified.") |
1bbd0b84 | 851 | #define FUNC_NAME s_scm_bind |
370312ae | 852 | { |
370312ae | 853 | struct sockaddr *soka; |
439006bf | 854 | int size; |
370312ae GH |
855 | int fd; |
856 | ||
78446828 | 857 | sock = SCM_COERCE_OUTPORT (sock); |
439006bf | 858 | SCM_VALIDATE_OPFPORT (1, sock); |
7cee5b31 | 859 | soka = scm_fill_sockaddr (scm_to_int (fam), address, &args, 3, FUNC_NAME, |
439006bf | 860 | &size); |
ee149d03 | 861 | fd = SCM_FPORT_FDES (sock); |
439006bf GH |
862 | if (bind (fd, soka, size) == -1) |
863 | { | |
864 | int save_errno = errno; | |
865 | ||
866 | free (soka); | |
867 | errno = save_errno; | |
1bbd0b84 | 868 | SCM_SYSERROR; |
439006bf GH |
869 | } |
870 | free (soka); | |
370312ae GH |
871 | return SCM_UNSPECIFIED; |
872 | } | |
1bbd0b84 | 873 | #undef FUNC_NAME |
370312ae | 874 | |
a1ec6916 | 875 | SCM_DEFINE (scm_listen, "listen", 2, 0, 0, |
1bbd0b84 | 876 | (SCM sock, SCM backlog), |
eefae538 | 877 | "Enable @var{sock} to accept connection\n" |
b380b885 MD |
878 | "requests. @var{backlog} is an integer specifying\n" |
879 | "the maximum length of the queue for pending connections.\n" | |
eefae538 GH |
880 | "If the queue fills, new clients will fail to connect until\n" |
881 | "the server calls @code{accept} to accept a connection from\n" | |
882 | "the queue.\n\n" | |
b380b885 | 883 | "The return value is unspecified.") |
1bbd0b84 | 884 | #define FUNC_NAME s_scm_listen |
370312ae GH |
885 | { |
886 | int fd; | |
78446828 | 887 | sock = SCM_COERCE_OUTPORT (sock); |
34d19ef6 | 888 | SCM_VALIDATE_OPFPORT (1, sock); |
ee149d03 | 889 | fd = SCM_FPORT_FDES (sock); |
7cee5b31 | 890 | if (listen (fd, scm_to_int (backlog)) == -1) |
1bbd0b84 | 891 | SCM_SYSERROR; |
370312ae GH |
892 | return SCM_UNSPECIFIED; |
893 | } | |
1bbd0b84 | 894 | #undef FUNC_NAME |
370312ae GH |
895 | |
896 | /* Put the components of a sockaddr into a new SCM vector. */ | |
370312ae | 897 | static SCM |
aca23b65 MV |
898 | scm_addr_vector (const struct sockaddr *address, int addr_size, |
899 | const char *proc) | |
0f2d19dd | 900 | { |
370312ae | 901 | short int fam = address->sa_family; |
1d1559ce | 902 | SCM result =SCM_EOL; |
34d19ef6 | 903 | |
439006bf | 904 | |
5a525b84 | 905 | switch (fam) |
0f2d19dd | 906 | { |
5a525b84 GH |
907 | case AF_INET: |
908 | { | |
e1368a8d | 909 | const struct sockaddr_in *nad = (struct sockaddr_in *) address; |
439006bf | 910 | |
1d1559ce | 911 | result = scm_c_make_vector (3, SCM_UNSPECIFIED); |
34d19ef6 | 912 | |
4057a3e0 MV |
913 | SCM_SIMPLE_VECTOR_SET(result, 0, |
914 | scm_from_short (fam)); | |
915 | SCM_SIMPLE_VECTOR_SET(result, 1, | |
916 | scm_from_ulong (ntohl (nad->sin_addr.s_addr))); | |
917 | SCM_SIMPLE_VECTOR_SET(result, 2, | |
918 | scm_from_ushort (ntohs (nad->sin_port))); | |
5a525b84 GH |
919 | } |
920 | break; | |
a57a0b1e | 921 | #ifdef HAVE_IPV6 |
5a525b84 GH |
922 | case AF_INET6: |
923 | { | |
e1368a8d | 924 | const struct sockaddr_in6 *nad = (struct sockaddr_in6 *) address; |
5a525b84 | 925 | |
1d1559ce | 926 | result = scm_c_make_vector (5, SCM_UNSPECIFIED); |
4057a3e0 MV |
927 | SCM_SIMPLE_VECTOR_SET(result, 0, scm_from_short (fam)); |
928 | SCM_SIMPLE_VECTOR_SET(result, 1, scm_from_ipv6 (nad->sin6_addr.s6_addr)); | |
929 | SCM_SIMPLE_VECTOR_SET(result, 2, scm_from_ushort (ntohs (nad->sin6_port))); | |
930 | SCM_SIMPLE_VECTOR_SET(result, 3, scm_from_uint32 (nad->sin6_flowinfo)); | |
5a525b84 | 931 | #ifdef HAVE_SIN6_SCOPE_ID |
4057a3e0 | 932 | SCM_SIMPLE_VECTOR_SET(result, 4, scm_from_ulong (nad->sin6_scope_id)); |
5a525b84 | 933 | #else |
4057a3e0 | 934 | SCM_SIMPLE_VECTOR_SET(result, 4, SCM_INUM0); |
0e958795 | 935 | #endif |
5a525b84 GH |
936 | } |
937 | break; | |
938 | #endif | |
939 | #ifdef HAVE_UNIX_DOMAIN_SOCKETS | |
940 | case AF_UNIX: | |
941 | { | |
e1368a8d | 942 | const struct sockaddr_un *nad = (struct sockaddr_un *) address; |
439006bf | 943 | |
1d1559ce | 944 | result = scm_c_make_vector (2, SCM_UNSPECIFIED); |
34d19ef6 | 945 | |
4057a3e0 | 946 | SCM_SIMPLE_VECTOR_SET(result, 0, scm_from_short (fam)); |
aca23b65 MV |
947 | /* When addr_size is not enough to cover sun_path, do not try |
948 | to access it. */ | |
949 | if (addr_size <= offsetof (struct sockaddr_un, sun_path)) | |
4057a3e0 | 950 | SCM_SIMPLE_VECTOR_SET(result, 1, SCM_BOOL_F); |
aca23b65 | 951 | else |
4057a3e0 | 952 | SCM_SIMPLE_VECTOR_SET(result, 1, scm_from_locale_string (nad->sun_path)); |
5a525b84 GH |
953 | } |
954 | break; | |
955 | #endif | |
956 | default: | |
957 | scm_misc_error (proc, "Unrecognised address family: ~A", | |
e11e83f3 | 958 | scm_list_1 (scm_from_int (fam))); |
0f2d19dd | 959 | } |
1d1559ce | 960 | return result; |
370312ae GH |
961 | } |
962 | ||
439006bf GH |
963 | /* calculate the size of a buffer large enough to hold any supported |
964 | sockaddr type. if the buffer isn't large enough, certain system | |
965 | calls will return a truncated address. */ | |
370312ae | 966 | |
439006bf GH |
967 | #if defined (HAVE_UNIX_DOMAIN_SOCKETS) |
968 | #define MAX_SIZE_UN sizeof (struct sockaddr_un) | |
0e958795 | 969 | #else |
439006bf | 970 | #define MAX_SIZE_UN 0 |
0e958795 | 971 | #endif |
439006bf | 972 | |
a57a0b1e | 973 | #if defined (HAVE_IPV6) |
5a525b84 GH |
974 | #define MAX_SIZE_IN6 sizeof (struct sockaddr_in6) |
975 | #else | |
976 | #define MAX_SIZE_IN6 0 | |
977 | #endif | |
978 | ||
979 | #define MAX_ADDR_SIZE max (max (sizeof (struct sockaddr_in), MAX_SIZE_IN6),\ | |
980 | MAX_SIZE_UN) | |
0f2d19dd | 981 | |
a1ec6916 | 982 | SCM_DEFINE (scm_accept, "accept", 1, 0, 0, |
1bbd0b84 | 983 | (SCM sock), |
eefae538 GH |
984 | "Accept a connection on a bound, listening socket.\n" |
985 | "If there\n" | |
986 | "are no pending connections in the queue, wait until\n" | |
987 | "one is available unless the non-blocking option has been\n" | |
988 | "set on the socket.\n\n" | |
b380b885 | 989 | "The return value is a\n" |
eefae538 GH |
990 | "pair in which the @emph{car} is a new socket port for the\n" |
991 | "connection and\n" | |
992 | "the @emph{cdr} is an object with address information about the\n" | |
993 | "client which initiated the connection.\n\n" | |
994 | "@var{sock} does not become part of the\n" | |
b380b885 | 995 | "connection and will continue to accept new requests.") |
1bbd0b84 | 996 | #define FUNC_NAME s_scm_accept |
0f2d19dd | 997 | { |
370312ae GH |
998 | int fd; |
999 | int newfd; | |
1000 | SCM address; | |
1001 | SCM newsock; | |
439006bf GH |
1002 | int addr_size = MAX_ADDR_SIZE; |
1003 | char max_addr[MAX_ADDR_SIZE]; | |
1004 | struct sockaddr *addr = (struct sockaddr *) max_addr; | |
370312ae | 1005 | |
78446828 | 1006 | sock = SCM_COERCE_OUTPORT (sock); |
439006bf | 1007 | SCM_VALIDATE_OPFPORT (1, sock); |
ee149d03 | 1008 | fd = SCM_FPORT_FDES (sock); |
439006bf GH |
1009 | newfd = accept (fd, addr, &addr_size); |
1010 | if (newfd == -1) | |
1011 | SCM_SYSERROR; | |
1012 | newsock = SCM_SOCK_FD_TO_PORT (newfd); | |
aca23b65 | 1013 | address = scm_addr_vector (addr, addr_size, FUNC_NAME); |
370312ae | 1014 | return scm_cons (newsock, address); |
0f2d19dd | 1015 | } |
1bbd0b84 | 1016 | #undef FUNC_NAME |
0f2d19dd | 1017 | |
a1ec6916 | 1018 | SCM_DEFINE (scm_getsockname, "getsockname", 1, 0, 0, |
1bbd0b84 | 1019 | (SCM sock), |
eefae538 | 1020 | "Return the address of @var{sock}, in the same form as the\n" |
1e6808ea MG |
1021 | "object returned by @code{accept}. On many systems the address\n" |
1022 | "of a socket in the @code{AF_FILE} namespace cannot be read.") | |
1bbd0b84 | 1023 | #define FUNC_NAME s_scm_getsockname |
0f2d19dd | 1024 | { |
370312ae | 1025 | int fd; |
439006bf GH |
1026 | int addr_size = MAX_ADDR_SIZE; |
1027 | char max_addr[MAX_ADDR_SIZE]; | |
1028 | struct sockaddr *addr = (struct sockaddr *) max_addr; | |
1029 | ||
78446828 | 1030 | sock = SCM_COERCE_OUTPORT (sock); |
34d19ef6 | 1031 | SCM_VALIDATE_OPFPORT (1, sock); |
ee149d03 | 1032 | fd = SCM_FPORT_FDES (sock); |
439006bf | 1033 | if (getsockname (fd, addr, &addr_size) == -1) |
1bbd0b84 | 1034 | SCM_SYSERROR; |
aca23b65 | 1035 | return scm_addr_vector (addr, addr_size, FUNC_NAME); |
0f2d19dd | 1036 | } |
1bbd0b84 | 1037 | #undef FUNC_NAME |
0f2d19dd | 1038 | |
a1ec6916 | 1039 | SCM_DEFINE (scm_getpeername, "getpeername", 1, 0, 0, |
1bbd0b84 | 1040 | (SCM sock), |
eefae538 | 1041 | "Return the address that @var{sock}\n" |
1e6808ea MG |
1042 | "is connected to, in the same form as the object returned by\n" |
1043 | "@code{accept}. On many systems the address of a socket in the\n" | |
1044 | "@code{AF_FILE} namespace cannot be read.") | |
1bbd0b84 | 1045 | #define FUNC_NAME s_scm_getpeername |
0f2d19dd | 1046 | { |
370312ae | 1047 | int fd; |
439006bf GH |
1048 | int addr_size = MAX_ADDR_SIZE; |
1049 | char max_addr[MAX_ADDR_SIZE]; | |
1050 | struct sockaddr *addr = (struct sockaddr *) max_addr; | |
1051 | ||
78446828 | 1052 | sock = SCM_COERCE_OUTPORT (sock); |
34d19ef6 | 1053 | SCM_VALIDATE_OPFPORT (1, sock); |
ee149d03 | 1054 | fd = SCM_FPORT_FDES (sock); |
439006bf | 1055 | if (getpeername (fd, addr, &addr_size) == -1) |
1bbd0b84 | 1056 | SCM_SYSERROR; |
aca23b65 | 1057 | return scm_addr_vector (addr, addr_size, FUNC_NAME); |
0f2d19dd | 1058 | } |
1bbd0b84 | 1059 | #undef FUNC_NAME |
0f2d19dd | 1060 | |
a1ec6916 | 1061 | SCM_DEFINE (scm_recv, "recv!", 2, 1, 0, |
1bbd0b84 | 1062 | (SCM sock, SCM buf, SCM flags), |
eefae538 GH |
1063 | "Receive data from a socket port.\n" |
1064 | "@var{sock} must already\n" | |
b380b885 MD |
1065 | "be bound to the address from which data is to be received.\n" |
1066 | "@var{buf} is a string into which\n" | |
eefae538 GH |
1067 | "the data will be written. The size of @var{buf} limits\n" |
1068 | "the amount of\n" | |
b380b885 | 1069 | "data which can be received: in the case of packet\n" |
eefae538 GH |
1070 | "protocols, if a packet larger than this limit is encountered\n" |
1071 | "then some data\n" | |
b380b885 MD |
1072 | "will be irrevocably lost.\n\n" |
1073 | "The optional @var{flags} argument is a value or\n" | |
1074 | "bitwise OR of MSG_OOB, MSG_PEEK, MSG_DONTROUTE etc.\n\n" | |
eefae538 GH |
1075 | "The value returned is the number of bytes read from the\n" |
1076 | "socket.\n\n" | |
1077 | "Note that the data is read directly from the socket file\n" | |
1078 | "descriptor:\n" | |
09831f94 | 1079 | "any unread buffered port data is ignored.") |
1bbd0b84 | 1080 | #define FUNC_NAME s_scm_recv |
0f2d19dd | 1081 | { |
370312ae GH |
1082 | int rv; |
1083 | int fd; | |
1084 | int flg; | |
cc95e00a MV |
1085 | char *dest; |
1086 | size_t len; | |
370312ae | 1087 | |
34d19ef6 HWN |
1088 | SCM_VALIDATE_OPFPORT (1, sock); |
1089 | SCM_VALIDATE_STRING (2, buf); | |
7cee5b31 MV |
1090 | if (SCM_UNBNDP (flags)) |
1091 | flg = 0; | |
1092 | else | |
1093 | flg = scm_to_int (flags); | |
ee149d03 | 1094 | fd = SCM_FPORT_FDES (sock); |
370312ae | 1095 | |
cc95e00a MV |
1096 | len = scm_i_string_length (buf); |
1097 | dest = scm_i_string_writable_chars (buf); | |
1098 | SCM_SYSCALL (rv = recv (fd, dest, len, flg)); | |
1099 | scm_i_string_stop_writing (); | |
1100 | ||
370312ae | 1101 | if (rv == -1) |
1bbd0b84 | 1102 | SCM_SYSERROR; |
370312ae | 1103 | |
396e5506 | 1104 | scm_remember_upto_here_1 (buf); |
7cee5b31 | 1105 | return scm_from_int (rv); |
370312ae | 1106 | } |
1bbd0b84 | 1107 | #undef FUNC_NAME |
370312ae | 1108 | |
a1ec6916 | 1109 | SCM_DEFINE (scm_send, "send", 2, 1, 0, |
1bbd0b84 | 1110 | (SCM sock, SCM message, SCM flags), |
eefae538 GH |
1111 | "Transmit the string @var{message} on a socket port @var{sock}.\n" |
1112 | "@var{sock} must already be bound to a destination address. The\n" | |
1113 | "value returned is the number of bytes transmitted --\n" | |
1114 | "it's possible for\n" | |
1115 | "this to be less than the length of @var{message}\n" | |
1116 | "if the socket is\n" | |
1117 | "set to be non-blocking. The optional @var{flags} argument\n" | |
1118 | "is a value or\n" | |
b380b885 | 1119 | "bitwise OR of MSG_OOB, MSG_PEEK, MSG_DONTROUTE etc.\n\n" |
eefae538 GH |
1120 | "Note that the data is written directly to the socket\n" |
1121 | "file descriptor:\n" | |
b380b885 | 1122 | "any unflushed buffered port data is ignored.") |
1bbd0b84 | 1123 | #define FUNC_NAME s_scm_send |
370312ae GH |
1124 | { |
1125 | int rv; | |
1126 | int fd; | |
1127 | int flg; | |
cc95e00a MV |
1128 | const char *src; |
1129 | size_t len; | |
370312ae | 1130 | |
78446828 | 1131 | sock = SCM_COERCE_OUTPORT (sock); |
34d19ef6 | 1132 | SCM_VALIDATE_OPFPORT (1, sock); |
a6d9e5ab | 1133 | SCM_VALIDATE_STRING (2, message); |
7cee5b31 MV |
1134 | if (SCM_UNBNDP (flags)) |
1135 | flg = 0; | |
1136 | else | |
1137 | flg = scm_to_int (flags); | |
ee149d03 | 1138 | fd = SCM_FPORT_FDES (sock); |
370312ae | 1139 | |
cc95e00a MV |
1140 | len = scm_i_string_length (message); |
1141 | src = scm_i_string_writable_chars (message); | |
1142 | SCM_SYSCALL (rv = send (fd, src, len, flg)); | |
1143 | scm_i_string_stop_writing (); | |
1144 | ||
370312ae | 1145 | if (rv == -1) |
1bbd0b84 | 1146 | SCM_SYSERROR; |
396e5506 MV |
1147 | |
1148 | scm_remember_upto_here_1 (message); | |
7cee5b31 | 1149 | return scm_from_int (rv); |
370312ae | 1150 | } |
1bbd0b84 | 1151 | #undef FUNC_NAME |
370312ae | 1152 | |
a1ec6916 | 1153 | SCM_DEFINE (scm_recvfrom, "recvfrom!", 2, 3, 0, |
60d02d09 | 1154 | (SCM sock, SCM str, SCM flags, SCM start, SCM end), |
eefae538 | 1155 | "Return data from the socket port @var{sock} and also\n" |
1e6808ea | 1156 | "information about where the data was received from.\n" |
eefae538 | 1157 | "@var{sock} must already be bound to the address from which\n" |
1e6808ea MG |
1158 | "data is to be received. @code{str}, is a string into which the\n" |
1159 | "data will be written. The size of @var{str} limits the amount\n" | |
1160 | "of data which can be received: in the case of packet protocols,\n" | |
1161 | "if a packet larger than this limit is encountered then some\n" | |
eefae538 | 1162 | "data will be irrevocably lost.\n\n" |
1e6808ea | 1163 | "The optional @var{flags} argument is a value or bitwise OR of\n" |
eefae538 | 1164 | "@code{MSG_OOB}, @code{MSG_PEEK}, @code{MSG_DONTROUTE} etc.\n\n" |
1e6808ea MG |
1165 | "The value returned is a pair: the @emph{car} is the number of\n" |
1166 | "bytes read from the socket and the @emph{cdr} an address object\n" | |
eefae538 GH |
1167 | "in the same form as returned by @code{accept}. The address\n" |
1168 | "will given as @code{#f} if not available, as is usually the\n" | |
1169 | "case for stream sockets.\n\n" | |
1e6808ea | 1170 | "The @var{start} and @var{end} arguments specify a substring of\n" |
eefae538 | 1171 | "@var{str} to which the data should be written.\n\n" |
1e6808ea MG |
1172 | "Note that the data is read directly from the socket file\n" |
1173 | "descriptor: any unread buffered port data is ignored.") | |
1bbd0b84 | 1174 | #define FUNC_NAME s_scm_recvfrom |
370312ae GH |
1175 | { |
1176 | int rv; | |
1177 | int fd; | |
1178 | int flg; | |
60d02d09 | 1179 | char *buf; |
396e5506 MV |
1180 | size_t offset; |
1181 | size_t cend; | |
370312ae | 1182 | SCM address; |
439006bf GH |
1183 | int addr_size = MAX_ADDR_SIZE; |
1184 | char max_addr[MAX_ADDR_SIZE]; | |
1185 | struct sockaddr *addr = (struct sockaddr *) max_addr; | |
370312ae | 1186 | |
34d19ef6 | 1187 | SCM_VALIDATE_OPFPORT (1, sock); |
60d02d09 | 1188 | fd = SCM_FPORT_FDES (sock); |
396e5506 MV |
1189 | |
1190 | SCM_VALIDATE_STRING (2, str); | |
cc95e00a | 1191 | scm_i_get_substring_spec (scm_i_string_length (str), |
396e5506 MV |
1192 | start, &offset, end, &cend); |
1193 | ||
1146b6cd GH |
1194 | if (SCM_UNBNDP (flags)) |
1195 | flg = 0; | |
370312ae | 1196 | else |
60d02d09 | 1197 | SCM_VALIDATE_ULONG_COPY (3, flags, flg); |
370312ae | 1198 | |
97d0e20b GH |
1199 | /* recvfrom will not necessarily return an address. usually nothing |
1200 | is returned for stream sockets. */ | |
cc95e00a | 1201 | buf = scm_i_string_writable_chars (str); |
439006bf | 1202 | addr->sa_family = AF_UNSPEC; |
60d02d09 | 1203 | SCM_SYSCALL (rv = recvfrom (fd, buf + offset, |
1146b6cd | 1204 | cend - offset, flg, |
439006bf | 1205 | addr, &addr_size)); |
cc95e00a MV |
1206 | scm_i_string_stop_writing (); |
1207 | ||
370312ae | 1208 | if (rv == -1) |
1bbd0b84 | 1209 | SCM_SYSERROR; |
eefae538 | 1210 | if (addr->sa_family != AF_UNSPEC) |
aca23b65 | 1211 | address = scm_addr_vector (addr, addr_size, FUNC_NAME); |
370312ae GH |
1212 | else |
1213 | address = SCM_BOOL_F; | |
1214 | ||
396e5506 | 1215 | scm_remember_upto_here_1 (str); |
e11e83f3 | 1216 | return scm_cons (scm_from_int (rv), address); |
0f2d19dd | 1217 | } |
1bbd0b84 | 1218 | #undef FUNC_NAME |
0f2d19dd | 1219 | |
a1ec6916 | 1220 | SCM_DEFINE (scm_sendto, "sendto", 4, 0, 1, |
1bbd0b84 | 1221 | (SCM sock, SCM message, SCM fam, SCM address, SCM args_and_flags), |
eefae538 GH |
1222 | "Transmit the string @var{message} on the socket port\n" |
1223 | "@var{sock}. The\n" | |
1224 | "destination address is specified using the @var{fam},\n" | |
1225 | "@var{address} and\n" | |
1226 | "@var{args_and_flags} arguments, in a similar way to the\n" | |
1227 | "@code{connect} procedure. @var{args_and_flags} contains\n" | |
1228 | "the usual connection arguments optionally followed by\n" | |
1229 | "a flags argument, which is a value or\n" | |
b380b885 | 1230 | "bitwise OR of MSG_OOB, MSG_PEEK, MSG_DONTROUTE etc.\n\n" |
eefae538 GH |
1231 | "The value returned is the number of bytes transmitted --\n" |
1232 | "it's possible for\n" | |
1233 | "this to be less than the length of @var{message} if the\n" | |
1234 | "socket is\n" | |
1235 | "set to be non-blocking.\n" | |
1236 | "Note that the data is written directly to the socket\n" | |
1237 | "file descriptor:\n" | |
b380b885 | 1238 | "any unflushed buffered port data is ignored.") |
1bbd0b84 | 1239 | #define FUNC_NAME s_scm_sendto |
370312ae GH |
1240 | { |
1241 | int rv; | |
1242 | int fd; | |
1243 | int flg; | |
1244 | struct sockaddr *soka; | |
439006bf | 1245 | int size; |
370312ae | 1246 | |
78446828 | 1247 | sock = SCM_COERCE_OUTPORT (sock); |
34d19ef6 | 1248 | SCM_VALIDATE_FPORT (1, sock); |
a6d9e5ab | 1249 | SCM_VALIDATE_STRING (2, message); |
ee149d03 | 1250 | fd = SCM_FPORT_FDES (sock); |
7cee5b31 | 1251 | soka = scm_fill_sockaddr (scm_to_int (fam), address, &args_and_flags, 4, |
1bbd0b84 | 1252 | FUNC_NAME, &size); |
d2e53ed6 | 1253 | if (scm_is_null (args_and_flags)) |
370312ae GH |
1254 | flg = 0; |
1255 | else | |
1256 | { | |
34d19ef6 | 1257 | SCM_VALIDATE_CONS (5, args_and_flags); |
e4b265d8 | 1258 | flg = SCM_NUM2ULONG (5, SCM_CAR (args_and_flags)); |
370312ae | 1259 | } |
396e5506 | 1260 | SCM_SYSCALL (rv = sendto (fd, |
cc95e00a MV |
1261 | scm_i_string_chars (message), |
1262 | scm_i_string_length (message), | |
ae2fa5bc | 1263 | flg, soka, size)); |
370312ae | 1264 | if (rv == -1) |
439006bf GH |
1265 | { |
1266 | int save_errno = errno; | |
1267 | free (soka); | |
1268 | errno = save_errno; | |
1269 | SCM_SYSERROR; | |
1270 | } | |
1271 | free (soka); | |
396e5506 MV |
1272 | |
1273 | scm_remember_upto_here_1 (message); | |
e11e83f3 | 1274 | return scm_from_int (rv); |
370312ae | 1275 | } |
1bbd0b84 | 1276 | #undef FUNC_NAME |
370312ae GH |
1277 | \f |
1278 | ||
1279 | ||
1280 | void | |
0f2d19dd | 1281 | scm_init_socket () |
0f2d19dd | 1282 | { |
370312ae GH |
1283 | /* protocol families. */ |
1284 | #ifdef AF_UNSPEC | |
e11e83f3 | 1285 | scm_c_define ("AF_UNSPEC", scm_from_int (AF_UNSPEC)); |
370312ae GH |
1286 | #endif |
1287 | #ifdef AF_UNIX | |
e11e83f3 | 1288 | scm_c_define ("AF_UNIX", scm_from_int (AF_UNIX)); |
370312ae GH |
1289 | #endif |
1290 | #ifdef AF_INET | |
e11e83f3 | 1291 | scm_c_define ("AF_INET", scm_from_int (AF_INET)); |
370312ae | 1292 | #endif |
3453619b | 1293 | #ifdef AF_INET6 |
e11e83f3 | 1294 | scm_c_define ("AF_INET6", scm_from_int (AF_INET6)); |
3453619b | 1295 | #endif |
370312ae GH |
1296 | |
1297 | #ifdef PF_UNSPEC | |
e11e83f3 | 1298 | scm_c_define ("PF_UNSPEC", scm_from_int (PF_UNSPEC)); |
370312ae GH |
1299 | #endif |
1300 | #ifdef PF_UNIX | |
e11e83f3 | 1301 | scm_c_define ("PF_UNIX", scm_from_int (PF_UNIX)); |
370312ae GH |
1302 | #endif |
1303 | #ifdef PF_INET | |
e11e83f3 | 1304 | scm_c_define ("PF_INET", scm_from_int (PF_INET)); |
370312ae | 1305 | #endif |
3453619b | 1306 | #ifdef PF_INET6 |
e11e83f3 | 1307 | scm_c_define ("PF_INET6", scm_from_int (PF_INET6)); |
3453619b | 1308 | #endif |
370312ae | 1309 | |
66c73b76 GH |
1310 | /* standard addresses. */ |
1311 | #ifdef INADDR_ANY | |
b9bd8526 | 1312 | scm_c_define ("INADDR_ANY", scm_from_ulong (INADDR_ANY)); |
66c73b76 GH |
1313 | #endif |
1314 | #ifdef INADDR_BROADCAST | |
b9bd8526 | 1315 | scm_c_define ("INADDR_BROADCAST", scm_from_ulong (INADDR_BROADCAST)); |
66c73b76 GH |
1316 | #endif |
1317 | #ifdef INADDR_NONE | |
b9bd8526 | 1318 | scm_c_define ("INADDR_NONE", scm_from_ulong (INADDR_NONE)); |
66c73b76 GH |
1319 | #endif |
1320 | #ifdef INADDR_LOOPBACK | |
b9bd8526 | 1321 | scm_c_define ("INADDR_LOOPBACK", scm_from_ulong (INADDR_LOOPBACK)); |
66c73b76 GH |
1322 | #endif |
1323 | ||
6f637a1b KR |
1324 | /* socket types. |
1325 | ||
1326 | SOCK_PACKET is deliberately omitted, the GNU/Linux socket(2) and | |
1327 | packet(7) advise that it's obsolete and strongly deprecated. */ | |
1328 | ||
370312ae | 1329 | #ifdef SOCK_STREAM |
e11e83f3 | 1330 | scm_c_define ("SOCK_STREAM", scm_from_int (SOCK_STREAM)); |
370312ae GH |
1331 | #endif |
1332 | #ifdef SOCK_DGRAM | |
e11e83f3 | 1333 | scm_c_define ("SOCK_DGRAM", scm_from_int (SOCK_DGRAM)); |
370312ae | 1334 | #endif |
6f637a1b KR |
1335 | #ifdef SOCK_SEQPACKET |
1336 | scm_c_define ("SOCK_SEQPACKET", scm_from_int (SOCK_SEQPACKET)); | |
1337 | #endif | |
370312ae | 1338 | #ifdef SOCK_RAW |
e11e83f3 | 1339 | scm_c_define ("SOCK_RAW", scm_from_int (SOCK_RAW)); |
370312ae | 1340 | #endif |
6f637a1b KR |
1341 | #ifdef SOCK_RDM |
1342 | scm_c_define ("SOCK_RDM", scm_from_int (SOCK_RDM)); | |
1343 | #endif | |
370312ae GH |
1344 | |
1345 | /* setsockopt level. */ | |
1346 | #ifdef SOL_SOCKET | |
e11e83f3 | 1347 | scm_c_define ("SOL_SOCKET", scm_from_int (SOL_SOCKET)); |
370312ae GH |
1348 | #endif |
1349 | #ifdef SOL_IP | |
e11e83f3 | 1350 | scm_c_define ("SOL_IP", scm_from_int (SOL_IP)); |
370312ae GH |
1351 | #endif |
1352 | #ifdef SOL_TCP | |
e11e83f3 | 1353 | scm_c_define ("SOL_TCP", scm_from_int (SOL_TCP)); |
370312ae GH |
1354 | #endif |
1355 | #ifdef SOL_UDP | |
e11e83f3 | 1356 | scm_c_define ("SOL_UDP", scm_from_int (SOL_UDP)); |
370312ae GH |
1357 | #endif |
1358 | ||
1359 | /* setsockopt names. */ | |
1360 | #ifdef SO_DEBUG | |
e11e83f3 | 1361 | scm_c_define ("SO_DEBUG", scm_from_int (SO_DEBUG)); |
370312ae GH |
1362 | #endif |
1363 | #ifdef SO_REUSEADDR | |
e11e83f3 | 1364 | scm_c_define ("SO_REUSEADDR", scm_from_int (SO_REUSEADDR)); |
370312ae GH |
1365 | #endif |
1366 | #ifdef SO_STYLE | |
e11e83f3 | 1367 | scm_c_define ("SO_STYLE", scm_from_int (SO_STYLE)); |
370312ae GH |
1368 | #endif |
1369 | #ifdef SO_TYPE | |
e11e83f3 | 1370 | scm_c_define ("SO_TYPE", scm_from_int (SO_TYPE)); |
370312ae GH |
1371 | #endif |
1372 | #ifdef SO_ERROR | |
e11e83f3 | 1373 | scm_c_define ("SO_ERROR", scm_from_int (SO_ERROR)); |
370312ae GH |
1374 | #endif |
1375 | #ifdef SO_DONTROUTE | |
e11e83f3 | 1376 | scm_c_define ("SO_DONTROUTE", scm_from_int (SO_DONTROUTE)); |
370312ae GH |
1377 | #endif |
1378 | #ifdef SO_BROADCAST | |
e11e83f3 | 1379 | scm_c_define ("SO_BROADCAST", scm_from_int (SO_BROADCAST)); |
370312ae GH |
1380 | #endif |
1381 | #ifdef SO_SNDBUF | |
e11e83f3 | 1382 | scm_c_define ("SO_SNDBUF", scm_from_int (SO_SNDBUF)); |
370312ae GH |
1383 | #endif |
1384 | #ifdef SO_RCVBUF | |
e11e83f3 | 1385 | scm_c_define ("SO_RCVBUF", scm_from_int (SO_RCVBUF)); |
370312ae GH |
1386 | #endif |
1387 | #ifdef SO_KEEPALIVE | |
e11e83f3 | 1388 | scm_c_define ("SO_KEEPALIVE", scm_from_int (SO_KEEPALIVE)); |
370312ae GH |
1389 | #endif |
1390 | #ifdef SO_OOBINLINE | |
e11e83f3 | 1391 | scm_c_define ("SO_OOBINLINE", scm_from_int (SO_OOBINLINE)); |
370312ae GH |
1392 | #endif |
1393 | #ifdef SO_NO_CHECK | |
e11e83f3 | 1394 | scm_c_define ("SO_NO_CHECK", scm_from_int (SO_NO_CHECK)); |
370312ae GH |
1395 | #endif |
1396 | #ifdef SO_PRIORITY | |
e11e83f3 | 1397 | scm_c_define ("SO_PRIORITY", scm_from_int (SO_PRIORITY)); |
370312ae GH |
1398 | #endif |
1399 | #ifdef SO_LINGER | |
e11e83f3 | 1400 | scm_c_define ("SO_LINGER", scm_from_int (SO_LINGER)); |
370312ae GH |
1401 | #endif |
1402 | ||
1403 | /* recv/send options. */ | |
1404 | #ifdef MSG_OOB | |
e11e83f3 | 1405 | scm_c_define ("MSG_OOB", scm_from_int (MSG_OOB)); |
370312ae GH |
1406 | #endif |
1407 | #ifdef MSG_PEEK | |
e11e83f3 | 1408 | scm_c_define ("MSG_PEEK", scm_from_int (MSG_PEEK)); |
370312ae GH |
1409 | #endif |
1410 | #ifdef MSG_DONTROUTE | |
e11e83f3 | 1411 | scm_c_define ("MSG_DONTROUTE", scm_from_int (MSG_DONTROUTE)); |
370312ae GH |
1412 | #endif |
1413 | ||
b4e15479 SJ |
1414 | #ifdef __MINGW32__ |
1415 | scm_i_init_socket_Win32 (); | |
1416 | #endif | |
1417 | ||
0f2d19dd | 1418 | scm_add_feature ("socket"); |
370312ae | 1419 | |
a0599745 | 1420 | #include "libguile/socket.x" |
0f2d19dd JB |
1421 | } |
1422 | ||
89e00824 ML |
1423 | |
1424 | /* | |
1425 | Local Variables: | |
1426 | c-file-style: "gnu" | |
1427 | End: | |
1428 | */ |