aboutsummaryrefslogtreecommitdiffstats
path: root/host
diff options
context:
space:
mode:
authorMartin Braun <martin.braun@ettus.com>2017-07-13 15:57:32 -0700
committerMartin Braun <martin.braun@ettus.com>2017-10-04 14:58:41 -0700
commitad1e0bc855cfa418f1a297c64e44a8352e80c4ee (patch)
tree94872818648329f110d604caf48f216eafd7987a /host
parentac6d27348d4e01035183423a031445808c0f6b90 (diff)
downloaduhd-ad1e0bc855cfa418f1a297c64e44a8352e80c4ee.tar.gz
uhd-ad1e0bc855cfa418f1a297c64e44a8352e80c4ee.tar.bz2
uhd-ad1e0bc855cfa418f1a297c64e44a8352e80c4ee.zip
udp: Added option to query local address of socket
Diffstat (limited to 'host')
-rw-r--r--host/include/uhd/transport/udp_zero_copy.hpp13
-rw-r--r--host/lib/transport/udp_wsa_zero_copy.cpp15
-rw-r--r--host/lib/transport/udp_zero_copy.cpp11
3 files changed, 38 insertions, 1 deletions
diff --git a/host/include/uhd/transport/udp_zero_copy.hpp b/host/include/uhd/transport/udp_zero_copy.hpp
index 851e004d7..5acd8dc56 100644
--- a/host/include/uhd/transport/udp_zero_copy.hpp
+++ b/host/include/uhd/transport/udp_zero_copy.hpp
@@ -68,7 +68,20 @@ public:
const device_addr_t &hints = device_addr_t()
);
+ /*! Return the local port of the UDP connection
+ *
+ * Port is in host byte order. No funny business here.
+ *
+ * \returns Port number or 0 if port number couldn't be identified.
+ */
virtual uint16_t get_local_port(void) const = 0;
+
+ /*! Return the local IP address of the UDP connection as a dotted string.
+ *
+ * \returns IP address as a string or empty string if the IP address could
+ * not be identified.
+ */
+ virtual std::string get_local_addr(void) const = 0;
};
}} //namespace
diff --git a/host/lib/transport/udp_wsa_zero_copy.cpp b/host/lib/transport/udp_wsa_zero_copy.cpp
index 66af394a9..68e94d319 100644
--- a/host/lib/transport/udp_wsa_zero_copy.cpp
+++ b/host/lib/transport/udp_wsa_zero_copy.cpp
@@ -286,6 +286,21 @@ public:
return local_port;
}
+ std::string get_local_addr(void) const {
+ // Behold the beauty of winsock
+ struct sockaddr_in addr_info;
+ int addr_len = sizeof(addr_info);
+ std::string local_addr;
+ if (getsockname(_sock_fd, (SOCKADDR*) &addr_info, &addr_len) == 0) {
+ // inet_ntoa() guarantees either NULL or null-terminated array
+ char *local_ip = inet_ntoa(addr_info.sin_addr);
+ if (local_ip) {
+ local_addr = std::string(local_ip);
+ }
+ }
+ return local_addr;
+ }
+
//! Read back the socket's buffer space reserved for receives
size_t get_recv_buff_size(void) {
int recv_buff_size = 0;
diff --git a/host/lib/transport/udp_zero_copy.cpp b/host/lib/transport/udp_zero_copy.cpp
index 814f41500..cd271eccf 100644
--- a/host/lib/transport/udp_zero_copy.cpp
+++ b/host/lib/transport/udp_zero_copy.cpp
@@ -244,7 +244,16 @@ public:
size_t get_num_send_frames(void) const {return _num_send_frames;}
size_t get_send_frame_size(void) const {return _send_frame_size;}
- uint16_t get_local_port(void) const {return _socket->local_endpoint().port();}
+
+ uint16_t get_local_port(void) const
+ {
+ return _socket->local_endpoint().port();
+ }
+
+ std::string get_local_addr(void) const
+ {
+ return _socket->local_endpoint().address().to_string();
+ }
private:
//memory management -> buffers and fifos