Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 1 | /* ip.access nanoBTS configuration tool */ |
| 2 | |
| 3 | /* (C) 2009-2010 by Harald Welte <laforge@gnumonks.org> |
| 4 | * All Rights Reserved |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU Affero General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU Affero General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Affero General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | */ |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 20 | |
| 21 | #include <unistd.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <sys/socket.h> |
| 26 | #include <netinet/in.h> |
| 27 | #include <arpa/inet.h> |
| 28 | |
| 29 | |
Pablo Neira Ayuso | dd5fff4 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 30 | #include <osmocom/core/select.h> |
| 31 | #include <osmocom/core/timer.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 32 | #include <openbsc/ipaccess.h> |
| 33 | #include <openbsc/gsm_data.h> |
| 34 | |
Harald Welte | 84acf05 | 2009-08-08 11:47:20 +0200 | [diff] [blame] | 35 | static int udp_sock(const char *ifname) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 36 | { |
| 37 | int fd, rc, bc = 1; |
| 38 | struct sockaddr_in sa; |
| 39 | |
| 40 | fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
| 41 | if (fd < 0) |
| 42 | return fd; |
| 43 | |
Harald Welte | 84acf05 | 2009-08-08 11:47:20 +0200 | [diff] [blame] | 44 | if (ifname) { |
| 45 | rc = setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, ifname, |
| 46 | strlen(ifname)); |
| 47 | if (rc < 0) |
| 48 | goto err; |
| 49 | } |
| 50 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 51 | sa.sin_family = AF_INET; |
| 52 | sa.sin_port = htons(3006); |
| 53 | sa.sin_addr.s_addr = INADDR_ANY; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 54 | |
| 55 | rc = bind(fd, (struct sockaddr *)&sa, sizeof(sa)); |
| 56 | if (rc < 0) |
| 57 | goto err; |
| 58 | |
| 59 | rc = setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &bc, sizeof(bc)); |
| 60 | if (rc < 0) |
| 61 | goto err; |
| 62 | |
| 63 | #if 0 |
Harald Welte | 71e49a0 | 2009-08-08 12:14:53 +0200 | [diff] [blame] | 64 | /* we cannot bind, since the response packets don't come from |
| 65 | * the broadcast address */ |
| 66 | sa.sin_family = AF_INET; |
| 67 | sa.sin_port = htons(3006); |
| 68 | inet_aton("255.255.255.255", &sa.sin_addr); |
| 69 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 70 | rc = connect(fd, (struct sockaddr *)&sa, sizeof(sa)); |
| 71 | if (rc < 0) |
| 72 | goto err; |
Holger Hans Peter Freyther | 7113514 | 2010-03-29 08:47:44 +0200 | [diff] [blame] | 73 | #endif |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 74 | return fd; |
| 75 | |
| 76 | err: |
| 77 | close(fd); |
| 78 | return rc; |
| 79 | } |
| 80 | |
Holger Hans Peter Freyther | 7113514 | 2010-03-29 08:47:44 +0200 | [diff] [blame] | 81 | const unsigned char find_pkt[] = { 0x00, 0x0b+8, IPAC_PROTO_IPACCESS, 0x00, |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 82 | IPAC_MSGT_ID_GET, |
| 83 | 0x01, IPAC_IDTAG_MACADDR, |
| 84 | 0x01, IPAC_IDTAG_IPADDR, |
| 85 | 0x01, IPAC_IDTAG_UNIT, |
| 86 | 0x01, IPAC_IDTAG_LOCATION1, |
| 87 | 0x01, IPAC_IDTAG_LOCATION2, |
| 88 | 0x01, IPAC_IDTAG_EQUIPVERS, |
| 89 | 0x01, IPAC_IDTAG_SWVERSION, |
| 90 | 0x01, IPAC_IDTAG_UNITNAME, |
| 91 | 0x01, IPAC_IDTAG_SERNR, |
| 92 | }; |
| 93 | |
| 94 | |
| 95 | static int bcast_find(int fd) |
| 96 | { |
| 97 | struct sockaddr_in sa; |
| 98 | |
| 99 | sa.sin_family = AF_INET; |
| 100 | sa.sin_port = htons(3006); |
| 101 | inet_aton("255.255.255.255", &sa.sin_addr); |
| 102 | |
| 103 | return sendto(fd, find_pkt, sizeof(find_pkt), 0, (struct sockaddr *) &sa, sizeof(sa)); |
| 104 | } |
| 105 | |
| 106 | static int parse_response(unsigned char *buf, int len) |
| 107 | { |
Holger Hans Peter Freyther | 7eb8a9a | 2011-04-18 17:04:00 +0200 | [diff] [blame^] | 108 | uint8_t t_len; |
| 109 | uint8_t t_tag; |
| 110 | uint8_t *cur = buf; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 111 | |
| 112 | while (cur < buf + len) { |
| 113 | t_len = *cur++; |
| 114 | t_tag = *cur++; |
| 115 | |
Pablo Neira Ayuso | 7b6408c | 2011-04-11 16:32:58 +0200 | [diff] [blame] | 116 | printf("%s='%s' ", ipaccess_idtag_name(t_tag), cur); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 117 | |
| 118 | cur += t_len; |
| 119 | } |
| 120 | printf("\n"); |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | static int read_response(int fd) |
| 125 | { |
| 126 | unsigned char buf[255]; |
| 127 | struct sockaddr_in sa; |
| 128 | int len; |
| 129 | socklen_t sa_len = sizeof(sa); |
| 130 | |
| 131 | len = recvfrom(fd, buf, sizeof(buf), 0, (struct sockaddr *)&sa, &sa_len); |
| 132 | if (len < 0) |
| 133 | return len; |
| 134 | |
Harald Welte | 71e49a0 | 2009-08-08 12:14:53 +0200 | [diff] [blame] | 135 | /* 2 bytes length, 1 byte protocol (0xfe) */ |
| 136 | if (buf[2] != 0xfe) |
| 137 | return 0; |
| 138 | |
| 139 | if (buf[4] != IPAC_MSGT_ID_RESP) |
| 140 | return 0; |
| 141 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 142 | return parse_response(buf+6, len-6); |
| 143 | } |
| 144 | |
| 145 | static int bfd_cb(struct bsc_fd *bfd, unsigned int flags) |
| 146 | { |
| 147 | if (flags & BSC_FD_READ) |
| 148 | return read_response(bfd->fd); |
| 149 | if (flags & BSC_FD_WRITE) { |
| 150 | bfd->when &= ~BSC_FD_WRITE; |
| 151 | return bcast_find(bfd->fd); |
| 152 | } |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | static struct timer_list timer; |
| 157 | |
| 158 | static void timer_cb(void *_data) |
| 159 | { |
| 160 | struct bsc_fd *bfd = _data; |
| 161 | |
| 162 | bfd->when |= BSC_FD_WRITE; |
| 163 | |
| 164 | bsc_schedule_timer(&timer, 5, 0); |
| 165 | } |
| 166 | |
| 167 | int main(int argc, char **argv) |
| 168 | { |
| 169 | struct bsc_fd bfd; |
Harald Welte | 84acf05 | 2009-08-08 11:47:20 +0200 | [diff] [blame] | 170 | char *ifname; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 171 | int rc; |
| 172 | |
| 173 | printf("ipaccess-find (C) 2009 by Harald Welte\n"); |
| 174 | printf("This is FREE SOFTWARE with ABSOLUTELY NO WARRANTY\n\n"); |
| 175 | |
Harald Welte | 81d517d | 2009-06-29 10:43:04 +0200 | [diff] [blame] | 176 | if (argc < 2) { |
Harald Welte | 84acf05 | 2009-08-08 11:47:20 +0200 | [diff] [blame] | 177 | fprintf(stdout, "you might need to specify the outgoing\n" |
| 178 | " network interface, e.g. ``%s eth0''\n", argv[0]); |
Pablo Neira Ayuso | b21408a | 2011-04-05 18:33:17 +0200 | [diff] [blame] | 179 | exit(EXIT_FAILURE); |
Harald Welte | 81d517d | 2009-06-29 10:43:04 +0200 | [diff] [blame] | 180 | } |
| 181 | |
Harald Welte | 84acf05 | 2009-08-08 11:47:20 +0200 | [diff] [blame] | 182 | ifname = argv[1]; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 183 | bfd.cb = bfd_cb; |
| 184 | bfd.when = BSC_FD_READ | BSC_FD_WRITE; |
Harald Welte | 84acf05 | 2009-08-08 11:47:20 +0200 | [diff] [blame] | 185 | bfd.fd = udp_sock(ifname); |
Harald Welte | 81d517d | 2009-06-29 10:43:04 +0200 | [diff] [blame] | 186 | if (bfd.fd < 0) { |
| 187 | perror("Cannot create local socket for broadcast udp"); |
| 188 | exit(1); |
| 189 | } |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 190 | |
| 191 | bsc_register_fd(&bfd); |
| 192 | |
| 193 | timer.cb = timer_cb; |
| 194 | timer.data = &bfd; |
| 195 | |
| 196 | bsc_schedule_timer(&timer, 5, 0); |
| 197 | |
| 198 | printf("Trying to find ip.access BTS by broadcast UDP...\n"); |
| 199 | |
| 200 | while (1) { |
| 201 | rc = bsc_select_main(0); |
| 202 | if (rc < 0) |
| 203 | exit(3); |
| 204 | } |
| 205 | |
| 206 | exit(0); |
| 207 | } |
| 208 | |