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 <fail.h>
|
1997-05-13 08:46:49 -07:00
|
|
|
#include <signals.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-04 07:15:31 -07:00
|
|
|
#ifndef _WIN32
|
2002-05-04 02:58:01 -07:00
|
|
|
#include <sys/types.h>
|
1995-05-08 08:18:32 -07:00
|
|
|
#include <netdb.h>
|
1996-09-04 07:15:31 -07:00
|
|
|
#endif
|
1995-05-08 08:18:32 -07:00
|
|
|
|
2002-05-04 02:58:01 -07:00
|
|
|
#define NETDB_BUFFER_SIZE 10000
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
2002-06-07 02:49:45 -07:00
|
|
|
#define GETHOSTBYADDR_IS_REENTRANT 1
|
|
|
|
#define GETHOSTBYNAME_IS_REENTRANT 1
|
2002-05-04 02:58:01 -07:00
|
|
|
#endif
|
|
|
|
|
1995-05-08 08:18:32 -07:00
|
|
|
static int entry_h_length;
|
|
|
|
|
|
|
|
extern int socket_domain_table[];
|
|
|
|
|
2001-02-28 09:20:27 -08:00
|
|
|
static value alloc_one_addr(char const *a)
|
1995-05-08 08:18:32 -07:00
|
|
|
{
|
2000-03-07 05:34:24 -08:00
|
|
|
struct in_addr addr;
|
2000-11-23 05:45:03 -08:00
|
|
|
memmove (&addr, a, entry_h_length);
|
2000-03-07 05:34:24 -08:00
|
|
|
return alloc_inet_addr(addr.s_addr);
|
1995-05-08 08:18:32 -07:00
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
static value alloc_host_entry(struct hostent *entry)
|
1995-05-08 08:18:32 -07:00
|
|
|
{
|
|
|
|
value res;
|
1997-05-26 10:16:31 -07:00
|
|
|
value name = Val_unit, aliases = Val_unit;
|
1998-03-13 11:59:11 -08:00
|
|
|
value addr_list = Val_unit, adr = Val_unit;
|
1995-05-08 08:18:32 -07:00
|
|
|
|
1998-03-13 11:59:11 -08:00
|
|
|
Begin_roots4 (name, aliases, addr_list, adr);
|
1997-06-13 08:52:43 -07:00
|
|
|
name = copy_string((char *)(entry->h_name));
|
2001-07-02 05:24:13 -07:00
|
|
|
aliases = copy_string_array((const char**)entry->h_aliases);
|
1997-05-26 10:16:31 -07:00
|
|
|
entry_h_length = entry->h_length;
|
1995-05-08 08:18:32 -07:00
|
|
|
#ifdef h_addr
|
2001-07-02 05:24:13 -07:00
|
|
|
addr_list = alloc_array(alloc_one_addr, (const char**)entry->h_addr_list);
|
1995-05-08 08:18:32 -07:00
|
|
|
#else
|
1998-03-13 11:59:11 -08:00
|
|
|
adr = alloc_one_addr(entry->h_addr);
|
1998-10-26 11:19:32 -08:00
|
|
|
addr_list = alloc_small(1, 0);
|
1998-03-13 11:59:11 -08:00
|
|
|
Field(addr_list, 0) = adr;
|
1995-05-08 08:18:32 -07:00
|
|
|
#endif
|
1998-10-26 11:19:32 -08:00
|
|
|
res = alloc_small(4, 0);
|
1997-05-26 10:16:31 -07:00
|
|
|
Field(res, 0) = name;
|
|
|
|
Field(res, 1) = aliases;
|
|
|
|
Field(res, 2) = entry->h_addrtype == PF_UNIX ? Val_int(0) : Val_int(1);
|
|
|
|
Field(res, 3) = addr_list;
|
|
|
|
End_roots();
|
1995-05-08 08:18:32 -07:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value unix_gethostbyaddr(value a)
|
1995-05-08 08:18:32 -07:00
|
|
|
{
|
2002-05-04 02:58:01 -07:00
|
|
|
uint32 adr = GET_INET_ADDR(a);
|
|
|
|
struct hostent * hp;
|
|
|
|
#if HAS_GETHOSTBYADDR_R == 7
|
|
|
|
struct hostent h;
|
|
|
|
char buffer[NETDB_BUFFER_SIZE];
|
|
|
|
int h_errnop;
|
|
|
|
enter_blocking_section();
|
|
|
|
hp = gethostbyaddr_r((char *) &adr, 4, AF_INET,
|
|
|
|
&h, buffer, sizeof(buffer), &h_errnop);
|
|
|
|
leave_blocking_section();
|
|
|
|
#elif HAS_GETHOSTBYADDR_R == 8
|
|
|
|
struct hostent h;
|
|
|
|
char buffer[NETDB_BUFFER_SIZE];
|
|
|
|
int h_errnop, rc;
|
|
|
|
enter_blocking_section();
|
|
|
|
rc = gethostbyaddr_r((char *) &adr, 4, AF_INET,
|
|
|
|
&h, buffer, sizeof(buffer), &hp, &h_errnop);
|
|
|
|
leave_blocking_section();
|
|
|
|
if (rc != 0) hp = NULL;
|
|
|
|
#else
|
|
|
|
#ifdef GETHOSTBYADDR_IS_REENTRANT
|
1996-04-01 07:26:38 -08:00
|
|
|
enter_blocking_section();
|
2002-05-04 02:58:01 -07:00
|
|
|
#endif
|
|
|
|
hp = gethostbyaddr((char *) &adr, 4, AF_INET);
|
|
|
|
#ifdef GETHOSTBYADDR_IS_REENTRANT
|
1996-04-01 07:26:38 -08:00
|
|
|
leave_blocking_section();
|
2002-05-04 02:58:01 -07:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
if (hp == (struct hostent *) NULL) raise_not_found();
|
|
|
|
return alloc_host_entry(hp);
|
1995-05-08 08:18:32 -07:00
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value unix_gethostbyname(value name)
|
1995-05-08 08:18:32 -07:00
|
|
|
{
|
2002-05-04 02:58:01 -07:00
|
|
|
struct hostent * hp;
|
|
|
|
char * hostname;
|
|
|
|
|
2002-06-07 02:49:45 -07:00
|
|
|
#if HAS_GETHOSTBYNAME_R || GETHOSTBYNAME_IS_REENTRANT
|
2002-05-04 02:58:01 -07:00
|
|
|
hostname = stat_alloc(string_length(name) + 1);
|
|
|
|
strcpy(hostname, String_val(name));
|
|
|
|
#else
|
|
|
|
hostname = String_val(name);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if HAS_GETHOSTBYNAME_R == 5
|
|
|
|
{
|
|
|
|
struct hostent h;
|
|
|
|
char buffer[NETDB_BUFFER_SIZE];
|
|
|
|
int h_errno;
|
|
|
|
enter_blocking_section();
|
|
|
|
hp = gethostbyname_r(hostname, &h, buffer, sizeof(buffer), &h_errno);
|
|
|
|
leave_blocking_section();
|
|
|
|
}
|
|
|
|
#elif HAS_GETHOSTBYNAME_R == 6
|
|
|
|
{
|
|
|
|
struct hostent h;
|
|
|
|
char buffer[NETDB_BUFFER_SIZE];
|
|
|
|
int h_errno, rc;
|
|
|
|
enter_blocking_section();
|
|
|
|
rc = gethostbyname_r(hostname, &h, buffer, sizeof(buffer), &hp, &h_errno);
|
|
|
|
leave_blocking_section();
|
|
|
|
if (rc != 0) hp = NULL;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#ifdef GETHOSTBYNAME_IS_REENTRANT
|
1996-04-01 07:26:38 -08:00
|
|
|
enter_blocking_section();
|
2002-05-04 02:58:01 -07:00
|
|
|
#endif
|
|
|
|
hp = gethostbyname(hostname);
|
|
|
|
#ifdef GETHOSTBYNAME_IS_REENTRANT
|
1996-04-01 07:26:38 -08:00
|
|
|
leave_blocking_section();
|
2002-05-04 02:58:01 -07:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2002-06-07 02:49:45 -07:00
|
|
|
#if HAS_GETHOSTBYNAME_R || GETHOSTBYNAME_IS_REENTRANT
|
2002-05-04 02:58:01 -07:00
|
|
|
stat_free(hostname);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (hp == (struct hostent *) NULL) raise_not_found();
|
|
|
|
return alloc_host_entry(hp);
|
1995-05-08 08:18:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value unix_gethostbyaddr(value name)
|
1995-05-08 08:18:32 -07:00
|
|
|
{ invalid_argument("gethostbyaddr not implemented"); }
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value unix_gethostbyname(value name)
|
1995-05-08 08:18:32 -07:00
|
|
|
{ invalid_argument("gethostbyname not implemented"); }
|
|
|
|
|
|
|
|
#endif
|