Geoff Salmon
2013-01-09 00:21:26 UTC
---
sk.c | 38 ++++++++++++++++++++++++++++++++++++++
sk.h | 10 ++++++++++
2 files changed, 48 insertions(+)
diff --git a/sk.c b/sk.c
index 452f898..e09d459 100644
--- a/sk.c
+++ b/sk.c
@@ -26,6 +26,8 @@
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>
+#include <ifaddrs.h>
+#include <stdlib.h>
#include "print.h"
#include "sk.h"
@@ -158,6 +160,42 @@ int sk_interface_macaddr(char *name, unsigned char *mac, int len)
return 0;
}
+int sk_interface_addr(char *name, int family, uint8_t *addr, int len)
+{
+ struct ifaddrs *ifaddr, *i;
+ int copy_len, result = -1;
+ void *copy_from;
+ if (getifaddrs(&ifaddr) == -1) {
+ pr_err("getifaddrs failed: %m");
+ return -1;
+ }
+ for (i = ifaddr; i; i = i->ifa_next) {
+ if (i->ifa_addr && family == i->ifa_addr->sa_family &&
+ strcmp(name, i->ifa_name) == 0)
+ {
+ switch (family) {
+ case AF_INET:
+ copy_len = 4;
+ copy_from = &((struct sockaddr_in *)i->ifa_addr)->sin_addr.s_addr;
+ break;
+ case AF_INET6:
+ copy_len = 16;
+ copy_from = &((struct sockaddr_in6 *)i->ifa_addr)->sin6_addr.s6_addr;
+ break;
+ default:
+ continue;
+ }
+ if (copy_len > len)
+ copy_len = len;
+ memcpy(addr, copy_from, copy_len);
+ result = copy_len;
+ break;
+ }
+ }
+ free(ifaddr);
+ return result;
+}
+
int sk_receive(int fd, void *buf, int buflen,
struct hw_timestamp *hwts, int flags)
{
diff --git a/sk.h b/sk.h
index 53ebcad..1923742 100644
--- a/sk.h
+++ b/sk.h
@@ -64,6 +64,16 @@ int sk_get_ts_info(char *name, struct sk_ts_info *sk_info);
int sk_interface_macaddr(char *name, unsigned char *mac, int len);
/**
+ * Obtains the first IP address assigned to a network interface.
+ * @param name The name of the interface
+ * @param family The family of the address to get: AF_INET or AF_INET6
+ * @param addr Buffer to hold the result
+ * @param len Length of 'addr'
+ * @return The number of bytes written to addr on success, -1 otherwise.
+ */
+int sk_interface_addr(char *name, int family, uint8_t *addr, int len);
+
+/**
* Read a message from a socket.
* @param fd An open socket.
* @param buf Buffer to receive the message.
sk.c | 38 ++++++++++++++++++++++++++++++++++++++
sk.h | 10 ++++++++++
2 files changed, 48 insertions(+)
diff --git a/sk.c b/sk.c
index 452f898..e09d459 100644
--- a/sk.c
+++ b/sk.c
@@ -26,6 +26,8 @@
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>
+#include <ifaddrs.h>
+#include <stdlib.h>
#include "print.h"
#include "sk.h"
@@ -158,6 +160,42 @@ int sk_interface_macaddr(char *name, unsigned char *mac, int len)
return 0;
}
+int sk_interface_addr(char *name, int family, uint8_t *addr, int len)
+{
+ struct ifaddrs *ifaddr, *i;
+ int copy_len, result = -1;
+ void *copy_from;
+ if (getifaddrs(&ifaddr) == -1) {
+ pr_err("getifaddrs failed: %m");
+ return -1;
+ }
+ for (i = ifaddr; i; i = i->ifa_next) {
+ if (i->ifa_addr && family == i->ifa_addr->sa_family &&
+ strcmp(name, i->ifa_name) == 0)
+ {
+ switch (family) {
+ case AF_INET:
+ copy_len = 4;
+ copy_from = &((struct sockaddr_in *)i->ifa_addr)->sin_addr.s_addr;
+ break;
+ case AF_INET6:
+ copy_len = 16;
+ copy_from = &((struct sockaddr_in6 *)i->ifa_addr)->sin6_addr.s6_addr;
+ break;
+ default:
+ continue;
+ }
+ if (copy_len > len)
+ copy_len = len;
+ memcpy(addr, copy_from, copy_len);
+ result = copy_len;
+ break;
+ }
+ }
+ free(ifaddr);
+ return result;
+}
+
int sk_receive(int fd, void *buf, int buflen,
struct hw_timestamp *hwts, int flags)
{
diff --git a/sk.h b/sk.h
index 53ebcad..1923742 100644
--- a/sk.h
+++ b/sk.h
@@ -64,6 +64,16 @@ int sk_get_ts_info(char *name, struct sk_ts_info *sk_info);
int sk_interface_macaddr(char *name, unsigned char *mac, int len);
/**
+ * Obtains the first IP address assigned to a network interface.
+ * @param name The name of the interface
+ * @param family The family of the address to get: AF_INET or AF_INET6
+ * @param addr Buffer to hold the result
+ * @param len Length of 'addr'
+ * @return The number of bytes written to addr on success, -1 otherwise.
+ */
+int sk_interface_addr(char *name, int family, uint8_t *addr, int len);
+
+/**
* Read a message from a socket.
* @param fd An open socket.
* @param buf Buffer to receive the message.
--
1.7.9.5
1.7.9.5