diff options
author | Stefan Metzmacher <metze@samba.org> | 2009-11-04 19:03:41 +0100 |
---|---|---|
committer | Stefan Metzmacher <metze@samba.org> | 2009-12-15 23:34:22 +0100 |
commit | edbf2caa6f8273227229cd8f1b293e95c6a6122d (patch) | |
tree | a81b2a12e33ea0b11fbae9c8dc53ec9bd9cbba61 /lib/tsocket | |
parent | 4a19adadcc543f289f4f1c70e696153b488f79c6 (diff) | |
download | samba-edbf2caa6f8273227229cd8f1b293e95c6a6122d.tar.gz samba-edbf2caa6f8273227229cd8f1b293e95c6a6122d.tar.bz2 samba-edbf2caa6f8273227229cd8f1b293e95c6a6122d.zip |
tsocket: add tsocket_address_bsd_sockaddr() and tsocket_address_bsd_from_sockaddr()
metze
Diffstat (limited to 'lib/tsocket')
-rw-r--r-- | lib/tsocket/tsocket.h | 15 | ||||
-rw-r--r-- | lib/tsocket/tsocket_bsd.c | 54 | ||||
-rw-r--r-- | lib/tsocket/tsocket_guide.txt | 23 |
3 files changed, 87 insertions, 5 deletions
diff --git a/lib/tsocket/tsocket.h b/lib/tsocket/tsocket.h index ae73113b5a..6e9fcbdcca 100644 --- a/lib/tsocket/tsocket.h +++ b/lib/tsocket/tsocket.h @@ -179,6 +179,21 @@ int _tstream_unix_socketpair(TALLOC_CTX *mem_ctx1, _tstream_unix_socketpair(mem_ctx1, stream1, mem_ctx2, stream2, \ __location__) +struct sockaddr; + +int _tsocket_address_bsd_from_sockaddr(TALLOC_CTX *mem_ctx, + struct sockaddr *sa, + size_t sa_socklen, + struct tsocket_address **_addr, + const char *location); +#define tsocket_address_bsd_from_sockaddr(mem_ctx, sa, sa_socklen, _addr) \ + _tsocket_address_bsd_from_sockaddr(mem_ctx, sa, sa_socklen, _addr, \ + __location__) + +ssize_t tsocket_address_bsd_sockaddr(const struct tsocket_address *addr, + struct sockaddr *sa, + size_t sa_socklen); + int _tstream_bsd_existing_socket(TALLOC_CTX *mem_ctx, int fd, struct tstream_context **_stream, diff --git a/lib/tsocket/tsocket_bsd.c b/lib/tsocket/tsocket_bsd.c index 05f5be19cb..d44525338c 100644 --- a/lib/tsocket/tsocket_bsd.c +++ b/lib/tsocket/tsocket_bsd.c @@ -201,11 +201,11 @@ struct tsocket_address_bsd { } u; }; -static int _tsocket_address_bsd_from_sockaddr(TALLOC_CTX *mem_ctx, - struct sockaddr *sa, - socklen_t sa_socklen, - struct tsocket_address **_addr, - const char *location) +int _tsocket_address_bsd_from_sockaddr(TALLOC_CTX *mem_ctx, + struct sockaddr *sa, + size_t sa_socklen, + struct tsocket_address **_addr, + const char *location) { struct tsocket_address *addr; struct tsocket_address_bsd *bsda; @@ -259,6 +259,50 @@ static int _tsocket_address_bsd_from_sockaddr(TALLOC_CTX *mem_ctx, return 0; } +ssize_t tsocket_address_bsd_sockaddr(const struct tsocket_address *addr, + struct sockaddr *sa, + size_t sa_socklen) +{ + struct tsocket_address_bsd *bsda = talloc_get_type(addr->private_data, + struct tsocket_address_bsd); + ssize_t rlen = 0; + + if (!bsda) { + errno = EINVAL; + return -1; + } + + switch (bsda->u.sa.sa_family) { + case AF_UNIX: + rlen = sizeof(struct sockaddr_un); + break; + case AF_INET: + rlen = sizeof(struct sockaddr_in); + break; +#ifdef HAVE_IPV6 + case AF_INET6: + rlen = sizeof(struct sockaddr_in6); + break; +#endif + default: + errno = EAFNOSUPPORT; + return -1; + } + + if (sa_socklen < rlen) { + errno = EINVAL; + return -1; + } + + if (sa_socklen > sizeof(struct sockaddr_storage)) { + memset(sa, 0, sa_socklen); + sa_socklen = sizeof(struct sockaddr_storage); + } + + memcpy(sa, &bsda->u.ss, sa_socklen); + return rlen; +} + int _tsocket_address_inet_from_strings(TALLOC_CTX *mem_ctx, const char *fam, const char *addr, diff --git a/lib/tsocket/tsocket_guide.txt b/lib/tsocket/tsocket_guide.txt index ed903c6027..287fe8a4e5 100644 --- a/lib/tsocket/tsocket_guide.txt +++ b/lib/tsocket/tsocket_guide.txt @@ -421,6 +421,29 @@ int tstream_unix_socketpair(TALLOC_CTX *mem_ctx1, TALLOC_CTX *mem_ctx2, struct tstream_context **stream2); +In some situations it's needed to create a tsocket_address from +a given 'struct sockaddr'. You can use tsocket_address_bsd_from_sockaddr() +for that. This should only be used if really needed, because of +already existing fixed APIs. Only AF_INET, AF_INET6 and AF_UNIX +sockets are allowed. The function returns -1 and set errno on error. +Otherwise it returns 0. + +int tsocket_address_bsd_from_sockaddr(TALLOC_CTX *mem_ctx, + struct sockaddr *sa, + socklen_t sa_socklen, + struct tsocket_address **addr); + +In some situations it's needed to get a 'struct a tsocket_address from +a given 'struct sockaddr'. You can use tsocket_address_bsd_from_sockaddr() +for that. This should only be used if really needed, because of +already existing fixed APIs. Only AF_INET, AF_INET6 and AF_UNIX +sockets are allowed. The function returns -1 and set errno on error. +Otherwise it returns 0. + +int tsocket_address_bsd_sockaddr(const struct tsocket_address *addr, + struct sockaddr *sa, + socklen_t *sa_socklen); + In some situations it's needed to wrap existing file descriptors into the tstream abstraction. You can use tstream_bsd_existing_socket() for that. But you should read the tsocket_bsd.c code and unterstand it |