l2ping.c revision 128076
1107120Sjulian/* 2107120Sjulian * l2ping.c 3107120Sjulian * 4107120Sjulian * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com> 5107120Sjulian * All rights reserved. 6107120Sjulian * 7107120Sjulian * Redistribution and use in source and binary forms, with or without 8107120Sjulian * modification, are permitted provided that the following conditions 9107120Sjulian * are met: 10107120Sjulian * 1. Redistributions of source code must retain the above copyright 11107120Sjulian * notice, this list of conditions and the following disclaimer. 12107120Sjulian * 2. Redistributions in binary form must reproduce the above copyright 13107120Sjulian * notice, this list of conditions and the following disclaimer in the 14107120Sjulian * documentation and/or other materials provided with the distribution. 15107120Sjulian * 16107120Sjulian * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17107120Sjulian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18107120Sjulian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19107120Sjulian * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20107120Sjulian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21107120Sjulian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22107120Sjulian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23107120Sjulian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24107120Sjulian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25107120Sjulian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26107120Sjulian * SUCH DAMAGE. 27107120Sjulian * 28121054Semax * $Id: l2ping.c,v 1.5 2003/05/16 19:54:40 max Exp $ 29107120Sjulian * $FreeBSD: head/usr.sbin/bluetooth/l2ping/l2ping.c 128076 2004-04-09 23:01:42Z emax $ 30107120Sjulian */ 31107120Sjulian 32107120Sjulian#include <sys/ioctl.h> 33107120Sjulian#include <sys/time.h> 34107120Sjulian#include <arpa/inet.h> 35107120Sjulian#include <netinet/in.h> 36107120Sjulian#include <assert.h> 37121054Semax#include <bluetooth.h> 38107120Sjulian#include <err.h> 39107120Sjulian#include <errno.h> 40107120Sjulian#include <stdio.h> 41107120Sjulian#include <stdlib.h> 42107120Sjulian#include <string.h> 43107120Sjulian#include <unistd.h> 44107120Sjulian 45107120Sjulianstatic void usage (void); 46107120Sjulianstatic void tv_sub (struct timeval *, struct timeval const *); 47107120Sjulianstatic double tv2msec (struct timeval const *); 48107120Sjulian 49107120Sjulian#undef min 50107120Sjulian#define min(x, y) (((x) > (y))? (y) : (x)) 51107120Sjulian 52107120Sjulianstatic char const pattern[] = "1234567890-"; 53107120Sjulian#define PATTERN_SIZE (sizeof(pattern) - 1) 54107120Sjulian 55107120Sjulian/* 56107120Sjulian * Main 57107120Sjulian */ 58107120Sjulian 59107120Sjulianint 60107120Sjulianmain(int argc, char *argv[]) 61107120Sjulian{ 62125943Semax bdaddr_t src, dst; 63125943Semax struct hostent *he = NULL; 64125943Semax uint8_t *echo_data = NULL; 65125943Semax struct sockaddr_l2cap sa; 66125943Semax int32_t n, s, count, wait, flood, echo_size; 67107120Sjulian 68107120Sjulian /* Set defaults */ 69114879Sjulian memcpy(&src, NG_HCI_BDADDR_ANY, sizeof(src)); 70114879Sjulian memcpy(&dst, NG_HCI_BDADDR_ANY, sizeof(dst)); 71114879Sjulian 72125943Semax echo_data = (uint8_t *) calloc(NG_L2CAP_MAX_ECHO_SIZE, sizeof(uint8_t)); 73125943Semax if (echo_data == NULL) { 74107120Sjulian fprintf(stderr, "Failed to allocate echo data buffer"); 75107120Sjulian exit(1); 76107120Sjulian } 77107120Sjulian 78125943Semax /* 79125943Semax * Set default echo size to the NG_L2CAP_MTU_MINIMUM minus 80125943Semax * the size of the L2CAP signalling command header. 81125943Semax */ 82125943Semax 83125943Semax echo_size = NG_L2CAP_MTU_MINIMUM - sizeof(ng_l2cap_cmd_hdr_t); 84125943Semax count = -1; /* unimited */ 85125943Semax wait = 1; /* sec */ 86107120Sjulian flood = 0; 87107120Sjulian 88107120Sjulian /* Parse command line arguments */ 89114879Sjulian while ((n = getopt(argc, argv, "a:c:fi:n:s:S:h")) != -1) { 90107120Sjulian switch (n) { 91107120Sjulian case 'a': 92121054Semax if (!bt_aton(optarg, &dst)) { 93121054Semax if ((he = bt_gethostbyname(optarg)) == NULL) 94121054Semax errx(1, "%s: %s", optarg, hstrerror(h_errno)); 95107120Sjulian 96121054Semax memcpy(&dst, he->h_addr, sizeof(dst)); 97121054Semax } 98121054Semax break; 99107120Sjulian 100121054Semax case 'S': 101121054Semax if (!bt_aton(optarg, &src)) { 102121054Semax if ((he = bt_gethostbyname(optarg)) == NULL) 103121054Semax errx(1, "%s: %s", optarg, hstrerror(h_errno)); 104121054Semax 105121054Semax memcpy(&src, he->h_addr, sizeof(src)); 106107120Sjulian } 107121054Semax break; 108107120Sjulian 109107120Sjulian case 'c': 110107120Sjulian count = atoi(optarg); 111107120Sjulian if (count <= 0) 112107120Sjulian usage(); 113107120Sjulian break; 114107120Sjulian 115107120Sjulian case 'f': 116107120Sjulian flood = 1; 117107120Sjulian break; 118107120Sjulian 119107120Sjulian case 'i': 120107120Sjulian wait = atoi(optarg); 121107120Sjulian if (wait <= 0) 122107120Sjulian usage(); 123107120Sjulian break; 124107120Sjulian 125107120Sjulian case 's': 126125943Semax echo_size = atoi(optarg); 127125943Semax if (echo_size < sizeof(int32_t)) 128107120Sjulian usage(); 129107120Sjulian 130125943Semax if (echo_size > NG_L2CAP_MAX_ECHO_SIZE) 131125943Semax echo_size = NG_L2CAP_MAX_ECHO_SIZE; 132107120Sjulian break; 133107120Sjulian 134114879Sjulian case 'h': 135107120Sjulian default: 136107120Sjulian usage(); 137107120Sjulian break; 138107120Sjulian } 139107120Sjulian } 140107120Sjulian 141114879Sjulian if (memcmp(&dst, NG_HCI_BDADDR_ANY, sizeof(dst)) == 0) 142107120Sjulian usage(); 143107120Sjulian 144107120Sjulian s = socket(PF_BLUETOOTH, SOCK_RAW, BLUETOOTH_PROTO_L2CAP); 145107120Sjulian if (s < 0) 146107120Sjulian err(2, "Could not create socket"); 147107120Sjulian 148114879Sjulian memset(&sa, 0, sizeof(sa)); 149114879Sjulian sa.l2cap_len = sizeof(sa); 150114879Sjulian sa.l2cap_family = AF_BLUETOOTH; 151114879Sjulian memcpy(&sa.l2cap_bdaddr, &src, sizeof(sa.l2cap_bdaddr)); 152107120Sjulian 153114879Sjulian if (bind(s, (struct sockaddr *) &sa, sizeof(sa)) < 0) 154114879Sjulian err(3, 155121054Semax"Could not bind socket, src bdaddr=%s", bt_ntoa(&sa.l2cap_bdaddr, NULL)); 156107120Sjulian 157114879Sjulian memset(&sa, 0, sizeof(sa)); 158114879Sjulian sa.l2cap_len = sizeof(sa); 159114879Sjulian sa.l2cap_family = AF_BLUETOOTH; 160114879Sjulian memcpy(&sa.l2cap_bdaddr, &dst, sizeof(sa.l2cap_bdaddr)); 161107120Sjulian 162114879Sjulian if (connect(s, (struct sockaddr *) &sa, sizeof(sa)) < 0) 163114879Sjulian err(4, 164121054Semax"Could not connect socket, dst bdaddr=%s", bt_ntoa(&sa.l2cap_bdaddr, NULL)); 165114879Sjulian 166107120Sjulian /* Fill pattern */ 167125943Semax for (n = 0; n < echo_size; ) { 168125943Semax int32_t avail = min(echo_size - n, PATTERN_SIZE); 169107120Sjulian 170125943Semax memcpy(echo_data + n, pattern, avail); 171107120Sjulian n += avail; 172107120Sjulian } 173107120Sjulian 174107120Sjulian /* Start ping'ing */ 175107120Sjulian for (n = 0; count == -1 || count > 0; n ++) { 176125943Semax struct ng_btsocket_l2cap_raw_ping r; 177125943Semax struct timeval a, b; 178125943Semax int32_t fail; 179125943Semax 180107120Sjulian if (gettimeofday(&a, NULL) < 0) 181107120Sjulian err(5, "Could not gettimeofday(a)"); 182107120Sjulian 183107120Sjulian fail = 0; 184125943Semax *((int32_t *) echo_data) = htonl(n); 185125943Semax 186125943Semax r.result = 0; 187125943Semax r.echo_size = echo_size; 188125943Semax r.echo_data = echo_data; 189107120Sjulian if (ioctl(s, SIOC_L2CAP_L2CA_PING, &r, sizeof(r)) < 0) { 190107120Sjulian r.result = errno; 191107120Sjulian fail = 1; 192107120Sjulian/* 193121054Semax warn("Could not ping, dst bdaddr=%s", 194121054Semax bt_ntoa(&r.echo_dst, NULL)); 195107120Sjulian*/ 196107120Sjulian } 197107120Sjulian 198107120Sjulian if (gettimeofday(&b, NULL) < 0) 199107120Sjulian err(7, "Could not gettimeofday(b)"); 200107120Sjulian 201107120Sjulian tv_sub(&b, &a); 202107120Sjulian 203107120Sjulian fprintf(stdout, 204121054Semax"%d bytes from %s seq_no=%d time=%.3f ms result=%#x %s\n", 205121054Semax r.echo_size, 206121054Semax bt_ntoa(&dst, NULL), 207125943Semax ntohl(*((int32_t *)(r.echo_data))), 208107120Sjulian tv2msec(&b), r.result, 209107120Sjulian ((fail == 0)? "" : strerror(errno))); 210107120Sjulian 211107120Sjulian if (!flood) { 212107120Sjulian /* Wait */ 213107120Sjulian a.tv_sec = wait; 214107120Sjulian a.tv_usec = 0; 215107120Sjulian select(0, NULL, NULL, NULL, &a); 216107120Sjulian } 217107120Sjulian 218107120Sjulian if (count != -1) 219107120Sjulian count --; 220107120Sjulian } 221107120Sjulian 222125943Semax free(echo_data); 223107120Sjulian close(s); 224107120Sjulian 225107120Sjulian return (0); 226107120Sjulian} /* main */ 227107120Sjulian 228107120Sjulian/* 229107120Sjulian * a -= b, for timevals 230107120Sjulian */ 231107120Sjulian 232107120Sjulianstatic void 233107120Sjuliantv_sub(struct timeval *a, struct timeval const *b) 234107120Sjulian{ 235107120Sjulian if (a->tv_usec < b->tv_usec) { 236107120Sjulian a->tv_usec += 1000000; 237107120Sjulian a->tv_sec -= 1; 238107120Sjulian } 239107120Sjulian 240107120Sjulian a->tv_usec -= b->tv_usec; 241107120Sjulian a->tv_sec -= b->tv_sec; 242107120Sjulian} /* tv_sub */ 243107120Sjulian 244107120Sjulian/* 245107120Sjulian * convert tv to msec 246107120Sjulian */ 247107120Sjulian 248107120Sjulianstatic double 249107120Sjuliantv2msec(struct timeval const *tvp) 250107120Sjulian{ 251107120Sjulian return(((double)tvp->tv_usec)/1000.0 + ((double)tvp->tv_sec)*1000.0); 252107120Sjulian} /* tv2msec */ 253107120Sjulian 254107120Sjulian/* 255107120Sjulian * Usage 256107120Sjulian */ 257107120Sjulian 258107120Sjulianstatic void 259107120Sjulianusage(void) 260107120Sjulian{ 261107120Sjulian fprintf(stderr, "Usage: l2ping -a bd_addr " \ 262114879Sjulian "[-S bd_addr -c count -i wait -s size -h]\n"); 263107120Sjulian fprintf(stderr, "Where:\n"); 264107120Sjulian fprintf(stderr, "\t-S bd_addr - Source BD_ADDR\n"); 265107120Sjulian fprintf(stderr, "\t-a bd_addr - Remote BD_ADDR to ping\n"); 266107120Sjulian fprintf(stderr, "\t-c count - Number of packets to send\n"); 267107120Sjulian fprintf(stderr, "\t-f - No delay (soft of flood)\n"); 268107120Sjulian fprintf(stderr, "\t-i wait - Delay between packets (sec)\n"); 269107120Sjulian fprintf(stderr, "\t-s size - Packet size (bytes), " \ 270128076Semax "between %zd and %zd\n", sizeof(int32_t), NG_L2CAP_MAX_ECHO_SIZE); 271114879Sjulian fprintf(stderr, "\t-h - Display this message\n"); 272107120Sjulian 273107120Sjulian exit(255); 274107120Sjulian} /* usage */ 275107120Sjulian 276