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 */
|
1999-11-17 10:59:06 -08:00
|
|
|
/* en Automatique. All rights reserved. This file is distributed */
|
2001-12-07 05:41:02 -08:00
|
|
|
/* under the terms of the GNU Library General Public License, with */
|
|
|
|
/* the special exception on linking described in file ../../LICENSE. */
|
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
|
|
|
|
|
2000-03-07 01:07:39 -08:00
|
|
|
value alloc_inet_addr(uint32 a)
|
1995-05-08 08:18:32 -07:00
|
|
|
{
|
|
|
|
value res;
|
2000-03-07 01:07:39 -08:00
|
|
|
/* Use a string rather than an abstract block so that it can be
|
|
|
|
marshaled safely. Remember that a is in network byte order,
|
|
|
|
hence can be marshaled safely. */
|
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;
|
|
|
|
}
|
|
|
|
|
2000-09-01 11:38:43 -07:00
|
|
|
void get_sockaddr(value mladr,
|
|
|
|
union sock_addr_union * adr /*out*/,
|
|
|
|
socklen_param_type * adr_len /*out*/)
|
1995-05-08 08:18:32 -07:00
|
|
|
{
|
2000-09-01 11:38:43 -07:00
|
|
|
switch(Tag_val(mladr)) {
|
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;
|
2000-09-01 11:38:43 -07:00
|
|
|
path = Field(mladr, 0);
|
1995-05-08 08:18:32 -07:00
|
|
|
len = string_length(path);
|
2000-09-01 11:38:43 -07:00
|
|
|
adr->s_unix.sun_family = AF_UNIX;
|
|
|
|
if (len >= sizeof(adr->s_unix.sun_path)) {
|
1995-05-08 08:18:32 -07:00
|
|
|
unix_error(ENAMETOOLONG, "", path);
|
|
|
|
}
|
2000-11-23 05:45:03 -08:00
|
|
|
memmove (adr->s_unix.sun_path, String_val(path), len + 1);
|
2000-09-01 11:38:43 -07:00
|
|
|
*adr_len =
|
|
|
|
((char *)&(adr->s_unix.sun_path) - (char *)&(adr->s_unix))
|
1996-01-11 06:16:18 -08:00
|
|
|
+ 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;
|
2000-09-01 11:38:43 -07:00
|
|
|
for (p = (char *) &adr->s_inet, n = sizeof(adr->s_inet);
|
1995-05-08 08:18:32 -07:00
|
|
|
n > 0; p++, n--)
|
|
|
|
*p = 0;
|
2000-09-01 11:38:43 -07:00
|
|
|
adr->s_inet.sin_family = AF_INET;
|
|
|
|
adr->s_inet.sin_addr.s_addr = GET_INET_ADDR(Field(mladr, 0));
|
|
|
|
adr->s_inet.sin_port = htons(Int_val(Field(mladr, 1)));
|
|
|
|
*adr_len = sizeof(struct sockaddr_in);
|
1995-05-08 08:18:32 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-09-01 11:38:43 -07:00
|
|
|
value alloc_sockaddr(union sock_addr_union * adr /*in*/,
|
|
|
|
socklen_param_type adr_len)
|
1995-05-08 08:18:32 -07:00
|
|
|
{
|
|
|
|
value res;
|
2000-09-01 11:38:43 -07:00
|
|
|
switch(adr->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:
|
2000-09-01 11:38:43 -07:00
|
|
|
{ value n = copy_string(adr->s_unix.sun_path);
|
1997-05-26 10:16:31 -07:00
|
|
|
Begin_root (n);
|
1998-10-26 11:19:32 -08:00
|
|
|
res = alloc_small(1, 0);
|
1999-11-29 11:04:56 -08:00
|
|
|
Field(res,0) = n;
|
1997-05-26 10:16:31 -07:00
|
|
|
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:
|
2000-09-01 11:38:43 -07:00
|
|
|
{ value a = alloc_inet_addr(adr->s_inet.sin_addr.s_addr);
|
1997-05-26 10:16:31 -07:00
|
|
|
Begin_root (a);
|
1998-10-26 11:19:32 -08:00
|
|
|
res = alloc_small(2, 1);
|
1999-11-29 11:04:56 -08:00
|
|
|
Field(res,0) = a;
|
2000-09-01 11:38:43 -07:00
|
|
|
Field(res,1) = Val_int(ntohs(adr->s_inet.sin_port));
|
1997-05-26 10:16:31 -07:00
|
|
|
End_roots();
|
1995-05-08 08:18:32 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
unix_error(EAFNOSUPPORT, "", Nothing);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|