fishfinder.c (3808B)
1 /* $Id: //depot/blt/util/fishfinder.c#2 $ 2 ** 3 ** Copyright 1998 Brian J. Swetland 4 ** All rights reserved. 5 ** 6 ** Redistribution and use in source and binary forms, with or without 7 ** modification, are permitted provided that the following conditions 8 ** are met: 9 ** 1. Redistributions of source code must retain the above copyright 10 ** notice, this list of conditions, and the following disclaimer. 11 ** 2. Redistributions in binary form must reproduce the above copyright 12 ** notice, this list of conditions, and the following disclaimer in the 13 ** documentation and/or other materials provided with the distribution. 14 ** 3. The name of the author may not be used to endorse or promote products 15 ** derived from this software without specific prior written permission. 16 ** 17 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 ** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 ** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 ** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 ** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 ** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 ** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 #include <stdio.h> 29 30 #include <sys/types.h> 31 #include <sys/socket.h> 32 #include <sys/stat.h> 33 #include <fcntl.h> 34 #include <netinet/in.h> 35 36 #undef uint32 37 38 #define NEED_TYPES 39 #include "dfp.h" 40 41 char *types[] = { "PING", "PONG", "SEND", "ACK ", "NAK ", "SYNC" }; 42 43 int main(int argc, char *argv[]) 44 { 45 struct sockaddr_in dst,src; 46 dfp_pkt_transfer dfp,dfp2; 47 char n2[17]; 48 int s; 49 50 51 src.sin_family = AF_INET; 52 src.sin_addr.s_addr = htonl(INADDR_ANY); 53 src.sin_port = htons(DEFAULT_DFP_PORT); 54 55 if((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) 56 perror("socket"); 57 58 if(bind(s, (struct sockaddr *) &src, sizeof(src)) == -1) 59 perror("bind"); 60 61 for(;;){ 62 if(recvfrom(s,(void *)&dfp,sizeof(dfp),0,NULL,NULL) < 0) { 63 perror("recvfrom"); 64 } 65 dfp.base.size = ntohs(dfp.base.size); 66 dfp.base.version = ntohs(dfp.base.version); 67 68 if(dfp.base.version != DFP_VERSION){ 69 printf("version mismatch!\n"); 70 continue; 71 } 72 73 if(dfp.base.type > 5) continue; 74 75 printf("%s: (%d,%d) -> (%d,%d) ", types[dfp.base.type], 76 dfp.base.src_tank_x, dfp.base.src_tank_y, 77 dfp.base.dst_tank_x, dfp.base.dst_tank_y, 78 dfp.base.size); 79 80 switch(dfp.base.type){ 81 case DFP_PKT_PING: 82 case DFP_PKT_PONG: 83 printf("\n"); 84 break; 85 case DFP_PKT_SYNC_FISH: 86 case DFP_PKT_SEND_FISH: 87 printf("[%02x%02x%02x%02x%02x%02x] ", 88 dfp.uid.creator_tank_x, dfp.uid.creator_tank_y, 89 dfp.uid.timestamp[0], dfp.uid.timestamp[1], 90 dfp.uid.timestamp[2], dfp.uid.timestamp[3]); 91 strncpy(n2,dfp.fish.name,16); 92 n2[16]=0; 93 printf("@(%3d,%3d) d(%3d,%3d) \"%s\"\n", 94 dfp.fish.x, dfp.fish.y, dfp.fish.dx, dfp.fish.dy, n2); 95 break; 96 case DFP_PKT_NACK_FISH: 97 case DFP_PKT_ACK_FISH: 98 printf("[%02x%02x%02x%02x%02x%02x]\n", 99 dfp.uid.creator_tank_x, dfp.uid.creator_tank_y, 100 dfp.uid.timestamp[0], dfp.uid.timestamp[1], 101 dfp.uid.timestamp[2], dfp.uid.timestamp[3]); 102 break; 103 } 104 } 105 106 close(s); 107 return 0; 108 }