l2ping.c revision 121054
1/* 2 * l2ping.c 3 * 4 * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * $Id: l2ping.c,v 1.5 2003/05/16 19:54:40 max Exp $ 29 * $FreeBSD: head/usr.sbin/bluetooth/l2ping/l2ping.c 121054 2003-10-12 22:04:24Z emax $ 30 */ 31 32#include <sys/ioctl.h> 33#include <sys/time.h> 34#include <arpa/inet.h> 35#include <netinet/in.h> 36#include <assert.h> 37#include <bluetooth.h> 38#include <err.h> 39#include <errno.h> 40#include <stdio.h> 41#include <stdlib.h> 42#include <string.h> 43#include <unistd.h> 44 45static void usage (void); 46static void tv_sub (struct timeval *, struct timeval const *); 47static double tv2msec (struct timeval const *); 48 49#undef min 50#define min(x, y) (((x) > (y))? (y) : (x)) 51 52static char const pattern[] = "1234567890-"; 53#define PATTERN_SIZE (sizeof(pattern) - 1) 54 55/* 56 * Main 57 */ 58 59int 60main(int argc, char *argv[]) 61{ 62 bdaddr_t src, dst; 63 struct hostent *he = NULL; 64 struct sockaddr_l2cap sa; 65 struct ng_btsocket_l2cap_raw_ping r; 66 int n, s, count, wait, flood, fail; 67 struct timeval a, b; 68 69 /* Set defaults */ 70 memcpy(&src, NG_HCI_BDADDR_ANY, sizeof(src)); 71 memcpy(&dst, NG_HCI_BDADDR_ANY, sizeof(dst)); 72 73 memset(&r, 0, sizeof(r)); 74 r.echo_data = calloc(NG_L2CAP_MAX_ECHO_SIZE, sizeof(u_int8_t)); 75 if (r.echo_data == NULL) { 76 fprintf(stderr, "Failed to allocate echo data buffer"); 77 exit(1); 78 } 79 80 r.echo_size = 64; /* bytes */ 81 count = -1; /* unlimited */ 82 wait = 1; /* sec */ 83 flood = 0; 84 85 /* Parse command line arguments */ 86 while ((n = getopt(argc, argv, "a:c:fi:n:s:S:h")) != -1) { 87 switch (n) { 88 case 'a': 89 if (!bt_aton(optarg, &dst)) { 90 if ((he = bt_gethostbyname(optarg)) == NULL) 91 errx(1, "%s: %s", optarg, hstrerror(h_errno)); 92 93 memcpy(&dst, he->h_addr, sizeof(dst)); 94 } 95 break; 96 97 case 'S': 98 if (!bt_aton(optarg, &src)) { 99 if ((he = bt_gethostbyname(optarg)) == NULL) 100 errx(1, "%s: %s", optarg, hstrerror(h_errno)); 101 102 memcpy(&src, he->h_addr, sizeof(src)); 103 } 104 break; 105 106 case 'c': 107 count = atoi(optarg); 108 if (count <= 0) 109 usage(); 110 break; 111 112 case 'f': 113 flood = 1; 114 break; 115 116 case 'i': 117 wait = atoi(optarg); 118 if (wait <= 0) 119 usage(); 120 break; 121 122 case 's': 123 r.echo_size = atoi(optarg); 124 if ((int) r.echo_size < sizeof(int)) 125 usage(); 126 127 if (r.echo_size > NG_L2CAP_MAX_ECHO_SIZE) 128 r.echo_size = NG_L2CAP_MAX_ECHO_SIZE; 129 break; 130 131 case 'h': 132 default: 133 usage(); 134 break; 135 } 136 } 137 138 if (memcmp(&dst, NG_HCI_BDADDR_ANY, sizeof(dst)) == 0) 139 usage(); 140 141 s = socket(PF_BLUETOOTH, SOCK_RAW, BLUETOOTH_PROTO_L2CAP); 142 if (s < 0) 143 err(2, "Could not create socket"); 144 145 memset(&sa, 0, sizeof(sa)); 146 sa.l2cap_len = sizeof(sa); 147 sa.l2cap_family = AF_BLUETOOTH; 148 memcpy(&sa.l2cap_bdaddr, &src, sizeof(sa.l2cap_bdaddr)); 149 150 if (bind(s, (struct sockaddr *) &sa, sizeof(sa)) < 0) 151 err(3, 152"Could not bind socket, src bdaddr=%s", bt_ntoa(&sa.l2cap_bdaddr, NULL)); 153 154 memset(&sa, 0, sizeof(sa)); 155 sa.l2cap_len = sizeof(sa); 156 sa.l2cap_family = AF_BLUETOOTH; 157 memcpy(&sa.l2cap_bdaddr, &dst, sizeof(sa.l2cap_bdaddr)); 158 159 if (connect(s, (struct sockaddr *) &sa, sizeof(sa)) < 0) 160 err(4, 161"Could not connect socket, dst bdaddr=%s", bt_ntoa(&sa.l2cap_bdaddr, NULL)); 162 163 /* Fill pattern */ 164 for (n = 0; n < r.echo_size; ) { 165 int avail = min(r.echo_size - n, PATTERN_SIZE); 166 167 memcpy(r.echo_data + n, pattern, avail); 168 n += avail; 169 } 170 171 /* Start ping'ing */ 172 for (n = 0; count == -1 || count > 0; n ++) { 173 if (gettimeofday(&a, NULL) < 0) 174 err(5, "Could not gettimeofday(a)"); 175 176 fail = 0; 177 *((int *)(r.echo_data)) = htonl(n); 178 if (ioctl(s, SIOC_L2CAP_L2CA_PING, &r, sizeof(r)) < 0) { 179 r.result = errno; 180 fail = 1; 181/* 182 warn("Could not ping, dst bdaddr=%s", 183 bt_ntoa(&r.echo_dst, NULL)); 184*/ 185 } 186 187 if (gettimeofday(&b, NULL) < 0) 188 err(7, "Could not gettimeofday(b)"); 189 190 tv_sub(&b, &a); 191 192 fprintf(stdout, 193"%d bytes from %s seq_no=%d time=%.3f ms result=%#x %s\n", 194 r.echo_size, 195 bt_ntoa(&dst, NULL), 196 ntohl(*((int *)(r.echo_data))), 197 tv2msec(&b), r.result, 198 ((fail == 0)? "" : strerror(errno))); 199 200 if (!flood) { 201 /* Wait */ 202 a.tv_sec = wait; 203 a.tv_usec = 0; 204 select(0, NULL, NULL, NULL, &a); 205 } 206 207 if (count != -1) 208 count --; 209 } 210 211 free(r.echo_data); 212 close(s); 213 214 return (0); 215} /* main */ 216 217/* 218 * a -= b, for timevals 219 */ 220 221static void 222tv_sub(struct timeval *a, struct timeval const *b) 223{ 224 if (a->tv_usec < b->tv_usec) { 225 a->tv_usec += 1000000; 226 a->tv_sec -= 1; 227 } 228 229 a->tv_usec -= b->tv_usec; 230 a->tv_sec -= b->tv_sec; 231} /* tv_sub */ 232 233/* 234 * convert tv to msec 235 */ 236 237static double 238tv2msec(struct timeval const *tvp) 239{ 240 return(((double)tvp->tv_usec)/1000.0 + ((double)tvp->tv_sec)*1000.0); 241} /* tv2msec */ 242 243/* 244 * Usage 245 */ 246 247static void 248usage(void) 249{ 250 fprintf(stderr, "Usage: l2ping -a bd_addr " \ 251 "[-S bd_addr -c count -i wait -s size -h]\n"); 252 fprintf(stderr, "Where:\n"); 253 fprintf(stderr, "\t-S bd_addr - Source BD_ADDR\n"); 254 fprintf(stderr, "\t-a bd_addr - Remote BD_ADDR to ping\n"); 255 fprintf(stderr, "\t-c count - Number of packets to send\n"); 256 fprintf(stderr, "\t-f - No delay (soft of flood)\n"); 257 fprintf(stderr, "\t-i wait - Delay between packets (sec)\n"); 258 fprintf(stderr, "\t-s size - Packet size (bytes), " \ 259 "between %d and %d\n", sizeof(int), NG_L2CAP_MAX_ECHO_SIZE); 260 fprintf(stderr, "\t-h - Display this message\n"); 261 262 exit(255); 263} /* usage */ 264 265