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 */
|
|
|
|
/* under the terms of the GNU Library General Public 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-03-07 01:07:39 -08:00
|
|
|
void get_sockaddr(value mladdr,
|
|
|
|
union sock_addr_union * addr /*out*/,
|
|
|
|
socklen_param_type * addr_len /*out*/)
|
1995-05-08 08:18:32 -07:00
|
|
|
{
|
2000-03-07 01:07:39 -08:00
|
|
|
switch(Tag_val(mladdr)) {
|
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-03-07 01:07:39 -08:00
|
|
|
path = Field(mladdr, 0);
|
1995-05-08 08:18:32 -07:00
|
|
|
len = string_length(path);
|
2000-03-07 01:07:39 -08:00
|
|
|
addr->s_unix.sun_family = AF_UNIX;
|
|
|
|
if (len >= sizeof(addr->s_unix.sun_path)) {
|
1995-05-08 08:18:32 -07:00
|
|
|
unix_error(ENAMETOOLONG, "", path);
|
|
|
|
}
|
2000-03-07 01:07:39 -08:00
|
|
|
bcopy(String_val(path), addr->s_unix.sun_path, (int) len + 1);
|
|
|
|
*addr_len =
|
|
|
|
((char *)&(addr->s_unix.sun_path) - (char *)&(addr->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-03-07 01:07:39 -08:00
|
|
|
for (p = (char *) &addr->s_inet, n = sizeof(addr->s_inet);
|
1995-05-08 08:18:32 -07:00
|
|
|
n > 0; p++, n--)
|
|
|
|
*p = 0;
|
2000-03-07 01:07:39 -08:00
|
|
|
addr->s_inet.sin_family = AF_INET;
|
|
|
|
addr->s_inet.sin_addr.s_addr = GET_INET_ADDR(Field(mladdr, 0));
|
|
|
|
addr->s_inet.sin_port = htons(Int_val(Field(mladdr, 1)));
|
|
|
|
*addr_len = sizeof(struct sockaddr_in);
|
1995-05-08 08:18:32 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-03-07 01:07:39 -08:00
|
|
|
value alloc_sockaddr(union sock_addr_union * addr /*in*/,
|
|
|
|
socklen_param_type addr_len)
|
1995-05-08 08:18:32 -07:00
|
|
|
{
|
|
|
|
value res;
|
2000-03-07 01:07:39 -08:00
|
|
|
switch(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:
|
2000-03-07 01:07:39 -08:00
|
|
|
{ value n = copy_string(addr->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-03-07 01:07:39 -08:00
|
|
|
{ value a = alloc_inet_addr(addr->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-03-07 01:07:39 -08:00
|
|
|
Field(res,1) = Val_int(ntohs(addr->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
|