diff options
Diffstat (limited to 'libnetutils')
-rw-r--r-- | libnetutils/Android.mk | 7 | ||||
-rw-r--r-- | libnetutils/dhcp_utils.c | 15 | ||||
-rw-r--r-- | libnetutils/dhcpclient.c | 26 | ||||
-rw-r--r-- | libnetutils/dhcptool.c | 43 | ||||
-rw-r--r-- | libnetutils/ifc_utils.c | 7 | ||||
-rw-r--r-- | libnetutils/packet.c | 3 |
6 files changed, 72 insertions, 29 deletions
diff --git a/libnetutils/Android.mk b/libnetutils/Android.mk index 1f61511..2060df4 100644 --- a/libnetutils/Android.mk +++ b/libnetutils/Android.mk @@ -17,3 +17,10 @@ LOCAL_MODULE := libnetutils LOCAL_CFLAGS := -Werror include $(BUILD_SHARED_LIBRARY) + +include $(CLEAR_VARS) +LOCAL_SRC_FILES := dhcptool.c +LOCAL_SHARED_LIBRARIES := libnetutils +LOCAL_MODULE := dhcptool +LOCAL_MODULE_TAGS := debug +include $(BUILD_EXECUTABLE) diff --git a/libnetutils/dhcp_utils.c b/libnetutils/dhcp_utils.c index e1df874..70e37c6 100644 --- a/libnetutils/dhcp_utils.c +++ b/libnetutils/dhcp_utils.c @@ -72,14 +72,16 @@ static int wait_for_property(const char *name, const char *desired_value, int ma maxnaps = 1; } - while (maxnaps-- > 0) { - usleep(NAP_TIME * 1000); + while (maxnaps-- >= 0) { if (property_get(name, value, NULL)) { if (desired_value == NULL || strcmp(value, desired_value) == 0) { return 0; } } + if (maxnaps >= 0) { + usleep(NAP_TIME * 1000); + } } return -1; /* failure */ } @@ -166,14 +168,6 @@ static int fill_ip_info(const char *interface, return 0; } -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 * configuring the interface. @@ -242,7 +236,6 @@ int dhcp_do_request(const char *interface, return -1; } if (strcmp(prop_value, "ok") == 0) { - char dns_prop_name[PROPERTY_KEY_MAX]; if (fill_ip_info(interface, ipaddr, gateway, prefixLength, dns, server, lease, vendorInfo, domain, mtu) == -1) { return -1; diff --git a/libnetutils/dhcpclient.c b/libnetutils/dhcpclient.c index b58120e..240a789 100644 --- a/libnetutils/dhcpclient.c +++ b/libnetutils/dhcpclient.c @@ -150,7 +150,7 @@ static const char *dhcp_type_to_name(uint32_t type) void dump_dhcp_info(dhcp_info *info) { - char addr[20], gway[20], mask[20]; + char addr[20], gway[20]; ALOGD("--- dhcp %s (%d) ---", dhcp_type_to_name(info->type), info->type); strcpy(addr, ipaddr(info->ipaddr)); @@ -236,13 +236,13 @@ int decode_dhcp_msg(dhcp_msg *msg, int len, dhcp_info *info) #if VERBOSE -static void hex2str(char *buf, const unsigned char *array, int len) +static void hex2str(char *buf, size_t buf_size, const unsigned char *array, int len) { int i; char *cp = buf; - + char *buf_end = buf + buf_size; for (i = 0; i < len; i++) { - cp += sprintf(cp, " %02x ", array[i]); + cp += snprintf(cp, buf_end - cp, " %02x ", array[i]); } } @@ -278,7 +278,7 @@ void dump_dhcp_msg(dhcp_msg *msg, int len) ALOGD("giaddr = %s", ipaddr(msg->giaddr)); c = msg->hlen > 16 ? 16 : msg->hlen; - hex2str(buf, msg->chaddr, c); + hex2str(buf, sizeof(buf), msg->chaddr, c); ALOGD("chaddr = {%s}", buf); for (n = 0; n < 64; n++) { @@ -327,7 +327,7 @@ void dump_dhcp_msg(dhcp_msg *msg, int len) memcpy(buf, &x[2], n); buf[n] = '\0'; } else { - hex2str(buf, &x[2], optsz); + hex2str(buf, sizeof(buf), &x[2], optsz); } if (x[0] == OPT_MESSAGE_TYPE) name = dhcp_type_to_name(x[2]); @@ -353,28 +353,28 @@ static int send_message(int sock, int if_index, dhcp_msg *msg, int size) static int is_valid_reply(dhcp_msg *msg, dhcp_msg *reply, int sz) { if (sz < DHCP_MSG_FIXED_SIZE) { - if (verbose) ALOGD("netcfg: Wrong size %d != %d\n", sz, DHCP_MSG_FIXED_SIZE); + if (verbose) ALOGD("Wrong size %d != %d\n", sz, DHCP_MSG_FIXED_SIZE); return 0; } if (reply->op != OP_BOOTREPLY) { - if (verbose) ALOGD("netcfg: Wrong Op %d != %d\n", reply->op, OP_BOOTREPLY); + if (verbose) ALOGD("Wrong Op %d != %d\n", reply->op, OP_BOOTREPLY); return 0; } if (reply->xid != msg->xid) { - if (verbose) ALOGD("netcfg: Wrong Xid 0x%x != 0x%x\n", ntohl(reply->xid), - ntohl(msg->xid)); + if (verbose) ALOGD("Wrong Xid 0x%x != 0x%x\n", ntohl(reply->xid), + ntohl(msg->xid)); return 0; } if (reply->htype != msg->htype) { - if (verbose) ALOGD("netcfg: Wrong Htype %d != %d\n", reply->htype, msg->htype); + if (verbose) ALOGD("Wrong Htype %d != %d\n", reply->htype, msg->htype); return 0; } if (reply->hlen != msg->hlen) { - if (verbose) ALOGD("netcfg: Wrong Hlen %d != %d\n", reply->hlen, msg->hlen); + if (verbose) ALOGD("Wrong Hlen %d != %d\n", reply->hlen, msg->hlen); return 0; } if (memcmp(msg->chaddr, reply->chaddr, msg->hlen)) { - if (verbose) ALOGD("netcfg: Wrong chaddr %x != %x\n", *(reply->chaddr),*(msg->chaddr)); + if (verbose) ALOGD("Wrong chaddr %x != %x\n", *(reply->chaddr),*(msg->chaddr)); return 0; } return 1; diff --git a/libnetutils/dhcptool.c b/libnetutils/dhcptool.c new file mode 100644 index 0000000..352ac5e --- /dev/null +++ b/libnetutils/dhcptool.c @@ -0,0 +1,43 @@ +/* + * Copyright 2015, 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. + */ + +#include <errno.h> +#include <error.h> +#include <stdbool.h> +#include <stdlib.h> + +#include <netutils/dhcp.h> +#include <netutils/ifc.h> + +int main(int argc, char* argv[]) { + if (argc != 2) { + error(EXIT_FAILURE, 0, "usage: %s INTERFACE", argv[0]); + } + + char* interface = argv[1]; + if (ifc_init()) { + error(EXIT_FAILURE, errno, "dhcptool %s: ifc_init failed", interface); + } + + int rc = do_dhcp(interface); + if (rc) { + error(0, errno, "dhcptool %s: do_dhcp failed", interface); + } + + ifc_close(); + + return rc ? EXIT_FAILURE : EXIT_SUCCESS; +} diff --git a/libnetutils/ifc_utils.c b/libnetutils/ifc_utils.c index 913f51e..7d2a5fb 100644 --- a/libnetutils/ifc_utils.c +++ b/libnetutils/ifc_utils.c @@ -123,7 +123,7 @@ int ifc_init(void) { int ret; if (ifc_ctl_sock == -1) { - ifc_ctl_sock = socket(AF_INET, SOCK_DGRAM, 0); + ifc_ctl_sock = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); if (ifc_ctl_sock < 0) { printerr("socket() failed: %s\n", strerror(errno)); } @@ -137,7 +137,7 @@ int ifc_init(void) int ifc_init6(void) { if (ifc_ctl_sock6 == -1) { - ifc_ctl_sock6 = socket(AF_INET6, SOCK_DGRAM, 0); + ifc_ctl_sock6 = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0); if (ifc_ctl_sock6 < 0) { printerr("socket() failed: %s\n", strerror(errno)); } @@ -316,7 +316,7 @@ int ifc_act_on_address(int action, const char *name, const char *address, req.n.nlmsg_len = NLMSG_ALIGN(req.n.nlmsg_len) + RTA_LENGTH(addrlen); memcpy(RTA_DATA(rta), addr, addrlen); - s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE); + s = socket(PF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE); if (send(s, &req, req.n.nlmsg_len, 0) < 0) { close(s); return -errno; @@ -421,7 +421,6 @@ int ifc_clear_addresses(const char *name) { int ifc_set_hwaddr(const char *name, const void *ptr) { - int r; struct ifreq ifr; ifc_init_ifr(name, &ifr); diff --git a/libnetutils/packet.c b/libnetutils/packet.c index 3cdefb0..cd26d05 100644 --- a/libnetutils/packet.c +++ b/libnetutils/packet.c @@ -15,6 +15,7 @@ */ #include <stdlib.h> +#include <string.h> #include <unistd.h> #include <sys/uio.h> #include <sys/socket.h> @@ -41,7 +42,7 @@ int fatal(); int open_raw_socket(const char *ifname __attribute__((unused)), uint8_t *hwaddr, int if_index) { - int s, flag; + int s; struct sockaddr_ll bindaddr; if((s = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP))) < 0) { |