Max has submitted this change and it was merged. ( 
https://gerrit.osmocom.org/11842 )

Change subject: Add osmo_sockaddr_in_to_str_and_uint()
......................................................................

Add osmo_sockaddr_in_to_str_and_uint()

It's similar to osmo_sockaddr_to_str_and_uint() but does not require odd
typecasting for AF_INET case. Make osmo_sockaddr_to_str_and_uint() into
wrapper around new function and make sure to check for address family
before typecasting. Also use proper return type.

Change-Id: Ie384483124d407a960ab6732e6a7fd90554389d2
---
M include/osmocom/core/socket.h
M src/socket.c
2 files changed, 23 insertions(+), 5 deletions(-)

Approvals:
  Pau Espin Pedrol: Looks good to me, approved
  Stefan Sperling: Looks good to me, but someone else must approve
  osmith: Looks good to me, but someone else must approve
  Jenkins Builder: Verified



diff --git a/include/osmocom/core/socket.h b/include/osmocom/core/socket.h
index 28f89a5..79a61bb 100644
--- a/include/osmocom/core/socket.h
+++ b/include/osmocom/core/socket.h
@@ -11,6 +11,7 @@
 #include <stdbool.h>
 #include <stddef.h>

+struct sockaddr_in;
 struct sockaddr;
 struct osmo_fd;

@@ -49,6 +50,8 @@

 unsigned int osmo_sockaddr_to_str_and_uint(char *addr, unsigned int addr_len, 
uint16_t *port,
                                           const struct sockaddr *sa);
+size_t osmo_sockaddr_in_to_str_and_uint(char *addr, unsigned int addr_len, 
uint16_t *port,
+                                       const struct sockaddr_in *sin);

 int osmo_sock_unix_init(uint16_t type, uint8_t proto,
                        const char *socket_path, unsigned int flags);
diff --git a/src/socket.c b/src/socket.c
index 0e17a28..e804ab5 100644
--- a/src/socket.c
+++ b/src/socket.c
@@ -569,6 +569,25 @@
        return 0;
 }

+/*! Convert sockaddr_in to IP address as char string and port as uint16_t.
+ *  \param[out] addr  String buffer to write IP address to, or NULL.
+ *  \param[out] addr_len  Size of \a addr.
+ *  \param[out] port  Pointer to uint16_t to write the port number to, or NULL.
+ *  \param[in] sin  Sockaddr to convert.
+ *  \returns the required string buffer size, like osmo_strlcpy(), or 0 if \a 
addr is NULL.
+ */
+size_t osmo_sockaddr_in_to_str_and_uint(char *addr, unsigned int addr_len, 
uint16_t *port,
+                                       const struct sockaddr_in *sin)
+{
+       if (port)
+               *port = ntohs(sin->sin_port);
+
+       if (addr)
+               return osmo_strlcpy(addr, inet_ntoa(sin->sin_addr), addr_len);
+
+       return 0;
+}
+
 /*! Convert sockaddr to IP address as char string and port as uint16_t.
  *  \param[out] addr  String buffer to write IP address to, or NULL.
  *  \param[out] addr_len  Size of \a addr.
@@ -581,11 +600,7 @@
 {
        const struct sockaddr_in *sin = (const struct sockaddr_in *)sa;

-       if (port)
-               *port = ntohs(sin->sin_port);
-       if (addr)
-               return osmo_strlcpy(addr, inet_ntoa(sin->sin_addr), addr_len);
-       return 0;
+       return osmo_sockaddr_in_to_str_and_uint(addr, addr_len, port, sin);
 }

 /*! Initialize a unix domain socket (including bind/connect)

--
To view, visit https://gerrit.osmocom.org/11842
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings

Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: Ie384483124d407a960ab6732e6a7fd90554389d2
Gerrit-Change-Number: 11842
Gerrit-PatchSet: 4
Gerrit-Owner: Max <[email protected]>
Gerrit-Reviewer: Harald Welte <[email protected]>
Gerrit-Reviewer: Jenkins Builder (1000002)
Gerrit-Reviewer: Max <[email protected]>
Gerrit-Reviewer: Pau Espin Pedrol <[email protected]>
Gerrit-Reviewer: Stefan Sperling <[email protected]>
Gerrit-Reviewer: osmith <[email protected]>

Reply via email to