1995-08-09 08:06:35 -07:00
|
|
|
/***********************************************************************/
|
|
|
|
/* */
|
1996-04-30 07:53:58 -07:00
|
|
|
/* Objective Caml */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* */
|
|
|
|
/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
|
|
|
|
/* */
|
1996-04-30 07:53:58 -07:00
|
|
|
/* Copyright 1996 Institut National de Recherche en Informatique et */
|
1998-10-26 11:19:32 -08:00
|
|
|
/* en Automatique. Distributed only by permission. */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
1997-06-13 08:52:43 -07:00
|
|
|
#include <string.h>
|
1995-05-08 08:18:32 -07:00
|
|
|
#include <mlvalues.h>
|
|
|
|
#include <alloc.h>
|
|
|
|
#include <memory.h>
|
|
|
|
#include <errno.h>
|
1996-09-04 07:15:31 -07:00
|
|
|
#include "unixsupport.h"
|
1995-05-08 08:18:32 -07:00
|
|
|
|
|
|
|
#ifdef HAS_SOCKETS
|
|
|
|
|
|
|
|
#include "socketaddr.h"
|
|
|
|
|
1996-09-05 06:31:54 -07:00
|
|
|
#ifdef _WIN32
|
|
|
|
#define EAFNOSUPPORT WSAEAFNOSUPPORT
|
|
|
|
#endif
|
|
|
|
|
1997-08-26 08:20:55 -07:00
|
|
|
union sock_addr_union sock_addr;
|
|
|
|
int sock_addr_len;
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
value alloc_inet_addr(unsigned int a)
|
1995-05-08 08:18:32 -07:00
|
|
|
{
|
|
|
|
value res;
|
1995-10-31 07:57:59 -08:00
|
|
|
res = alloc_string(sizeof(uint32));
|
1995-05-08 08:18:32 -07:00
|
|
|
GET_INET_ADDR(res) = a;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
void get_sockaddr(value a)
|
1995-05-08 08:18:32 -07:00
|
|
|
{
|
|
|
|
switch(Tag_val(a)) {
|
1996-09-04 07:15:31 -07:00
|
|
|
#ifndef _WIN32
|
1995-05-08 08:18:32 -07:00
|
|
|
case 0: /* ADDR_UNIX */
|
|
|
|
{ value path;
|
|
|
|
mlsize_t len;
|
|
|
|
path = Field(a, 0);
|
|
|
|
len = string_length(path);
|
|
|
|
sock_addr.s_unix.sun_family = AF_UNIX;
|
|
|
|
if (len >= sizeof(sock_addr.s_unix.sun_path)) {
|
|
|
|
unix_error(ENAMETOOLONG, "", path);
|
|
|
|
}
|
|
|
|
bcopy(String_val(path), sock_addr.s_unix.sun_path, (int) len + 1);
|
1996-01-11 06:16:18 -08:00
|
|
|
sock_addr_len =
|
|
|
|
((char *)&(sock_addr.s_unix.sun_path) - (char *)&(sock_addr.s_unix))
|
|
|
|
+ len;
|
1995-05-08 08:18:32 -07:00
|
|
|
break;
|
|
|
|
}
|
1996-09-04 07:15:31 -07:00
|
|
|
#endif
|
1995-05-08 08:18:32 -07:00
|
|
|
case 1: /* ADDR_INET */
|
|
|
|
{
|
|
|
|
char * p;
|
|
|
|
int n;
|
|
|
|
for (p = (char *) &sock_addr.s_inet, n = sizeof(sock_addr.s_inet);
|
|
|
|
n > 0; p++, n--)
|
|
|
|
*p = 0;
|
|
|
|
sock_addr.s_inet.sin_family = AF_INET;
|
|
|
|
sock_addr.s_inet.sin_addr.s_addr = GET_INET_ADDR(Field(a, 0));
|
|
|
|
sock_addr.s_inet.sin_port = htons(Int_val(Field(a, 1)));
|
|
|
|
sock_addr_len = sizeof(struct sockaddr_in);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
value alloc_sockaddr(void)
|
1995-05-08 08:18:32 -07:00
|
|
|
{
|
|
|
|
value res;
|
|
|
|
switch(sock_addr.s_gen.sa_family) {
|
1996-09-04 07:15:31 -07:00
|
|
|
#ifndef _WIN32
|
1995-05-08 08:18:32 -07:00
|
|
|
case AF_UNIX:
|
1997-05-26 10:16:31 -07:00
|
|
|
{ value n = copy_string(sock_addr.s_unix.sun_path);
|
|
|
|
Begin_root (n);
|
1998-10-26 11:19:32 -08:00
|
|
|
res = alloc_small(1, 0);
|
1997-05-26 10:16:31 -07:00
|
|
|
Field(res,0) = n;
|
|
|
|
End_roots();
|
1995-05-08 08:18:32 -07:00
|
|
|
break;
|
|
|
|
}
|
1996-09-04 07:15:31 -07:00
|
|
|
#endif
|
1995-05-08 08:18:32 -07:00
|
|
|
case AF_INET:
|
1997-05-26 10:16:31 -07:00
|
|
|
{ value a = alloc_inet_addr(sock_addr.s_inet.sin_addr.s_addr);
|
|
|
|
Begin_root (a);
|
1998-10-26 11:19:32 -08:00
|
|
|
res = alloc_small(2, 1);
|
1997-05-26 10:16:31 -07:00
|
|
|
Field(res,0) = a;
|
|
|
|
Field(res,1) = Val_int(ntohs(sock_addr.s_inet.sin_port));
|
|
|
|
End_roots();
|
1995-05-08 08:18:32 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
unix_error(EAFNOSUPPORT, "", Nothing);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|