blob: c8de157b96af05654e97c8cf2cd1dfb33c6dd4bc [file] [log] [blame]
Harald Welte9af6ddf2011-01-01 15:25:50 +01001/* 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 Welte923a3bd2009-02-14 12:51:36 +000020
21#include <unistd.h>
22#include <stdio.h>
23#include <stdlib.h>
Harald Welte923a3bd2009-02-14 12:51:36 +000024#include <sys/socket.h>
25#include <netinet/in.h>
26#include <arpa/inet.h>
27
28
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010029#include <osmocom/core/select.h>
30#include <osmocom/core/timer.h>
Harald Welte4f361fc2009-02-15 15:32:53 +000031#include <openbsc/ipaccess.h>
Harald Welte37881962009-04-30 15:15:37 +000032#include <openbsc/gsm_data.h>
33
Harald Weltee26d0792009-08-08 11:47:20 +020034static int udp_sock(const char *ifname)
Harald Welte923a3bd2009-02-14 12:51:36 +000035{
36 int fd, rc, bc = 1;
37 struct sockaddr_in sa;
38
39 fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
40 if (fd < 0)
41 return fd;
42
Harald Weltee26d0792009-08-08 11:47:20 +020043 if (ifname) {
Nikola Kolev10bad102014-05-08 12:45:20 +030044#ifdef __FreeBSD__
45 rc = setsockopt(fd, SOL_SOCKET, IP_RECVIF, ifname,
46 strlen(ifname));
47#else
Harald Weltee26d0792009-08-08 11:47:20 +020048 rc = setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, ifname,
49 strlen(ifname));
Nikola Kolev10bad102014-05-08 12:45:20 +030050#endif
Harald Weltee26d0792009-08-08 11:47:20 +020051 if (rc < 0)
52 goto err;
53 }
54
Holger Hans Peter Freytherd5c270e2013-07-03 10:19:37 +020055 memset(&sa, 0, sizeof(sa));
Harald Welte923a3bd2009-02-14 12:51:36 +000056 sa.sin_family = AF_INET;
57 sa.sin_port = htons(3006);
58 sa.sin_addr.s_addr = INADDR_ANY;
Harald Welte923a3bd2009-02-14 12:51:36 +000059
60 rc = bind(fd, (struct sockaddr *)&sa, sizeof(sa));
61 if (rc < 0)
62 goto err;
63
64 rc = setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &bc, sizeof(bc));
65 if (rc < 0)
66 goto err;
67
68#if 0
Harald Welte81cff3c2009-08-08 12:14:53 +020069 /* we cannot bind, since the response packets don't come from
70 * the broadcast address */
71 sa.sin_family = AF_INET;
72 sa.sin_port = htons(3006);
73 inet_aton("255.255.255.255", &sa.sin_addr);
74
Harald Welte923a3bd2009-02-14 12:51:36 +000075 rc = connect(fd, (struct sockaddr *)&sa, sizeof(sa));
76 if (rc < 0)
77 goto err;
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +020078#endif
Harald Welte923a3bd2009-02-14 12:51:36 +000079 return fd;
80
81err:
82 close(fd);
83 return rc;
84}
85
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +020086const unsigned char find_pkt[] = { 0x00, 0x0b+8, IPAC_PROTO_IPACCESS, 0x00,
Harald Welte4f361fc2009-02-15 15:32:53 +000087 IPAC_MSGT_ID_GET,
88 0x01, IPAC_IDTAG_MACADDR,
89 0x01, IPAC_IDTAG_IPADDR,
90 0x01, IPAC_IDTAG_UNIT,
91 0x01, IPAC_IDTAG_LOCATION1,
92 0x01, IPAC_IDTAG_LOCATION2,
93 0x01, IPAC_IDTAG_EQUIPVERS,
94 0x01, IPAC_IDTAG_SWVERSION,
95 0x01, IPAC_IDTAG_UNITNAME,
96 0x01, IPAC_IDTAG_SERNR,
97 };
Harald Welte923a3bd2009-02-14 12:51:36 +000098
99
100static int bcast_find(int fd)
101{
102 struct sockaddr_in sa;
103
104 sa.sin_family = AF_INET;
105 sa.sin_port = htons(3006);
106 inet_aton("255.255.255.255", &sa.sin_addr);
107
108 return sendto(fd, find_pkt, sizeof(find_pkt), 0, (struct sockaddr *) &sa, sizeof(sa));
109}
110
111static int parse_response(unsigned char *buf, int len)
112{
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200113 uint8_t t_len;
114 uint8_t t_tag;
115 uint8_t *cur = buf;
Harald Welte923a3bd2009-02-14 12:51:36 +0000116
Harald Welte923a3bd2009-02-14 12:51:36 +0000117 while (cur < buf + len) {
118 t_len = *cur++;
119 t_tag = *cur++;
120
Pablo Neira Ayuso23b1b802011-04-11 16:32:58 +0200121 printf("%s='%s' ", ipaccess_idtag_name(t_tag), cur);
Harald Welte923a3bd2009-02-14 12:51:36 +0000122
123 cur += t_len;
124 }
125 printf("\n");
126 return 0;
127}
128
129static int read_response(int fd)
130{
131 unsigned char buf[255];
132 struct sockaddr_in sa;
133 int len;
134 socklen_t sa_len = sizeof(sa);
135
136 len = recvfrom(fd, buf, sizeof(buf), 0, (struct sockaddr *)&sa, &sa_len);
137 if (len < 0)
138 return len;
139
Harald Welte81cff3c2009-08-08 12:14:53 +0200140 /* 2 bytes length, 1 byte protocol (0xfe) */
141 if (buf[2] != 0xfe)
142 return 0;
143
144 if (buf[4] != IPAC_MSGT_ID_RESP)
145 return 0;
146
Harald Welte4593ff32009-05-01 14:53:36 +0000147 return parse_response(buf+6, len-6);
Harald Welte923a3bd2009-02-14 12:51:36 +0000148}
149
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200150static int bfd_cb(struct osmo_fd *bfd, unsigned int flags)
Harald Welte923a3bd2009-02-14 12:51:36 +0000151{
152 if (flags & BSC_FD_READ)
153 return read_response(bfd->fd);
154 if (flags & BSC_FD_WRITE) {
155 bfd->when &= ~BSC_FD_WRITE;
156 return bcast_find(bfd->fd);
157 }
158 return 0;
159}
160
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200161static struct osmo_timer_list timer;
Harald Welte923a3bd2009-02-14 12:51:36 +0000162
163static void timer_cb(void *_data)
164{
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200165 struct osmo_fd *bfd = _data;
Harald Welte923a3bd2009-02-14 12:51:36 +0000166
167 bfd->when |= BSC_FD_WRITE;
168
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200169 osmo_timer_schedule(&timer, 5, 0);
Harald Welte923a3bd2009-02-14 12:51:36 +0000170}
171
172int main(int argc, char **argv)
173{
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200174 struct osmo_fd bfd;
Holger Hans Peter Freytherd3080be2011-05-14 19:46:05 +0200175 char *ifname = NULL;
Harald Welte923a3bd2009-02-14 12:51:36 +0000176 int rc;
177
178 printf("ipaccess-find (C) 2009 by Harald Welte\n");
179 printf("This is FREE SOFTWARE with ABSOLUTELY NO WARRANTY\n\n");
180
Harald Welte042401c2009-06-29 10:43:04 +0200181 if (argc < 2) {
Harald Weltee26d0792009-08-08 11:47:20 +0200182 fprintf(stdout, "you might need to specify the outgoing\n"
183 " network interface, e.g. ``%s eth0''\n", argv[0]);
Holger Hans Peter Freyther1e9ae4b2011-05-17 20:34:53 +0200184 } else {
Holger Hans Peter Freytherd3080be2011-05-14 19:46:05 +0200185 ifname = argv[1];
Harald Welte042401c2009-06-29 10:43:04 +0200186 }
187
Harald Welte923a3bd2009-02-14 12:51:36 +0000188 bfd.cb = bfd_cb;
189 bfd.when = BSC_FD_READ | BSC_FD_WRITE;
Harald Weltee26d0792009-08-08 11:47:20 +0200190 bfd.fd = udp_sock(ifname);
Harald Welte042401c2009-06-29 10:43:04 +0200191 if (bfd.fd < 0) {
192 perror("Cannot create local socket for broadcast udp");
193 exit(1);
194 }
Harald Welte923a3bd2009-02-14 12:51:36 +0000195
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200196 osmo_fd_register(&bfd);
Harald Welte923a3bd2009-02-14 12:51:36 +0000197
198 timer.cb = timer_cb;
199 timer.data = &bfd;
200
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200201 osmo_timer_schedule(&timer, 5, 0);
Harald Welte923a3bd2009-02-14 12:51:36 +0000202
203 printf("Trying to find ip.access BTS by broadcast UDP...\n");
204
205 while (1) {
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200206 rc = osmo_select_main(0);
Harald Welte923a3bd2009-02-14 12:51:36 +0000207 if (rc < 0)
208 exit(3);
209 }
210
211 exit(0);
212}
213