summaryrefslogtreecommitdiff
path: root/ext/socket
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>1999-05-07 08:24:37 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>1999-05-07 08:24:37 +0000
commit3376271d9e3c6b96acb63720eb4d59643abeb32c (patch)
tree9a9d174d90329951c082fb46e950a8edcdb001b8 /ext/socket
parentc09a226803d177f2ab3426ec2c72a3186d789010 (diff)
990507
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_3@460 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/socket')
-rw-r--r--ext/socket/extconf.rb33
-rw-r--r--ext/socket/getaddrinfo.c28
-rw-r--r--ext/socket/getnameinfo.c15
-rw-r--r--ext/socket/socket.c34
4 files changed, 47 insertions, 63 deletions
diff --git a/ext/socket/extconf.rb b/ext/socket/extconf.rb
index 28a96f4b38..12b24667b4 100644
--- a/ext/socket/extconf.rb
+++ b/ext/socket/extconf.rb
@@ -239,45 +239,20 @@ end
$objs = ["socket.o"]
if $getaddr_info_ok
- if have_func("getaddrinfo") and
- have_func("getnameinfo")
+ if have_func("getaddrinfo") and have_func("getnameinfo")
have_getaddrinfo = true
end
end
if have_getaddrinfo
$CFLAGS="-DHAVE_GETADDRINFO "+$CFLAGS
- if try_link(<<EOF)
-#include <sys/types.h>
-#include <netdb.h>
-#include <string.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-int
-main()
-{
- struct sockaddr_storage storage;
- struct sockaddr_storage *addr = 0;
-
- addr->__ss_family = &storage.__ss_family;
- addr->__ss_len = &storage.__ss_len;
- return 0;
-}
-EOF
- sockaddr_storage=true
- $CFLAGS+=" -DHAVE_SS_LEN"
- end
else
sockaddr_storage=true
$CFLAGS="-I. "+$CFLAGS
$objs += "getaddrinfo.o"
$objs += "getnameinfo.o"
- have_func("inet_ntop")
- have_func("inet_pton")
-end
-
-if sockaddr_storage
- $CFLAGS="-DSOCKADDR_STORAGE=sockaddr_storage "+$CFLAGS
+ have_func("inet_ntop") or have_func("inet_ntoa")
+ have_func("inet_pton") or have_func("inet_aton")
end
have_header("sys/un.h")
@@ -287,7 +262,7 @@ if have_func(test_func)
unless have_func("gethostname")
have_func("uname")
end
- if ENV["SOCKS_SERVER"] # test if SOCKSsocket needed
+ if ENV["SOCKS_SERVER"] or enable_config("socks", false)
if have_library("socks", "Rconnect")
$CFLAGS="-DSOCKS"
end
diff --git a/ext/socket/getaddrinfo.c b/ext/socket/getaddrinfo.c
index fc3f654cee..64d29b17ab 100644
--- a/ext/socket/getaddrinfo.c
+++ b/ext/socket/getaddrinfo.c
@@ -212,20 +212,32 @@ str_isnumber(p)
#ifndef HAVE_INET_PTON
-#ifndef INADDR_NONE
-# define INADDR_NONE 0xffffffff
-#endif
-
static int
inet_pton(af, hostname, pton)
int af;
const char *hostname;
- char *pton;
+ void *pton;
{
struct in_addr in;
- in.s_addr = inet_addr(hostname);
- if (in.s_addr == INADDR_NONE)
- return 0;
+
+#ifdef HAVE_INET_ATON
+ if (!inet_aton(hostname, &in.s_addr))
+ return 0;
+#else
+ int d1, d2, d3, d4;
+ char ch;
+
+ if (sscanf(hostname, "%d.%d.%d.%d%c", &d1, &d2, &d3, &d4, &ch) == 4 &&
+ 0 <= d1 && d1 <= 255 && 0 <= d2 && d2 <= 255 &&
+ 0 <= d3 && d3 <= 255 && 0 <= d4 && d4 <= 255) {
+ in.s_addr = htonl(
+ ((long) d1 << 24) | ((long) d2 << 16) |
+ ((long) d3 << 8) | ((long) d4 << 0));
+ }
+ else {
+ return 0;
+ }
+#endif
memcpy(pton, &in, sizeof(in));
return 1;
}
diff --git a/ext/socket/getnameinfo.c b/ext/socket/getnameinfo.c
index e217b50895..e4a57c6554 100644
--- a/ext/socket/getnameinfo.c
+++ b/ext/socket/getnameinfo.c
@@ -83,16 +83,23 @@ struct sockinet {
#define ENI_SALEN 6
#ifndef HAVE_INET_NTOP
-static char *
+static const char *
inet_ntop(af, addr, numaddr, numaddr_len)
int af;
- char *addr;
+ __const void *addr;
char *numaddr;
- int numaddr_len;
+ size_t numaddr_len;
{
+#ifdef HAVE_INET_NTOA
struct in_addr in;
memcpy(&in.s_addr, addr, sizeof(in.s_addr));
- strcpy(numaddr, inet_ntoa(in));
+ strncpy(numaddr, numaddr_len, inet_ntoa(in));
+#else
+ unsigned long x = ntohl(*(unsigned long*)addr);
+ snprintf(numaddr, numaddr_len, "%d.%d.%d.%d",
+ (int) (x>>24) & 0xff, (int) (x>>16) & 0xff,
+ (int) (x>> 8) & 0xff, (int) (x>> 0) & 0xff);
+#endif
return numaddr;
}
#endif
diff --git a/ext/socket/socket.c b/ext/socket/socket.c
index b0f108a2c1..6126ddf2b9 100644
--- a/ext/socket/socket.c
+++ b/ext/socket/socket.c
@@ -49,14 +49,6 @@ extern int rb_thread_select(int, fd_set*, fd_set*, fd_set*, struct timeval*); /*
#endif
#include "sockport.h"
-#ifdef SOCKADDR_STORAGE
-# define SS_LEN(ss) (ss)->ss_len
-#else
-# define SOCKADDR_STORAGE sockaddr
-# undef ss_family
-# define ss_family sa_family
-#endif
-
VALUE rb_cBasicSocket;
VALUE rb_cIPSocket;
VALUE rb_cTCPSocket;
@@ -700,7 +692,7 @@ static VALUE
tcp_s_gethostbyname(obj, host)
VALUE obj, host;
{
- struct SOCKADDR_STORAGE addr;
+ struct sockaddr addr;
struct hostent *h;
char **pch;
VALUE ary, names;
@@ -717,7 +709,7 @@ tcp_s_gethostbyname(obj, host)
else {
setipaddr(STR2CSTR(host), (struct sockaddr *)&addr);
}
- switch (addr.ss_family) {
+ switch (addr.sa_family) {
case AF_INET:
{
struct sockaddr_in *sin;
@@ -760,7 +752,7 @@ tcp_s_gethostbyname(obj, host)
rb_ary_push(ary, INT2NUM(h->h_addrtype));
#ifdef h_addr
for (pch = h->h_addr_list; *pch; pch++) {
- switch (addr.ss_family) {
+ switch (addr.sa_family) {
case AF_INET:
{
struct sockaddr_in sin;
@@ -849,7 +841,7 @@ tcp_accept(sock)
VALUE sock;
{
OpenFile *fptr;
- struct SOCKADDR_STORAGE from;
+ struct sockaddr from;
int fromlen;
GetOpenFile(sock, fptr);
@@ -915,7 +907,7 @@ ip_addr(sock)
VALUE sock;
{
OpenFile *fptr;
- struct SOCKADDR_STORAGE addr;
+ struct sockaddr addr;
int len = sizeof addr;
GetOpenFile(sock, fptr);
@@ -930,7 +922,7 @@ ip_peeraddr(sock)
VALUE sock;
{
OpenFile *fptr;
- struct SOCKADDR_STORAGE addr;
+ struct sockaddr addr;
int len = sizeof addr;
GetOpenFile(sock, fptr);
@@ -944,7 +936,7 @@ static VALUE
ip_s_getaddress(obj, host)
VALUE obj, host;
{
- struct SOCKADDR_STORAGE addr;
+ struct sockaddr addr;
if (rb_obj_is_kind_of(host, rb_cInteger)) {
int i = NUM2INT(host);
@@ -1068,7 +1060,7 @@ static VALUE
udp_bind(sock, host, port)
VALUE sock, host, port;
{
- struct SOCKADDR_STORAGE addr;
+ struct sockaddr addr;
OpenFile *fptr;
struct addrinfo *res0, *res;
@@ -1538,7 +1530,7 @@ static VALUE
sock_s_gethostbyname(obj, host)
VALUE obj, host;
{
- struct SOCKADDR_STORAGE addr;
+ struct sockaddr addr;
struct hostent *h;
if (rb_obj_is_kind_of(host, rb_cInteger)) {
@@ -1553,7 +1545,7 @@ sock_s_gethostbyname(obj, host)
else {
setipaddr(STR2CSTR(host), (struct sockaddr *)&addr);
}
- switch (addr.ss_family) {
+ switch (addr.sa_family) {
case AF_INET:
{
struct sockaddr_in *sin;
@@ -1709,7 +1701,7 @@ sock_s_getnameinfo(argc, argv)
int fl;
struct addrinfo hints, *res = NULL;
int error;
- struct SOCKADDR_STORAGE ss;
+ struct sockaddr ss;
struct sockaddr *sap;
sa = flags = Qnil;
@@ -1720,11 +1712,9 @@ sock_s_getnameinfo(argc, argv)
rb_raise(rb_eTypeError, "sockaddr length too big");
}
memcpy(&ss, RSTRING(sa)->ptr, RSTRING(sa)->len);
-#ifdef HAVE_SS_LEN
- if (RSTRING(sa)->len != SS_LEN(&ss)) {
+ if (RSTRING(sa)->len != SA_LEN(&ss)) {
rb_raise(rb_eTypeError, "sockaddr size differs - should not happen");
}
-#endif
sap = (struct sockaddr *)&ss;
}
else if (TYPE(sa) == T_ARRAY) {