Harald Welte | 923a3bd | 2009-02-14 12:51:36 +0000 | [diff] [blame] | 1 | |
| 2 | #include <unistd.h> |
| 3 | #include <stdio.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <sys/types.h> |
| 6 | #include <sys/socket.h> |
| 7 | #include <netinet/in.h> |
| 8 | #include <arpa/inet.h> |
| 9 | |
| 10 | |
| 11 | #include <openbsc/select.h> |
| 12 | #include <openbsc/timer.h> |
Harald Welte | 4f361fc | 2009-02-15 15:32:53 +0000 | [diff] [blame] | 13 | #include <openbsc/ipaccess.h> |
Harald Welte | 923a3bd | 2009-02-14 12:51:36 +0000 | [diff] [blame] | 14 | |
| 15 | static int udp_sock(void) |
| 16 | { |
| 17 | int fd, rc, bc = 1; |
| 18 | struct sockaddr_in sa; |
| 19 | |
| 20 | fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
| 21 | if (fd < 0) |
| 22 | return fd; |
| 23 | |
| 24 | sa.sin_family = AF_INET; |
| 25 | sa.sin_port = htons(3006); |
| 26 | sa.sin_addr.s_addr = INADDR_ANY; |
| 27 | inet_aton("192.168.100.11", &sa.sin_addr); |
| 28 | |
| 29 | rc = bind(fd, (struct sockaddr *)&sa, sizeof(sa)); |
| 30 | if (rc < 0) |
| 31 | goto err; |
| 32 | |
| 33 | rc = setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &bc, sizeof(bc)); |
| 34 | if (rc < 0) |
| 35 | goto err; |
| 36 | |
| 37 | #if 0 |
| 38 | rc = connect(fd, (struct sockaddr *)&sa, sizeof(sa)); |
| 39 | if (rc < 0) |
| 40 | goto err; |
| 41 | #endif |
| 42 | return fd; |
| 43 | |
| 44 | err: |
| 45 | close(fd); |
| 46 | return rc; |
| 47 | } |
| 48 | |
Harald Welte | 4f361fc | 2009-02-15 15:32:53 +0000 | [diff] [blame] | 49 | const unsigned char find_pkt[] = { 0x00, 0x0b+8, IPAC_PROTO_IPACCESS, 0x00, |
| 50 | IPAC_MSGT_ID_GET, |
| 51 | 0x01, IPAC_IDTAG_MACADDR, |
| 52 | 0x01, IPAC_IDTAG_IPADDR, |
| 53 | 0x01, IPAC_IDTAG_UNIT, |
| 54 | 0x01, IPAC_IDTAG_LOCATION1, |
| 55 | 0x01, IPAC_IDTAG_LOCATION2, |
| 56 | 0x01, IPAC_IDTAG_EQUIPVERS, |
| 57 | 0x01, IPAC_IDTAG_SWVERSION, |
| 58 | 0x01, IPAC_IDTAG_UNITNAME, |
| 59 | 0x01, IPAC_IDTAG_SERNR, |
| 60 | }; |
Harald Welte | 923a3bd | 2009-02-14 12:51:36 +0000 | [diff] [blame] | 61 | |
| 62 | |
| 63 | static int bcast_find(int fd) |
| 64 | { |
| 65 | struct sockaddr_in sa; |
| 66 | |
| 67 | sa.sin_family = AF_INET; |
| 68 | sa.sin_port = htons(3006); |
| 69 | inet_aton("255.255.255.255", &sa.sin_addr); |
| 70 | |
| 71 | return sendto(fd, find_pkt, sizeof(find_pkt), 0, (struct sockaddr *) &sa, sizeof(sa)); |
| 72 | } |
| 73 | |
| 74 | static int parse_response(unsigned char *buf, int len) |
| 75 | { |
| 76 | u_int8_t t_len; |
| 77 | u_int8_t t_tag; |
| 78 | u_int8_t *cur = buf; |
| 79 | |
| 80 | cur += 6; |
| 81 | |
| 82 | while (cur < buf + len) { |
| 83 | t_len = *cur++; |
| 84 | t_tag = *cur++; |
| 85 | |
Harald Welte | 4f361fc | 2009-02-15 15:32:53 +0000 | [diff] [blame] | 86 | printf("tag(%02x)='%s' ", t_tag, cur); |
Harald Welte | 923a3bd | 2009-02-14 12:51:36 +0000 | [diff] [blame] | 87 | |
| 88 | cur += t_len; |
| 89 | } |
| 90 | printf("\n"); |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | static int read_response(int fd) |
| 95 | { |
| 96 | unsigned char buf[255]; |
| 97 | struct sockaddr_in sa; |
| 98 | int len; |
| 99 | socklen_t sa_len = sizeof(sa); |
| 100 | |
| 101 | len = recvfrom(fd, buf, sizeof(buf), 0, (struct sockaddr *)&sa, &sa_len); |
| 102 | if (len < 0) |
| 103 | return len; |
| 104 | |
| 105 | return parse_response(buf, len); |
| 106 | } |
| 107 | |
| 108 | static int bfd_cb(struct bsc_fd *bfd, unsigned int flags) |
| 109 | { |
| 110 | if (flags & BSC_FD_READ) |
| 111 | return read_response(bfd->fd); |
| 112 | if (flags & BSC_FD_WRITE) { |
| 113 | bfd->when &= ~BSC_FD_WRITE; |
| 114 | return bcast_find(bfd->fd); |
| 115 | } |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | static struct timer_list timer; |
| 120 | |
| 121 | static void timer_cb(void *_data) |
| 122 | { |
| 123 | struct bsc_fd *bfd = _data; |
| 124 | |
| 125 | bfd->when |= BSC_FD_WRITE; |
| 126 | |
| 127 | schedule_timer(&timer, 5, 0); |
| 128 | } |
| 129 | |
| 130 | int main(int argc, char **argv) |
| 131 | { |
| 132 | struct bsc_fd bfd; |
| 133 | int rc; |
| 134 | |
| 135 | printf("ipaccess-find (C) 2009 by Harald Welte\n"); |
| 136 | printf("This is FREE SOFTWARE with ABSOLUTELY NO WARRANTY\n\n"); |
| 137 | |
| 138 | bfd.cb = bfd_cb; |
| 139 | bfd.when = BSC_FD_READ | BSC_FD_WRITE; |
| 140 | bfd.fd = udp_sock(); |
| 141 | if (bfd.fd < 0) |
| 142 | exit(2); |
| 143 | |
| 144 | bsc_register_fd(&bfd); |
| 145 | |
| 146 | timer.cb = timer_cb; |
| 147 | timer.data = &bfd; |
| 148 | |
| 149 | schedule_timer(&timer, 5, 0); |
| 150 | |
| 151 | printf("Trying to find ip.access BTS by broadcast UDP...\n"); |
| 152 | |
| 153 | while (1) { |
| 154 | rc = bsc_select_main(); |
| 155 | if (rc < 0) |
| 156 | exit(3); |
| 157 | } |
| 158 | |
| 159 | exit(0); |
| 160 | } |
| 161 | |