Add generic handling for hardware addresses and setup
authorMarcel Holtmann <marcel@holtmann.org>
Tue, 8 Jan 2008 11:08:23 +0000 (12:08 +0100)
committerMarcel Holtmann <marcel@holtmann.org>
Tue, 8 Jan 2008 11:08:23 +0000 (12:08 +0100)
src/Makefile.am
src/connman.h
src/iface-inet.c [new file with mode: 0644]
src/iface.c

index cd80c8d..e207525 100644 (file)
@@ -6,7 +6,7 @@ dbus_DATA = connman.conf
 sbin_PROGRAMS = connmand
 
 connmand_SOURCES = main.c connman.h manager.c plugin.c \
-                       iface.c iface-helper.c rtnl.c dhcp.c
+                       iface.c iface-helper.c iface-inet.c rtnl.c dhcp.c
 
 connmand_LDADD = @HAL_LIBS@ @GDBUS_LIBS@ @GMODULE_LIBS@
  
index 7f789fe..98d8ea8 100644 (file)
@@ -55,6 +55,9 @@ void __connman_iface_cleanup(void);
 struct connman_iface *__connman_iface_find(int index);
 void __connman_iface_list(DBusMessageIter *iter);
 
+int __connman_iface_create_identifier(struct connman_iface *iface);
+int __connman_iface_init_via_inet(struct connman_iface *iface);
+
 const char *__connman_iface_type2string(enum connman_iface_type type);
 const char *__connman_iface_state2string(enum connman_iface_state state);
 const char *__connman_iface_policy2string(enum connman_iface_policy policy);
diff --git a/src/iface-inet.c b/src/iface-inet.c
new file mode 100644 (file)
index 0000000..664d298
--- /dev/null
@@ -0,0 +1,110 @@
+/*
+ *
+ *  Connection Manager
+ *
+ *  Copyright (C) 2007  Intel Corporation. All rights reserved.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License version 2 as
+ *  published by the Free Software Foundation.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <errno.h>
+#include <unistd.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <net/if.h>
+#include <net/ethernet.h>
+
+#include "connman.h"
+
+int __connman_iface_create_identifier(struct connman_iface *iface)
+{
+       struct ifreq ifr;
+       struct ether_addr *eth;
+       int sk, err;
+
+       DBG("iface %p", iface);
+
+       sk = socket(PF_INET, SOCK_DGRAM, 0);
+       if (sk < 0)
+               return -EIO;
+
+       memset(&ifr, 0, sizeof(ifr));
+       ifr.ifr_ifindex = iface->index;
+
+       err = ioctl(sk, SIOCGIFNAME, &ifr);
+
+       if (err == 0)
+               err = ioctl(sk, SIOCGIFHWADDR, &ifr);
+
+       close(sk);
+
+       if (err < 0)
+               return -EIO;
+
+       iface->identifier = malloc(18);
+       if (iface->identifier == NULL)
+               return -ENOMEM;
+
+       eth = (void *) &ifr.ifr_hwaddr.sa_data;
+       sprintf(iface->identifier, "%02X-%02X-%02X-%02X-%02X-%02X",
+                                               eth->ether_addr_octet[0],
+                                               eth->ether_addr_octet[1],
+                                               eth->ether_addr_octet[2],
+                                               eth->ether_addr_octet[3],
+                                               eth->ether_addr_octet[4],
+                                               eth->ether_addr_octet[5]);
+
+       return 0;
+}
+
+int __connman_iface_init_via_inet(struct connman_iface *iface)
+{
+       struct ifreq ifr;
+       int sk, err;
+
+       DBG("iface %p", iface);
+
+       sk = socket(PF_INET, SOCK_DGRAM, 0);
+       if (sk < 0)
+               return -EIO;
+
+       memset(&ifr, 0, sizeof(ifr));
+       ifr.ifr_ifindex = iface->index;
+
+       err = ioctl(sk, SIOCGIFNAME, &ifr);
+
+       if (err == 0)
+               err = ioctl(sk, SIOCGIFFLAGS, &ifr);
+
+       close(sk);
+
+       if (err < 0)
+               return -EIO;
+
+       if (ifr.ifr_flags & IFF_UP)
+               iface->state = CONNMAN_IFACE_STATE_ENABLED;
+       else
+               iface->state = CONNMAN_IFACE_STATE_OFF;
+
+       if (ifr.ifr_flags & IFF_RUNNING)
+               iface->state = CONNMAN_IFACE_STATE_CARRIER;
+
+       return 0;
+}
index 1c0c882..c9d2595 100644 (file)
@@ -827,6 +827,10 @@ static int probe_device(LibHalContext *ctx,
                return -1;
        }
 
+       __connman_iface_create_identifier(iface);
+
+       __connman_iface_init_via_inet(iface);
+
        iface->driver = driver;
 
        conn = libhal_ctx_get_dbus_connection(ctx);