"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 2 | /* |
| 3 | * socket.c -- DHCP server client/server socket creation |
| 4 | * |
| 5 | * udhcp client/server |
| 6 | * Copyright (C) 1999 Matthew Ramsay <matthewr@moreton.com.au> |
| 7 | * Chris Trew <ctrew@moreton.com.au> |
| 8 | * |
| 9 | * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001 |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 24 | */ |
| 25 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 26 | #include <net/if.h> |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 27 | #include <features.h> |
Denis Vlasenko | a7189f0 | 2006-11-17 20:29:00 +0000 | [diff] [blame] | 28 | #if (__GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1) || defined _NEWLIB_VERSION |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 29 | #include <netpacket/packet.h> |
| 30 | #include <net/ethernet.h> |
| 31 | #else |
| 32 | #include <asm/types.h> |
| 33 | #include <linux/if_packet.h> |
| 34 | #include <linux/if_ether.h> |
| 35 | #endif |
| 36 | |
| 37 | #include "common.h" |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 38 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 39 | |
| 40 | int read_interface(char *interface, int *ifindex, uint32_t *addr, uint8_t *arp) |
| 41 | { |
| 42 | int fd; |
| 43 | struct ifreq ifr; |
| 44 | struct sockaddr_in *our_ip; |
| 45 | |
| 46 | memset(&ifr, 0, sizeof(struct ifreq)); |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 47 | fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW); |
| 48 | if (fd < 0) { |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 49 | bb_perror_msg("socket failed"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 50 | return -1; |
| 51 | } |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 52 | |
| 53 | ifr.ifr_addr.sa_family = AF_INET; |
| 54 | strcpy(ifr.ifr_name, interface); |
| 55 | if (addr) { |
| 56 | if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) { |
| 57 | bb_perror_msg("SIOCGIFADDR failed, is the interface up and configured?"); |
| 58 | close(fd); |
| 59 | return -1; |
| 60 | } |
| 61 | our_ip = (struct sockaddr_in *) &ifr.ifr_addr; |
| 62 | *addr = our_ip->sin_addr.s_addr; |
| 63 | DEBUG("%s (our ip) = %s", ifr.ifr_name, inet_ntoa(our_ip->sin_addr)); |
| 64 | } |
| 65 | |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 66 | if (ifindex) { |
| 67 | if (ioctl(fd, SIOCGIFINDEX, &ifr) == 0) { |
| 68 | bb_perror_msg("SIOCGIFINDEX failed"); |
| 69 | close(fd); |
| 70 | return -1; |
| 71 | } |
| 72 | DEBUG("adapter index %d", ifr.ifr_ifindex); |
| 73 | *ifindex = ifr.ifr_ifindex; |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 76 | if (arp) { |
| 77 | if (ioctl(fd, SIOCGIFHWADDR, &ifr) != 0) { |
| 78 | bb_perror_msg("SIOCGIFHWADDR failed"); |
| 79 | close(fd); |
| 80 | return -1; |
| 81 | } |
| 82 | memcpy(arp, ifr.ifr_hwaddr.sa_data, 6); |
| 83 | DEBUG("adapter hardware address %02x:%02x:%02x:%02x:%02x:%02x", |
| 84 | arp[0], arp[1], arp[2], arp[3], arp[4], arp[5]); |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | |
| 91 | int listen_socket(uint32_t ip, int port, char *inf) |
| 92 | { |
| 93 | struct ifreq interface; |
| 94 | int fd; |
| 95 | struct sockaddr_in addr; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 96 | |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 97 | DEBUG("Opening listen socket on 0x%08x:%d %s", ip, port, inf); |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 98 | fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
| 99 | if (fd < 0) { |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 100 | bb_perror_msg("socket"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 101 | return -1; |
| 102 | } |
| 103 | |
| 104 | memset(&addr, 0, sizeof(addr)); |
| 105 | addr.sin_family = AF_INET; |
| 106 | addr.sin_port = htons(port); |
| 107 | addr.sin_addr.s_addr = ip; |
| 108 | |
Denis Vlasenko | 48237b0 | 2006-11-22 23:22:06 +0000 | [diff] [blame^] | 109 | if (setsockopt_reuseaddr(fd) == -1) { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 110 | close(fd); |
| 111 | return -1; |
| 112 | } |
Denis Vlasenko | 48237b0 | 2006-11-22 23:22:06 +0000 | [diff] [blame^] | 113 | if (setsockopt_broadcast(fd) == -1) { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 114 | close(fd); |
| 115 | return -1; |
| 116 | } |
| 117 | |
| 118 | strncpy(interface.ifr_name, inf, IFNAMSIZ); |
Denis Vlasenko | 48237b0 | 2006-11-22 23:22:06 +0000 | [diff] [blame^] | 119 | if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, (char *)&interface, sizeof(interface)) < 0) { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 120 | close(fd); |
| 121 | return -1; |
| 122 | } |
| 123 | |
| 124 | if (bind(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr)) == -1) { |
| 125 | close(fd); |
| 126 | return -1; |
| 127 | } |
| 128 | |
| 129 | return fd; |
| 130 | } |