summaryrefslogtreecommitdiff
path: root/source4/lib
diff options
context:
space:
mode:
authorAndrew Bartlett <abartlet@samba.org>2006-01-09 22:12:53 +0000
committerGerald (Jerry) Carter <jerry@samba.org>2007-10-10 13:49:57 -0500
commitf55ea8bb3dca868e21663cd90eaea7a35cd7886c (patch)
tree80aab2a3f10310e1946821603752cd407e435214 /source4/lib
parent806b3fdbc12b3284ab9872a4ecae3a7ee34ea171 (diff)
downloadsamba-f55ea8bb3dca868e21663cd90eaea7a35cd7886c.tar.gz
samba-f55ea8bb3dca868e21663cd90eaea7a35cd7886c.tar.bz2
samba-f55ea8bb3dca868e21663cd90eaea7a35cd7886c.zip
r12804: This patch reworks the Samba4 sockets layer to use a socket_address
structure that is more generic than just 'IP/port'. It now passes make test, and has been reviewed and updated by metze. (Thankyou *very* much). This passes 'make test' as well as kerberos use (not currently in the testsuite). The original purpose of this patch was to have Samba able to pass a socket address stucture from the BSD layer into the kerberos routines and back again. It also removes nbt_peer_addr, which was being used for a similar purpose. It is a large change, but worthwhile I feel. Andrew Bartlett (This used to be commit 88198c4881d8620a37086f80e4da5a5b71c5bbb2)
Diffstat (limited to 'source4/lib')
-rw-r--r--source4/lib/messaging/messaging.c30
-rw-r--r--source4/lib/socket/access.c16
-rw-r--r--source4/lib/socket/connect.c58
-rw-r--r--source4/lib/socket/connect_multi.c15
-rw-r--r--source4/lib/socket/socket.c103
-rw-r--r--source4/lib/socket/socket.h65
-rw-r--r--source4/lib/socket/socket_ipv4.c274
-rw-r--r--source4/lib/socket/socket_ipv6.c185
-rw-r--r--source4/lib/socket/socket_unix.c175
9 files changed, 592 insertions, 329 deletions
diff --git a/source4/lib/messaging/messaging.c b/source4/lib/messaging/messaging.c
index aa2fe0b150..df15928235 100644
--- a/source4/lib/messaging/messaging.c
+++ b/source4/lib/messaging/messaging.c
@@ -147,10 +147,20 @@ static NTSTATUS try_send(struct messaging_rec *rec)
size_t nsent;
void *priv;
NTSTATUS status;
+ struct socket_address *path;
+
+ /* rec->path is the path of the *other* socket, where we want
+ * this to end up */
+ path = socket_address_from_strings(msg, msg->sock->backend_name,
+ rec->path, 0);
+ if (!path) {
+ return NT_STATUS_NO_MEMORY;
+ }
/* we send with privileges so messages work from any context */
priv = root_privileges();
- status = socket_sendto(msg->sock, &rec->packet, &nsent, 0, rec->path, 0);
+ status = socket_sendto(msg->sock, &rec->packet, &nsent, 0, path);
+ talloc_free(path);
talloc_free(priv);
return status;
@@ -382,7 +392,8 @@ struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx, uint32_t server_id
{
struct messaging_context *msg;
NTSTATUS status;
- char *path;
+ struct socket_address *path;
+ char *dir;
msg = talloc(mem_ctx, struct messaging_context);
if (msg == NULL) {
@@ -394,9 +405,9 @@ struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx, uint32_t server_id
}
/* create the messaging directory if needed */
- path = smbd_tmp_path(msg, "messaging");
- mkdir(path, 0700);
- talloc_free(path);
+ dir = smbd_tmp_path(msg, "messaging");
+ mkdir(dir, 0700);
+ talloc_free(dir);
msg->base_path = smbd_tmp_path(msg, "messaging");
msg->path = messaging_path(msg, server_id);
@@ -418,7 +429,14 @@ struct messaging_context *messaging_init(TALLOC_CTX *mem_ctx, uint32_t server_id
deleted) on exit */
talloc_steal(msg, msg->sock);
- status = socket_listen(msg->sock, msg->path, 0, 50, 0);
+ path = socket_address_from_strings(msg, msg->sock->backend_name,
+ msg->path, 0);
+ if (!path) {
+ talloc_free(msg);
+ return NULL;
+ }
+
+ status = socket_listen(msg->sock, path, 50, 0);
if (!NT_STATUS_IS_OK(status)) {
DEBUG(0,("Unable to setup messaging listener for '%s':%s\n", msg->path, nt_errstr(status)));
talloc_free(msg);
diff --git a/source4/lib/socket/access.c b/source4/lib/socket/access.c
index 1d0a90f1ee..8e57ca5aff 100644
--- a/source4/lib/socket/access.c
+++ b/source4/lib/socket/access.c
@@ -310,7 +310,8 @@ BOOL socket_check_access(struct socket_context *sock,
const char **allow_list, const char **deny_list)
{
BOOL ret;
- const char *name="", *addr;
+ const char *name="";
+ struct socket_address *addr;
TALLOC_CTX *mem_ctx;
if ((!deny_list || *deny_list==0) &&
@@ -324,13 +325,18 @@ BOOL socket_check_access(struct socket_context *sock,
}
addr = socket_get_peer_addr(sock, mem_ctx);
+ if (!addr) {
+ DEBUG(0,("socket_check_access: Denied connection from unknown host: could not get peer address from kernel\n"));
+ talloc_free(mem_ctx);
+ return False;
+ }
/* bypass gethostbyaddr() calls if the lists only contain IP addrs */
if (!only_ipaddrs_in_list(allow_list) ||
!only_ipaddrs_in_list(deny_list)) {
name = socket_get_peer_name(sock, mem_ctx);
if (!name) {
- name = addr;
+ name = addr->addr;
}
}
@@ -340,14 +346,14 @@ BOOL socket_check_access(struct socket_context *sock,
return False;
}
- ret = allow_access(mem_ctx, deny_list, allow_list, name, addr);
+ ret = allow_access(mem_ctx, deny_list, allow_list, name, addr->addr);
if (ret) {
DEBUG(2,("socket_check_access: Allowed connection to '%s' from %s (%s)\n",
- service_name, name, addr));
+ service_name, name, addr->addr));
} else {
DEBUG(0,("socket_check_access: Denied connection to '%s' from %s (%s)\n",
- service_name, name, addr));
+ service_name, name, addr->addr));
}
talloc_free(mem_ctx);
diff --git a/source4/lib/socket/connect.c b/source4/lib/socket/connect.c
index dd3a6047a6..dc64198caa 100644
--- a/source4/lib/socket/connect.c
+++ b/source4/lib/socket/connect.c
@@ -30,10 +30,8 @@
struct connect_state {
struct socket_context *sock;
- const char *my_address;
- int my_port;
- const char *server_address;
- int server_port;
+ const struct socket_address *my_address;
+ const struct socket_address *server_address;
uint32_t flags;
};
@@ -62,9 +60,7 @@ static void socket_send_connect(struct composite_context *result)
result->status = socket_connect(state->sock,
state->my_address,
- state->my_port,
state->server_address,
- state->server_port,
state->flags);
if (NT_STATUS_IS_ERR(result->status) &&
!NT_STATUS_EQUAL(result->status,
@@ -85,10 +81,8 @@ static void socket_send_connect(struct composite_context *result)
send a socket connect, potentially doing some name resolution first
*/
struct composite_context *socket_connect_send(struct socket_context *sock,
- const char *my_address,
- int my_port,
- const char *server_address,
- int server_port,
+ struct socket_address *my_address,
+ struct socket_address *server_address,
uint32_t flags,
struct event_context *event_ctx)
{
@@ -101,33 +95,39 @@ struct composite_context *socket_connect_send(struct socket_context *sock,
result->event_ctx = event_ctx;
state = talloc_zero(result, struct connect_state);
- if (composite_nomem(state, result)) goto failed;
+ if (composite_nomem(state, result)) return result;
result->private_data = state;
state->sock = talloc_reference(state, sock);
- if (composite_nomem(state->sock, result)) goto failed;
+ if (composite_nomem(state->sock, result)) return result;
if (my_address) {
- state->my_address = talloc_strdup(state, my_address);
- if (composite_nomem(state->my_address, result)) goto failed;
+ void *ref = talloc_reference(state, my_address);
+ if (composite_nomem(ref, result)) {
+ return result;
+ }
+ state->my_address = my_address;
}
- state->my_port = my_port;
- state->server_address = talloc_strdup(state, server_address);
- if (composite_nomem(state->server_address, result)) goto failed;
+ {
+ void *ref = talloc_reference(state, server_address);
+ if (composite_nomem(ref, result)) {
+ return result;
+ }
+ state->server_address = server_address;
+ }
- state->server_port = server_port;
state->flags = flags;
set_blocking(socket_get_fd(sock), False);
- if (strcmp(sock->backend_name, "ipv4") == 0) {
+ if (server_address->addr && strcmp(sock->backend_name, "ipv4") == 0) {
struct nbt_name name;
struct composite_context *creq;
- make_nbt_name_client(&name, server_address);
+ make_nbt_name_client(&name, server_address->addr);
creq = resolve_name_send(&name, result->event_ctx,
lp_name_resolve_order());
- if (composite_nomem(creq, result)) goto failed;
+ if (composite_nomem(creq, result)) return result;
composite_continue(result, creq, continue_resolve_name, result);
return result;
}
@@ -135,10 +135,6 @@ struct composite_context *socket_connect_send(struct socket_context *sock,
socket_send_connect(result);
return result;
-
-failed:
- composite_error(result, result->status);
- return result;
}
/*
@@ -172,7 +168,9 @@ static void continue_resolve_name(struct composite_context *creq)
result->status = resolve_name_recv(creq, state, &addr);
if (!composite_is_ok(result)) return;
- state->server_address = addr;
+ state->server_address = socket_address_from_strings(state, state->sock->backend_name,
+ addr, state->server_address->port);
+ if (composite_nomem(state->server_address, result)) return;
socket_send_connect(result);
}
@@ -205,12 +203,12 @@ NTSTATUS socket_connect_recv(struct composite_context *result)
like socket_connect() but takes an event context, doing a semi-async connect
*/
NTSTATUS socket_connect_ev(struct socket_context *sock,
- const char *my_address, int my_port,
- const char *server_address, int server_port,
+ struct socket_address *my_address,
+ struct socket_address *server_address,
uint32_t flags, struct event_context *ev)
{
struct composite_context *ctx;
- ctx = socket_connect_send(sock, my_address, my_port,
- server_address, server_port, flags, ev);
+ ctx = socket_connect_send(sock, my_address,
+ server_address, flags, ev);
return socket_connect_recv(ctx);
}
diff --git a/source4/lib/socket/connect_multi.c b/source4/lib/socket/connect_multi.c
index 6a90ef9a05..7396435075 100644
--- a/source4/lib/socket/connect_multi.c
+++ b/source4/lib/socket/connect_multi.c
@@ -49,7 +49,7 @@ struct connect_multi_state {
struct connect_one_state {
struct composite_context *result;
struct socket_context *sock;
- uint16_t port;
+ struct socket_address *addr;
};
static void continue_resolve_name(struct composite_context *creq);
@@ -144,15 +144,18 @@ static void connect_multi_next_socket(struct composite_context *result)
if (composite_nomem(state, result)) return;
state->result = result;
- state->port = multi->ports[next];
-
result->status = socket_create("ipv4", SOCKET_TYPE_STREAM, &state->sock, 0);
if (!composite_is_ok(result)) return;
+ /* Form up the particular address we are interested in */
+ state->addr = socket_address_from_strings(state, state->sock->backend_name,
+ multi->server_address, multi->ports[next]);
+ if (composite_nomem(state->addr, result)) return;
+
talloc_steal(state, state->sock);
- creq = socket_connect_send(state->sock, NULL, 0,
- multi->server_address, state->port, 0, result->event_ctx);
+ creq = socket_connect_send(state->sock, NULL,
+ state->addr, 0, result->event_ctx);
if (composite_nomem(creq, result)) return;
talloc_steal(state, creq);
@@ -220,7 +223,7 @@ static void continue_one(struct composite_context *creq)
if (NT_STATUS_IS_OK(status)) {
multi->sock = talloc_steal(multi, state->sock);
- multi->result_port = state->port;
+ multi->result_port = state->addr->port;
}
talloc_free(state);
diff --git a/source4/lib/socket/socket.c b/source4/lib/socket/socket.c
index 380071f46d..23117f2fa9 100644
--- a/source4/lib/socket/socket.c
+++ b/source4/lib/socket/socket.c
@@ -95,8 +95,8 @@ NTSTATUS socket_create(const char *name, enum socket_type type,
}
NTSTATUS socket_connect(struct socket_context *sock,
- const char *my_address, int my_port,
- const char *server_address, int server_port,
+ const struct socket_address *my_address,
+ const struct socket_address *server_address,
uint32_t flags)
{
if (sock == NULL) {
@@ -110,7 +110,7 @@ NTSTATUS socket_connect(struct socket_context *sock,
return NT_STATUS_NOT_IMPLEMENTED;
}
- return sock->ops->fn_connect(sock, my_address, my_port, server_address, server_port, flags);
+ return sock->ops->fn_connect(sock, my_address, server_address, flags);
}
NTSTATUS socket_connect_complete(struct socket_context *sock, uint32_t flags)
@@ -121,7 +121,9 @@ NTSTATUS socket_connect_complete(struct socket_context *sock, uint32_t flags)
return sock->ops->fn_connect_complete(sock, flags);
}
-NTSTATUS socket_listen(struct socket_context *sock, const char *my_address, int port, int queue_size, uint32_t flags)
+NTSTATUS socket_listen(struct socket_context *sock,
+ const struct socket_address *my_address,
+ int queue_size, uint32_t flags)
{
if (sock == NULL) {
return NT_STATUS_CONNECTION_DISCONNECTED;
@@ -134,7 +136,7 @@ NTSTATUS socket_listen(struct socket_context *sock, const char *my_address, int
return NT_STATUS_NOT_IMPLEMENTED;
}
- return sock->ops->fn_listen(sock, my_address, port, queue_size, flags);
+ return sock->ops->fn_listen(sock, my_address, queue_size, flags);
}
NTSTATUS socket_accept(struct socket_context *sock, struct socket_context **new_sock)
@@ -194,7 +196,7 @@ NTSTATUS socket_recv(struct socket_context *sock, void *buf,
NTSTATUS socket_recvfrom(struct socket_context *sock, void *buf,
size_t wantlen, size_t *nread, uint32_t flags,
- const char **src_addr, int *src_port)
+ TALLOC_CTX *mem_ctx, struct socket_address **src_addr)
{
if (sock == NULL) {
return NT_STATUS_CONNECTION_DISCONNECTED;
@@ -208,7 +210,7 @@ NTSTATUS socket_recvfrom(struct socket_context *sock, void *buf,
}
return sock->ops->fn_recvfrom(sock, buf, wantlen, nread, flags,
- src_addr, src_port);
+ mem_ctx, src_addr);
}
NTSTATUS socket_send(struct socket_context *sock,
@@ -242,7 +244,7 @@ NTSTATUS socket_send(struct socket_context *sock,
NTSTATUS socket_sendto(struct socket_context *sock,
const DATA_BLOB *blob, size_t *sendlen, uint32_t flags,
- const char *dest_addr, int dest_port)
+ const struct socket_address *dest_addr)
{
if (sock == NULL) {
return NT_STATUS_CONNECTION_DISCONNECTED;
@@ -260,7 +262,7 @@ NTSTATUS socket_sendto(struct socket_context *sock,
return NT_STATUS_NOT_IMPLEMENTED;
}
- return sock->ops->fn_sendto(sock, blob, sendlen, flags, dest_addr, dest_port);
+ return sock->ops->fn_sendto(sock, blob, sendlen, flags, dest_addr);
}
@@ -300,7 +302,7 @@ char *socket_get_peer_name(struct socket_context *sock, TALLOC_CTX *mem_ctx)
return sock->ops->fn_get_peer_name(sock, mem_ctx);
}
-char *socket_get_peer_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx)
+struct socket_address *socket_get_peer_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx)
{
if (!sock->ops->fn_get_peer_addr) {
return NULL;
@@ -309,16 +311,7 @@ char *socket_get_peer_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx)
return sock->ops->fn_get_peer_addr(sock, mem_ctx);
}
-int socket_get_peer_port(struct socket_context *sock)
-{
- if (!sock->ops->fn_get_peer_port) {
- return -1;
- }
-
- return sock->ops->fn_get_peer_port(sock);
-}
-
-char *socket_get_my_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx)
+struct socket_address *socket_get_my_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx)
{
if (!sock->ops->fn_get_my_addr) {
return NULL;
@@ -327,15 +320,6 @@ char *socket_get_my_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx)
return sock->ops->fn_get_my_addr(sock, mem_ctx);
}
-int socket_get_my_port(struct socket_context *sock)
-{
- if (!sock->ops->fn_get_my_port) {
- return -1;
- }
-
- return sock->ops->fn_get_my_port(sock);
-}
-
int socket_get_fd(struct socket_context *sock)
{
if (!sock->ops->fn_get_fd) {
@@ -367,19 +351,70 @@ NTSTATUS socket_dup(struct socket_context *sock)
}
-const struct socket_ops *socket_getops_byname(const char *name, enum socket_type type)
+/* Create a new socket_address. The type must match the socket type.
+ * The host parameter may be an IP or a hostname
+ */
+
+struct socket_address *socket_address_from_strings(TALLOC_CTX *mem_ctx,
+ const char *family,
+ const char *host,
+ int port)
+{
+ struct socket_address *addr = talloc(mem_ctx, struct socket_address);
+ if (!addr) {
+ return NULL;
+ }
+
+ addr->family = family;
+ addr->addr = talloc_strdup(addr, host);
+ if (!addr->addr) {
+ talloc_free(addr);
+ return NULL;
+ }
+ addr->port = port;
+ addr->sockaddr = NULL;
+ addr->sockaddrlen = 0;
+
+ return addr;
+}
+
+/* Create a new socket_address. Copy the struct sockaddr into the new
+ * structure. Used for hooks in the kerberos libraries, where they
+ * supply only a struct sockaddr */
+
+struct socket_address *socket_address_from_sockaddr(TALLOC_CTX *mem_ctx,
+ struct sockaddr *sockaddr,
+ size_t sockaddrlen)
+{
+ struct socket_address *addr = talloc(mem_ctx, struct socket_address);
+ if (!addr) {
+ return NULL;
+ }
+ addr->family = NULL;
+ addr->addr = NULL;
+ addr->port = 0;
+ addr->sockaddr = talloc_memdup(addr, sockaddr, sockaddrlen);
+ if (!addr->sockaddr) {
+ talloc_free(addr);
+ return NULL;
+ }
+ addr->sockaddrlen = sockaddrlen;
+ return addr;
+}
+
+const struct socket_ops *socket_getops_byname(const char *family, enum socket_type type)
{
extern const struct socket_ops *socket_ipv4_ops(enum socket_type );
extern const struct socket_ops *socket_ipv6_ops(enum socket_type );
extern const struct socket_ops *socket_unixdom_ops(enum socket_type );
- if (strcmp("ip", name) == 0 ||
- strcmp("ipv4", name) == 0) {
+ if (strcmp("ip", family) == 0 ||
+ strcmp("ipv4", family) == 0) {
return socket_ipv4_ops(type);
}
#if HAVE_SOCKET_IPV6
- if (strcmp("ipv6", name) == 0) {
+ if (strcmp("ipv6", family) == 0) {
if (lp_parm_bool(-1, "socket", "noipv6", False)) {
DEBUG(3, ("IPv6 support was disabled in smb.conf"));
return NULL;
@@ -388,7 +423,7 @@ const struct socket_ops *socket_getops_byname(const char *name, enum socket_type
}
#endif
- if (strcmp("unix", name) == 0) {
+ if (strcmp("unix", family) == 0) {
return socket_unixdom_ops(type);
}
diff --git a/source4/lib/socket/socket.h b/source4/lib/socket/socket.h
index 165fbb6377..f3e3ba8341 100644
--- a/source4/lib/socket/socket.h
+++ b/source4/lib/socket/socket.h
@@ -28,6 +28,14 @@ enum socket_type {
SOCKET_TYPE_DGRAM
};
+struct socket_address {
+ const char *family;
+ char *addr;
+ int port;
+ struct sockaddr *sockaddr;
+ size_t sockaddrlen;
+};
+
struct socket_ops {
const char *name;
@@ -35,9 +43,9 @@ struct socket_ops {
/* client ops */
NTSTATUS (*fn_connect)(struct socket_context *sock,
- const char *my_address, int my_port,
- const char *server_address, int server_port,
- uint32_t flags);
+ const struct socket_address *my_address,
+ const struct socket_address *server_address,
+ uint32_t flags);
/* complete a non-blocking connect */
NTSTATUS (*fn_connect_complete)(struct socket_context *sock,
@@ -45,8 +53,10 @@ struct socket_ops {
/* server ops */
NTSTATUS (*fn_listen)(struct socket_context *sock,
- const char *my_address, int port, int queue_size, uint32_t flags);
- NTSTATUS (*fn_accept)(struct socket_context *sock, struct socket_context **new_sock);
+ const struct socket_address *my_address,
+ int queue_size, uint32_t flags);
+ NTSTATUS (*fn_accept)(struct socket_context *sock,
+ struct socket_context **new_sock);
/* general ops */
NTSTATUS (*fn_recv)(struct socket_context *sock, void *buf,
@@ -56,10 +66,10 @@ struct socket_ops {
NTSTATUS (*fn_sendto)(struct socket_context *sock,
const DATA_BLOB *blob, size_t *sendlen, uint32_t flags,
- const char *dest_addr, int dest_port);
+ const struct socket_address *dest_addr);
NTSTATUS (*fn_recvfrom)(struct socket_context *sock,
void *buf, size_t wantlen, size_t *nread, uint32_t flags,
- const char **src_addr, int *src_port);
+ TALLOC_CTX *addr_ctx, struct socket_address **src_addr);
NTSTATUS (*fn_pending)(struct socket_context *sock, size_t *npending);
void (*fn_close)(struct socket_context *sock);
@@ -67,10 +77,8 @@ struct socket_ops {
NTSTATUS (*fn_set_option)(struct socket_context *sock, const char *option, const char *val);
char *(*fn_get_peer_name)(struct socket_context *sock, TALLOC_CTX *mem_ctx);
- char *(*fn_get_peer_addr)(struct socket_context *sock, TALLOC_CTX *mem_ctx);
- int (*fn_get_peer_port)(struct socket_context *sock);
- char *(*fn_get_my_addr)(struct socket_context *sock, TALLOC_CTX *mem_ctx);
- int (*fn_get_my_port)(struct socket_context *sock);
+ struct socket_address *(*fn_get_peer_addr)(struct socket_context *sock, TALLOC_CTX *mem_ctx);
+ struct socket_address *(*fn_get_my_addr)(struct socket_context *sock, TALLOC_CTX *mem_ctx);
int (*fn_get_fd)(struct socket_context *sock);
};
@@ -110,31 +118,38 @@ struct socket_context {
NTSTATUS socket_create(const char *name, enum socket_type type,
struct socket_context **new_sock, uint32_t flags);
NTSTATUS socket_connect(struct socket_context *sock,
- const char *my_address, int my_port,
- const char *server_address, int server_port,
+ const struct socket_address *my_address,
+ const struct socket_address *server_address,
uint32_t flags);
NTSTATUS socket_connect_complete(struct socket_context *sock, uint32_t flags);
-NTSTATUS socket_listen(struct socket_context *sock, const char *my_address, int port, int queue_size, uint32_t flags);
+NTSTATUS socket_listen(struct socket_context *sock,
+ const struct socket_address *my_address,
+ int queue_size, uint32_t flags);
NTSTATUS socket_accept(struct socket_context *sock, struct socket_context **new_sock);
NTSTATUS socket_recv(struct socket_context *sock, void *buf,
size_t wantlen, size_t *nread, uint32_t flags);
NTSTATUS socket_recvfrom(struct socket_context *sock, void *buf,
size_t wantlen, size_t *nread, uint32_t flags,
- const char **src_addr, int *src_port);
+ TALLOC_CTX *addr_ctx, struct socket_address **src_addr);
NTSTATUS socket_send(struct socket_context *sock,
const DATA_BLOB *blob, size_t *sendlen, uint32_t flags);
NTSTATUS socket_sendto(struct socket_context *sock,
const DATA_BLOB *blob, size_t *sendlen, uint32_t flags,
- const char *dest_addr, int dest_port);
+ const struct socket_address *dest_addr);
NTSTATUS socket_pending(struct socket_context *sock, size_t *npending);
NTSTATUS socket_set_option(struct socket_context *sock, const char *option, const char *val);
char *socket_get_peer_name(struct socket_context *sock, TALLOC_CTX *mem_ctx);
-char *socket_get_peer_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx);
-int socket_get_peer_port(struct socket_context *sock);
-char *socket_get_my_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx);
-int socket_get_my_port(struct socket_context *sock);
+struct socket_address *socket_get_peer_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx);
+struct socket_address *socket_get_my_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx);
int socket_get_fd(struct socket_context *sock);
NTSTATUS socket_dup(struct socket_context *sock);
+struct socket_address *socket_address_from_strings(TALLOC_CTX *mem_ctx,
+ const char *type,
+ const char *host,
+ int port);
+struct socket_address *socket_address_from_sockaddr(TALLOC_CTX *mem_ctx,
+ struct sockaddr *sockaddr,
+ size_t addrlen);
const struct socket_ops *socket_getops_byname(const char *name, enum socket_type type);
BOOL allow_access(TALLOC_CTX *mem_ctx,
const char **deny_list, const char **allow_list,
@@ -144,16 +159,14 @@ BOOL socket_check_access(struct socket_context *sock,
const char **allow_list, const char **deny_list);
struct composite_context *socket_connect_send(struct socket_context *sock,
- const char *my_address,
- int my_port,
- const char *server_address,
- int server_port,
+ struct socket_address *my_address,
+ struct socket_address *server_address,
uint32_t flags,
struct event_context *event_ctx);
NTSTATUS socket_connect_recv(struct composite_context *ctx);
NTSTATUS socket_connect_ev(struct socket_context *sock,
- const char *my_address, int my_port,
- const char *server_address, int server_port,
+ struct socket_address *my_address,
+ struct socket_address *server_address,
uint32_t flags, struct event_context *ev);
struct composite_context *socket_connect_multi_send(TALLOC_CTX *mem_ctx,
diff --git a/source4/lib/socket/socket_ipv4.c b/source4/lib/socket/socket_ipv4.c
index 34ae0b2b73..9e1f60d40a 100644
--- a/source4/lib/socket/socket_ipv4.c
+++ b/source4/lib/socket/socket_ipv4.c
@@ -85,49 +85,63 @@ static NTSTATUS ipv4_connect_complete(struct socket_context *sock, uint32_t flag
static NTSTATUS ipv4_connect(struct socket_context *sock,
- const char *my_address, int my_port,
- const char *srv_address, int srv_port,
- uint32_t flags)
+ const struct socket_address *my_address,
+ const struct socket_address *srv_address,
+ uint32_t flags)
{
struct sockaddr_in srv_addr;
struct ipv4_addr my_ip;
struct ipv4_addr srv_ip;
int ret;
- my_ip = interpret_addr2(my_address);
-
- if (my_ip.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);
-#endif
- my_addr.sin_addr.s_addr = my_ip.addr;
- 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 (my_address && my_address->sockaddr) {
+ ret = bind(sock->fd, my_address->sockaddr, my_address->sockaddrlen);
if (ret == -1) {
return map_nt_error_from_unix(errno);
}
+ } else if (my_address) {
+ my_ip = interpret_addr2(my_address->addr);
+
+ if (my_ip.addr != 0 || my_address->port != 0) {
+ struct sockaddr_in my_addr;
+ ZERO_STRUCT(my_addr);
+#ifdef HAVE_SOCK_SIN_LEN
+ my_addr.sin_len = sizeof(my_addr);
+#endif
+ my_addr.sin_addr.s_addr = my_ip.addr;
+ my_addr.sin_port = htons(my_address->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);
+ }
+ }
}
- srv_ip = interpret_addr2(srv_address);
- if (!srv_ip.addr) {
- return NT_STATUS_BAD_NETWORK_NAME;
- }
-
- ZERO_STRUCT(srv_addr);
+ if (srv_address->sockaddr) {
+ ret = connect(sock->fd, srv_address->sockaddr, srv_address->sockaddrlen);
+ if (ret == -1) {
+ return map_nt_error_from_unix(errno);
+ }
+ } else {
+ srv_ip = interpret_addr2(srv_address->addr);
+ if (!srv_ip.addr) {
+ return NT_STATUS_BAD_NETWORK_NAME;
+ }
+
+ ZERO_STRUCT(srv_addr);
#ifdef HAVE_SOCK_SIN_LEN
- srv_addr.sin_len = sizeof(srv_addr);
+ srv_addr.sin_len = sizeof(srv_addr);
#endif
- srv_addr.sin_addr.s_addr= srv_ip.addr;
- srv_addr.sin_port = htons(srv_port);
- srv_addr.sin_family = PF_INET;
+ srv_addr.sin_addr.s_addr= srv_ip.addr;
+ srv_addr.sin_port = htons(srv_address->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);
+ ret = connect(sock->fd, (const struct sockaddr *)&srv_addr, sizeof(srv_addr));
+ if (ret == -1) {
+ return map_nt_error_from_unix(errno);
+ }
}
return ipv4_connect_complete(sock, flags);
@@ -139,7 +153,7 @@ static NTSTATUS ipv4_connect(struct socket_context *sock,
use for DGRAM sockets, but in reality only a bind() is done
*/
static NTSTATUS ipv4_listen(struct socket_context *sock,
- const char *my_address, int port,
+ const struct socket_address *my_address,
int queue_size, uint32_t flags)
{
struct sockaddr_in my_addr;
@@ -148,17 +162,22 @@ static NTSTATUS ipv4_listen(struct socket_context *sock,
socket_set_option(sock, "SO_REUSEADDR=1", NULL);
- ip_addr = interpret_addr2(my_address);
-
- ZERO_STRUCT(my_addr);
+ if (my_address->sockaddr) {
+ ret = bind(sock->fd, my_address->sockaddr, my_address->sockaddrlen);
+ } else {
+ ip_addr = interpret_addr2(my_address->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.s_addr = ip_addr.addr;
- my_addr.sin_port = htons(port);
- my_addr.sin_family = PF_INET;
+ my_addr.sin_addr.s_addr = ip_addr.addr;
+ my_addr.sin_port = htons(my_address->port);
+ my_addr.sin_family = PF_INET;
+
+ ret = bind(sock->fd, (struct sockaddr *)&my_addr, sizeof(my_addr));
+ }
- ret = bind(sock->fd, (struct sockaddr *)&my_addr, sizeof(my_addr));
if (ret == -1) {
return map_nt_error_from_unix(errno);
}
@@ -263,14 +282,29 @@ static NTSTATUS ipv4_recv(struct socket_context *sock, void *buf,
static NTSTATUS ipv4_recvfrom(struct socket_context *sock, void *buf,
size_t wantlen, size_t *nread, uint32_t flags,
- const char **src_addr, int *src_port)
+ TALLOC_CTX *addr_ctx, struct socket_address **_src)
{
ssize_t gotlen;
int flgs = 0;
- struct sockaddr_in from_addr;
- socklen_t from_len = sizeof(from_addr);
+ struct sockaddr_in *from_addr;
+ socklen_t from_len = sizeof(*from_addr);
+ struct socket_address *src;
const char *addr;
+
+ src = talloc(addr_ctx, struct socket_address);
+ if (!src) {
+ return NT_STATUS_NO_MEMORY;
+ }
+
+ src->family = sock->backend_name;
+ from_addr = talloc(src, struct sockaddr_in);
+ if (!from_addr) {
+ talloc_free(src);
+ return NT_STATUS_NO_MEMORY;
+ }
+
+ src->sockaddr = (struct sockaddr *)from_addr;
if (flags & SOCKET_FLAG_PEEK) {
flgs |= MSG_PEEK;
}
@@ -282,23 +316,31 @@ static NTSTATUS ipv4_recvfrom(struct socket_context *sock, void *buf,
*nread = 0;
gotlen = recvfrom(sock->fd, buf, wantlen, flgs,
- (struct sockaddr *)&from_addr, &from_len);
+ src->sockaddr, &from_len);
if (gotlen == 0) {
+ talloc_free(src);
return NT_STATUS_END_OF_FILE;
} else if (gotlen == -1) {
+ talloc_free(src);
return map_nt_error_from_unix(errno);
}
- addr = inet_ntoa(from_addr.sin_addr);
+ src->sockaddrlen = from_len;
+
+ addr = inet_ntoa(from_addr->sin_addr);
if (addr == NULL) {
+ talloc_free(src);
return NT_STATUS_INTERNAL_ERROR;
}
- *src_addr = talloc_strdup(sock, addr);
- NT_STATUS_HAVE_NO_MEMORY(*src_addr);
- *src_port = ntohs(from_addr.sin_port);
-
- *nread = gotlen;
+ src->addr = talloc_strdup(src, addr);
+ if (src->addr == NULL) {
+ talloc_free(src);
+ return NT_STATUS_NO_MEMORY;
+ }
+ src->port = ntohs(from_addr->sin_port);
+ *nread = gotlen;
+ *_src = src;
return NT_STATUS_OK;
}
@@ -322,26 +364,32 @@ static NTSTATUS ipv4_send(struct socket_context *sock,
static NTSTATUS ipv4_sendto(struct socket_context *sock,
const DATA_BLOB *blob, size_t *sendlen, uint32_t flags,
- const char *dest_addr, int dest_port)
+ const struct socket_address *dest_addr)
{
ssize_t len;
int flgs = 0;
- struct sockaddr_in srv_addr;
- struct ipv4_addr addr;
- ZERO_STRUCT(srv_addr);
+ if (dest_addr->sockaddr) {
+ len = sendto(sock->fd, blob->data, blob->length, flgs,
+ dest_addr->sockaddr, dest_addr->sockaddrlen);
+ } else {
+ struct sockaddr_in srv_addr;
+ struct ipv4_addr addr;
+
+ ZERO_STRUCT(srv_addr);
#ifdef HAVE_SOCK_SIN_LEN
- srv_addr.sin_len = sizeof(srv_addr);
+ srv_addr.sin_len = sizeof(srv_addr);
#endif
- addr = interpret_addr2(dest_addr);
- srv_addr.sin_addr.s_addr = addr.addr;
- srv_addr.sin_port = htons(dest_port);
- srv_addr.sin_family = PF_INET;
-
- *sendlen = 0;
-
- len = sendto(sock->fd, blob->data, blob->length, flgs,
- (struct sockaddr *)&srv_addr, sizeof(srv_addr));
+ addr = interpret_addr2(dest_addr->addr);
+ srv_addr.sin_addr.s_addr = addr.addr;
+ srv_addr.sin_port = htons(dest_addr->port);
+ srv_addr.sin_family = PF_INET;
+
+ *sendlen = 0;
+
+ len = sendto(sock->fd, blob->data, blob->length, flgs,
+ (struct sockaddr *)&srv_addr, sizeof(srv_addr));
+ }
if (len == -1) {
return map_nt_error_from_unix(errno);
}
@@ -377,62 +425,95 @@ static char *ipv4_get_peer_name(struct socket_context *sock, TALLOC_CTX *mem_ctx
return talloc_strdup(mem_ctx, he->h_name);
}
-static char *ipv4_get_peer_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx)
+static struct socket_address *ipv4_get_peer_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx)
{
- struct sockaddr_in peer_addr;
- socklen_t len = sizeof(peer_addr);
+ struct sockaddr_in *peer_addr;
+ socklen_t len = sizeof(*peer_addr);
+ const char *addr;
+ struct socket_address *peer;
int ret;
+
+ peer = talloc(mem_ctx, struct socket_address);
+ if (!peer) {
+ return NULL;
+ }
+
+ peer->family = sock->backend_name;
+ peer_addr = talloc(peer, struct sockaddr_in);
+ if (!peer_addr) {
+ talloc_free(peer);
+ return NULL;
+ }
- ret = getpeername(sock->fd, (struct sockaddr *)&peer_addr, &len);
+ peer->sockaddr = (struct sockaddr *)peer_addr;
+
+ ret = getpeername(sock->fd, peer->sockaddr, &len);
if (ret == -1) {
+ talloc_free(peer);
return NULL;
}
- return talloc_strdup(mem_ctx, inet_ntoa(peer_addr.sin_addr));
-}
+ peer->sockaddrlen = len;
-static int ipv4_get_peer_port(struct socket_context *sock)
-{
- struct sockaddr_in peer_addr;
- socklen_t len = sizeof(peer_addr);
- int ret;
-
- ret = getpeername(sock->fd, (struct sockaddr *)&peer_addr, &len);
- if (ret == -1) {
- return -1;
+ addr = inet_ntoa(peer_addr->sin_addr);
+ if (addr == NULL) {
+ talloc_free(peer);
+ return NULL;
+ }
+ peer->addr = talloc_strdup(peer, addr);
+ if (!peer->addr) {
+ talloc_free(peer);
+ return NULL;
}
+ peer->port = ntohs(peer_addr->sin_port);
- return ntohs(peer_addr.sin_port);
+ return peer;
}
-static char *ipv4_get_my_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx)
+static struct socket_address *ipv4_get_my_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx)
{
- struct sockaddr_in my_addr;
- socklen_t len = sizeof(my_addr);
+ struct sockaddr_in *local_addr;
+ socklen_t len = sizeof(*local_addr);
+ const char *addr;
+ struct socket_address *local;
int ret;
+
+ local = talloc(mem_ctx, struct socket_address);
+ if (!local) {
+ return NULL;
+ }
+
+ local->family = sock->backend_name;
+ local_addr = talloc(local, struct sockaddr_in);
+ if (!local_addr) {
+ talloc_free(local);
+ return NULL;
+ }
- ret = getsockname(sock->fd, (struct sockaddr *)&my_addr, &len);
+ local->sockaddr = (struct sockaddr *)local_addr;
+
+ ret = getsockname(sock->fd, local->sockaddr, &len);
if (ret == -1) {
+ talloc_free(local);
return NULL;
}
- return talloc_strdup(mem_ctx, inet_ntoa(my_addr.sin_addr));
-}
-
-static int ipv4_get_my_port(struct socket_context *sock)
-{
- struct sockaddr_in my_addr;
- socklen_t len = sizeof(my_addr);
- int ret;
+ local->sockaddrlen = len;
- ret = getsockname(sock->fd, (struct sockaddr *)&my_addr, &len);
- if (ret == -1) {
- return -1;
+ addr = inet_ntoa(local_addr->sin_addr);
+ if (addr == NULL) {
+ talloc_free(local);
+ return NULL;
+ }
+ local->addr = talloc_strdup(local, addr);
+ if (!local->addr) {
+ talloc_free(local);
+ return NULL;
}
+ local->port = ntohs(local_addr->sin_port);
- return ntohs(my_addr.sin_port);
+ return local;
}
-
static int ipv4_get_fd(struct socket_context *sock)
{
return sock->fd;
@@ -466,10 +547,7 @@ static const struct socket_ops ipv4_ops = {
.fn_get_peer_name = ipv4_get_peer_name,
.fn_get_peer_addr = ipv4_get_peer_addr,
- .fn_get_peer_port = ipv4_get_peer_port,
.fn_get_my_addr = ipv4_get_my_addr,
- .fn_get_my_port = ipv4_get_my_port,
-
.fn_get_fd = ipv4_get_fd
};
diff --git a/source4/lib/socket/socket_ipv6.c b/source4/lib/socket/socket_ipv6.c
index bc47d4f554..90eb27b811 100644
--- a/source4/lib/socket/socket_ipv6.c
+++ b/source4/lib/socket/socket_ipv6.c
@@ -81,41 +81,52 @@ static NTSTATUS ipv6_tcp_connect_complete(struct socket_context *sock, uint32_t
}
static NTSTATUS ipv6_tcp_connect(struct socket_context *sock,
- const char *my_address, int my_port,
- const char *srv_address, int srv_port,
+ const struct socket_address *my_address,
+ const struct socket_address *srv_address,
uint32_t flags)
{
- struct sockaddr_in6 srv_addr;
- struct in6_addr my_ip;
- struct in6_addr srv_ip;
int ret;
- my_ip = interpret_addr6(my_address);
-
- if (memcmp(&my_ip, &in6addr_any, sizeof(my_ip)) || my_port != 0) {
- struct sockaddr_in6 my_addr;
- ZERO_STRUCT(my_addr);
- my_addr.sin6_addr = my_ip;
- my_addr.sin6_port = htons(my_port);
- my_addr.sin6_family = PF_INET6;
-
- ret = bind(sock->fd, (struct sockaddr *)&my_addr, sizeof(my_addr));
+ if (my_address && my_address->sockaddr) {
+ ret = bind(sock->fd, my_address->sockaddr, my_address->sockaddrlen);
if (ret == -1) {
return map_nt_error_from_unix(errno);
}
+ } else if (my_address) {
+ struct in6_addr my_ip;
+ my_ip = interpret_addr6(my_address->addr);
+
+ if (memcmp(&my_ip, &in6addr_any, sizeof(my_ip)) || my_address->port != 0) {
+ struct sockaddr_in6 my_addr;
+ ZERO_STRUCT(my_addr);
+ my_addr.sin6_addr = my_ip;
+ my_addr.sin6_port = htons(my_address->port);
+ my_addr.sin6_family = PF_INET6;
+
+ ret = bind(sock->fd, (struct sockaddr *)&my_addr, sizeof(my_addr));
+ if (ret == -1) {
+ return map_nt_error_from_unix(errno);
+ }
+ }
}
- srv_ip = interpret_addr6(srv_address);
- if (memcmp(&srv_ip, &in6addr_any, sizeof(srv_ip)) == 0) {
- return NT_STATUS_BAD_NETWORK_NAME;
+ if (srv_address->sockaddr) {
+ ret = connect(sock->fd, srv_address->sockaddr, srv_address->sockaddrlen);
+ } else {
+ struct in6_addr srv_ip;
+ struct sockaddr_in6 srv_addr;
+ srv_ip = interpret_addr6(srv_address->addr);
+ if (memcmp(&srv_ip, &in6addr_any, sizeof(srv_ip)) == 0) {
+ return NT_STATUS_BAD_NETWORK_NAME;
+ }
+
+ ZERO_STRUCT(srv_addr);
+ srv_addr.sin6_addr = srv_ip;
+ srv_addr.sin6_port = htons(srv_address->port);
+ srv_addr.sin6_family = PF_INET6;
+
+ ret = connect(sock->fd, (const struct sockaddr *)&srv_addr, sizeof(srv_addr));
}
-
- ZERO_STRUCT(srv_addr);
- srv_addr.sin6_addr = srv_ip;
- srv_addr.sin6_port = htons(srv_port);
- srv_addr.sin6_family = PF_INET6;
-
- ret = connect(sock->fd, (const struct sockaddr *)&srv_addr, sizeof(srv_addr));
if (ret == -1) {
return map_nt_error_from_unix(errno);
}
@@ -124,8 +135,8 @@ static NTSTATUS ipv6_tcp_connect(struct socket_context *sock,
}
static NTSTATUS ipv6_tcp_listen(struct socket_context *sock,
- const char *my_address, int port,
- int queue_size, uint32_t flags)
+ const struct socket_address *my_address,
+ int queue_size, uint32_t flags)
{
struct sockaddr_in6 my_addr;
struct in6_addr ip_addr;
@@ -133,14 +144,19 @@ static NTSTATUS ipv6_tcp_listen(struct socket_context *sock,
socket_set_option(sock, "SO_REUSEADDR=1", NULL);
- ip_addr = interpret_addr6(my_address);
-
- ZERO_STRUCT(my_addr);
- my_addr.sin6_addr = ip_addr;
- my_addr.sin6_port = htons(port);
- my_addr.sin6_family = PF_INET6;
+ if (my_address->sockaddr) {
+ ret = bind(sock->fd, my_address->sockaddr, my_address->sockaddrlen);
+ } else {
+ ip_addr = interpret_addr6(my_address->addr);
+
+ ZERO_STRUCT(my_addr);
+ my_addr.sin6_addr = ip_addr;
+ my_addr.sin6_port = htons(my_address->port);
+ my_addr.sin6_family = PF_INET6;
+
+ ret = bind(sock->fd, (struct sockaddr *)&my_addr, sizeof(my_addr));
+ }
- ret = bind(sock->fd, (struct sockaddr *)&my_addr, sizeof(my_addr));
if (ret == -1) {
return map_nt_error_from_unix(errno);
}
@@ -280,73 +296,98 @@ static char *ipv6_tcp_get_peer_name(struct socket_context *sock, TALLOC_CTX *mem
return talloc_strdup(mem_ctx, he->h_name);
}
-static char *ipv6_tcp_get_peer_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx)
+static struct socket_address *ipv6_tcp_get_peer_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx)
{
- struct sockaddr_in6 peer_addr;
- socklen_t len = sizeof(peer_addr);
+ struct sockaddr_in6 *peer_addr;
+ socklen_t len = sizeof(*peer_addr);
+ struct socket_address *peer;
int ret;
struct hostent *he;
+
+ peer = talloc(mem_ctx, struct socket_address);
+ if (!peer) {
+ return NULL;
+ }
+
+ peer->family = sock->backend_name;
+ peer_addr = talloc(peer, struct sockaddr_in6);
+ if (!peer_addr) {
+ talloc_free(peer);
+ return NULL;
+ }
- ret = getpeername(sock->fd, (struct sockaddr *)&peer_addr, &len);
+ peer->sockaddr = (struct sockaddr *)peer_addr;
+
+ ret = getpeername(sock->fd, peer->sockaddr, &len);
if (ret == -1) {
+ talloc_free(peer);
return NULL;
}
- he = gethostbyaddr((char *)&peer_addr.sin6_addr, sizeof(peer_addr.sin6_addr), AF_INET6);
+ peer->sockaddrlen = len;
+
+ he = gethostbyaddr((char *)&peer_addr->sin6_addr, len, AF_INET6);
if (!he || !he->h_name) {
+ talloc_free(peer);
return NULL;
}
- return talloc_strdup(mem_ctx, he->h_name);
-}
-
-static int ipv6_tcp_get_peer_port(struct socket_context *sock)
-{
- struct sockaddr_in6 peer_addr;
- socklen_t len = sizeof(peer_addr);
- int ret;
-
- ret = getpeername(sock->fd, (struct sockaddr *)&peer_addr, &len);
- if (ret == -1) {
- return -1;
+ peer->addr = talloc_strdup(mem_ctx, he->h_name);
+ if (!peer->addr) {
+ talloc_free(peer);
+ return NULL;
}
+ peer->port = ntohs(peer_addr->sin6_port);
- return ntohs(peer_addr.sin6_port);
+ return peer;
}
-static char *ipv6_tcp_get_my_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx)
+static struct socket_address *ipv6_tcp_get_my_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx)
{
- struct sockaddr_in6 my_addr;
- socklen_t len = sizeof(my_addr);
+ struct sockaddr_in6 *local_addr;
+ socklen_t len = sizeof(*local_addr);
+ struct socket_address *local;
int ret;
struct hostent *he;
-
- ret = getsockname(sock->fd, (struct sockaddr *)&my_addr, &len);
- if (ret == -1) {
+
+ local = talloc(mem_ctx, struct socket_address);
+ if (!local) {
+ return NULL;
+ }
+
+ local->family = sock->backend_name;
+ local_addr = talloc(local, struct sockaddr_in6);
+ if (!local_addr) {
+ talloc_free(local);
return NULL;
}
- he = gethostbyaddr((char *)&my_addr.sin6_addr, sizeof(my_addr.sin6_addr), AF_INET6);
- if (he == NULL) {
+ local->sockaddr = (struct sockaddr *)local_addr;
+
+ ret = getsockname(sock->fd, local->sockaddr, &len);
+ if (ret == -1) {
+ talloc_free(local);
return NULL;
}
- return talloc_strdup(mem_ctx, he->h_name);
-}
+ local->sockaddrlen = len;
-static int ipv6_tcp_get_my_port(struct socket_context *sock)
-{
- struct sockaddr_in6 my_addr;
- socklen_t len = sizeof(my_addr);
- int ret;
+ he = gethostbyaddr((char *)&local_addr->sin6_addr, len, AF_INET6);
- ret = getsockname(sock->fd, (struct sockaddr *)&my_addr, &len);
- if (ret == -1) {
- return -1;
+ if (!he || !he->h_name) {
+ talloc_free(local);
+ return NULL;
+ }
+
+ local->addr = talloc_strdup(mem_ctx, he->h_name);
+ if (!local->addr) {
+ talloc_free(local);
+ return NULL;
}
+ local->port = ntohs(local_addr->sin6_port);
- return ntohs(my_addr.sin6_port);
+ return local;
}
static int ipv6_tcp_get_fd(struct socket_context *sock)
@@ -369,9 +410,7 @@ static const struct socket_ops ipv6_tcp_ops = {
.fn_get_peer_name = ipv6_tcp_get_peer_name,
.fn_get_peer_addr = ipv6_tcp_get_peer_addr,
- .fn_get_peer_port = ipv6_tcp_get_peer_port,
.fn_get_my_addr = ipv6_tcp_get_my_addr,
- .fn_get_my_port = ipv6_tcp_get_my_port,
.fn_get_fd = ipv6_tcp_get_fd
};
diff --git a/source4/lib/socket/socket_unix.c b/source4/lib/socket/socket_unix.c
index 0c65bb46fb..5e04ce0d4c 100644
--- a/source4/lib/socket/socket_unix.c
+++ b/source4/lib/socket/socket_unix.c
@@ -95,22 +95,26 @@ static NTSTATUS unixdom_connect_complete(struct socket_context *sock, uint32_t f
}
static NTSTATUS unixdom_connect(struct socket_context *sock,
- const char *my_address, int my_port,
- const char *srv_address, int srv_port,
+ const struct socket_address *my_address,
+ const struct socket_address *srv_address,
uint32_t flags)
{
- struct sockaddr_un srv_addr;
int ret;
- if (strlen(srv_address)+1 > sizeof(srv_addr.sun_path)) {
- return NT_STATUS_OBJECT_PATH_INVALID;
- }
-
- ZERO_STRUCT(srv_addr);
- srv_addr.sun_family = AF_UNIX;
- strncpy(srv_addr.sun_path, srv_address, sizeof(srv_addr.sun_path));
+ if (srv_address->sockaddr) {
+ ret = connect(sock->fd, srv_address->sockaddr, srv_address->sockaddrlen);
+ } else {
+ struct sockaddr_un srv_addr;
+ if (strlen(srv_address->addr)+1 > sizeof(srv_addr.sun_path)) {
+ return NT_STATUS_OBJECT_PATH_INVALID;
+ }
+
+ ZERO_STRUCT(srv_addr);
+ srv_addr.sun_family = AF_UNIX;
+ strncpy(srv_addr.sun_path, srv_address->addr, sizeof(srv_addr.sun_path));
- ret = connect(sock->fd, (const struct sockaddr *)&srv_addr, sizeof(srv_addr));
+ ret = connect(sock->fd, (const struct sockaddr *)&srv_addr, sizeof(srv_addr));
+ }
if (ret == -1) {
return unixdom_error(errno);
}
@@ -119,24 +123,32 @@ static NTSTATUS unixdom_connect(struct socket_context *sock,
}
static NTSTATUS unixdom_listen(struct socket_context *sock,
- const char *my_address, int port,
+ const struct socket_address *my_address,
int queue_size, uint32_t flags)
{
struct sockaddr_un my_addr;
int ret;
- if (strlen(my_address)+1 > sizeof(my_addr.sun_path)) {
- return NT_STATUS_OBJECT_PATH_INVALID;
- }
-
/* delete if it already exists */
- unlink(my_address);
-
- ZERO_STRUCT(my_addr);
- my_addr.sun_family = AF_UNIX;
- strncpy(my_addr.sun_path, my_address, sizeof(my_addr.sun_path));
+ if (my_address->addr) {
+ unlink(my_address->addr);
+ }
- ret = bind(sock->fd, (struct sockaddr *)&my_addr, sizeof(my_addr));
+ if (my_address && my_address->sockaddr) {
+ ret = bind(sock->fd, (struct sockaddr *)&my_addr, sizeof(my_addr));
+ } else {
+
+ if (strlen(my_address->addr)+1 > sizeof(my_addr.sun_path)) {
+ return NT_STATUS_OBJECT_PATH_INVALID;
+ }
+
+
+ ZERO_STRUCT(my_addr);
+ my_addr.sun_family = AF_UNIX;
+ strncpy(my_addr.sun_path, my_address->addr, sizeof(my_addr.sun_path));
+
+ ret = bind(sock->fd, (struct sockaddr *)&my_addr, sizeof(my_addr));
+ }
if (ret == -1) {
return unixdom_error(errno);
}
@@ -156,7 +168,7 @@ static NTSTATUS unixdom_listen(struct socket_context *sock,
}
sock->state = SOCKET_STATE_SERVER_LISTEN;
- sock->private_data = (void *)talloc_strdup(sock, my_address);
+ sock->private_data = (void *)talloc_strdup(sock, my_address->addr);
return NT_STATUS_OK;
}
@@ -255,24 +267,29 @@ static NTSTATUS unixdom_send(struct socket_context *sock,
static NTSTATUS unixdom_sendto(struct socket_context *sock,
const DATA_BLOB *blob, size_t *sendlen, uint32_t flags,
- const char *dest_addr, int dest_port)
+ const struct socket_address *dest)
{
ssize_t len;
int flgs = 0;
- struct sockaddr_un srv_addr;
-
- if (strlen(dest_addr)+1 > sizeof(srv_addr.sun_path)) {
- return NT_STATUS_OBJECT_PATH_INVALID;
- }
-
- ZERO_STRUCT(srv_addr);
- srv_addr.sun_family = AF_UNIX;
- strncpy(srv_addr.sun_path, dest_addr, sizeof(srv_addr.sun_path));
-
*sendlen = 0;
-
- len = sendto(sock->fd, blob->data, blob->length, flgs,
- (struct sockaddr *)&srv_addr, sizeof(srv_addr));
+
+ if (dest->sockaddr) {
+ len = sendto(sock->fd, blob->data, blob->length, flgs,
+ dest->sockaddr, dest->sockaddrlen);
+ } else {
+ struct sockaddr_un srv_addr;
+
+ if (strlen(dest->addr)+1 > sizeof(srv_addr.sun_path)) {
+ return NT_STATUS_OBJECT_PATH_INVALID;
+ }
+
+ ZERO_STRUCT(srv_addr);
+ srv_addr.sun_family = AF_UNIX;
+ strncpy(srv_addr.sun_path, dest->addr, sizeof(srv_addr.sun_path));
+
+ len = sendto(sock->fd, blob->data, blob->length, flgs,
+ (struct sockaddr *)&srv_addr, sizeof(srv_addr));
+ }
if (len == -1) {
return map_nt_error_from_unix(errno);
}
@@ -294,24 +311,82 @@ static char *unixdom_get_peer_name(struct socket_context *sock, TALLOC_CTX *mem_
return talloc_strdup(mem_ctx, "LOCAL/unixdom");
}
-static char *unixdom_get_peer_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx)
+static struct socket_address *unixdom_get_peer_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx)
{
- return talloc_strdup(mem_ctx, "LOCAL/unixdom");
-}
+ struct sockaddr_in *peer_addr;
+ socklen_t len = sizeof(*peer_addr);
+ struct socket_address *peer;
+ int ret;
-static int unixdom_get_peer_port(struct socket_context *sock)
-{
- return 0;
-}
+ peer = talloc(mem_ctx, struct socket_address);
+ if (!peer) {
+ return NULL;
+ }
+
+ peer->family = sock->backend_name;
+ peer_addr = talloc(peer, struct sockaddr_in);
+ if (!peer_addr) {
+ talloc_free(peer);
+ return NULL;
+ }
-static char *unixdom_get_my_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx)
-{
- return talloc_strdup(mem_ctx, "LOCAL/unixdom");
+ peer->sockaddr = (struct sockaddr *)peer_addr;
+
+ ret = getpeername(sock->fd, peer->sockaddr, &len);
+ if (ret == -1) {
+ talloc_free(peer);
+ return NULL;
+ }
+
+ peer->sockaddrlen = len;
+
+ peer->port = 0;
+ peer->addr = talloc_strdup(peer, "LOCAL/unixdom");
+ if (!peer->addr) {
+ talloc_free(peer);
+ return NULL;
+ }
+
+ return peer;
}
-static int unixdom_get_my_port(struct socket_context *sock)
+static struct socket_address *unixdom_get_my_addr(struct socket_context *sock, TALLOC_CTX *mem_ctx)
{
- return 0;
+ struct sockaddr_in *local_addr;
+ socklen_t len = sizeof(*local_addr);
+ struct socket_address *local;
+ int ret;
+
+ local = talloc(mem_ctx, struct socket_address);
+ if (!local) {
+ return NULL;
+ }
+
+ local->family = sock->backend_name;
+ local_addr = talloc(local, struct sockaddr_in);
+ if (!local_addr) {
+ talloc_free(local);
+ return NULL;
+ }
+
+ local->sockaddr = (struct sockaddr *)local_addr;
+
+ ret = getsockname(sock->fd, local->sockaddr, &len);
+ if (ret == -1) {
+ talloc_free(local);
+ return NULL;
+ }
+
+ local->sockaddrlen = len;
+
+ local->port = 0;
+ local->addr = talloc_strdup(local, "LOCAL/unixdom");
+ if (!local->addr) {
+ talloc_free(local);
+ return NULL;
+ }
+
+ return local;
}
static int unixdom_get_fd(struct socket_context *sock)
@@ -346,9 +421,7 @@ static const struct socket_ops unixdom_ops = {
.fn_get_peer_name = unixdom_get_peer_name,
.fn_get_peer_addr = unixdom_get_peer_addr,
- .fn_get_peer_port = unixdom_get_peer_port,
.fn_get_my_addr = unixdom_get_my_addr,
- .fn_get_my_port = unixdom_get_my_port,
.fn_get_fd = unixdom_get_fd
};