Change in libosmocore[master]: add osmo_sock_get_{local, remote}_ip{, _port}()
Harald Welte has submitted this change and it was merged. ( https://gerrit.osmocom.org/11455 ) Change subject: add osmo_sock_get_{local,remote}_ip{,_port}() .. add osmo_sock_get_{local,remote}_ip{,_port}() Return only the IP or port of either the local or remote connection, not the whole set of IP and port of both the local and remote connection like osmo_sock_get_name() does it. This is needed for OS#2841, where we only want to print the remote IP. Related: OS#2841 Change-Id: I6803c204771c59a2002bc6a0e6b79c83c35f87e1 --- M include/osmocom/core/socket.h M src/socket.c 2 files changed, 92 insertions(+), 25 deletions(-) Approvals: Harald Welte: Looks good to me, approved Jenkins Builder: Verified diff --git a/include/osmocom/core/socket.h b/include/osmocom/core/socket.h index f23a243..28f89a5 100644 --- a/include/osmocom/core/socket.h +++ b/include/osmocom/core/socket.h @@ -9,6 +9,7 @@ #include #include +#include struct sockaddr; struct osmo_fd; @@ -56,6 +57,11 @@ const char *socket_path, unsigned int flags); char *osmo_sock_get_name(void *ctx, int fd); +int osmo_sock_get_local_ip(int fd, char *host, size_t len); +int osmo_sock_get_local_ip_port(int fd, char *port, size_t len); +int osmo_sock_get_remote_ip(int fd, char *host, size_t len); +int osmo_sock_get_remote_ip_port(int fd, char *port, size_t len); + int osmo_sock_mcast_loop_set(int fd, bool enable); int osmo_sock_mcast_ttl_set(int fd, uint8_t ttl); diff --git a/src/socket.c b/src/socket.c index bb5505f..c7e1c9d 100644 --- a/src/socket.c +++ b/src/socket.c @@ -682,6 +682,86 @@ return osmo_fd_init_ofd(ofd, osmo_sock_unix_init(type, proto, socket_path, flags)); } +/*! Get the IP and/or port number on socket. This is for internal usage. + * Convenience wrappers: osmo_sock_get_local_ip(), + * osmo_sock_get_local_ip_port(), osmo_sock_get_remote_ip(), + * osmo_sock_get_remote_ip_port() and osmo_sock_get_name() + * \param[in] fd file descriptor of socket + * \param[out] ip IP address (will be filled in when not NULL) + * \param[in] ip_len length of the ip buffer + * \param[out] port number (will be filled in when not NULL) + * \param[in] port_len length of the port buffer + * \param[in] local (true) or remote (false) name will get looked at + * \returns 0 on success; negative otherwise + */ +static int osmo_sock_get_name2(int fd, char *ip, size_t ip_len, char *port, size_t port_len, bool local) +{ + struct sockaddr sa; + socklen_t len = sizeof(sa); + char ipbuf[64], portbuf[16]; + int rc; + + rc = local ? getsockname(fd, , ) : getpeername(fd, , ); + if (rc < 0) + return rc; + + rc = getnameinfo(, len, ipbuf, sizeof(ipbuf), +portbuf, sizeof(portbuf), +NI_NUMERICHOST | NI_NUMERICSERV); + if (rc < 0) + return rc; + + if (ip) + strncpy(ip, ipbuf, ip_len); + if (port) + strncpy(port, portbuf, port_len); + return 0; +} + +/*! Get local IP address on socket + * \param[in] fd file descriptor of socket + * \param[out] ip IP address (will be filled in) + * \param[in] len length of the output buffer + * \returns 0 on success; negative otherwise + */ +int osmo_sock_get_local_ip(int fd, char *ip, size_t len) +{ + return osmo_sock_get_name2(fd, ip, len, NULL, 0, true); +} + +/*! Get local port on socket + * \param[in] fd file descriptor of socket + * \param[out] port number (will be filled in) + * \param[in] len length of the output buffer + * \returns 0 on success; negative otherwise + */ +int osmo_sock_get_local_ip_port(int fd, char *port, size_t len) +{ + return osmo_sock_get_name2(fd, NULL, 0, port, len, true); +} + +/*! Get remote IP address on socket + * \param[in] fd file descriptor of socket + * \param[out] ip IP address (will be filled in) + * \param[in] len length of the output buffer + * \returns 0 on success; negative otherwise + */ +int osmo_sock_get_remote_ip(int fd, char *ip, size_t len) +{ + return osmo_sock_get_name2(fd, ip, len, NULL, 0, false); +} + +/*! Get remote port on socket + * \param[in] fd file descriptor of socket + * \param[out] port number (will be filled in) + * \param[in] len length of the output buffer + * \returns 0 on success; negative otherwise + */ +int osmo_sock_get_remote_ip_port(int fd, char *port, size_t len) +{ + return osmo_sock_get_name2(fd, NULL, 0, port, len, false); +} + /*! Get address/port information on socket in dyn-alloc string * \param[in] ctx talloc context from which to allocate string buffer * \param[in] fd file descriptor of socket @@ -689,37 +769,18 @@ */ char *osmo_sock_get_name(void *ctx, int fd) { - struct sockaddr sa_l, sa_r; - socklen_t sa_len_l = sizeof(sa_l); - socklen_t sa_len_r = sizeof(sa_r); char hostbuf_l[64],
Change in libosmocore[master]: add osmo_sock_get_{local, remote}_ip{, _port}()
Harald Welte has posted comments on this change. ( https://gerrit.osmocom.org/11455 ) Change subject: add osmo_sock_get_{local,remote}_ip{,_port}() .. Patch Set 6: Code-Review+2 -- To view, visit https://gerrit.osmocom.org/11455 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-MessageType: comment Gerrit-Change-Id: I6803c204771c59a2002bc6a0e6b79c83c35f87e1 Gerrit-Change-Number: 11455 Gerrit-PatchSet: 6 Gerrit-Owner: osmith Gerrit-Reviewer: Harald Welte Gerrit-Reviewer: Jenkins Builder (102) Gerrit-Reviewer: osmith Gerrit-CC: Max Gerrit-Comment-Date: Fri, 26 Oct 2018 17:54:52 + Gerrit-HasComments: No Gerrit-HasLabels: Yes
Change in libosmocore[master]: add osmo_sock_get_{local, remote}_ip{, _port}()
Harald Welte has uploaded a new patch set (#6) to the change originally created by osmith. ( https://gerrit.osmocom.org/11455 ) Change subject: add osmo_sock_get_{local,remote}_ip{,_port}() .. add osmo_sock_get_{local,remote}_ip{,_port}() Return only the IP or port of either the local or remote connection, not the whole set of IP and port of both the local and remote connection like osmo_sock_get_name() does it. This is needed for OS#2841, where we only want to print the remote IP. Related: OS#2841 Change-Id: I6803c204771c59a2002bc6a0e6b79c83c35f87e1 --- M include/osmocom/core/socket.h M src/socket.c 2 files changed, 92 insertions(+), 25 deletions(-) git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/55/11455/6 -- To view, visit https://gerrit.osmocom.org/11455 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-MessageType: newpatchset Gerrit-Change-Id: I6803c204771c59a2002bc6a0e6b79c83c35f87e1 Gerrit-Change-Number: 11455 Gerrit-PatchSet: 6 Gerrit-Owner: osmith Gerrit-Reviewer: Harald Welte Gerrit-Reviewer: Jenkins Builder (102) Gerrit-Reviewer: osmith Gerrit-CC: Max
Change in libosmocore[master]: add osmo_sock_get_{local, remote}_ip{, _port}()
Hello Harald Welte, Jenkins Builder, I'd like you to reexamine a change. Please visit https://gerrit.osmocom.org/11455 to look at the new patch set (#5). Change subject: add osmo_sock_get_{local,remote}_ip{,_port}() .. add osmo_sock_get_{local,remote}_ip{,_port}() Return only the IP or port of either the local or remote connection, not the whole set of IP and port of both the local and remote connection like osmo_sock_get_name() does it. This is needed for OS#2841, where we only want to print the remote IP. Related: OS#2841 Change-Id: I6803c204771c59a2002bc6a0e6b79c83c35f87e1 --- M include/osmocom/core/socket.h M src/socket.c 2 files changed, 92 insertions(+), 25 deletions(-) git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/55/11455/5 -- To view, visit https://gerrit.osmocom.org/11455 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-MessageType: newpatchset Gerrit-Change-Id: I6803c204771c59a2002bc6a0e6b79c83c35f87e1 Gerrit-Change-Number: 11455 Gerrit-PatchSet: 5 Gerrit-Owner: osmith Gerrit-Reviewer: Harald Welte Gerrit-Reviewer: Jenkins Builder (102) Gerrit-Reviewer: osmith Gerrit-CC: Max
Change in libosmocore[master]: add osmo_sock_get_{local, remote}_ip{, _port}()
Hello Harald Welte, Jenkins Builder, I'd like you to reexamine a change. Please visit https://gerrit.osmocom.org/11455 to look at the new patch set (#4). Change subject: add osmo_sock_get_{local,remote}_ip{,_port}() .. add osmo_sock_get_{local,remote}_ip{,_port}() Return only the IP or port of either the local or remote connection, not the whole set of IP and port of both the local and remote connection like osmo_sock_get_name() does it. This is needed for OS#2841, where we only want to print the remote IP. Related: OS#2841 Change-Id: I6803c204771c59a2002bc6a0e6b79c83c35f87e1 --- M include/osmocom/core/socket.h M src/socket.c 2 files changed, 92 insertions(+), 25 deletions(-) git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/55/11455/4 -- To view, visit https://gerrit.osmocom.org/11455 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-MessageType: newpatchset Gerrit-Change-Id: I6803c204771c59a2002bc6a0e6b79c83c35f87e1 Gerrit-Change-Number: 11455 Gerrit-PatchSet: 4 Gerrit-Owner: osmith Gerrit-Reviewer: Harald Welte Gerrit-Reviewer: Jenkins Builder (102) Gerrit-Reviewer: osmith Gerrit-CC: Max
Change in libosmocore[master]: add osmo_sock_get_{local, remote}_ip{, _port}()
Hello Harald Welte, Jenkins Builder, I'd like you to reexamine a change. Please visit https://gerrit.osmocom.org/11455 to look at the new patch set (#3). Change subject: add osmo_sock_get_{local,remote}_ip{,_port}() .. add osmo_sock_get_{local,remote}_ip{,_port}() Return only the IP or port of either the local or remote connection, not the whole set of IP and port of both the local and remote connection like osmo_sock_get_name() does it. This is needed for OS#2841, where we only want to print the remote IP. Related: OS#2841 Change-Id: I6803c204771c59a2002bc6a0e6b79c83c35f87e1 --- M include/osmocom/core/socket.h M src/socket.c 2 files changed, 92 insertions(+), 25 deletions(-) git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/55/11455/3 -- To view, visit https://gerrit.osmocom.org/11455 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-MessageType: newpatchset Gerrit-Change-Id: I6803c204771c59a2002bc6a0e6b79c83c35f87e1 Gerrit-Change-Number: 11455 Gerrit-PatchSet: 3 Gerrit-Owner: osmith Gerrit-Reviewer: Harald Welte Gerrit-Reviewer: Jenkins Builder (102) Gerrit-Reviewer: osmith Gerrit-CC: Max
Change in libosmocore[master]: add osmo_sock_get_{local, remote}_ip{, _port}()
Harald Welte has posted comments on this change. ( https://gerrit.osmocom.org/11455 ) Change subject: add osmo_sock_get_{local,remote}_ip{,_port}() .. Patch Set 2: Code-Review-1 (1 comment) https://gerrit.osmocom.org/#/c/11455/2/include/osmocom/core/socket.h File include/osmocom/core/socket.h: https://gerrit.osmocom.org/#/c/11455/2/include/osmocom/core/socket.h@60 PS2, Line 60: bool please don't make them return bool. We don't have any functions in osmocom that return bool, *except* for predicate functions that are used to check for some condition. The general semantics are, like many things, like in the linux kernel: 0 is success, negativ is error, and positive is used to e.g. indicate the size/number of bytes/... -- To view, visit https://gerrit.osmocom.org/11455 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-MessageType: comment Gerrit-Change-Id: I6803c204771c59a2002bc6a0e6b79c83c35f87e1 Gerrit-Change-Number: 11455 Gerrit-PatchSet: 2 Gerrit-Owner: osmith Gerrit-Reviewer: Harald Welte Gerrit-Reviewer: Jenkins Builder (102) Gerrit-Reviewer: osmith Gerrit-CC: Max Gerrit-Comment-Date: Fri, 26 Oct 2018 14:12:48 + Gerrit-HasComments: Yes Gerrit-HasLabels: Yes
Change in libosmocore[master]: add osmo_sock_get_{local, remote}_ip{, _port}()
osmith has posted comments on this change. ( https://gerrit.osmocom.org/11455 ) Change subject: add osmo_sock_get_{local,remote}_ip{,_port}() .. Patch Set 2: (Tested both osmo_sock_get_name() and osmo_sock_get_remote_ip(), and they are working as expected.) -- To view, visit https://gerrit.osmocom.org/11455 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-MessageType: comment Gerrit-Change-Id: I6803c204771c59a2002bc6a0e6b79c83c35f87e1 Gerrit-Change-Number: 11455 Gerrit-PatchSet: 2 Gerrit-Owner: osmith Gerrit-Reviewer: Harald Welte Gerrit-Reviewer: Jenkins Builder (102) Gerrit-Reviewer: osmith Gerrit-CC: Max Gerrit-Comment-Date: Fri, 26 Oct 2018 13:46:18 + Gerrit-HasComments: No Gerrit-HasLabels: No
Change in libosmocore[master]: add osmo_sock_get_{local, remote}_ip{, _port}()
osmith has posted comments on this change. ( https://gerrit.osmocom.org/11455 ) Change subject: add osmo_sock_get_{local,remote}_ip{,_port}() .. Patch Set 2: Let's try this again: without defines, no heap-allocated buffer and less duplicate code :) -- To view, visit https://gerrit.osmocom.org/11455 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-MessageType: comment Gerrit-Change-Id: I6803c204771c59a2002bc6a0e6b79c83c35f87e1 Gerrit-Change-Number: 11455 Gerrit-PatchSet: 2 Gerrit-Owner: osmith Gerrit-Reviewer: Harald Welte Gerrit-Reviewer: Jenkins Builder (102) Gerrit-Reviewer: osmith Gerrit-CC: Max Gerrit-Comment-Date: Fri, 26 Oct 2018 13:44:17 + Gerrit-HasComments: No Gerrit-HasLabels: No
Change in libosmocore[master]: add osmo_sock_get_{local, remote}_ip{, _port}()
Hello Harald Welte, Jenkins Builder, I'd like you to reexamine a change. Please visit https://gerrit.osmocom.org/11455 to look at the new patch set (#2). Change subject: add osmo_sock_get_{local,remote}_ip{,_port}() .. add osmo_sock_get_{local,remote}_ip{,_port}() Return only the IP or port of either the local or remote connection, not the whole set of IP and port of both the local and remote connection like osmo_sock_get_name() does it. This is needed for OS#2841, where we only want to print the remote IP. Related: OS#2841 Change-Id: I6803c204771c59a2002bc6a0e6b79c83c35f87e1 --- M include/osmocom/core/socket.h M src/socket.c 2 files changed, 92 insertions(+), 25 deletions(-) git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/55/11455/2 -- To view, visit https://gerrit.osmocom.org/11455 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-MessageType: newpatchset Gerrit-Change-Id: I6803c204771c59a2002bc6a0e6b79c83c35f87e1 Gerrit-Change-Number: 11455 Gerrit-PatchSet: 2 Gerrit-Owner: osmith Gerrit-Reviewer: Harald Welte Gerrit-Reviewer: Jenkins Builder (102) Gerrit-Reviewer: osmith Gerrit-CC: Max
Change in libosmocore[master]: add osmo_sock_get_{local, remote}_ip{, _port}()
Harald Welte has posted comments on this change. ( https://gerrit.osmocom.org/11455 ) Change subject: add osmo_sock_get_{local,remote}_ip{,_port}() .. Patch Set 1: Code-Review-1 (2 comments) https://gerrit.osmocom.org/#/c/11455/1/include/osmocom/core/socket.h File include/osmocom/core/socket.h: https://gerrit.osmocom.org/#/c/11455/1/include/osmocom/core/socket.h@59 PS1, Line 59: char *osmo_sock_get_name2(void *ctx, int fd, bool local, bool port); > I thought that was necessary to use it with the defines below. […] just don't try to prematurely optimzie with #defines and make those defines below public functions/symbols. at that point you can have the osmo_sock_get_name2() function static and not declare it in the header here. https://gerrit.osmocom.org/#/c/11455/1/src/socket.c File src/socket.c: https://gerrit.osmocom.org/#/c/11455/1/src/socket.c@712 PS1, Line 712: talloc_strdup(ctx, port ? portbuf : hostbuf) I thought the point was that you wanted to avoid returning a heap-allocated buffer with those new functions? -- To view, visit https://gerrit.osmocom.org/11455 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-MessageType: comment Gerrit-Change-Id: I6803c204771c59a2002bc6a0e6b79c83c35f87e1 Gerrit-Change-Number: 11455 Gerrit-PatchSet: 1 Gerrit-Owner: osmith Gerrit-Reviewer: Harald Welte Gerrit-Reviewer: Jenkins Builder (102) Gerrit-Reviewer: osmith Gerrit-CC: Max Gerrit-Comment-Date: Thu, 25 Oct 2018 17:57:51 + Gerrit-HasComments: Yes Gerrit-HasLabels: Yes
Change in libosmocore[master]: add osmo_sock_get_{local, remote}_ip{, _port}()
Max has posted comments on this change. ( https://gerrit.osmocom.org/11455 ) Change subject: add osmo_sock_get_{local,remote}_ip{,_port}() .. Patch Set 1: Not sure how can we make this function - maybe we can somehow use OSMO_DEPRECATED_OUTSIDE_LIBOSMOCORE to discourage public use of the function. Not sure how to make it work with #define wrappers though. Alternatively, we can just adjust the comment and make it regular public function. -- To view, visit https://gerrit.osmocom.org/11455 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-MessageType: comment Gerrit-Change-Id: I6803c204771c59a2002bc6a0e6b79c83c35f87e1 Gerrit-Change-Number: 11455 Gerrit-PatchSet: 1 Gerrit-Owner: osmith Gerrit-Reviewer: Jenkins Builder (102) Gerrit-Reviewer: osmith Gerrit-CC: Max Gerrit-Comment-Date: Thu, 25 Oct 2018 15:32:59 + Gerrit-HasComments: No Gerrit-HasLabels: No
Change in libosmocore[master]: add osmo_sock_get_{local, remote}_ip{, _port}()
osmith has posted comments on this change. ( https://gerrit.osmocom.org/11455 ) Change subject: add osmo_sock_get_{local,remote}_ip{,_port}() .. Patch Set 1: (2 comments) https://gerrit.osmocom.org/#/c/11455/1/include/osmocom/core/socket.h File include/osmocom/core/socket.h: https://gerrit.osmocom.org/#/c/11455/1/include/osmocom/core/socket.h@59 PS1, Line 59: char *osmo_sock_get_name2(void *ctx, int fd, bool local, bool port); > If that's meant for internal use (as per comment in . […] I thought that was necessary to use it with the defines below. How do we not make it public, add an underscore in front of the function? https://gerrit.osmocom.org/#/c/11455/1/src/socket.c File src/socket.c: https://gerrit.osmocom.org/#/c/11455/1/src/socket.c@714 PS1, Line 714: > There seems to be some code duplication with osmo_sock_get_name() - any > chance to unify that? Good question. I'll try it out tomorrow by adding hostbuf and portbuf arguments to osmo_sock_get_name2, and if they are not NULL, write the results into them. Then it's possible to use osmo_sock_get_name2() from the function below and make it a bit less redundant. -- To view, visit https://gerrit.osmocom.org/11455 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-MessageType: comment Gerrit-Change-Id: I6803c204771c59a2002bc6a0e6b79c83c35f87e1 Gerrit-Change-Number: 11455 Gerrit-PatchSet: 1 Gerrit-Owner: osmith Gerrit-Reviewer: Jenkins Builder (102) Gerrit-Reviewer: osmith Gerrit-CC: Max Gerrit-Comment-Date: Thu, 25 Oct 2018 15:01:17 + Gerrit-HasComments: Yes Gerrit-HasLabels: No
Change in libosmocore[master]: add osmo_sock_get_{local, remote}_ip{, _port}()
Max has posted comments on this change. ( https://gerrit.osmocom.org/11455 ) Change subject: add osmo_sock_get_{local,remote}_ip{,_port}() .. Patch Set 1: (2 comments) https://gerrit.osmocom.org/#/c/11455/1/include/osmocom/core/socket.h File include/osmocom/core/socket.h: https://gerrit.osmocom.org/#/c/11455/1/include/osmocom/core/socket.h@59 PS1, Line 59: char *osmo_sock_get_name2(void *ctx, int fd, bool local, bool port); If that's meant for internal use (as per comment in .c) than why do we add it to public API? https://gerrit.osmocom.org/#/c/11455/1/src/socket.c File src/socket.c: https://gerrit.osmocom.org/#/c/11455/1/src/socket.c@714 PS1, Line 714: There seems to be some code duplication with osmo_sock_get_name() - any chance to unify that? -- To view, visit https://gerrit.osmocom.org/11455 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-MessageType: comment Gerrit-Change-Id: I6803c204771c59a2002bc6a0e6b79c83c35f87e1 Gerrit-Change-Number: 11455 Gerrit-PatchSet: 1 Gerrit-Owner: osmith Gerrit-Reviewer: Jenkins Builder (102) Gerrit-CC: Max Gerrit-Comment-Date: Thu, 25 Oct 2018 13:07:08 + Gerrit-HasComments: Yes Gerrit-HasLabels: No
Change in libosmocore[master]: add osmo_sock_get_{local, remote}_ip{, _port}()
osmith has uploaded this change for review. ( https://gerrit.osmocom.org/11455 Change subject: add osmo_sock_get_{local,remote}_ip{,_port}() .. add osmo_sock_get_{local,remote}_ip{,_port}() Return only the IP or port of either the local or remote connection, not the whole set of IP and port of both the local and remote connection like osmo_sock_get_name() does it. This is needed for OS#2841, where we only want to print the remote IP. Related: OS#2841 Change-Id: I6803c204771c59a2002bc6a0e6b79c83c35f87e1 --- M include/osmocom/core/socket.h M src/socket.c 2 files changed, 35 insertions(+), 0 deletions(-) git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/55/11455/1 diff --git a/include/osmocom/core/socket.h b/include/osmocom/core/socket.h index f23a243..6f3de0f 100644 --- a/include/osmocom/core/socket.h +++ b/include/osmocom/core/socket.h @@ -56,6 +56,11 @@ const char *socket_path, unsigned int flags); char *osmo_sock_get_name(void *ctx, int fd); +char *osmo_sock_get_name2(void *ctx, int fd, bool local, bool port); +#define osmo_sock_get_local_ip(ctx, fd) osmo_sock_get_name2(ctx, fd, false, false) +#define osmo_sock_get_local_ip_port(ctx, fd) osmo_sock_get_name2(ctx, fd, false, true) +#define osmo_sock_get_remote_ip(ctx, fd) osmo_sock_get_name2(ctx, fd, true, false) +#define osmo_sock_get_remote_ip_port(ctx, fd) osmo_sock_get_name2(ctx, fd, true, true) int osmo_sock_mcast_loop_set(int fd, bool enable); int osmo_sock_mcast_ttl_set(int fd, uint8_t ttl); diff --git a/src/socket.c b/src/socket.c index bb5505f..dedf2b6 100644 --- a/src/socket.c +++ b/src/socket.c @@ -682,6 +682,36 @@ return osmo_fd_init_ofd(ofd, osmo_sock_unix_init(type, proto, socket_path, flags)); } +/*! Get one IP or port number on socket in dyn-alloc string. This is + * for internal usage. Convenience wrappers: osmo_sock_get_local_ip(), + * osmo_sock_get_local_ip_port(), osmo_sock_get_remote_ip() and + * osmo_sock_get_remote_ip_port(). + * \param[in] ctx talloc context from which to allocate string buffer + * \param[in] fd file descriptor of socket + * \param[in] local (true) or remote (false) name will get looked at + * \param[in] port (true) or ip address (false) will be returned + * \returns string with IP or port + */ +char *osmo_sock_get_name2(void *ctx, int fd, bool local, bool port) +{ + struct sockaddr sa; + socklen_t len = sizeof(sa); + char hostbuf[64], portbuf[16]; + int rc; + + rc = local ? getsockname(fd, , ) : getpeername(fd, , ); + if (rc < 0) + return NULL; + + rc = getnameinfo(, len, hostbuf, sizeof(hostbuf), +portbuf, sizeof(portbuf), +NI_NUMERICHOST | NI_NUMERICSERV); + if (rc < 0) + return NULL; + + return talloc_strdup(ctx, port ? portbuf : hostbuf); +} + /*! Get address/port information on socket in dyn-alloc string * \param[in] ctx talloc context from which to allocate string buffer * \param[in] fd file descriptor of socket -- To view, visit https://gerrit.osmocom.org/11455 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-MessageType: newchange Gerrit-Change-Id: I6803c204771c59a2002bc6a0e6b79c83c35f87e1 Gerrit-Change-Number: 11455 Gerrit-PatchSet: 1 Gerrit-Owner: osmith