diff options
author | Marcel Holtmann <marcel@holtmann.org> | 2008-01-08 12:08:23 +0100 |
---|---|---|
committer | Marcel Holtmann <marcel@holtmann.org> | 2008-01-08 12:08:23 +0100 |
commit | 588882dd73aa475dd262d117c71dcabcde5e6d9c (patch) | |
tree | 917c2957eb48b3099bfbaccf4f28773bf9c0cd36 /src/iface-inet.c | |
parent | f4292c41c31dc7d0bb9fb3ed6061b6e9ef6ac484 (diff) | |
download | connman-588882dd73aa475dd262d117c71dcabcde5e6d9c.tar.gz connman-588882dd73aa475dd262d117c71dcabcde5e6d9c.tar.bz2 connman-588882dd73aa475dd262d117c71dcabcde5e6d9c.zip |
Add generic handling for hardware addresses and setup
Diffstat (limited to 'src/iface-inet.c')
-rw-r--r-- | src/iface-inet.c | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/src/iface-inet.c b/src/iface-inet.c new file mode 100644 index 00000000..664d2980 --- /dev/null +++ b/src/iface-inet.c @@ -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; +} |