blob: 4b5f6122a754a31e173fe722ffe2438c0434c49c [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) {
44 rc = setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, ifname,
45 strlen(ifname));
46 if (rc < 0)
47 goto err;
48 }
49
Harald Welte923a3bd2009-02-14 12:51:36 +000050 sa.sin_family = AF_INET;
51 sa.sin_port = htons(3006);
52 sa.sin_addr.s_addr = INADDR_ANY;
Harald Welte923a3bd2009-02-14 12:51:36 +000053
54 rc = bind(fd, (struct sockaddr *)&sa, sizeof(sa));
55 if (rc < 0)
56 goto err;
57
58 rc = setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &bc, sizeof(bc));
59 if (rc < 0)
60 goto err;
61
62#if 0
Harald Welte81cff3c2009-08-08 12:14:53 +020063 /* we cannot bind, since the response packets don't come from
64 * the broadcast address */
65 sa.sin_family = AF_INET;
66 sa.sin_port = htons(3006);
67 inet_aton("255.255.255.255", &sa.sin_addr);
68
Harald Welte923a3bd2009-02-14 12:51:36 +000069 rc = connect(fd, (struct sockaddr *)&sa, sizeof(sa));
70 if (rc < 0)
71 goto err;
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +020072#endif
Harald Welte923a3bd2009-02-14 12:51:36 +000073 return fd;
74
75err:
76 close(fd);
77 return rc;
78}
79
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +020080const unsigned char find_pkt[] = { 0x00, 0x0b+8, IPAC_PROTO_IPACCESS, 0x00,
Harald Welte4f361fc2009-02-15 15:32:53 +000081 IPAC_MSGT_ID_GET,
82 0x01, IPAC_IDTAG_MACADDR,
83 0x01, IPAC_IDTAG_IPADDR,
84 0x01, IPAC_IDTAG_UNIT,
85 0x01, IPAC_IDTAG_LOCATION1,
86 0x01, IPAC_IDTAG_LOCATION2,
87 0x01, IPAC_IDTAG_EQUIPVERS,
88 0x01, IPAC_IDTAG_SWVERSION,
89 0x01, IPAC_IDTAG_UNITNAME,
90 0x01, IPAC_IDTAG_SERNR,
91 };
Harald Welte923a3bd2009-02-14 12:51:36 +000092
93
94static int bcast_find(int fd)
95{
96 struct sockaddr_in sa;
97
98 sa.sin_family = AF_INET;
99 sa.sin_port = htons(3006);
100 inet_aton("255.255.255.255", &sa.sin_addr);
101
102 return sendto(fd, find_pkt, sizeof(find_pkt), 0, (struct sockaddr *) &sa, sizeof(sa));
103}
104
105static int parse_response(unsigned char *buf, int len)
106{
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200107 uint8_t t_len;
108 uint8_t t_tag;
109 uint8_t *cur = buf;
Harald Welte923a3bd2009-02-14 12:51:36 +0000110
Harald Welte923a3bd2009-02-14 12:51:36 +0000111 while (cur < buf + len) {
112 t_len = *cur++;
113 t_tag = *cur++;
114
Pablo Neira Ayuso23b1b802011-04-11 16:32:58 +0200115 printf("%s='%s' ", ipaccess_idtag_name(t_tag), cur);
Harald Welte923a3bd2009-02-14 12:51:36 +0000116
117 cur += t_len;
118 }
119 printf("\n");
120 return 0;
121}
122
123static int read_response(int fd)
124{
125 unsigned char buf[255];
126 struct sockaddr_in sa;
127 int len;
128 socklen_t sa_len = sizeof(sa);
129
130 len = recvfrom(fd, buf, sizeof(buf), 0, (struct sockaddr *)&sa, &sa_len);
131 if (len < 0)
132 return len;
133
Harald Welte81cff3c2009-08-08 12:14:53 +0200134 /* 2 bytes length, 1 byte protocol (0xfe) */
135 if (buf[2] != 0xfe)
136 return 0;
137
138 if (buf[4] != IPAC_MSGT_ID_RESP)
139 return 0;
140
Harald Welte4593ff32009-05-01 14:53:36 +0000141 return parse_response(buf+6, len-6);
Harald Welte923a3bd2009-02-14 12:51:36 +0000142}
143
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200144static int bfd_cb(struct osmo_fd *bfd, unsigned int flags)
Harald Welte923a3bd2009-02-14 12:51:36 +0000145{
146 if (flags & BSC_FD_READ)
147 return read_response(bfd->fd);
148 if (flags & BSC_FD_WRITE) {
149 bfd->when &= ~BSC_FD_WRITE;
150 return bcast_find(bfd->fd);
151 }
152 return 0;
153}
154
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200155static struct osmo_timer_list timer;
Harald Welte923a3bd2009-02-14 12:51:36 +0000156
157static void timer_cb(void *_data)
158{
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200159 struct osmo_fd *bfd = _data;
Harald Welte923a3bd2009-02-14 12:51:36 +0000160
161 bfd->when |= BSC_FD_WRITE;
162
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200163 osmo_timer_schedule(&timer, 5, 0);
Harald Welte923a3bd2009-02-14 12:51:36 +0000164}
165
166int main(int argc, char **argv)
167{
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200168 struct osmo_fd bfd;
Holger Hans Peter Freytherd3080be2011-05-14 19:46:05 +0200169 char *ifname = NULL;
Harald Welte923a3bd2009-02-14 12:51:36 +0000170 int rc;
171
172 printf("ipaccess-find (C) 2009 by Harald Welte\n");
173 printf("This is FREE SOFTWARE with ABSOLUTELY NO WARRANTY\n\n");
174
Harald Welte042401c2009-06-29 10:43:04 +0200175 if (argc < 2) {
Harald Weltee26d0792009-08-08 11:47:20 +0200176 fprintf(stdout, "you might need to specify the outgoing\n"
177 " network interface, e.g. ``%s eth0''\n", argv[0]);
Holger Hans Peter Freytherd3080be2011-05-14 19:46:05 +0200178 ifname = argv[1];
Harald Welte042401c2009-06-29 10:43:04 +0200179 }
180
Harald Welte923a3bd2009-02-14 12:51:36 +0000181 bfd.cb = bfd_cb;
182 bfd.when = BSC_FD_READ | BSC_FD_WRITE;
Harald Weltee26d0792009-08-08 11:47:20 +0200183 bfd.fd = udp_sock(ifname);
Harald Welte042401c2009-06-29 10:43:04 +0200184 if (bfd.fd < 0) {
185 perror("Cannot create local socket for broadcast udp");
186 exit(1);
187 }
Harald Welte923a3bd2009-02-14 12:51:36 +0000188
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200189 osmo_fd_register(&bfd);
Harald Welte923a3bd2009-02-14 12:51:36 +0000190
191 timer.cb = timer_cb;
192 timer.data = &bfd;
193
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200194 osmo_timer_schedule(&timer, 5, 0);
Harald Welte923a3bd2009-02-14 12:51:36 +0000195
196 printf("Trying to find ip.access BTS by broadcast UDP...\n");
197
198 while (1) {
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200199 rc = osmo_select_main(0);
Harald Welte923a3bd2009-02-14 12:51:36 +0000200 if (rc < 0)
201 exit(3);
202 }
203
204 exit(0);
205}
206