summaryrefslogtreecommitdiff
path: root/source4/lib
diff options
context:
space:
mode:
authorAndrew Tridgell <tridge@samba.org>2004-10-27 03:15:42 +0000
committerGerald (Jerry) Carter <jerry@samba.org>2007-10-10 13:04:49 -0500
commit9d055846f225bea4953822f40fab1d2f1a2e2d07 (patch)
tree9a513f3eeb7223a96e1df1d65060b095002e534e /source4/lib
parent5ae448116165a6bb9d792686db825b8b47f27201 (diff)
downloadsamba-9d055846f225bea4953822f40fab1d2f1a2e2d07.tar.gz
samba-9d055846f225bea4953822f40fab1d2f1a2e2d07.tar.bz2
samba-9d055846f225bea4953822f40fab1d2f1a2e2d07.zip
r3278: - rewrote the client side rpc connection code to use lib/socket/
rather than doing everything itself. This greatly simplifies the code, although I really don't like the socket_recv() interface (it always allocates memory for you, which means an extra memcpy in this code) - fixed several bugs in the socket_ipv4.c code, in particular client side code used a non-blocking connect but didn't handle EINPROGRESS, so it had no chance of working. Also fixed the error codes, using map_nt_error_from_unix() - cleaned up and expanded map_nt_error_from_unix() - changed interpret_addr2() to not take a mem_ctx. It makes absolutely no sense to allocate a fixed size 4 byte structure like this. Dozens of places in the code were also using interpret_addr2() incorrectly (precisely because the allocation made no sense) (This used to be commit 7f2c771b0e0e98c5c9e5cf662592d64d34ff1205)
Diffstat (limited to 'source4/lib')
-rw-r--r--source4/lib/interface.c10
-rw-r--r--source4/lib/socket/socket.c2
-rw-r--r--source4/lib/socket/socket_ipv4.c105
-rw-r--r--source4/lib/socket/socket_unix.c26
-rw-r--r--source4/lib/util.c24
-rw-r--r--source4/lib/util_sock.c8
-rw-r--r--source4/lib/wins_srv.c4
7 files changed, 54 insertions, 125 deletions
diff --git a/source4/lib/interface.c b/source4/lib/interface.c
index 14abfbd09a..b842fbb292 100644
--- a/source4/lib/interface.c
+++ b/source4/lib/interface.c
@@ -116,7 +116,7 @@ static void interpret_interface(TALLOC_CTX *mem_ctx, const char *token)
/* maybe it is a DNS name */
p = strchr_m(token,'/');
if (!p) {
- ip = *interpret_addr2(mem_ctx, token);
+ ip = interpret_addr2(token);
for (i=0;i<total_probed;i++) {
if (ip.s_addr == probed_ifaces[i].ip.s_addr &&
!ip_equal(allones_ip, probed_ifaces[i].netmask)) {
@@ -132,10 +132,10 @@ static void interpret_interface(TALLOC_CTX *mem_ctx, const char *token)
/* parse it into an IP address/netmasklength pair */
*p++ = 0;
- ip = *interpret_addr2(mem_ctx, token);
+ ip = interpret_addr2(token);
if (strlen(p) > 2) {
- nmask = *interpret_addr2(mem_ctx, p);
+ nmask = interpret_addr2(p);
} else {
nmask.s_addr = htonl(((ALLONES >> atoi(p)) ^ ALLONES));
}
@@ -174,8 +174,8 @@ void load_interfaces(void)
return;
}
- allones_ip = *interpret_addr2(mem_ctx, "255.255.255.255");
- loopback_ip = *interpret_addr2(mem_ctx, "127.0.0.1");
+ allones_ip = interpret_addr2("255.255.255.255");
+ loopback_ip = interpret_addr2("127.0.0.1");
SAFE_FREE(probed_ifaces);
diff --git a/source4/lib/socket/socket.c b/source4/lib/socket/socket.c
index f70a76262b..f5ee84a7cc 100644
--- a/source4/lib/socket/socket.c
+++ b/source4/lib/socket/socket.c
@@ -134,7 +134,7 @@ NTSTATUS socket_accept(struct socket_context *sock, struct socket_context **new_
}
NTSTATUS socket_recv(struct socket_context *sock, TALLOC_CTX *mem_ctx,
- DATA_BLOB *blob, size_t wantlen, uint32_t flags)
+ DATA_BLOB *blob, size_t wantlen, uint32_t flags)
{
if (sock->type != SOCKET_TYPE_STREAM) {
return NT_STATUS_INVALID_PARAMETER;
diff --git a/source4/lib/socket/socket_ipv4.c b/source4/lib/socket/socket_ipv4.c
index 23e34dd39b..20dfd3c92f 100644
--- a/source4/lib/socket/socket_ipv4.c
+++ b/source4/lib/socket/socket_ipv4.c
@@ -24,8 +24,7 @@ static NTSTATUS ipv4_tcp_init(struct socket_context *sock)
{
sock->fd = socket(PF_INET, SOCK_STREAM, 0);
if (sock->fd == -1) {
- /* TODO: we need to map from errno to NTSTATUS here! */
- return NT_STATUS_FOOBAR;
+ return map_nt_error_from_unix(errno);
}
return NT_STATUS_OK;
@@ -37,41 +36,34 @@ static void ipv4_tcp_close(struct socket_context *sock)
}
static NTSTATUS ipv4_tcp_connect(struct socket_context *sock,
- const char *my_address, int my_port,
- const char *srv_address, int srv_port,
- uint32_t flags)
+ const char *my_address, int my_port,
+ const char *srv_address, int srv_port,
+ uint32_t flags)
{
- struct sockaddr_in my_addr;
struct sockaddr_in srv_addr;
struct in_addr my_ip;
struct in_addr srv_ip;
int ret;
- ret = inet_aton(my_address, &my_ip);
- if (ret == 0) {
- /* not a valid ipv4 address */
- return NT_STATUS_FOOBAR;
- }
+ my_ip = interpret_addr2(my_address);
- ZERO_STRUCT(my_addr);
+ if (my_ip.s_addr != 0 || my_port != 0) {
+ struct sockaddr_in my_addr;
+ ZERO_STRUCT(my_addr);
#ifdef HAVE_SOCK_SIN_LEN
- my_addr.sin_len = sizeof(my_addr);
+ my_addr.sin_len = sizeof(my_addr);
#endif
- my_addr.sin_addr = my_ip;
- my_addr.sin_port = htons(my_port);
- my_addr.sin_family = PF_INET;
-
- ret = inet_aton(srv_address, &srv_ip);
- if (ret == 0) {
- /* not a valid ipv4 address */
- return NT_STATUS_FOOBAR;
+ my_addr.sin_addr = my_ip;
+ my_addr.sin_port = htons(my_port);
+ my_addr.sin_family = PF_INET;
+
+ ret = bind(sock->fd, (struct sockaddr *)&my_addr, sizeof(my_addr));
+ if (ret == -1) {
+ return map_nt_error_from_unix(errno);
+ }
}
- ret = bind(sock->fd, (struct sockaddr *)&my_addr, sizeof(my_addr));
- if (ret == -1) {
- /* TODO: we need to map from errno to NTSTATUS here! */
- return NT_STATUS_FOOBAR;
- }
+ srv_ip = interpret_addr2(srv_address);
ZERO_STRUCT(srv_addr);
#ifdef HAVE_SOCK_SIN_LEN
@@ -81,20 +73,18 @@ static NTSTATUS ipv4_tcp_connect(struct socket_context *sock,
srv_addr.sin_port = htons(srv_port);
srv_addr.sin_family = PF_INET;
+ ret = connect(sock->fd, (const struct sockaddr *)&srv_addr, sizeof(srv_addr));
+ if (ret == -1) {
+ return map_nt_error_from_unix(errno);
+ }
+
if (!(flags & SOCKET_FLAG_BLOCK)) {
ret = set_blocking(sock->fd, False);
if (ret == -1) {
- /* TODO: we need to map from errno to NTSTATUS here! */
- return NT_STATUS_FOOBAR;
+ return map_nt_error_from_unix(errno);
}
}
- ret = connect(sock->fd, (const struct sockaddr *)&srv_addr, sizeof(srv_addr));
- if (ret == -1) {
- /* TODO: we need to map from errno to NTSTATUS here! */
- return NT_STATUS_FOOBAR;
- }
-
sock->state = SOCKET_STATE_CLIENT_CONNECTED;
return NT_STATUS_OK;
@@ -108,11 +98,7 @@ static NTSTATUS ipv4_tcp_listen(struct socket_context *sock,
struct in_addr ip_addr;
int ret;
- ret = inet_aton(my_address, &ip_addr);
- if (ret == 0) {
- /* not a valid ipv4 address */
- return NT_STATUS_FOOBAR;
- }
+ ip_addr = interpret_addr2(my_address);
ZERO_STRUCT(my_addr);
#ifdef HAVE_SOCK_SIN_LEN
@@ -124,21 +110,18 @@ static NTSTATUS ipv4_tcp_listen(struct socket_context *sock,
ret = bind(sock->fd, (struct sockaddr *)&my_addr, sizeof(my_addr));
if (ret == -1) {
- /* TODO: we need to map from errno to NTSTATUS here! */
- return NT_STATUS_FOOBAR;
+ return map_nt_error_from_unix(errno);
}
ret = listen(sock->fd, queue_size);
if (ret == -1) {
- /* TODO: we need to map from errno to NTSTATUS here! */
- return NT_STATUS_FOOBAR;
+ return map_nt_error_from_unix(errno);
}
if (!(flags & SOCKET_FLAG_BLOCK)) {
ret = set_blocking(sock->fd, False);
if (ret == -1) {
- /* TODO: we need to map from errno to NTSTATUS here! */
- return NT_STATUS_FOOBAR;
+ return map_nt_error_from_unix(errno);
}
}
@@ -155,8 +138,7 @@ static NTSTATUS ipv4_tcp_accept(struct socket_context *sock, struct socket_conte
new_fd = accept(sock->fd, (struct sockaddr *)&cli_addr, &cli_addr_len);
if (new_fd == -1) {
- /* TODO: we need to map from errno to NTSTATUS here! */
- return NT_STATUS_FOOBAR;
+ return map_nt_error_from_unix(errno);
}
/* TODO: we could add a 'accept_check' hook here
@@ -257,34 +239,7 @@ static NTSTATUS ipv4_tcp_send(struct socket_context *sock, TALLOC_CTX *mem_ctx,
len = send(sock->fd, blob->data, blob->length, flgs);
if (len == -1) {
- NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
- switch (errno) {
- case EBADF:
- case ENOTSOCK:
- case EFAULT:
- case EINVAL:
- status = NT_STATUS_INVALID_PARAMETER;
- break;
- case EMSGSIZE:
- status = NT_STATUS_INVALID_BUFFER_SIZE;
- break;
- case EAGAIN:
- /*case EWOULDBLOCK: this is an alis of EAGAIN --metze */
- case EINTR:
- *sendlen = 0;
- status = STATUS_MORE_ENTRIES;
- break;
- case ENOBUFS:
- status = NT_STATUS_FOOBAR;
- break;
- case ENOMEM:
- status = NT_STATUS_NO_MEMORY;
- break;
- case EPIPE:
- status = NT_STATUS_CONNECTION_DISCONNECTED;
- break;
- }
- return status;
+ return map_nt_error_from_unix(errno);
}
*sendlen = len;
diff --git a/source4/lib/socket/socket_unix.c b/source4/lib/socket/socket_unix.c
index d75bb973fc..90802eae66 100644
--- a/source4/lib/socket/socket_unix.c
+++ b/source4/lib/socket/socket_unix.c
@@ -30,31 +30,7 @@
*/
static NTSTATUS unixdom_error(int ernum)
{
- switch (ernum) {
- case EBADF:
- case ENOTCONN:
- case ENOTSOCK:
- case EFAULT:
- case EINVAL:
- return NT_STATUS_INVALID_PARAMETER;
- case EAGAIN:
- case EINTR:
- return STATUS_MORE_ENTRIES;
- case ECONNREFUSED:
- return NT_STATUS_CONNECTION_REFUSED;
- case ENOBUFS:
- case ENOMEM:
- return NT_STATUS_NO_MEMORY;
- case ENFILE:
- case EMFILE:
- return NT_STATUS_INSUFFICIENT_RESOURCES;
- case EPIPE:
- return NT_STATUS_CONNECTION_DISCONNECTED;
- case EMSGSIZE:
- return NT_STATUS_INVALID_BUFFER_SIZE;
- }
-
- return NT_STATUS_UNSUCCESSFUL;
+ return map_nt_error_from_unix(ernum);
}
static NTSTATUS unixdom_init(struct socket_context *sock)
diff --git a/source4/lib/util.c b/source4/lib/util.c
index 0982694823..d7c5661f7d 100644
--- a/source4/lib/util.c
+++ b/source4/lib/util.c
@@ -406,16 +406,18 @@ BOOL is_ipaddress(const char *str)
/****************************************************************************
Interpret an internet address or name into an IP address in 4 byte form.
****************************************************************************/
-
uint32_t interpret_addr(const char *str)
{
struct hostent *hp;
uint32_t res;
- if (strcmp(str,"0.0.0.0") == 0)
- return(0);
- if (strcmp(str,"255.255.255.255") == 0)
- return(0xFFFFFFFF);
+ if (str == NULL ||
+ strcmp(str,"0.0.0.0") == 0) {
+ return 0;
+ }
+ if (strcmp(str,"255.255.255.255") == 0) {
+ return 0xFFFFFFFF;
+ }
/* if it's in the form of an IP address then get the lib to interpret it */
if (is_ipaddress(str)) {
@@ -444,16 +446,12 @@ uint32_t interpret_addr(const char *str)
/*******************************************************************
A convenient addition to interpret_addr().
******************************************************************/
-
-struct in_addr *interpret_addr2(TALLOC_CTX *mem_ctx, const char *str)
+struct in_addr interpret_addr2(const char *str)
{
- struct in_addr *ret;
+ struct in_addr ret;
uint32_t a = interpret_addr(str);
-
- ret = talloc(mem_ctx, sizeof(struct in_addr));
- if (!ret) return NULL;
- ret->s_addr = a;
- return(ret);
+ ret.s_addr = a;
+ return ret;
}
/*******************************************************************
diff --git a/source4/lib/util_sock.c b/source4/lib/util_sock.c
index 387c72599a..638c44f705 100644
--- a/source4/lib/util_sock.c
+++ b/source4/lib/util_sock.c
@@ -408,14 +408,14 @@ int open_udp_socket(const char *host, int port)
int type = SOCK_DGRAM;
struct sockaddr_in sock_out;
int res;
- struct in_addr *addr;
+ struct in_addr addr;
TALLOC_CTX *mem_ctx;
mem_ctx = talloc_init("open_udp_socket");
if (!mem_ctx) {
return -1;
}
- addr = interpret_addr2(mem_ctx, host);
+ addr = interpret_addr2(host);
res = socket(PF_INET, type, 0);
if (res == -1) {
@@ -423,7 +423,7 @@ int open_udp_socket(const char *host, int port)
}
memset((char *)&sock_out,'\0',sizeof(sock_out));
- putip((char *)&sock_out.sin_addr,(char *)addr);
+ putip((char *)&sock_out.sin_addr,(char *)&addr);
sock_out.sin_port = htons(port);
sock_out.sin_family = PF_INET;
@@ -508,7 +508,7 @@ char *get_socket_name(TALLOC_CTX *mem_ctx, int fd, BOOL force_lookup)
name_buf = talloc_strdup(mem_ctx, "UNKNOWN");
if (fd == -1) return name_buf;
- addr = *interpret_addr2(mem_ctx, p);
+ addr = interpret_addr2(p);
/* Look up the remote host name. */
if ((hp = gethostbyaddr((char *)&addr.s_addr, sizeof(addr.s_addr), AF_INET)) == 0) {
diff --git a/source4/lib/wins_srv.c b/source4/lib/wins_srv.c
index 71368658b0..eb7f280e6f 100644
--- a/source4/lib/wins_srv.c
+++ b/source4/lib/wins_srv.c
@@ -174,11 +174,11 @@ static void parse_ip(TALLOC_CTX *mem_ctx, struct tagged_ip *ip, const char *str)
char *s = strchr(str, ':');
if (!s) {
fstrcpy(ip->tag, "*");
- ip->ip = *interpret_addr2(mem_ctx, str);
+ ip->ip = interpret_addr2(str);
return;
}
- ip->ip = *interpret_addr2(mem_ctx, s+1);
+ ip->ip = interpret_addr2(s+1);
fstrcpy(ip->tag, str);
s = strchr(ip->tag, ':');
if (s) *s = 0;