Skip to content
Snippets Groups Projects
Commit 58b64ace authored by Jean-Baptiste Queru's avatar Jean-Baptiste Queru
Browse files

Revert "- creates proper ifc.h and dhcp.h headers for libnetutils"

This reverts commit c88e09cb.
parent 838336fa
Branches
Tags
No related merge requests found
/*
* Copyright 2008, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _NETUTILS_IFC_H_
#define _NETUTILS_IFC_H_
#include <sys/cdefs.h>
#include <arpa/inet.h>
__BEGIN_DECLS
extern int ifc_init(void);
extern void ifc_close(void);
extern int ifc_get_ifindex(const char *name, int *if_indexp);
extern int ifc_get_hwaddr(const char *name, void *ptr);
extern int ifc_up(const char *name);
extern int ifc_down(const char *name);
extern int ifc_enable(const char *ifname);
extern int ifc_disable(const char *ifname);
extern int ifc_reset_connections(const char *ifname);
extern int ifc_set_addr(const char *name, in_addr_t addr);
extern int ifc_set_mask(const char *name, in_addr_t mask);
extern int ifc_set_hwaddr(const char *name, const void *ptr);
extern int ifc_add_host_route(const char *name, in_addr_t addr);
extern int ifc_remove_host_routes(const char *name);
extern int ifc_get_default_route(const char *ifname);
extern int ifc_set_default_route(const char *ifname, in_addr_t gateway);
extern int ifc_create_default_route(const char *name, in_addr_t addr);
extern int ifc_remove_default_route(const char *ifname);
extern int ifc_get_info(const char *name, in_addr_t *addr, in_addr_t *mask,
in_addr_t *flags);
extern int ifc_configure(const char *ifname, in_addr_t address,
in_addr_t netmask, in_addr_t gateway,
in_addr_t dns1, in_addr_t dns2);
__END_DECLS
#endif /* _NETUTILS_IFC_H_ */
...@@ -115,14 +115,6 @@ static void fill_ip_info(const char *interface, ...@@ -115,14 +115,6 @@ static void fill_ip_info(const char *interface,
} }
} }
static const char *ipaddr_to_string(in_addr_t addr)
{
struct in_addr in_addr;
in_addr.s_addr = addr;
return inet_ntoa(in_addr);
}
/* /*
* Start the dhcp client daemon, and wait for it to finish * Start the dhcp client daemon, and wait for it to finish
* configuring the interface. * configuring the interface.
...@@ -173,13 +165,7 @@ int dhcp_do_request(const char *interface, ...@@ -173,13 +165,7 @@ int dhcp_do_request(const char *interface,
return -1; return -1;
} }
if (strcmp(prop_value, "ok") == 0) { if (strcmp(prop_value, "ok") == 0) {
char dns_prop_name[PROPERTY_KEY_MAX];
fill_ip_info(interface, ipaddr, gateway, mask, dns1, dns2, server, lease); fill_ip_info(interface, ipaddr, gateway, mask, dns1, dns2, server, lease);
/* copy the dhcp.XXX.dns properties to net.XXX.dns */
snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns1", interface);
property_set(dns_prop_name, *dns1 ? ipaddr_to_string(*dns1) : "");
snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns2", interface);
property_set(dns_prop_name, *dns2 ? ipaddr_to_string(*dns2) : "");
return 0; return 0;
} else { } else {
snprintf(errmsg, sizeof(errmsg), "DHCP result was %s", prop_value); snprintf(errmsg, sizeof(errmsg), "DHCP result was %s", prop_value);
......
...@@ -36,8 +36,8 @@ ...@@ -36,8 +36,8 @@
#include <dirent.h> #include <dirent.h>
#include <netutils/ifc.h>
#include "dhcpmsg.h" #include "dhcpmsg.h"
#include "ifc_utils.h"
#include "packet.h" #include "packet.h"
#define VERBOSE 2 #define VERBOSE 2
...@@ -85,12 +85,16 @@ int fatal(const char *reason) ...@@ -85,12 +85,16 @@ int fatal(const char *reason)
// exit(1); // exit(1);
} }
const char *ipaddr(in_addr_t addr) const char *ipaddr(uint32_t addr)
{ {
struct in_addr in_addr; static char buf[32];
in_addr.s_addr = addr; sprintf(buf,"%d.%d.%d.%d",
return inet_ntoa(in_addr); addr & 255,
((addr >> 8) & 255),
((addr >> 16) & 255),
(addr >> 24));
return buf;
} }
typedef struct dhcp_info dhcp_info; typedef struct dhcp_info dhcp_info;
...@@ -124,11 +128,31 @@ void get_dhcp_info(uint32_t *ipaddr, uint32_t *gateway, uint32_t *mask, ...@@ -124,11 +128,31 @@ void get_dhcp_info(uint32_t *ipaddr, uint32_t *gateway, uint32_t *mask,
*lease = last_good_info.lease; *lease = last_good_info.lease;
} }
static int dhcp_configure(const char *ifname, dhcp_info *info) static int ifc_configure(const char *ifname, dhcp_info *info)
{ {
char dns_prop_name[PROPERTY_KEY_MAX];
if (ifc_set_addr(ifname, info->ipaddr)) {
printerr("failed to set ipaddr %s: %s\n", ipaddr(info->ipaddr), strerror(errno));
return -1;
}
if (ifc_set_mask(ifname, info->netmask)) {
printerr("failed to set netmask %s: %s\n", ipaddr(info->netmask), strerror(errno));
return -1;
}
if (ifc_create_default_route(ifname, info->gateway)) {
printerr("failed to set default route %s: %s\n", ipaddr(info->gateway), strerror(errno));
return -1;
}
snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns1", ifname);
property_set(dns_prop_name, info->dns1 ? ipaddr(info->dns1) : "");
snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns2", ifname);
property_set(dns_prop_name, info->dns2 ? ipaddr(info->dns2) : "");
last_good_info = *info; last_good_info = *info;
return ifc_configure(ifname, info->ipaddr, info->netmask, info->gateway,
info->dns1, info->dns2); return 0;
} }
static const char *dhcp_type_to_name(uint32_t type) static const char *dhcp_type_to_name(uint32_t type)
...@@ -425,7 +449,7 @@ int dhcp_init_ifc(const char *ifname) ...@@ -425,7 +449,7 @@ int dhcp_init_ifc(const char *ifname)
printerr("timed out\n"); printerr("timed out\n");
if ( info.type == DHCPOFFER ) { if ( info.type == DHCPOFFER ) {
printerr("no acknowledgement from DHCP server\nconfiguring %s with offered parameters\n", ifname); printerr("no acknowledgement from DHCP server\nconfiguring %s with offered parameters\n", ifname);
return dhcp_configure(ifname, &info); return ifc_configure(ifname, &info);
} }
errno = ETIME; errno = ETIME;
close(s); close(s);
...@@ -506,7 +530,7 @@ int dhcp_init_ifc(const char *ifname) ...@@ -506,7 +530,7 @@ int dhcp_init_ifc(const char *ifname)
if (info.type == DHCPACK) { if (info.type == DHCPACK) {
printerr("configuring %s\n", ifname); printerr("configuring %s\n", ifname);
close(s); close(s);
return dhcp_configure(ifname, &info); return ifc_configure(ifname, &info);
} else if (info.type == DHCPNAK) { } else if (info.type == DHCPNAK) {
printerr("configuration request denied\n"); printerr("configuration request denied\n");
close(s); close(s);
......
...@@ -27,8 +27,6 @@ ...@@ -27,8 +27,6 @@
#include <arpa/inet.h> #include <arpa/inet.h>
#include <linux/if.h> #include <linux/if.h>
#include <linux/if_ether.h>
#include <linux/if_arp.h>
#include <linux/sockios.h> #include <linux/sockios.h>
#include <linux/route.h> #include <linux/route.h>
#include <linux/wireless.h> #include <linux/wireless.h>
...@@ -47,7 +45,7 @@ ...@@ -47,7 +45,7 @@
static int ifc_ctl_sock = -1; static int ifc_ctl_sock = -1;
void printerr(char *fmt, ...); void printerr(char *fmt, ...);
static const char *ipaddr_to_string(in_addr_t addr) static const char *ipaddr_to_string(uint32_t addr)
{ {
struct in_addr in_addr; struct in_addr in_addr;
...@@ -90,7 +88,7 @@ int ifc_get_hwaddr(const char *name, void *ptr) ...@@ -90,7 +88,7 @@ int ifc_get_hwaddr(const char *name, void *ptr)
r = ioctl(ifc_ctl_sock, SIOCGIFHWADDR, &ifr); r = ioctl(ifc_ctl_sock, SIOCGIFHWADDR, &ifr);
if(r < 0) return -1; if(r < 0) return -1;
memcpy(ptr, &ifr.ifr_hwaddr.sa_data, ETH_ALEN); memcpy(ptr, &ifr.ifr_hwaddr.sa_data, 6);
return 0; return 0;
} }
...@@ -145,17 +143,6 @@ int ifc_set_addr(const char *name, in_addr_t addr) ...@@ -145,17 +143,6 @@ int ifc_set_addr(const char *name, in_addr_t addr)
return ioctl(ifc_ctl_sock, SIOCSIFADDR, &ifr); return ioctl(ifc_ctl_sock, SIOCSIFADDR, &ifr);
} }
int ifc_set_hwaddr(const char *name, const void *ptr)
{
int r;
struct ifreq ifr;
ifc_init_ifr(name, &ifr);
ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
memcpy(&ifr.ifr_hwaddr.sa_data, ptr, ETH_ALEN);
return ioctl(ifc_ctl_sock, SIOCSIFHWADDR, &ifr);
}
int ifc_set_mask(const char *name, in_addr_t mask) int ifc_set_mask(const char *name, in_addr_t mask)
{ {
struct ifreq ifr; struct ifreq ifr;
...@@ -442,9 +429,9 @@ ifc_configure(const char *ifname, ...@@ -442,9 +429,9 @@ ifc_configure(const char *ifname,
ifc_close(); ifc_close();
snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns1", ifname); snprintf(dns_prop_name, sizeof(dns_prop_name), "dhcp.%s.dns1", ifname);
property_set(dns_prop_name, dns1 ? ipaddr_to_string(dns1) : ""); property_set(dns_prop_name, dns1 ? ipaddr_to_string(dns1) : "");
snprintf(dns_prop_name, sizeof(dns_prop_name), "net.%s.dns2", ifname); snprintf(dns_prop_name, sizeof(dns_prop_name), "dhcp.%s.dns2", ifname);
property_set(dns_prop_name, dns2 ? ipaddr_to_string(dns2) : ""); property_set(dns_prop_name, dns2 ? ipaddr_to_string(dns2) : "");
return 0; return 0;
......
/* /*
* Copyright 2010, The Android Open Source Project * Copyright 2008, The Android Open Source Project
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -14,27 +14,22 @@ ...@@ -14,27 +14,22 @@
* limitations under the License. * limitations under the License.
*/ */
#ifndef _NETUTILS_DHCP_H_ #ifndef _IFC_UTILS_H_
#define _NETUTILS_DHCP_H_ #define _IFC_UTILS_H_
#include <sys/cdefs.h> int ifc_init(void);
#include <arpa/inet.h>
__BEGIN_DECLS int ifc_get_ifindex(const char *name, int *if_indexp);
int ifc_get_hwaddr(const char *name, void *ptr);
extern int do_dhcp(char *iname); int ifc_up(const char *name);
extern int dhcp_do_request(const char *ifname, int ifc_down(const char *name);
in_addr_t *ipaddr,
in_addr_t *gateway,
in_addr_t *mask,
in_addr_t *dns1,
in_addr_t *dns2,
in_addr_t *server,
uint32_t *lease);
extern int dhcp_stop(const char *ifname);
extern int dhcp_release_lease(const char *ifname);
extern char *dhcp_get_errmsg();
__END_DECLS int ifc_set_addr(const char *name, unsigned addr);
int ifc_set_mask(const char *name, unsigned mask);
#endif /* _NETUTILS_DHCP_H_ */ int ifc_create_default_route(const char *name, unsigned addr);
int ifc_get_info(const char *name, unsigned *addr, unsigned *mask, unsigned *flags);
#endif
...@@ -19,13 +19,17 @@ ...@@ -19,13 +19,17 @@
#include <stdlib.h> #include <stdlib.h>
#include <errno.h> #include <errno.h>
#include <dirent.h> #include <dirent.h>
#include <netinet/ether.h>
#include <netutils/ifc.h>
#include <netutils/dhcp.h>
static int verbose = 0; static int verbose = 0;
int ifc_init();
void ifc_close();
int ifc_up(char *iname);
int ifc_down(char *iname);
int ifc_remove_host_routes(char *iname);
int ifc_remove_default_route(char *iname);
int ifc_get_info(const char *name, unsigned *addr, unsigned *mask, unsigned *flags);
int do_dhcp(char *iname);
void die(const char *reason) void die(const char *reason)
{ {
...@@ -33,12 +37,16 @@ void die(const char *reason) ...@@ -33,12 +37,16 @@ void die(const char *reason)
exit(1); exit(1);
} }
const char *ipaddr(in_addr_t addr) const char *ipaddr(unsigned addr)
{ {
struct in_addr in_addr; static char buf[32];
in_addr.s_addr = addr; sprintf(buf,"%d.%d.%d.%d",
return inet_ntoa(in_addr); addr & 255,
((addr >> 8) & 255),
((addr >> 16) & 255),
(addr >> 24));
return buf;
} }
void usage(void) void usage(void)
...@@ -78,15 +86,6 @@ int dump_interfaces(void) ...@@ -78,15 +86,6 @@ int dump_interfaces(void)
return 0; return 0;
} }
int set_hwaddr(const char *name, const char *asc) {
struct ether_addr *addr = ether_aton(asc);
if (!addr) {
printf("Failed to parse '%s'\n", asc);
return -1;
}
return ifc_set_hwaddr(name, addr->ether_addr_octet);
}
struct struct
{ {
const char *name; const char *name;
...@@ -98,7 +97,6 @@ struct ...@@ -98,7 +97,6 @@ struct
{ "down", 1, ifc_down }, { "down", 1, ifc_down },
{ "flhosts", 1, ifc_remove_host_routes }, { "flhosts", 1, ifc_remove_host_routes },
{ "deldefault", 1, ifc_remove_default_route }, { "deldefault", 1, ifc_remove_default_route },
{ "hwaddr", 2, set_hwaddr },
{ 0, 0, 0 }, { 0, 0, 0 },
}; };
......
...@@ -27,15 +27,35 @@ ...@@ -27,15 +27,35 @@
#include <sysutils/ServiceManager.h> #include <sysutils/ServiceManager.h>
#include <netutils/ifc.h>
#include <netutils/dhcp.h>
#include "DhcpClient.h" #include "DhcpClient.h"
#include "DhcpState.h" #include "DhcpState.h"
#include "DhcpListener.h" #include "DhcpListener.h"
#include "IDhcpEventHandlers.h" #include "IDhcpEventHandlers.h"
#include "Controller.h" #include "Controller.h"
extern "C" {
int ifc_disable(const char *ifname);
int ifc_add_host_route(const char *ifname, uint32_t addr);
int ifc_remove_host_routes(const char *ifname);
int ifc_set_default_route(const char *ifname, uint32_t gateway);
int ifc_get_default_route(const char *ifname);
int ifc_remove_default_route(const char *ifname);
int ifc_reset_connections(const char *ifname);
int ifc_configure(const char *ifname, in_addr_t ipaddr, in_addr_t netmask, in_addr_t gateway, in_addr_t dns1, in_addr_t dns2);
int dhcp_do_request(const char *ifname,
in_addr_t *ipaddr,
in_addr_t *gateway,
in_addr_t *mask,
in_addr_t *dns1,
in_addr_t *dns2,
in_addr_t *server,
uint32_t *lease);
int dhcp_stop(const char *ifname);
int dhcp_release_lease(const char *ifname);
char *dhcp_get_errmsg();
}
DhcpClient::DhcpClient(IDhcpEventHandlers *handlers) : DhcpClient::DhcpClient(IDhcpEventHandlers *handlers) :
mState(DhcpState::INIT), mHandlers(handlers) { mState(DhcpState::INIT), mHandlers(handlers) {
mServiceManager = new ServiceManager(); mServiceManager = new ServiceManager();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment