"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 | |
| 26 | #include <sys/types.h> |
| 27 | #include <sys/socket.h> |
| 28 | #include <sys/ioctl.h> |
| 29 | #include <netinet/in.h> |
| 30 | #include <unistd.h> |
| 31 | #include <string.h> |
| 32 | #include <arpa/inet.h> |
| 33 | #include <net/if.h> |
| 34 | #include <errno.h> |
| 35 | #include <features.h> |
| 36 | #if (__GLIBC__ >= 2 && __GLIBC_MINOR >= 1) || defined _NEWLIB_VERSION |
| 37 | #include <netpacket/packet.h> |
| 38 | #include <net/ethernet.h> |
| 39 | #else |
| 40 | #include <asm/types.h> |
| 41 | #include <linux/if_packet.h> |
| 42 | #include <linux/if_ether.h> |
| 43 | #endif |
| 44 | |
| 45 | #include "common.h" |
| 46 | #include "socket.h" |
| 47 | |
| 48 | int read_interface(char *interface, int *ifindex, uint32_t *addr, uint8_t *arp) |
| 49 | { |
| 50 | int fd; |
| 51 | struct ifreq ifr; |
| 52 | struct sockaddr_in *our_ip; |
| 53 | |
| 54 | memset(&ifr, 0, sizeof(struct ifreq)); |
| 55 | if((fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) >= 0) { |
| 56 | ifr.ifr_addr.sa_family = AF_INET; |
| 57 | strcpy(ifr.ifr_name, interface); |
| 58 | |
| 59 | if (addr) { |
| 60 | if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) { |
| 61 | our_ip = (struct sockaddr_in *) &ifr.ifr_addr; |
| 62 | *addr = our_ip->sin_addr.s_addr; |
| 63 | DEBUG(LOG_INFO, "%s (our ip) = %s", ifr.ifr_name, inet_ntoa(our_ip->sin_addr)); |
| 64 | } else { |
| 65 | LOG(LOG_ERR, "SIOCGIFADDR failed, is the interface up and configured?: %m"); |
| 66 | close(fd); |
| 67 | return -1; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | if (ioctl(fd, SIOCGIFINDEX, &ifr) == 0) { |
| 72 | DEBUG(LOG_INFO, "adapter index %d", ifr.ifr_ifindex); |
| 73 | *ifindex = ifr.ifr_ifindex; |
| 74 | } else { |
| 75 | LOG(LOG_ERR, "SIOCGIFINDEX failed!: %m"); |
| 76 | close(fd); |
| 77 | return -1; |
| 78 | } |
| 79 | if (ioctl(fd, SIOCGIFHWADDR, &ifr) == 0) { |
| 80 | memcpy(arp, ifr.ifr_hwaddr.sa_data, 6); |
| 81 | DEBUG(LOG_INFO, "adapter hardware address %02x:%02x:%02x:%02x:%02x:%02x", |
| 82 | arp[0], arp[1], arp[2], arp[3], arp[4], arp[5]); |
| 83 | } else { |
| 84 | LOG(LOG_ERR, "SIOCGIFHWADDR failed!: %m"); |
| 85 | close(fd); |
| 86 | return -1; |
| 87 | } |
| 88 | } else { |
| 89 | LOG(LOG_ERR, "socket failed!: %m"); |
| 90 | return -1; |
| 91 | } |
| 92 | close(fd); |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | |
| 97 | int listen_socket(uint32_t ip, int port, char *inf) |
| 98 | { |
| 99 | struct ifreq interface; |
| 100 | int fd; |
| 101 | struct sockaddr_in addr; |
| 102 | int n = 1; |
| 103 | |
| 104 | DEBUG(LOG_INFO, "Opening listen socket on 0x%08x:%d %s", ip, port, inf); |
| 105 | if ((fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { |
| 106 | DEBUG(LOG_ERR, "socket call failed: %m"); |
| 107 | return -1; |
| 108 | } |
| 109 | |
| 110 | memset(&addr, 0, sizeof(addr)); |
| 111 | addr.sin_family = AF_INET; |
| 112 | addr.sin_port = htons(port); |
| 113 | addr.sin_addr.s_addr = ip; |
| 114 | |
| 115 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &n, sizeof(n)) == -1) { |
| 116 | close(fd); |
| 117 | return -1; |
| 118 | } |
| 119 | if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, (char *) &n, sizeof(n)) == -1) { |
| 120 | close(fd); |
| 121 | return -1; |
| 122 | } |
| 123 | |
| 124 | strncpy(interface.ifr_name, inf, IFNAMSIZ); |
| 125 | if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,(char *)&interface, sizeof(interface)) < 0) { |
| 126 | close(fd); |
| 127 | return -1; |
| 128 | } |
| 129 | |
| 130 | if (bind(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr)) == -1) { |
| 131 | close(fd); |
| 132 | return -1; |
| 133 | } |
| 134 | |
| 135 | return fd; |
| 136 | } |