1999-09-14 09:32:05 -04:00
|
|
|
/*
|
|
|
|
* Scheme48/scsh network interface.
|
|
|
|
* Routines that require custom C support.
|
|
|
|
* Copyright (c) 1994 by Brian D. Carlstrom
|
|
|
|
* Copyright (c) 1994 by Olin Shivers
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "cstuff.h"
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
2000-07-10 14:01:53 -04:00
|
|
|
#include <fcntl.h>
|
1999-09-14 09:32:05 -04:00
|
|
|
/* Make sure our exports match up w/the implementation: */
|
|
|
|
#include "network1.h"
|
2000-07-10 14:01:53 -04:00
|
|
|
#include "scheme48.h"
|
2000-05-16 05:24:54 -04:00
|
|
|
//extern int h_errno;
|
1999-09-14 09:32:05 -04:00
|
|
|
|
2000-07-14 12:30:02 -04:00
|
|
|
/* to extract a 4 byte long value from a scheme string */
|
|
|
|
|
|
|
|
#define GET_LONG(x,n) (*((u_long *)(S48_ADDRESS_AFTER_HEADER((x),unsigned char)+(n*4))))
|
|
|
|
|
|
|
|
#define SET_LONG(x,n,v) GET_LONG((x),(n))=(u_long)(v);
|
|
|
|
|
2000-07-12 13:28:56 -04:00
|
|
|
s48_value long2byte_vector (long number)
|
|
|
|
{
|
|
|
|
s48_value bv = s48_make_byte_vector (4, 0);
|
|
|
|
S48_BYTE_VECTOR_SET(bv, 0, number & 0xff);
|
|
|
|
S48_BYTE_VECTOR_SET(bv, 1, (number >> 8) & 0xff);
|
|
|
|
S48_BYTE_VECTOR_SET(bv, 2, (number >> 16) & 0xff);
|
|
|
|
S48_BYTE_VECTOR_SET(bv, 3, (number >> 24) & 0xff);
|
|
|
|
return bv;
|
|
|
|
}
|
|
|
|
|
|
|
|
long byte_vector2long (s48_value bv)
|
|
|
|
{
|
|
|
|
long number = S48_BYTE_VECTOR_REF (bv, 0);
|
|
|
|
number |= (S48_BYTE_VECTOR_REF (bv, 1) << 8);
|
|
|
|
number |= (S48_BYTE_VECTOR_REF (bv, 2) << 16);
|
|
|
|
number |= (S48_BYTE_VECTOR_REF (bv, 3) << 24);
|
|
|
|
return number;
|
|
|
|
}
|
|
|
|
|
1999-09-14 09:32:05 -04:00
|
|
|
/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
|
2000-07-12 13:28:56 -04:00
|
|
|
s48_value scheme_bind(s48_value sock, s48_value family, s48_value scheme_name)
|
1999-09-14 09:32:05 -04:00
|
|
|
{
|
2000-07-12 13:28:56 -04:00
|
|
|
int sockfd = s48_extract_fixnum (sock);
|
|
|
|
switch(s48_extract_fixnum (family))
|
1999-09-14 09:32:05 -04:00
|
|
|
{
|
|
|
|
case AF_UNIX:
|
|
|
|
{
|
|
|
|
struct sockaddr_un name;
|
1999-09-15 20:20:37 -04:00
|
|
|
int scheme_length=S48_STRING_LENGTH(scheme_name);
|
1999-09-14 09:32:05 -04:00
|
|
|
|
|
|
|
name.sun_family=AF_UNIX;
|
|
|
|
if (scheme_length>=(108-1)) /* save space for \0 */
|
2000-07-12 13:28:56 -04:00
|
|
|
return(-1); // TODO: check this in scheme !
|
1999-09-14 09:32:05 -04:00
|
|
|
strncpy(name.sun_path,
|
1999-10-08 14:19:37 -04:00
|
|
|
s48_extract_string(scheme_name),
|
1999-09-14 09:32:05 -04:00
|
|
|
scheme_length); /* copy to c string */
|
|
|
|
name.sun_path[scheme_length]='\0'; /* add null */
|
2000-07-12 13:28:56 -04:00
|
|
|
if ( bind(sockfd,(struct sockaddr *)&name,sizeof(name)) < 0)
|
|
|
|
s48_raise_os_error (errno);
|
|
|
|
return S48_UNSPECIFIC;
|
1999-09-14 09:32:05 -04:00
|
|
|
}
|
|
|
|
case AF_INET:
|
|
|
|
{
|
|
|
|
struct sockaddr_in name;
|
|
|
|
|
2000-07-12 13:28:56 -04:00
|
|
|
u_long addr = htonl(byte_vector2long (S48_CAR (scheme_name)));
|
|
|
|
u_short port = htons(s48_extract_fixnum (S48_CDR (scheme_name)));
|
2000-07-13 09:45:00 -04:00
|
|
|
|
1999-09-14 09:32:05 -04:00
|
|
|
name.sin_family=AF_INET;
|
|
|
|
name.sin_addr.s_addr=addr;
|
|
|
|
name.sin_port=port;
|
2000-07-12 13:28:56 -04:00
|
|
|
if (bind(sockfd,(struct sockaddr *)&name,sizeof(name)) < 0)
|
|
|
|
s48_raise_os_error (errno);
|
2000-07-13 09:45:00 -04:00
|
|
|
|
2000-07-12 13:28:56 -04:00
|
|
|
return S48_UNSPECIFIC;
|
1999-09-14 09:32:05 -04:00
|
|
|
}
|
|
|
|
default:
|
2000-07-13 09:45:00 -04:00
|
|
|
s48_raise_argtype_error (family);
|
1999-09-14 09:32:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
|
2000-07-10 14:32:45 -04:00
|
|
|
s48_value scheme_connect(s48_value sock, s48_value family, s48_value scheme_name)
|
1999-09-14 09:32:05 -04:00
|
|
|
{
|
2000-07-10 14:32:45 -04:00
|
|
|
int sockfd = s48_extract_fixnum (sock);
|
|
|
|
switch(s48_extract_fixnum (family))
|
1999-09-14 09:32:05 -04:00
|
|
|
{
|
|
|
|
case AF_UNIX:
|
|
|
|
{
|
|
|
|
struct sockaddr_un name;
|
2000-07-10 14:32:45 -04:00
|
|
|
int ret;
|
1999-09-15 20:20:37 -04:00
|
|
|
int scheme_length=S48_STRING_LENGTH(scheme_name);
|
1999-09-14 09:32:05 -04:00
|
|
|
|
|
|
|
name.sun_family=AF_UNIX;
|
|
|
|
if (scheme_length>=(108-1)) /* save space for \0 */
|
|
|
|
return(-1);
|
|
|
|
strncpy(name.sun_path,
|
2000-07-12 13:28:56 -04:00
|
|
|
s48_extract_string (scheme_name),
|
1999-09-14 09:32:05 -04:00
|
|
|
scheme_length); /* copy to c string */
|
|
|
|
name.sun_path[scheme_length]='\0'; /* add null */
|
2000-07-10 14:32:45 -04:00
|
|
|
|
|
|
|
if (connect(sockfd,(struct sockaddr *)&name,sizeof(name)) == 0)
|
|
|
|
return S48_TRUE;
|
|
|
|
|
|
|
|
if (errno != EWOULDBLOCK && errno != EINTR && errno != EALREADY
|
|
|
|
&& errno != EINPROGRESS && errno != EAGAIN)
|
|
|
|
s48_raise_os_error(errno);
|
|
|
|
|
|
|
|
if (! (s48_add_pending_fd(sockfd, 0)))
|
|
|
|
s48_raise_out_of_memory_error();
|
|
|
|
|
2000-07-12 13:28:56 -04:00
|
|
|
if (errno == EINPROGRESS)
|
|
|
|
return s48_enter_fixnum (0);
|
|
|
|
else return S48_FALSE;
|
2000-07-10 14:32:45 -04:00
|
|
|
|
1999-09-14 09:32:05 -04:00
|
|
|
}
|
|
|
|
case AF_INET:
|
|
|
|
{
|
|
|
|
struct sockaddr_in name;
|
|
|
|
|
2000-07-12 13:28:56 -04:00
|
|
|
u_long addr= htonl(byte_vector2long (S48_CAR (scheme_name)));
|
|
|
|
u_short port= htons(s48_extract_fixnum (S48_CDR (scheme_name)));
|
1999-09-14 09:32:05 -04:00
|
|
|
|
|
|
|
name.sin_family=AF_INET;
|
|
|
|
name.sin_addr.s_addr=addr;
|
|
|
|
name.sin_port=port;
|
2000-07-10 14:32:45 -04:00
|
|
|
|
|
|
|
if (connect(sockfd,(struct sockaddr *)&name,sizeof(name)) == 0)
|
|
|
|
return S48_TRUE;
|
|
|
|
|
|
|
|
if (errno != EWOULDBLOCK && errno != EINTR && errno != EALREADY
|
|
|
|
&& errno != EINPROGRESS && errno != EAGAIN)
|
|
|
|
s48_raise_os_error(errno);
|
2000-07-11 06:30:23 -04:00
|
|
|
|
2000-07-10 14:32:45 -04:00
|
|
|
if (! (s48_add_pending_fd(sockfd, 0)))
|
|
|
|
s48_raise_out_of_memory_error();
|
|
|
|
|
2000-07-11 06:30:23 -04:00
|
|
|
if (errno == EINPROGRESS)
|
|
|
|
return s48_enter_fixnum (0);
|
|
|
|
else return S48_FALSE;
|
2000-07-10 14:32:45 -04:00
|
|
|
|
1999-09-14 09:32:05 -04:00
|
|
|
}
|
|
|
|
default:
|
2000-07-13 09:45:00 -04:00
|
|
|
s48_raise_argtype_error (family); /* error unknown address family */
|
1999-09-14 09:32:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
|
2000-07-12 13:28:56 -04:00
|
|
|
s48_value scheme_accept(s48_value sockfd_tagged, s48_value family)
|
1999-09-14 09:32:05 -04:00
|
|
|
{
|
2000-07-10 14:01:53 -04:00
|
|
|
int sockfd = s48_extract_fixnum (sockfd_tagged);
|
|
|
|
switch(s48_extract_fixnum (family))
|
1999-09-14 09:32:05 -04:00
|
|
|
{
|
|
|
|
case AF_UNIX:
|
|
|
|
{
|
|
|
|
struct sockaddr_un name;
|
2000-05-16 05:24:54 -04:00
|
|
|
size_t namelen=sizeof(name);
|
1999-09-14 09:32:05 -04:00
|
|
|
int newsockfd=accept(sockfd,(struct sockaddr *)&name,&namelen);
|
|
|
|
|
|
|
|
if (newsockfd < 0)
|
2000-07-10 14:32:45 -04:00
|
|
|
{
|
|
|
|
if ((errno != EWOULDBLOCK) && (errno != EINTR) && (errno != EAGAIN))
|
|
|
|
s48_raise_os_error(errno);
|
|
|
|
if (! s48_add_pending_fd(sockfd, 1))// 1 for is_input
|
|
|
|
s48_raise_out_of_memory_error();
|
|
|
|
return S48_FALSE;
|
|
|
|
}
|
1999-09-14 09:32:05 -04:00
|
|
|
|
2000-07-10 14:01:53 -04:00
|
|
|
return(s48_enter_fixnum (newsockfd));
|
1999-09-14 09:32:05 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case AF_INET:
|
|
|
|
{
|
|
|
|
struct sockaddr_in name;
|
|
|
|
int namelen=sizeof(name);
|
2000-07-10 14:01:53 -04:00
|
|
|
int newsockfd;
|
2000-07-12 13:28:56 -04:00
|
|
|
s48_value result, sock_addr;
|
|
|
|
S48_DECLARE_GC_PROTECT(2);
|
|
|
|
newsockfd = accept (sockfd,
|
|
|
|
(struct sockaddr *)&name,&namelen);
|
1999-09-14 09:32:05 -04:00
|
|
|
|
|
|
|
if (newsockfd < 0)
|
2000-07-10 14:01:53 -04:00
|
|
|
{
|
|
|
|
if ((errno != EWOULDBLOCK) && (errno != EINTR) && (errno != EAGAIN))
|
|
|
|
s48_raise_os_error(errno);
|
|
|
|
if (! s48_add_pending_fd(sockfd, 1))// 1 for is_input
|
|
|
|
s48_raise_out_of_memory_error();
|
|
|
|
return S48_FALSE;
|
|
|
|
}
|
|
|
|
fcntl(newsockfd, F_SETFL, O_NONBLOCK);
|
2000-07-12 13:28:56 -04:00
|
|
|
S48_GC_PROTECT_2 (result, sock_addr);
|
|
|
|
sock_addr = long2byte_vector (ntohl(name.sin_addr.s_addr));
|
|
|
|
|
|
|
|
result = s48_cons (sock_addr, s48_enter_fixnum (ntohs(name.sin_port)));
|
|
|
|
|
|
|
|
result = s48_cons (s48_enter_fixnum (newsockfd),
|
|
|
|
result);
|
|
|
|
S48_GC_UNPROTECT();
|
|
|
|
return result;
|
1999-09-14 09:32:05 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2000-07-10 14:01:53 -04:00
|
|
|
s48_raise_argtype_error (family); /* error unknown address family */
|
1999-09-14 09:32:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-07-12 13:28:56 -04:00
|
|
|
s48_value make_addr (long s_addr, int s_port)
|
|
|
|
{
|
|
|
|
s48_value result, sock_addr;
|
|
|
|
S48_DECLARE_GC_PROTECT(2);
|
|
|
|
S48_GC_PROTECT_2 (result, sock_addr);
|
|
|
|
sock_addr = long2byte_vector (ntohl (s_addr));
|
|
|
|
|
|
|
|
result = s48_cons (sock_addr, s48_enter_fixnum (ntohs (s_port)));
|
|
|
|
S48_GC_UNPROTECT();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
1999-09-14 09:32:05 -04:00
|
|
|
/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
|
2000-07-12 13:28:56 -04:00
|
|
|
s48_value scheme_peer_name(s48_value sock, s48_value family)
|
1999-09-14 09:32:05 -04:00
|
|
|
{
|
2000-07-12 13:28:56 -04:00
|
|
|
int sockfd = s48_extract_fixnum (sock);
|
|
|
|
switch(s48_extract_fixnum (family))
|
1999-09-14 09:32:05 -04:00
|
|
|
{
|
|
|
|
case AF_INET:
|
|
|
|
{
|
|
|
|
struct sockaddr_in name;
|
|
|
|
int namelen=sizeof(name);
|
|
|
|
int value=getpeername(sockfd,(struct sockaddr *)&name,&namelen);
|
|
|
|
|
2000-07-12 13:28:56 -04:00
|
|
|
if (value < 0) s48_raise_os_error (errno);
|
|
|
|
|
|
|
|
return (make_addr (name.sin_addr.s_addr, name.sin_port));
|
1999-09-14 09:32:05 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2000-07-12 13:28:56 -04:00
|
|
|
s48_raise_argtype_error (family); /* error unknown address family */
|
1999-09-14 09:32:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
|
2000-07-12 13:28:56 -04:00
|
|
|
s48_value scheme_socket_name(s48_value sock, s48_value family)
|
1999-09-14 09:32:05 -04:00
|
|
|
{
|
2000-07-12 13:28:56 -04:00
|
|
|
int sockfd = s48_extract_fixnum (sock);
|
|
|
|
switch(s48_extract_fixnum (family))
|
1999-09-14 09:32:05 -04:00
|
|
|
{
|
|
|
|
case AF_INET:
|
|
|
|
{
|
|
|
|
struct sockaddr_in name;
|
|
|
|
int namelen=sizeof(name);
|
|
|
|
int value=getsockname(sockfd,(struct sockaddr *)&name,&namelen);
|
|
|
|
|
2000-07-12 13:28:56 -04:00
|
|
|
if (value < 0) s48_raise_os_error (errno);
|
|
|
|
|
|
|
|
return(make_addr (name.sin_addr.s_addr,
|
|
|
|
name.sin_port));
|
1999-09-14 09:32:05 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2000-07-12 13:28:56 -04:00
|
|
|
s48_raise_argtype_error (family); /* error unknown address family */
|
1999-09-14 09:32:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
|
|
|
|
int scheme_socket_pair(int type, int *s1, int *s2)
|
|
|
|
{
|
|
|
|
int sv[2];
|
|
|
|
if( socketpair(PF_UNIX,type,0,sv) ) {
|
|
|
|
*s1 = 0; *s2 = 0;
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
*s1 = sv[0]; *s2 = sv[1];
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
|
2000-07-14 12:30:02 -04:00
|
|
|
s48_value
|
|
|
|
recv_substring(s48_value scm_sockfd, s48_value flags, s48_value buf,
|
|
|
|
s48_value scm_start, s48_value scm_end)
|
1999-09-14 09:32:05 -04:00
|
|
|
{
|
2000-07-14 12:30:02 -04:00
|
|
|
struct sockaddr_in name;
|
|
|
|
int namelen=sizeof(name);
|
|
|
|
int sockfd = s48_extract_fixnum (scm_sockfd);
|
|
|
|
int start = s48_extract_fixnum (scm_start);
|
|
|
|
int end = s48_extract_fixnum (scm_end);
|
|
|
|
char* buf_part = s48_extract_string (buf) + start;
|
|
|
|
|
|
|
|
int cc=recvfrom(sockfd,
|
|
|
|
buf_part, end-start,
|
|
|
|
s48_extract_fixnum (flags),
|
|
|
|
(struct sockaddr *)&name, &namelen);
|
|
|
|
s48_value result;
|
|
|
|
|
|
|
|
if (cc >= 0)
|
|
|
|
return (s48_cons (s48_enter_fixnum (cc),
|
|
|
|
s48_cons (make_addr (name.sin_addr.s_addr,
|
|
|
|
htonl((u_long)ntohs(name.sin_port))),
|
|
|
|
S48_NULL)));
|
|
|
|
|
|
|
|
|
|
|
|
if ((errno != EWOULDBLOCK) && (errno != EINTR) && (errno != EAGAIN))
|
|
|
|
s48_raise_os_error(errno);
|
|
|
|
if (! s48_add_pending_fd(sockfd, 1))// 1 for is_input
|
|
|
|
s48_raise_out_of_memory_error();
|
|
|
|
return S48_FALSE;
|
1999-09-14 09:32:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
|
2000-07-14 12:30:02 -04:00
|
|
|
s48_value send_substring(s48_value scm_sockfd,
|
|
|
|
s48_value scm_flags,
|
|
|
|
s48_value buf,
|
|
|
|
s48_value scm_start,
|
|
|
|
s48_value scm_end,
|
|
|
|
s48_value scm_family,
|
|
|
|
s48_value scheme_name)
|
1999-09-14 09:32:05 -04:00
|
|
|
{
|
2000-07-14 12:30:02 -04:00
|
|
|
int n;
|
|
|
|
int s = s48_extract_fixnum (scm_sockfd);
|
|
|
|
int flags = s48_extract_fixnum (scm_flags);
|
|
|
|
int start = s48_extract_fixnum (scm_start);
|
|
|
|
int end = s48_extract_fixnum (scm_end);
|
|
|
|
char* buf_part = s48_extract_string (buf) + start;
|
|
|
|
|
|
|
|
switch(s48_extract_fixnum (scm_family))
|
1999-09-14 09:32:05 -04:00
|
|
|
{
|
|
|
|
case 0: /* only with connected sockets */
|
|
|
|
{
|
2000-07-14 12:30:02 -04:00
|
|
|
n = send(s, buf_part, end-start, flags);
|
|
|
|
break;
|
1999-09-14 09:32:05 -04:00
|
|
|
}
|
|
|
|
case AF_UNIX:
|
|
|
|
{
|
|
|
|
struct sockaddr_un name;
|
1999-09-15 20:20:37 -04:00
|
|
|
int scheme_length=S48_STRING_LENGTH(scheme_name);
|
1999-09-14 09:32:05 -04:00
|
|
|
|
|
|
|
name.sun_family=AF_UNIX;
|
|
|
|
if (scheme_length>=(108-1)) /* save space for \0 */
|
|
|
|
return(-1);
|
|
|
|
strncpy(name.sun_path,
|
2000-07-12 13:28:56 -04:00
|
|
|
s48_extract_string(scheme_name),
|
1999-09-14 09:32:05 -04:00
|
|
|
scheme_length); /* copy to c string */
|
|
|
|
name.sun_path[scheme_length]='\0'; /* add null */
|
2000-07-14 12:30:02 -04:00
|
|
|
n = sendto(s,
|
|
|
|
buf_part, end-start,
|
|
|
|
flags,
|
|
|
|
(struct sockaddr *)&name, sizeof(name));
|
1999-09-14 09:32:05 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case AF_INET:
|
|
|
|
{
|
|
|
|
struct sockaddr_in name;
|
2000-07-14 12:30:02 -04:00
|
|
|
u_long addr = htonl (byte_vector2long (S48_CAR (scheme_name)));
|
|
|
|
u_short port = htons(s48_extract_fixnum (S48_CDR (scheme_name)));
|
1999-09-14 09:32:05 -04:00
|
|
|
name.sin_family=AF_INET;
|
|
|
|
name.sin_addr.s_addr=addr;
|
|
|
|
name.sin_port=port;
|
|
|
|
|
2000-07-14 12:30:02 -04:00
|
|
|
n = sendto(s,
|
|
|
|
buf_part, end-start,
|
|
|
|
flags,
|
|
|
|
(struct sockaddr *)&name, sizeof(name));
|
|
|
|
break;
|
1999-09-14 09:32:05 -04:00
|
|
|
}
|
|
|
|
default:
|
2000-07-14 12:30:02 -04:00
|
|
|
s48_raise_argtype_error (s48_extract_fixnum (scm_family));
|
|
|
|
/* error unknown address family */
|
1999-09-14 09:32:05 -04:00
|
|
|
}
|
2000-07-14 12:30:02 -04:00
|
|
|
|
|
|
|
if (n >= 0)
|
|
|
|
return s48_enter_fixnum (n);
|
|
|
|
|
|
|
|
if ((errno != EWOULDBLOCK) && (errno != EINTR) && (errno != EAGAIN))
|
|
|
|
s48_raise_os_error(errno);
|
|
|
|
|
|
|
|
if (! s48_add_pending_fd(s, 0))// 0 for is_input
|
|
|
|
s48_raise_out_of_memory_error();
|
|
|
|
|
|
|
|
return S48_FALSE;
|
1999-09-14 09:32:05 -04:00
|
|
|
}
|
2000-07-14 12:30:02 -04:00
|
|
|
|
|
|
|
|
1999-09-14 09:32:05 -04:00
|
|
|
|
|
|
|
/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
|
|
|
|
int scheme_getsockopt (int s,
|
|
|
|
int level,
|
|
|
|
int optname)
|
|
|
|
{
|
|
|
|
int optval;
|
|
|
|
int optlen=sizeof(optval);
|
|
|
|
|
|
|
|
if (getsockopt(s,level,optname,(char *)&optval,&optlen) == -1)
|
|
|
|
return(-1);
|
|
|
|
return(optval);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int scheme_getsockopt_linger (int s,
|
|
|
|
int level,
|
|
|
|
int optname,
|
|
|
|
int *out_time)
|
|
|
|
{
|
|
|
|
struct linger optval;
|
|
|
|
int optlen=sizeof(optval);
|
|
|
|
|
|
|
|
if (getsockopt(s,level,optname,(char *)&optval,&optlen) == -1) {
|
|
|
|
out_time = 0;
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
*out_time=optval.l_linger;
|
|
|
|
return(optval.l_onoff);
|
|
|
|
}
|
|
|
|
|
|
|
|
int scheme_getsockopt_timeout (int s,
|
|
|
|
int level,
|
|
|
|
int optname,
|
|
|
|
int *out_usec)
|
|
|
|
{
|
|
|
|
struct timeval optval;
|
2000-05-16 05:24:54 -04:00
|
|
|
size_t optlen=sizeof(optval);
|
1999-09-14 09:32:05 -04:00
|
|
|
|
|
|
|
if (getsockopt(s,level,optname,(char *)&optval,&optlen) == -1) {
|
|
|
|
out_usec = 0;
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
*out_usec=optval.tv_usec;
|
|
|
|
return(optval.tv_sec);
|
|
|
|
}
|
|
|
|
/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
|
|
|
|
int scheme_setsockopt (int s,
|
|
|
|
int level,
|
|
|
|
int optname,
|
|
|
|
int optval)
|
|
|
|
{
|
|
|
|
return(setsockopt(s,level,optname,(char *)&optval,sizeof(optval)));
|
|
|
|
}
|
|
|
|
|
|
|
|
int scheme_setsockopt_linger (int s,
|
|
|
|
int level,
|
|
|
|
int optname,
|
|
|
|
int onoff,
|
|
|
|
int linger)
|
|
|
|
{
|
|
|
|
struct linger optval;
|
|
|
|
|
|
|
|
optval.l_onoff=onoff;
|
|
|
|
optval.l_linger=linger;
|
|
|
|
|
|
|
|
return(setsockopt(s,level,optname,(char *)&optval,sizeof(optval)));
|
|
|
|
}
|
|
|
|
|
|
|
|
int scheme_setsockopt_timeout (int s,
|
|
|
|
int level,
|
|
|
|
int optname,
|
|
|
|
int sec,
|
|
|
|
int usec)
|
|
|
|
{
|
|
|
|
struct timeval optval;
|
|
|
|
optval.tv_sec=sec;
|
|
|
|
optval.tv_usec=usec;
|
|
|
|
|
|
|
|
return(setsockopt(s,level,optname,(char *)&optval,sizeof(optval)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
|
|
|
|
/* Routines for looking up hosts */
|
|
|
|
/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
|
|
|
|
|
2000-07-12 13:28:56 -04:00
|
|
|
static s48_value host_info_type_binding = S48_FALSE;
|
1999-09-14 09:32:05 -04:00
|
|
|
|
2000-07-12 13:28:56 -04:00
|
|
|
s48_value host_ent2host_info (struct hostent * host)
|
|
|
|
{
|
|
|
|
s48_value host_info = S48_FALSE;
|
2000-07-13 09:45:00 -04:00
|
|
|
s48_value list = S48_FALSE;
|
2000-07-12 13:28:56 -04:00
|
|
|
long * ptr;
|
|
|
|
int i;
|
|
|
|
S48_DECLARE_GC_PROTECT (2);
|
1999-09-14 09:32:05 -04:00
|
|
|
|
|
|
|
if(host==NULL)
|
2000-07-12 13:28:56 -04:00
|
|
|
{
|
|
|
|
return(s48_enter_fixnum (h_errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (host_info_type_binding == S48_FALSE)
|
1999-09-14 09:32:05 -04:00
|
|
|
{
|
2000-07-12 13:28:56 -04:00
|
|
|
S48_GC_PROTECT_GLOBAL(host_info_type_binding);
|
|
|
|
host_info_type_binding = s48_get_imported_binding ("host-info-type");
|
1999-09-14 09:32:05 -04:00
|
|
|
}
|
2000-07-12 13:28:56 -04:00
|
|
|
|
2000-07-13 09:45:00 -04:00
|
|
|
S48_GC_PROTECT_2 (host_info, list);
|
2000-07-12 13:28:56 -04:00
|
|
|
|
|
|
|
host_info = s48_make_record (host_info_type_binding);
|
|
|
|
S48_RECORD_SET (host_info, 0, s48_enter_string (host->h_name));
|
1999-09-14 09:32:05 -04:00
|
|
|
|
2000-07-12 13:28:56 -04:00
|
|
|
ptr = (long *)host->h_aliases;
|
2000-07-13 09:45:00 -04:00
|
|
|
list = S48_NULL;
|
2000-07-12 13:28:56 -04:00
|
|
|
i = 0;
|
|
|
|
while (*ptr)
|
|
|
|
{
|
2000-07-13 09:45:00 -04:00
|
|
|
list = s48_cons (s48_enter_string (host->h_aliases[i]), list);
|
2000-07-12 13:28:56 -04:00
|
|
|
ptr++;
|
|
|
|
i ++;
|
|
|
|
}
|
|
|
|
|
2000-07-13 09:45:00 -04:00
|
|
|
S48_RECORD_SET (host_info, 1, list);
|
2000-07-12 13:28:56 -04:00
|
|
|
|
|
|
|
ptr = (long *)host->h_addr_list;
|
2000-07-13 09:45:00 -04:00
|
|
|
list = S48_NULL;
|
2000-07-12 13:28:56 -04:00
|
|
|
i = 0;
|
|
|
|
while (*ptr)
|
|
|
|
{
|
2000-07-13 09:45:00 -04:00
|
|
|
list =
|
2000-07-12 13:28:56 -04:00
|
|
|
s48_cons (long2byte_vector (ntohl (*(long *)(host->h_addr_list[i]))),
|
2000-07-13 09:45:00 -04:00
|
|
|
list);
|
2000-07-12 13:28:56 -04:00
|
|
|
ptr++;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2000-07-13 09:45:00 -04:00
|
|
|
S48_RECORD_SET (host_info, 2, list);
|
2000-07-12 13:28:56 -04:00
|
|
|
|
|
|
|
S48_GC_UNPROTECT ();
|
|
|
|
return host_info;
|
1999-09-14 09:32:05 -04:00
|
|
|
}
|
|
|
|
|
2000-07-12 13:28:56 -04:00
|
|
|
s48_value scheme_host_address2host_info(s48_value addr_port)
|
1999-09-14 09:32:05 -04:00
|
|
|
{
|
|
|
|
struct in_addr name;
|
|
|
|
struct hostent *host;
|
|
|
|
|
2000-07-12 13:28:56 -04:00
|
|
|
u_long addr = htonl(byte_vector2long (S48_CAR (addr_port)));
|
|
|
|
name.s_addr = addr;
|
|
|
|
|
|
|
|
host=gethostbyaddr((char *)&name,sizeof(name),AF_INET);
|
|
|
|
return (host_ent2host_info (host));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
s48_value scheme_host_name2host_info(s48_value s_name)
|
|
|
|
{
|
|
|
|
struct in_addr name;
|
|
|
|
struct hostent *host;
|
|
|
|
char * scheme_name = s48_extract_string (s_name);
|
|
|
|
|
1999-09-14 09:32:05 -04:00
|
|
|
if ((name.s_addr=inet_addr(scheme_name)) != -1)
|
|
|
|
host=gethostbyaddr((char *)&name,sizeof(name),AF_INET);
|
|
|
|
else
|
|
|
|
host=gethostbyname(scheme_name);
|
|
|
|
|
2000-07-12 13:28:56 -04:00
|
|
|
return (host_ent2host_info (host));
|
1999-09-14 09:32:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
|
|
|
|
/* Routines for looking up networks */
|
|
|
|
/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
|
2000-07-13 09:45:00 -04:00
|
|
|
static s48_value network_info_type_binding = S48_FALSE;
|
|
|
|
|
|
|
|
s48_value netent2net_info(struct netent *net)
|
1999-09-14 09:32:05 -04:00
|
|
|
{
|
2000-07-13 09:45:00 -04:00
|
|
|
s48_value network_info = S48_FALSE;
|
|
|
|
s48_value list = S48_FALSE;
|
|
|
|
long * ptr;
|
|
|
|
int i;
|
|
|
|
S48_DECLARE_GC_PROTECT (2);
|
|
|
|
|
|
|
|
if (net==NULL) return S48_FALSE;
|
|
|
|
|
|
|
|
if (network_info_type_binding == S48_FALSE)
|
|
|
|
{
|
|
|
|
S48_GC_PROTECT_GLOBAL(network_info_type_binding);
|
|
|
|
network_info_type_binding =
|
|
|
|
s48_get_imported_binding ("network-info-type");
|
|
|
|
}
|
|
|
|
S48_GC_PROTECT_2 (network_info, list);
|
|
|
|
|
|
|
|
network_info = s48_make_record (network_info_type_binding);
|
|
|
|
S48_RECORD_SET (network_info, 0, s48_enter_string (net->n_name));
|
|
|
|
|
|
|
|
ptr = (long *)net->n_aliases;
|
|
|
|
list = S48_NULL;
|
|
|
|
i = 0;
|
|
|
|
while (*ptr)
|
|
|
|
{
|
|
|
|
list = s48_cons (s48_enter_string (net->n_aliases[i]), list);
|
|
|
|
ptr++;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
S48_RECORD_SET (network_info, 1, list);
|
|
|
|
S48_RECORD_SET (network_info, 2, long2byte_vector (net->n_net));
|
1999-09-14 09:32:05 -04:00
|
|
|
|
2000-07-13 09:45:00 -04:00
|
|
|
S48_GC_UNPROTECT ();
|
|
|
|
|
|
|
|
return network_info;
|
1999-09-14 09:32:05 -04:00
|
|
|
}
|
|
|
|
|
2000-07-13 09:45:00 -04:00
|
|
|
s48_value scheme_net_address2net_info(s48_value net_addr)
|
1999-09-14 09:32:05 -04:00
|
|
|
{
|
2000-07-13 09:45:00 -04:00
|
|
|
struct netent *net;
|
|
|
|
// expects host byte order :
|
|
|
|
net=getnetbyaddr(byte_vector2long (net_addr),AF_INET);
|
1999-09-14 09:32:05 -04:00
|
|
|
|
2000-07-13 09:45:00 -04:00
|
|
|
return netent2net_info (net);
|
|
|
|
}
|
|
|
|
|
|
|
|
s48_value scheme_net_name2net_info(s48_value scheme_name)
|
|
|
|
{
|
|
|
|
struct netent *net = getnetbyname(s48_extract_string (scheme_name));
|
|
|
|
return netent2net_info (net);
|
1999-09-14 09:32:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
|
|
|
|
/* Routines for looking up services */
|
|
|
|
/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
|
|
|
|
|
2000-07-13 09:45:00 -04:00
|
|
|
|
|
|
|
static s48_value service_info_type_binding = S48_FALSE;
|
|
|
|
|
|
|
|
s48_value servent2service_info(struct servent *serv)
|
1999-09-14 09:32:05 -04:00
|
|
|
{
|
2000-07-13 09:45:00 -04:00
|
|
|
s48_value service_info = S48_FALSE;
|
|
|
|
s48_value list = S48_FALSE;
|
|
|
|
long * ptr;
|
|
|
|
int i;
|
|
|
|
S48_DECLARE_GC_PROTECT (2);
|
|
|
|
|
|
|
|
if (serv==NULL) return S48_FALSE;
|
|
|
|
|
|
|
|
if (service_info_type_binding == S48_FALSE)
|
|
|
|
{
|
|
|
|
S48_GC_PROTECT_GLOBAL(service_info_type_binding);
|
|
|
|
service_info_type_binding =
|
|
|
|
s48_get_imported_binding ("service-info-type");
|
|
|
|
}
|
|
|
|
S48_GC_PROTECT_2 (service_info, list);
|
|
|
|
|
|
|
|
service_info = s48_make_record (service_info_type_binding);
|
|
|
|
|
|
|
|
S48_RECORD_SET (service_info, 0, s48_enter_string (serv->s_name));
|
|
|
|
|
|
|
|
ptr = (long *)serv->s_aliases;
|
|
|
|
list = S48_NULL;
|
|
|
|
i = 0;
|
|
|
|
while (*ptr)
|
|
|
|
{
|
|
|
|
list = s48_cons (s48_enter_string (serv->s_aliases[i]), list);
|
|
|
|
ptr++;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
S48_RECORD_SET (service_info, 1, list);
|
|
|
|
S48_RECORD_SET (service_info, 2, s48_enter_fixnum (ntohs (serv->s_port)));
|
|
|
|
S48_RECORD_SET (service_info, 3, s48_enter_string (serv->s_proto));
|
|
|
|
|
|
|
|
S48_GC_UNPROTECT ();
|
|
|
|
|
|
|
|
return service_info;
|
|
|
|
}
|
1999-09-14 09:32:05 -04:00
|
|
|
|
2000-07-13 09:45:00 -04:00
|
|
|
s48_value scheme_serv_port2serv_info(s48_value in_port,
|
|
|
|
s48_value in_proto)
|
|
|
|
{
|
|
|
|
struct servent *serv;
|
|
|
|
char * proto;
|
|
|
|
if (in_proto == S48_FALSE) proto = NULL;
|
|
|
|
else proto = s48_extract_string (in_proto);
|
1999-09-14 09:32:05 -04:00
|
|
|
|
2000-07-13 09:45:00 -04:00
|
|
|
serv = getservbyport(ntohs(s48_extract_fixnum (in_port)),
|
|
|
|
proto);
|
1999-09-14 09:32:05 -04:00
|
|
|
|
2000-07-13 09:45:00 -04:00
|
|
|
return servent2service_info (serv);
|
1999-09-14 09:32:05 -04:00
|
|
|
}
|
|
|
|
|
2000-07-13 09:45:00 -04:00
|
|
|
s48_value scheme_serv_name2serv_info(s48_value in_name,
|
|
|
|
s48_value in_proto)
|
1999-09-14 09:32:05 -04:00
|
|
|
{
|
2000-07-13 09:45:00 -04:00
|
|
|
struct servent *serv;
|
|
|
|
char * proto;
|
1999-09-14 09:32:05 -04:00
|
|
|
|
2000-07-13 09:45:00 -04:00
|
|
|
if (in_proto == S48_FALSE) proto = NULL;
|
|
|
|
else proto = s48_extract_string (in_proto);
|
|
|
|
|
|
|
|
serv = getservbyname(s48_extract_string (in_name),
|
|
|
|
proto);
|
1999-09-14 09:32:05 -04:00
|
|
|
|
2000-07-13 09:45:00 -04:00
|
|
|
return servent2service_info (serv);
|
1999-09-14 09:32:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
|
|
|
|
/* Routines for looking up protocols */
|
|
|
|
/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
|
2000-07-13 09:45:00 -04:00
|
|
|
|
|
|
|
static s48_value protocol_info_type_binding = S48_FALSE;
|
|
|
|
|
|
|
|
s48_value protoent2protocol_info(struct protoent *proto)
|
1999-09-14 09:32:05 -04:00
|
|
|
{
|
2000-07-13 09:45:00 -04:00
|
|
|
s48_value protocol_info = S48_FALSE;
|
|
|
|
s48_value list = S48_FALSE;
|
|
|
|
long * ptr;
|
|
|
|
int i;
|
|
|
|
S48_DECLARE_GC_PROTECT (2);
|
|
|
|
|
|
|
|
if (proto==NULL) return S48_FALSE;
|
|
|
|
|
|
|
|
if (protocol_info_type_binding == S48_FALSE)
|
|
|
|
{
|
|
|
|
S48_GC_PROTECT_GLOBAL(protocol_info_type_binding);
|
|
|
|
protocol_info_type_binding =
|
|
|
|
s48_get_imported_binding ("protocol-info-type");
|
|
|
|
}
|
|
|
|
S48_GC_PROTECT_2 (protocol_info, list);
|
|
|
|
|
|
|
|
protocol_info = s48_make_record (protocol_info_type_binding);
|
1999-09-14 09:32:05 -04:00
|
|
|
|
2000-07-13 09:45:00 -04:00
|
|
|
S48_RECORD_SET (protocol_info, 0, s48_enter_string (proto->p_name));
|
|
|
|
|
|
|
|
ptr = (long *)proto->p_aliases;
|
|
|
|
list = S48_NULL;
|
|
|
|
i = 0;
|
|
|
|
while (*ptr)
|
|
|
|
{
|
|
|
|
list = s48_cons (s48_enter_string (proto->p_aliases[i]), list);
|
|
|
|
ptr++;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
S48_RECORD_SET (protocol_info, 1, list);
|
|
|
|
S48_RECORD_SET (protocol_info, 2, s48_enter_fixnum (proto->p_proto));
|
|
|
|
|
|
|
|
S48_GC_UNPROTECT ();
|
|
|
|
|
|
|
|
return protocol_info;
|
1999-09-14 09:32:05 -04:00
|
|
|
}
|
|
|
|
|
2000-07-13 09:45:00 -04:00
|
|
|
s48_value scheme_proto_num2proto_info(s48_value in_proto)
|
1999-09-14 09:32:05 -04:00
|
|
|
{
|
2000-07-13 09:45:00 -04:00
|
|
|
struct protoent *proto;
|
|
|
|
|
|
|
|
proto=getprotobynumber(s48_extract_fixnum (in_proto));
|
1999-09-14 09:32:05 -04:00
|
|
|
|
2000-07-13 09:45:00 -04:00
|
|
|
return protoent2protocol_info (proto);
|
1999-09-14 09:32:05 -04:00
|
|
|
}
|
|
|
|
|
2000-07-13 09:45:00 -04:00
|
|
|
s48_value scheme_proto_name2proto_info(s48_value in_name)
|
1999-09-14 09:32:05 -04:00
|
|
|
{
|
2000-07-13 09:45:00 -04:00
|
|
|
struct protoent *proto=getprotobyname(s48_extract_string (in_name));
|
|
|
|
|
|
|
|
return protoent2protocol_info (proto);
|
1999-09-14 09:32:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-07-14 12:30:02 -04:00
|
|
|
/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
|
|
|
|
/* Low Level Junk */
|
|
|
|
/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
|
|
|
|
/* svec is a Scheme vector of C carriers. Scan over the C longs
|
|
|
|
** in cvec, and initialise the corresponding carriers in svec.
|
|
|
|
*/
|
|
|
|
void set_longvec_carriers(s48_value svec, long const * const * cvec)
|
|
|
|
{
|
|
|
|
int svec_len = S48_VECTOR_LENGTH(svec);
|
|
|
|
long const * const *cv = cvec;
|
|
|
|
s48_value s = S48_VECTOR_REF(svec,0); //JMG hack
|
|
|
|
s48_value *sv = &s;
|
|
|
|
|
|
|
|
for(; svec_len > 0; cv++, sv++, svec_len-- ) {
|
|
|
|
/* *sv is a (make-string 4) */
|
|
|
|
s48_value carrier = *sv;
|
|
|
|
(*((u_long *)(S48_ADDRESS_AFTER_HEADER(carrier,unsigned char))))
|
|
|
|
=(long)**cv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* One arg, a zero-terminated C word vec. Returns length.
|
|
|
|
** The terminating null is not counted. Returns #f on NULL.
|
|
|
|
*/
|
|
|
|
|
|
|
|
s48_value veclen(const long *vec)
|
|
|
|
{
|
|
|
|
const long *vptr = vec;
|
|
|
|
if( !vptr ) return S48_FALSE;
|
|
|
|
while( *vptr ) vptr++;
|
|
|
|
return s48_enter_fixnum(vptr - vec);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-07-13 09:45:00 -04:00
|
|
|
|