Permalink
Browse files

Add function:booth client can use the host name.

Signed-off-by: Yuich SEINO <seino.cluster2@gmail.com>
  • Loading branch information...
1 parent a55b182 commit f7aea17d5e1eb3ad31af305307f33381686e63d5 @seinocluster2 seinocluster2 committed Sep 21, 2012
Showing with 33 additions and 1 deletion.
  1. +33 −1 src/main.c
View
@@ -38,6 +38,9 @@
#include <sys/ioctl.h>
#include <termios.h>
#include <signal.h>
+#include <netdb.h>
+#include <arpa/inet.h>
+#include <sys/types.h>
#include "log.h"
#include "booth.h"
#include "config.h"
@@ -770,11 +773,35 @@ void safe_copy(char *dest, char *value, size_t buflen, const char *description)
strncpy(dest, value, buflen - 1);
}
+static int host_convert(char *hostname, char *ip_str, size_t ip_size)
+{
+ struct addrinfo *result = NULL, hints = {0};
+ int re = -1;
+
+ memset(&hints, 0, sizeof(hints));
+ hints.ai_family = BOOTH_PROTO_FAMILY;
+ hints.ai_socktype = SOCK_DGRAM;
+
+ re = getaddrinfo(hostname, NULL, &hints, &result);
+
+ if (re == 0) {
+ struct in_addr addr = ((struct sockaddr_in *)result->ai_addr)->sin_addr;
+ const char *re_ntop = inet_ntop(BOOTH_PROTO_FAMILY, &addr, ip_str, ip_size);
+ if (re_ntop == NULL) {
+ re = -1;
+ }
+ }
+
+ freeaddrinfo(result);
+ return re;
+}
+
static int read_arguments(int argc, char **argv)
{
int optchar;
char *arg1 = argv[1];
char *op = NULL;
+ char site_arg[INET_ADDRSTRLEN] = {0};
if (argc < 2 || !strcmp(arg1, "help") || !strcmp(arg1, "--help") ||
!strcmp(arg1, "-h")) {
@@ -861,7 +888,12 @@ static int read_arguments(int argc, char **argv)
case 's':
if (cl.op == OP_GRANT || cl.op == OP_REVOKE) {
- safe_copy(cl.site, optarg, sizeof(cl.ticket), "site name");
+ int re = host_convert(optarg, site_arg, INET_ADDRSTRLEN);
+ if (re == 0) {
+ safe_copy(cl.site, site_arg, sizeof(cl.ticket), "site name");
+ } else {
+ safe_copy(cl.site, optarg, sizeof(cl.ticket), "site name");
+ }
} else {
print_usage();
exit(EXIT_FAILURE);

0 comments on commit f7aea17

Please sign in to comment.