[1765] | 1 | /* vi: set sw=4 ts=4: */
|
---|
[821] | 2 | /*
|
---|
[2725] | 3 | * DHCP server client/server socket creation
|
---|
[821] | 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 | */
|
---|
[3232] | 25 | #include "common.h"
|
---|
[821] | 26 | #include <net/if.h>
|
---|
| 27 |
|
---|
[2725] | 28 | int FAST_FUNC udhcp_read_interface(const char *interface, int *ifindex, uint32_t *nip, uint8_t *mac)
|
---|
[821] | 29 | {
|
---|
[3232] | 30 | /* char buffer instead of bona-fide struct avoids aliasing warning */
|
---|
| 31 | char ifr_buf[sizeof(struct ifreq)];
|
---|
| 32 | struct ifreq *const ifr = (void *)ifr_buf;
|
---|
| 33 |
|
---|
[821] | 34 | int fd;
|
---|
| 35 | struct sockaddr_in *our_ip;
|
---|
| 36 |
|
---|
[3232] | 37 | memset(ifr, 0, sizeof(*ifr));
|
---|
[1765] | 38 | fd = xsocket(AF_INET, SOCK_RAW, IPPROTO_RAW);
|
---|
[821] | 39 |
|
---|
[3232] | 40 | ifr->ifr_addr.sa_family = AF_INET;
|
---|
| 41 | strncpy_IFNAMSIZ(ifr->ifr_name, interface);
|
---|
[2725] | 42 | if (nip) {
|
---|
[3232] | 43 | if (ioctl_or_perror(fd, SIOCGIFADDR, ifr,
|
---|
[1765] | 44 | "is interface %s up and configured?", interface)
|
---|
| 45 | ) {
|
---|
| 46 | close(fd);
|
---|
| 47 | return -1;
|
---|
[821] | 48 | }
|
---|
[3232] | 49 | our_ip = (struct sockaddr_in *) &ifr->ifr_addr;
|
---|
[2725] | 50 | *nip = our_ip->sin_addr.s_addr;
|
---|
| 51 | log1("IP %s", inet_ntoa(our_ip->sin_addr));
|
---|
[1765] | 52 | }
|
---|
[821] | 53 |
|
---|
[1765] | 54 | if (ifindex) {
|
---|
[3232] | 55 | if (ioctl_or_warn(fd, SIOCGIFINDEX, ifr) != 0) {
|
---|
[821] | 56 | close(fd);
|
---|
| 57 | return -1;
|
---|
| 58 | }
|
---|
[3232] | 59 | log1("Adapter index %d", ifr->ifr_ifindex);
|
---|
| 60 | *ifindex = ifr->ifr_ifindex;
|
---|
[1765] | 61 | }
|
---|
| 62 |
|
---|
[2725] | 63 | if (mac) {
|
---|
[3232] | 64 | if (ioctl_or_warn(fd, SIOCGIFHWADDR, ifr) != 0) {
|
---|
[821] | 65 | close(fd);
|
---|
| 66 | return -1;
|
---|
| 67 | }
|
---|
[3232] | 68 | memcpy(mac, ifr->ifr_hwaddr.sa_data, 6);
|
---|
[2725] | 69 | log1("MAC %02x:%02x:%02x:%02x:%02x:%02x",
|
---|
| 70 | mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
---|
[821] | 71 | }
|
---|
[1765] | 72 |
|
---|
[821] | 73 | close(fd);
|
---|
| 74 | return 0;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[1765] | 77 | /* 1. None of the callers expects it to ever fail */
|
---|
| 78 | /* 2. ip was always INADDR_ANY */
|
---|
[2725] | 79 | int FAST_FUNC udhcp_listen_socket(/*uint32_t ip,*/ int port, const char *inf)
|
---|
[821] | 80 | {
|
---|
[1765] | 81 | int fd;
|
---|
[821] | 82 | struct sockaddr_in addr;
|
---|
[3232] | 83 | char *colon;
|
---|
[821] | 84 |
|
---|
[2725] | 85 | log1("Opening listen socket on *:%d %s", port, inf);
|
---|
[1765] | 86 | fd = xsocket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
---|
[821] | 87 |
|
---|
[1765] | 88 | setsockopt_reuseaddr(fd);
|
---|
| 89 | if (setsockopt_broadcast(fd) == -1)
|
---|
| 90 | bb_perror_msg_and_die("SO_BROADCAST");
|
---|
| 91 |
|
---|
[3232] | 92 | /* SO_BINDTODEVICE doesn't work on ethernet aliases (ethN:M) */
|
---|
| 93 | colon = strrchr(inf, ':');
|
---|
| 94 | if (colon)
|
---|
| 95 | *colon = '\0';
|
---|
| 96 |
|
---|
[2725] | 97 | if (setsockopt_bindtodevice(fd, inf))
|
---|
| 98 | xfunc_die(); /* warning is already printed */
|
---|
[1765] | 99 |
|
---|
[3232] | 100 | if (colon)
|
---|
| 101 | *colon = ':';
|
---|
| 102 |
|
---|
[821] | 103 | memset(&addr, 0, sizeof(addr));
|
---|
| 104 | addr.sin_family = AF_INET;
|
---|
| 105 | addr.sin_port = htons(port);
|
---|
[1765] | 106 | /* addr.sin_addr.s_addr = ip; - all-zeros is INADDR_ANY */
|
---|
| 107 | xbind(fd, (struct sockaddr *)&addr, sizeof(addr));
|
---|
[821] | 108 |
|
---|
| 109 | return fd;
|
---|
| 110 | }
|
---|