[systemd] Obtain the address from the directly from the hostname.
authorŁukasz Stelmach <l.stelmach@samsung.com>
Thu, 17 Jan 2013 14:51:12 +0000 (15:51 +0100)
committerŁukasz Stelmach <l.stelmach@samsung.com>
Tue, 22 Jan 2013 09:31:53 +0000 (10:31 +0100)
systemd does not set HOSTNAME environment variable. Use a more direct
way to get the hostname and try to resolve it if its not numerical IPv4
address. If the hostname is not numerical appropriate entry in /etc/hosts
should be provided.

Change-Id: Ieb1fa80cc8a16b196409c7c14efffb55caa06cde
Signed-off-by: Łukasz Stelmach <l.stelmach@samsung.com>
include/emuld_common.h
src/emuld.c

index bfcc527933feb02a6ca396337694321d5908197c..5e96735e8966e82dc32525682be47600915b3f28 100644 (file)
@@ -30,6 +30,8 @@
 #include <string.h>
 #include <assert.h>
 #include <errno.h>
+#include <netdb.h>
+#include <unistd.h>
 
 // define group id
 // return value to the injector
index 4670b0bcea7f55f8c1b666f5461c9d88347eada4..4bb60e6b233b1ca81ca5dce086a6232f53f89f5c 100644 (file)
@@ -1,4 +1,4 @@
-/*
+/* -*- Mode: C; c-basic-offset: 8; indent-tabs-mode: t -*-
  * emulator-daemon
  *
  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
@@ -449,39 +449,71 @@ int parse_val(char *buff, unsigned char data, char *parsbuf)
        return 0;
 }
 
+#define STR_HELPER(x) #x
+#define STR(x) STR_HELPER(x)
+
 void udp_init(void)
 {
+       char emul_ip[HOST_NAME_MAX+1];
+       struct addrinfo *res;
+       struct addrinfo hints;
+
        LOG("start");
-       char* emul_ip = getenv("HOSTNAME");
-       if(emul_ip == NULL)
+
+       memset(emul_ip, 0, sizeof(emul_ip));
+       if (gethostname(emul_ip, sizeof(emul_ip)) < 0)
+       {
+               LOG("gethostname(): %s", strerror(errno));
+               assert(0);
+       }
+
+       memset(&hints, 0, sizeof(hints));
+       hints.ai_family=AF_INET;
+       hints.ai_socktype=SOCK_DGRAM;
+       hints.ai_protocol=IPPROTO_UDP;
+
+       if (getaddrinfo(emul_ip, STR(SENSORD_PORT), &hints, &res) != 0)
        {
-               LOG("emul_ip is null");
+               LOG("getaddrinfo(sensord): %s", strerror(errno));
                assert(0);
        }
 
-       if ((uSensordFd=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1){
+       if ((uSensordFd=socket(res->ai_family, res->ai_socktype, res->ai_protocol))==-1){
                fprintf(stderr, "socket error!\n");
        }
 
+       if (res->ai_addrlen > sizeof(si_sensord_other))
+       {
+               LOG("sockaddr structure too big");
+               assert(0);
+       }
        memset((char *) &si_sensord_other, 0, sizeof(si_sensord_other));
-       si_sensord_other.sin_family = AF_INET;
-       si_sensord_other.sin_port = htons(sensord_port);
-       if (inet_aton(emul_ip, &si_sensord_other.sin_addr)==0) {
-               fprintf(stderr, "inet_aton() failed\n");
+       memcpy((char *) &si_sensord_other, res->ai_addr, res->ai_addrlen);
+       freeaddrinfo(res);
+
+       if (getaddrinfo(emul_ip, STR(GPSD_PORT), &hints, &res) != 0)
+       {
+               LOG("getaddrinfo(gpsd): %s", strerror(errno));
+               assert(0);
        }
 
-       if ((uGpsdFd=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1){
+       if ((uGpsdFd=socket(res->ai_family, res->ai_socktype, res->ai_protocol))==-1){
                fprintf(stderr, "socket error!\n");
        }
 
-       memset((char *) &si_gpsd_other, 0, sizeof(si_gpsd_other));
-       si_gpsd_other.sin_family = AF_INET;
-       si_gpsd_other.sin_port = htons(gpsd_port);
-       if (inet_aton(emul_ip, &si_gpsd_other.sin_addr)==0) {
-               fprintf(stderr, "inet_aton() failed\n");
+       if (res->ai_addrlen > sizeof(si_gpsd_other))
+       {
+               LOG("sockaddr structure too big");
+               assert(0);
        }
+       memset((char *) &si_gpsd_other, 0, sizeof(si_gpsd_other));
+       memcpy((char *) &si_gpsd_other, res->ai_addr, res->ai_addrlen);
+       freeaddrinfo(res);
 }
 
+#undef STR_HELPER
+#undef STR
+
 int recv_data(int event_fd, char** r_databuf, int size)
 {
        int recvd_size = 0;