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