2011-01-01 14:25:50 +00:00
|
|
|
/* ip.access nanoBTS configuration tool */
|
|
|
|
|
|
|
|
/* (C) 2009-2010 by Harald Welte <laforge@gnumonks.org>
|
|
|
|
* All Rights Reserved
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2009-02-14 12:51:36 +00:00
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
|
|
|
|
2011-03-22 15:47:59 +00:00
|
|
|
#include <osmocom/core/select.h>
|
|
|
|
#include <osmocom/core/timer.h>
|
2014-08-20 21:46:40 +00:00
|
|
|
#include <osmocom/gsm/protocol/ipaccess.h>
|
|
|
|
#include <osmocom/gsm/ipa.h>
|
2017-09-04 13:15:32 +00:00
|
|
|
#include <osmocom/bsc/gsm_data.h>
|
2009-04-30 15:15:37 +00:00
|
|
|
|
2009-08-08 09:47:20 +00:00
|
|
|
static int udp_sock(const char *ifname)
|
2009-02-14 12:51:36 +00:00
|
|
|
{
|
|
|
|
int fd, rc, bc = 1;
|
|
|
|
struct sockaddr_in sa;
|
|
|
|
|
|
|
|
fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
|
|
|
if (fd < 0)
|
|
|
|
return fd;
|
|
|
|
|
2009-08-08 09:47:20 +00:00
|
|
|
if (ifname) {
|
2014-05-08 09:45:20 +00:00
|
|
|
#ifdef __FreeBSD__
|
|
|
|
rc = setsockopt(fd, SOL_SOCKET, IP_RECVIF, ifname,
|
|
|
|
strlen(ifname));
|
|
|
|
#else
|
2009-08-08 09:47:20 +00:00
|
|
|
rc = setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, ifname,
|
|
|
|
strlen(ifname));
|
2014-05-08 09:45:20 +00:00
|
|
|
#endif
|
2009-08-08 09:47:20 +00:00
|
|
|
if (rc < 0)
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2013-07-03 08:19:37 +00:00
|
|
|
memset(&sa, 0, sizeof(sa));
|
2009-02-14 12:51:36 +00:00
|
|
|
sa.sin_family = AF_INET;
|
|
|
|
sa.sin_port = htons(3006);
|
|
|
|
sa.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
|
|
|
|
rc = bind(fd, (struct sockaddr *)&sa, sizeof(sa));
|
|
|
|
if (rc < 0)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
rc = setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &bc, sizeof(bc));
|
|
|
|
if (rc < 0)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
#if 0
|
2009-08-08 10:14:53 +00:00
|
|
|
/* we cannot bind, since the response packets don't come from
|
|
|
|
* the broadcast address */
|
|
|
|
sa.sin_family = AF_INET;
|
|
|
|
sa.sin_port = htons(3006);
|
|
|
|
inet_aton("255.255.255.255", &sa.sin_addr);
|
|
|
|
|
2009-02-14 12:51:36 +00:00
|
|
|
rc = connect(fd, (struct sockaddr *)&sa, sizeof(sa));
|
|
|
|
if (rc < 0)
|
|
|
|
goto err;
|
2010-03-29 06:47:44 +00:00
|
|
|
#endif
|
2009-02-14 12:51:36 +00:00
|
|
|
return fd;
|
|
|
|
|
|
|
|
err:
|
|
|
|
close(fd);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2010-03-29 06:47:44 +00:00
|
|
|
const unsigned char find_pkt[] = { 0x00, 0x0b+8, IPAC_PROTO_IPACCESS, 0x00,
|
2009-02-15 15:32:53 +00:00
|
|
|
IPAC_MSGT_ID_GET,
|
|
|
|
0x01, IPAC_IDTAG_MACADDR,
|
|
|
|
0x01, IPAC_IDTAG_IPADDR,
|
|
|
|
0x01, IPAC_IDTAG_UNIT,
|
|
|
|
0x01, IPAC_IDTAG_LOCATION1,
|
|
|
|
0x01, IPAC_IDTAG_LOCATION2,
|
|
|
|
0x01, IPAC_IDTAG_EQUIPVERS,
|
|
|
|
0x01, IPAC_IDTAG_SWVERSION,
|
|
|
|
0x01, IPAC_IDTAG_UNITNAME,
|
|
|
|
0x01, IPAC_IDTAG_SERNR,
|
|
|
|
};
|
2009-02-14 12:51:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
static int bcast_find(int fd)
|
|
|
|
{
|
|
|
|
struct sockaddr_in sa;
|
|
|
|
|
|
|
|
sa.sin_family = AF_INET;
|
|
|
|
sa.sin_port = htons(3006);
|
|
|
|
inet_aton("255.255.255.255", &sa.sin_addr);
|
|
|
|
|
|
|
|
return sendto(fd, find_pkt, sizeof(find_pkt), 0, (struct sockaddr *) &sa, sizeof(sa));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int parse_response(unsigned char *buf, int len)
|
|
|
|
{
|
2011-04-18 15:04:00 +00:00
|
|
|
uint8_t t_len;
|
|
|
|
uint8_t t_tag;
|
|
|
|
uint8_t *cur = buf;
|
2009-02-14 12:51:36 +00:00
|
|
|
|
|
|
|
while (cur < buf + len) {
|
|
|
|
t_len = *cur++;
|
|
|
|
t_tag = *cur++;
|
|
|
|
|
2014-08-20 21:46:40 +00:00
|
|
|
printf("%s='%s' ", ipa_ccm_idtag_name(t_tag), cur);
|
2009-02-14 12:51:36 +00:00
|
|
|
|
|
|
|
cur += t_len;
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int read_response(int fd)
|
|
|
|
{
|
|
|
|
unsigned char buf[255];
|
|
|
|
struct sockaddr_in sa;
|
|
|
|
int len;
|
|
|
|
socklen_t sa_len = sizeof(sa);
|
|
|
|
|
|
|
|
len = recvfrom(fd, buf, sizeof(buf), 0, (struct sockaddr *)&sa, &sa_len);
|
|
|
|
if (len < 0)
|
|
|
|
return len;
|
|
|
|
|
2016-11-10 17:29:50 +00:00
|
|
|
/* 2 bytes length, 1 byte protocol */
|
|
|
|
if (buf[2] != IPAC_PROTO_IPACCESS)
|
2009-08-08 10:14:53 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (buf[4] != IPAC_MSGT_ID_RESP)
|
|
|
|
return 0;
|
|
|
|
|
2009-05-01 14:53:36 +00:00
|
|
|
return parse_response(buf+6, len-6);
|
2009-02-14 12:51:36 +00:00
|
|
|
}
|
|
|
|
|
2011-05-06 10:11:23 +00:00
|
|
|
static int bfd_cb(struct osmo_fd *bfd, unsigned int flags)
|
2009-02-14 12:51:36 +00:00
|
|
|
{
|
|
|
|
if (flags & BSC_FD_READ)
|
|
|
|
return read_response(bfd->fd);
|
|
|
|
if (flags & BSC_FD_WRITE) {
|
|
|
|
bfd->when &= ~BSC_FD_WRITE;
|
|
|
|
return bcast_find(bfd->fd);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-05-06 10:11:06 +00:00
|
|
|
static struct osmo_timer_list timer;
|
2009-02-14 12:51:36 +00:00
|
|
|
|
|
|
|
static void timer_cb(void *_data)
|
|
|
|
{
|
2011-05-06 10:11:23 +00:00
|
|
|
struct osmo_fd *bfd = _data;
|
2009-02-14 12:51:36 +00:00
|
|
|
|
|
|
|
bfd->when |= BSC_FD_WRITE;
|
|
|
|
|
2011-05-06 10:11:06 +00:00
|
|
|
osmo_timer_schedule(&timer, 5, 0);
|
2009-02-14 12:51:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2011-05-06 10:11:23 +00:00
|
|
|
struct osmo_fd bfd;
|
2011-05-14 17:46:05 +00:00
|
|
|
char *ifname = NULL;
|
2009-02-14 12:51:36 +00:00
|
|
|
int rc;
|
|
|
|
|
2016-02-18 10:15:16 +00:00
|
|
|
printf("abisip-find (C) 2009 by Harald Welte\n");
|
2009-02-14 12:51:36 +00:00
|
|
|
printf("This is FREE SOFTWARE with ABSOLUTELY NO WARRANTY\n\n");
|
|
|
|
|
2009-06-29 08:43:04 +00:00
|
|
|
if (argc < 2) {
|
2009-08-08 09:47:20 +00:00
|
|
|
fprintf(stdout, "you might need to specify the outgoing\n"
|
|
|
|
" network interface, e.g. ``%s eth0''\n", argv[0]);
|
2011-05-17 18:34:53 +00:00
|
|
|
} else {
|
2011-05-14 17:46:05 +00:00
|
|
|
ifname = argv[1];
|
2009-06-29 08:43:04 +00:00
|
|
|
}
|
|
|
|
|
2009-02-14 12:51:36 +00:00
|
|
|
bfd.cb = bfd_cb;
|
|
|
|
bfd.when = BSC_FD_READ | BSC_FD_WRITE;
|
2009-08-08 09:47:20 +00:00
|
|
|
bfd.fd = udp_sock(ifname);
|
2009-06-29 08:43:04 +00:00
|
|
|
if (bfd.fd < 0) {
|
|
|
|
perror("Cannot create local socket for broadcast udp");
|
|
|
|
exit(1);
|
|
|
|
}
|
2009-02-14 12:51:36 +00:00
|
|
|
|
2016-11-26 13:57:23 +00:00
|
|
|
rc = osmo_fd_register(&bfd);
|
|
|
|
if (rc < 0) {
|
|
|
|
fprintf(stderr, "Cannot register FD\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2009-02-14 12:51:36 +00:00
|
|
|
|
2017-05-08 18:57:52 +00:00
|
|
|
osmo_timer_setup(&timer, timer_cb, &bfd);
|
2011-05-06 10:11:06 +00:00
|
|
|
osmo_timer_schedule(&timer, 5, 0);
|
2009-02-14 12:51:36 +00:00
|
|
|
|
|
|
|
printf("Trying to find ip.access BTS by broadcast UDP...\n");
|
|
|
|
|
|
|
|
while (1) {
|
2011-05-06 10:11:23 +00:00
|
|
|
rc = osmo_select_main(0);
|
2009-02-14 12:51:36 +00:00
|
|
|
if (rc < 0)
|
|
|
|
exit(3);
|
|
|
|
}
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|