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
|
|
|
|
|
2005-03-24 09:20:54 -08:00
|
|
|
CAMLexport value alloc_inet_addr(struct in_addr * 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,
|
2004-04-09 06:25:23 -07:00
|
|
|
hence is marshaled in an endian-independent manner. */
|
|
|
|
res = alloc_string(4);
|
|
|
|
memcpy(String_val(res), a, 4);
|
1995-05-08 08:18:32 -07:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2004-04-09 06:25:23 -07:00
|
|
|
#ifdef HAS_IPV6
|
|
|
|
|
2005-03-24 09:20:54 -08:00
|
|
|
CAMLexport value alloc_inet6_addr(struct in6_addr * a)
|
2004-04-09 06:25:23 -07:00
|
|
|
{
|
|
|
|
value res;
|
|
|
|
res = alloc_string(16);
|
|
|
|
memcpy(String_val(res), a, 16);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
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 */
|
2004-04-09 06:25:23 -07:00
|
|
|
#ifdef HAS_IPV6
|
|
|
|
if (string_length(Field(mladr, 0)) == 16) {
|
|
|
|
memset(&adr->s_inet6, 0, sizeof(struct sockaddr_in6));
|
|
|
|
adr->s_inet6.sin6_family = AF_INET6;
|
|
|
|
adr->s_inet6.sin6_addr = GET_INET6_ADDR(Field(mladr, 0));
|
|
|
|
adr->s_inet6.sin6_port = htons(Int_val(Field(mladr, 1)));
|
2010-01-20 08:26:46 -08:00
|
|
|
#ifdef SIN6_LEN
|
|
|
|
adr->s_inet6.sin6_len = sizeof(struct sockaddr_in6);
|
|
|
|
#endif
|
2004-04-09 06:25:23 -07:00
|
|
|
*adr_len = sizeof(struct sockaddr_in6);
|
1995-05-08 08:18:32 -07:00
|
|
|
break;
|
|
|
|
}
|
2004-04-09 06:25:23 -07:00
|
|
|
#endif
|
|
|
|
memset(&adr->s_inet, 0, sizeof(struct sockaddr_in));
|
|
|
|
adr->s_inet.sin_family = AF_INET;
|
|
|
|
adr->s_inet.sin_addr = GET_INET_ADDR(Field(mladr, 0));
|
|
|
|
adr->s_inet.sin_port = htons(Int_val(Field(mladr, 1)));
|
2010-01-20 08:26:46 -08:00
|
|
|
#ifdef SIN6_LEN
|
|
|
|
adr->s_inet.sin_len = sizeof(struct sockaddr_in);
|
|
|
|
#endif
|
2004-04-09 06:25:23 -07:00
|
|
|
*adr_len = sizeof(struct sockaddr_in);
|
|
|
|
break;
|
1995-05-08 08:18:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-09-01 11:38:43 -07:00
|
|
|
value alloc_sockaddr(union sock_addr_union * adr /*in*/,
|
2005-03-24 09:20:54 -08:00
|
|
|
socklen_param_type adr_len, int close_on_error)
|
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:
|
2004-04-09 06:25:23 -07:00
|
|
|
{ value a = alloc_inet_addr(&adr->s_inet.sin_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;
|
|
|
|
}
|
2004-04-09 06:25:23 -07:00
|
|
|
#ifdef HAS_IPV6
|
|
|
|
case AF_INET6:
|
|
|
|
{ value a = alloc_inet6_addr(&adr->s_inet6.sin6_addr);
|
|
|
|
Begin_root (a);
|
|
|
|
res = alloc_small(2, 1);
|
|
|
|
Field(res,0) = a;
|
|
|
|
Field(res,1) = Val_int(ntohs(adr->s_inet6.sin6_port));
|
|
|
|
End_roots();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
1995-05-08 08:18:32 -07:00
|
|
|
default:
|
2005-03-24 09:20:54 -08:00
|
|
|
if (close_on_error != -1) close (close_on_error);
|
1995-05-08 08:18:32 -07:00
|
|
|
unix_error(EAFNOSUPPORT, "", Nothing);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|