Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
net: socketutils: Add utils to manipulate network address strings
Two utils to manipulate addresses in format "addr[:port]". I.e., network address (domain name or numeric), optionally followed by port number: * net_addr_str_find_port(), to return pointer to port number substring (or NULL if not present). * net_getaddrinfo_addr_str(), which is effectively getaddrinfo() wrapper taking a "addr[:port]" string as a parameter. The header file is named socketutils.h to emphasize that these utility functions are implemented on top of BSD Sockets API (and other POSIX/ANSI C functions), and thus portable to other POSIX systems (e.g., Linux), so can be used in apps testing POSIX compatibility. More utility functions (beyond address manipulation) can be added later. Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
- Loading branch information
Showing
4 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright (c) 2019 Linaro Limited | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
/** | ||
* @brief Find port in addr:port string. | ||
* | ||
* @param addr_str String of addr[:port] format | ||
* | ||
* @return Pointer to "port" part, or NULL is none. | ||
*/ | ||
const char *net_addr_str_find_port(const char *addr_str); | ||
|
||
/** | ||
* @brief Call getaddrinfo() on addr:port string | ||
* | ||
* Convenience function to split addr[:port] string into address vs port | ||
* components (or use default port number), and call getaddrinfo() on the | ||
* result. | ||
* | ||
* @param addr_str String of addr[:port] format | ||
* @param def_port Default port number to use if addr_str doesn't contain it | ||
* @param hints getaddrinfo() hints | ||
* @param res Result of getaddrinfo() (freeaddrinfo() should be called on it | ||
* as usual. | ||
* | ||
* @return Result of getaddrinfo() call. | ||
*/ | ||
int net_getaddrinfo_addr_str(const char *addr_str, const char *def_port, | ||
const struct addrinfo *hints, | ||
struct addrinfo **res); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
zephyr_sources( | ||
addr_utils.c | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright (c) 2019 Linaro Limited | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include <net/socket.h> | ||
|
||
/* These utility functions are intended to be on top of POSIX API, and don't | ||
* make sense if it's not available. | ||
*/ | ||
#ifdef CONFIG_NET_SOCKETS_POSIX_NAMES | ||
|
||
const char *net_addr_str_find_port(const char *addr_str) | ||
{ | ||
const char *p = strrchr(addr_str, ':'); | ||
|
||
if (p == NULL) { | ||
return NULL; | ||
} | ||
|
||
/* If it's not IPv6 numeric notation, we guaranteedly got a port */ | ||
if (*addr_str != '[') { | ||
return p + 1; | ||
} | ||
|
||
/* IPv6 numeric address, and ':' preceeded by ']' */ | ||
if (p[-1] == ']') { | ||
return p + 1; | ||
} | ||
|
||
/* Otherwise, just raw IPv6 address, ':' is component separator */ | ||
return NULL; | ||
} | ||
|
||
int net_getaddrinfo_addr_str(const char *addr_str, const char *def_port, | ||
const struct addrinfo *hints, | ||
struct addrinfo **res) | ||
{ | ||
const char *port; | ||
char host[NI_MAXHOST]; | ||
|
||
if (addr_str == NULL) { | ||
errno = EINVAL; | ||
return -1; | ||
} | ||
|
||
port = net_addr_str_find_port(addr_str); | ||
|
||
if (port == NULL) { | ||
port = def_port; | ||
} else { | ||
int host_len = port - addr_str - 1; | ||
|
||
if (host_len > sizeof(host) - 1) { | ||
errno = EINVAL; | ||
return -1; | ||
} | ||
strncpy(host, addr_str, host_len + 1); | ||
host[host_len] = '\0'; | ||
addr_str = host; | ||
} | ||
|
||
return getaddrinfo(addr_str, port, hints, res); | ||
} | ||
|
||
#endif |