2000-09-03 17:30:41 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| PHP version 4.0 |
|
|
|
|
+----------------------------------------------------------------------+
|
2001-02-26 14:11:02 +08:00
|
|
|
| Copyright (c) 1997-2001 The PHP Group |
|
2000-09-03 17:30:41 +08:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| This source file is subject to version 2.02 of the PHP license, |
|
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
|
|
|
| available at through the world-wide-web at |
|
|
|
|
| http://www.php.net/license/2_02.txt. |
|
|
|
|
| If you did not receive a copy of the PHP license and are unable to |
|
|
|
|
| obtain it through the world-wide-web, please send a note to |
|
|
|
|
| license@php.net so we can mail you a copy immediately. |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Authors: Stig Venaas <venaas@uninett.no> |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
/* $Id$ */
|
|
|
|
|
2000-09-04 20:58:08 +08:00
|
|
|
#include "php.h"
|
|
|
|
|
2000-09-03 17:30:41 +08:00
|
|
|
#ifdef PHP_WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
#include <winsock.h>
|
|
|
|
#define O_RDONLY _O_RDONLY
|
|
|
|
#include "win32/param.h"
|
|
|
|
#else
|
|
|
|
#include <sys/param.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#if HAVE_SYS_SOCKET_H
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef PHP_WIN32
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netdb.h>
|
2001-01-22 01:29:15 +08:00
|
|
|
#if HAVE_ARPA_INET_H
|
2000-09-03 17:30:41 +08:00
|
|
|
#include <arpa/inet.h>
|
2001-01-22 01:29:15 +08:00
|
|
|
#endif
|
2001-02-25 05:14:18 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef HAVE_INET_ATON
|
2000-09-06 03:06:29 +08:00
|
|
|
int inet_aton(const char *, struct in_addr *);
|
2000-09-03 17:30:41 +08:00
|
|
|
#endif
|
|
|
|
|
2000-09-06 03:06:29 +08:00
|
|
|
#include "php_network.h"
|
|
|
|
|
2000-09-03 17:30:41 +08:00
|
|
|
#ifdef PHP_WIN32
|
|
|
|
#undef AF_UNIX
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(AF_UNIX)
|
|
|
|
#include <sys/un.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef PHP_WIN32
|
|
|
|
# define SOCK_ERR INVALID_SOCKET
|
|
|
|
# define SOCK_CONN_ERR SOCKET_ERROR
|
|
|
|
#else
|
|
|
|
# define SOCK_ERR -1
|
|
|
|
# define SOCK_CONN_ERR -1
|
|
|
|
#endif
|
|
|
|
|
2001-01-13 21:02:19 +08:00
|
|
|
#ifdef HAVE_GETADDRINFO
|
2001-01-13 19:48:47 +08:00
|
|
|
#ifdef HAVE_GAI_STRERROR
|
|
|
|
# define PHP_GAI_STRERROR(x) (gai_strerror(x))
|
|
|
|
#else
|
|
|
|
# define PHP_GAI_STRERROR(x) (php_gai_strerror(x))
|
2001-01-13 21:02:19 +08:00
|
|
|
static char *php_gai_strerror(int code)
|
|
|
|
{
|
2001-01-13 19:48:47 +08:00
|
|
|
static struct {
|
|
|
|
int code;
|
|
|
|
const char *msg;
|
|
|
|
} values[] = {
|
|
|
|
# ifdef EAI_ADDRFAMILY
|
|
|
|
{EAI_ADDRFAMILY, "Address family for hostname not supported"},
|
|
|
|
# endif
|
|
|
|
{EAI_AGAIN, "Temporary failure in name resolution"},
|
|
|
|
{EAI_BADFLAGS, "Bad value for ai_flags"},
|
|
|
|
{EAI_FAIL, "Non-recoverable failure in name resolution"},
|
|
|
|
{EAI_FAMILY, "ai_family not supported"},
|
|
|
|
{EAI_MEMORY, "Memory allocation failure"},
|
|
|
|
# ifdef EAI_NODATA
|
|
|
|
{EAI_NODATA, "No address associated with hostname"},
|
|
|
|
# endif
|
|
|
|
{EAI_NONAME, "Name or service not known"},
|
|
|
|
{EAI_SERVICE, "Servname not supported for ai_socktype"},
|
|
|
|
{EAI_SOCKTYPE, "ai_socktype not supported"},
|
|
|
|
{EAI_SYSTEM, "System error"},
|
|
|
|
{0, NULL}
|
|
|
|
};
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; values[i].msg != NULL; i++) {
|
|
|
|
if (values[i].code == code) {
|
|
|
|
return (char *)values[i].msg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "Unknown error";
|
|
|
|
}
|
|
|
|
#endif
|
2001-01-13 21:02:19 +08:00
|
|
|
#endif
|
2001-01-13 19:48:47 +08:00
|
|
|
|
2000-09-05 21:56:11 +08:00
|
|
|
static void php_network_freeaddresses(struct sockaddr **sal)
|
|
|
|
{
|
|
|
|
struct sockaddr **sap;
|
2000-09-03 17:30:41 +08:00
|
|
|
|
2000-09-05 21:56:11 +08:00
|
|
|
if (sal == NULL)
|
|
|
|
return;
|
|
|
|
for (sap = sal; *sap != NULL; sap++)
|
2000-09-06 01:37:44 +08:00
|
|
|
efree(*sap);
|
|
|
|
efree(sal);
|
2000-09-05 21:56:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int php_network_getaddresses(const char *host, struct sockaddr ***sal)
|
2000-09-03 17:30:41 +08:00
|
|
|
{
|
2000-09-05 21:56:11 +08:00
|
|
|
struct sockaddr **sap;
|
2001-01-13 19:48:47 +08:00
|
|
|
|
|
|
|
if (host == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2000-09-05 21:56:11 +08:00
|
|
|
#ifdef HAVE_GETADDRINFO
|
2001-01-13 19:48:47 +08:00
|
|
|
struct addrinfo hints, *res, *sai;
|
2000-09-05 21:56:11 +08:00
|
|
|
int n;
|
2000-09-03 17:30:41 +08:00
|
|
|
|
2001-01-13 19:48:47 +08:00
|
|
|
memset(&hints, '\0', sizeof(hints));
|
|
|
|
hints.ai_family = AF_UNSPEC;
|
2001-01-17 14:40:48 +08:00
|
|
|
if ((n = getaddrinfo(host, NULL, &hints, &res))) {
|
2001-01-13 19:48:47 +08:00
|
|
|
php_error(E_WARNING, "php_network_getaddresses: getaddrinfo failed: %s", PHP_GAI_STRERROR(n));
|
2000-09-05 21:56:11 +08:00
|
|
|
return -1;
|
2001-01-13 19:48:47 +08:00
|
|
|
}
|
2000-09-05 21:56:11 +08:00
|
|
|
|
2001-01-13 19:48:47 +08:00
|
|
|
sai = res;
|
|
|
|
for (n=2; (sai = sai->ai_next) != NULL; n++);
|
|
|
|
*sal = emalloc(n * sizeof(*sal));
|
|
|
|
sai = res;
|
2000-09-05 21:56:11 +08:00
|
|
|
sap = *sal;
|
2001-01-13 19:48:47 +08:00
|
|
|
do {
|
|
|
|
switch (sai->ai_family) {
|
2000-10-19 20:18:02 +08:00
|
|
|
# ifdef HAVE_IPV6
|
2001-01-13 19:48:47 +08:00
|
|
|
case AF_INET6:
|
2000-09-06 01:37:44 +08:00
|
|
|
*sap = emalloc(sizeof(struct sockaddr_in6));
|
2000-09-05 21:56:11 +08:00
|
|
|
*(struct sockaddr_in6 *)*sap =
|
|
|
|
*((struct sockaddr_in6 *)sai->ai_addr);
|
2001-01-13 19:48:47 +08:00
|
|
|
sap++;
|
|
|
|
break;
|
2000-09-05 21:56:11 +08:00
|
|
|
# endif
|
2001-01-13 19:48:47 +08:00
|
|
|
case AF_INET:
|
2000-09-06 01:37:44 +08:00
|
|
|
*sap = emalloc(sizeof(struct sockaddr_in));
|
2000-09-05 21:56:11 +08:00
|
|
|
*(struct sockaddr_in *)*sap =
|
|
|
|
*((struct sockaddr_in *)sai->ai_addr);
|
2001-01-13 19:48:47 +08:00
|
|
|
sap++;
|
2000-09-18 23:15:27 +08:00
|
|
|
break;
|
2001-01-13 19:48:47 +08:00
|
|
|
}
|
|
|
|
} while ((sai = sai->ai_next) != NULL);
|
|
|
|
freeaddrinfo(res);
|
2000-09-05 21:56:11 +08:00
|
|
|
#else
|
|
|
|
struct hostent *host_info;
|
|
|
|
struct in_addr in;
|
|
|
|
|
|
|
|
if (!inet_aton(host, &in)) {
|
|
|
|
/* XXX NOT THREAD SAFE */
|
|
|
|
host_info = gethostbyname(host);
|
2001-01-13 19:48:47 +08:00
|
|
|
if (host_info == NULL) {
|
|
|
|
php_error(E_WARNING, "php_network_getaddresses: gethostbyname failed");
|
2000-09-05 21:56:11 +08:00
|
|
|
return -1;
|
2001-01-13 19:48:47 +08:00
|
|
|
}
|
2000-09-05 21:56:11 +08:00
|
|
|
in = *((struct in_addr *) host_info->h_addr);
|
|
|
|
}
|
|
|
|
|
2000-09-06 01:37:44 +08:00
|
|
|
*sal = emalloc(2 * sizeof(*sal));
|
2000-09-05 21:56:11 +08:00
|
|
|
sap = *sal;
|
2000-09-06 01:37:44 +08:00
|
|
|
*sap = emalloc(sizeof(struct sockaddr_in));
|
2000-09-05 21:56:11 +08:00
|
|
|
(*sap)->sa_family = AF_INET;
|
|
|
|
((struct sockaddr_in *)*sap)->sin_addr = in;
|
|
|
|
sap++;
|
|
|
|
#endif
|
2001-01-13 19:48:47 +08:00
|
|
|
}
|
|
|
|
*sap = NULL;
|
|
|
|
return 0;
|
2000-09-03 17:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Creates a socket of type socktype and connects to the given host and
|
|
|
|
* port, returns the created socket on success, else returns -1.
|
2000-09-04 03:12:28 +08:00
|
|
|
* timeout gives timeout in seconds, 0 means blocking mode.
|
2000-09-03 17:30:41 +08:00
|
|
|
*/
|
2000-09-06 03:06:29 +08:00
|
|
|
int php_hostconnect(char *host, unsigned short port, int socktype, int timeout)
|
2000-09-03 17:30:41 +08:00
|
|
|
{
|
|
|
|
int s;
|
2000-09-05 21:56:11 +08:00
|
|
|
struct sockaddr **sal, **psal;
|
2000-09-03 17:30:41 +08:00
|
|
|
|
2000-09-05 21:56:11 +08:00
|
|
|
if (php_network_getaddresses(host, &sal))
|
2000-09-03 17:30:41 +08:00
|
|
|
return -1;
|
2000-09-05 21:56:11 +08:00
|
|
|
|
|
|
|
psal = sal;
|
|
|
|
while (*sal != NULL) {
|
|
|
|
s = socket((*sal)->sa_family, socktype, 0);
|
|
|
|
if (s != SOCK_ERR) {
|
|
|
|
switch ((*sal)->sa_family) {
|
2000-10-19 20:18:02 +08:00
|
|
|
#if defined( HAVE_GETADDRINFO ) && defined( HAVE_IPV6 )
|
2000-09-05 21:56:11 +08:00
|
|
|
case AF_INET6: {
|
|
|
|
struct sockaddr_in6 *sa =
|
|
|
|
(struct sockaddr_in6 *)*sal;
|
|
|
|
|
|
|
|
sa->sin6_family = (*sal)->sa_family;
|
|
|
|
sa->sin6_port = htons(port);
|
|
|
|
if (connect(s, (struct sockaddr *) sa,
|
|
|
|
sizeof(*sa)) != SOCK_CONN_ERR)
|
|
|
|
goto ok;
|
|
|
|
} break;
|
|
|
|
#endif
|
|
|
|
case AF_INET: {
|
|
|
|
struct sockaddr_in *sa =
|
|
|
|
(struct sockaddr_in *)*sal;
|
|
|
|
|
|
|
|
sa->sin_family = (*sal)->sa_family;
|
|
|
|
sa->sin_port = htons(port);
|
|
|
|
if (connect(s, (struct sockaddr *) sa,
|
|
|
|
sizeof(*sa)) != SOCK_CONN_ERR)
|
|
|
|
goto ok;
|
|
|
|
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
close (s);
|
|
|
|
}
|
|
|
|
sal++;
|
2000-09-03 17:30:41 +08:00
|
|
|
}
|
2000-09-05 21:56:11 +08:00
|
|
|
php_network_freeaddresses(psal);
|
2001-01-13 19:48:47 +08:00
|
|
|
php_error(E_WARNING, "php_hostconnect: connect failed");
|
2000-09-05 21:56:11 +08:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
ok:
|
|
|
|
php_network_freeaddresses(psal);
|
2000-09-03 17:30:41 +08:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* tab-width: 8
|
|
|
|
* c-basic-offset: 8
|
|
|
|
* End:
|
|
|
|
*/
|