tAdd function to quickly resolve hostnames to in_addr - synk - synchronize files between hosts
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
(DIR) commit b90c17add7b3b57172237a1eea69253d90f2407c
(DIR) parent 82cf376888b0a53cfc1928a739a951bd0fbf30b1
(HTM) Author: Willy <willyatmailoodotorg>
Date: Fri, 2 Sep 2016 18:57:30 +0200
Add function to quickly resolve hostnames to in_addr
Diffstat:
M synk.c | 20 ++++++++++++++++++++
1 file changed, 20 insertions(+), 0 deletions(-)
---
(DIR) diff --git a/synk.c b/synk.c
t@@ -4,6 +4,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <netdb.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/queue.h>
t@@ -64,6 +65,7 @@ int serverloop(in_addr_t, in_port_t);
char *echo(char * []);
char **concat(int, ...);
+struct in_addr *resolve(char *);
struct peer_t *addpeer(struct peers_t *, in_addr_t, in_port_t);
long gettimestamp(const char *path);
t@@ -150,6 +152,24 @@ concat(int n, ...)
}
/*
+ * Put an hostname, get an in_addr!
+ * This is intended to be consumed directly, as gethostbyname() might
+ * return a pointer to a static buffer
+ */
+struct in_addr *
+resolve(char *hostname)
+{
+ struct hostent *he;
+
+ if (!(he = gethostbyname(hostname))) {
+ herror(hostname);
+ return NULL;
+ }
+
+ return ((struct in_addr **)he->h_addr_list)[0];
+}
+
+/*
* Returns the UNIX timestamp for the given file, or -1 in case stat(2)
* is in error.
*/