actions/listenclient.c

Go to the documentation of this file.
00001 /*************************************
00002 * Sample client for talking to a
00003 * socket listener
00004 *************************************/
00005 #include <arpa/inet.h>
00006 #include <sys/socket.h>
00007 #include <stdio.h>
00008 #include <string.h>
00009 #include <netdb.h>
00010 #include "action_alert_listensocket.h"
00011 
00012 /************************************
00013 * The one and only main
00014 ************************************/
00015 int main(int argc, char**argv){
00016         unsigned char           Buff[1024];
00017         struct hostent*         he;
00018         int                                     sockfd, numbytes;
00019         struct sockaddr_in      their_addr;
00020         unsigned short          port;
00021         DRec*                           drec;
00022         struct tm*                      tm;
00023 
00024         if (argc != 3) {
00025                 printf("usage: %s hostname port\n", argv[0]);
00026                 exit(1);
00027         }
00028 
00029         if ((he=gethostbyname(argv[1])) == NULL) {  // get the host info 
00030                 printf("Failed to resolve %s\n",argv[1]);
00031                 exit(1);
00032         }
00033 
00034         port=atoi(argv[2]);
00035         if (port==0xFFFF){
00036                 printf("Invalid port number %s\n",argv[2]);
00037                 exit(1);
00038         }
00039 
00040         if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
00041                 printf("Failed to create socket\n");
00042                 exit(1);
00043         }
00044 
00045         their_addr.sin_family = AF_INET;    // host byte order 
00046         their_addr.sin_port = htons(port);  // short, network byte order 
00047         their_addr.sin_addr = *((struct in_addr *)he->h_addr);
00048         memset(&(their_addr.sin_zero), '\0', 8);  // zero the rest of the struct 
00049 
00050         if (connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1) {
00051                 printf("Failed to connect to %s:%u\n",argv[1], port);
00052                 exit(1);
00053         }
00054 
00055         if ((numbytes=recv(sockfd, Buff, 1024-1, 0)) == -1) {
00056                 printf("Failed recieve banner\n");
00057                 exit(1);
00058         }
00059 
00060         if (strncmp("200", Buff, 3)!=0){
00061                 printf("Expected 200 Hello\n");
00062                 exit(1);
00063         }
00064 
00065         /*loop through and read the messages*/
00066         while (1){
00067                 bzero(Buff, 1024);
00068                 numbytes=recv(sockfd, &Buff, sizeof(DRec),0);
00069                 if (numbytes!=sizeof(DRec)){
00070                         printf("Unexpected data %i\n",numbytes);
00071                         exit(1);
00072                 }
00073                 drec=(DRec*)Buff;
00074                 printf("From the header:\n");
00075                 printf("  PreMagic %p\n", ntohl(drec->PreMagic));
00076                 printf("  Type %u\n",drec->Type);
00077                 printf("  Len %u\n",ntohs(drec->Len));
00078                 printf("\n");
00079                                 
00080                 numbytes=recv(sockfd, &Buff[sizeof(DRec)], ntohs(drec->Len)-sizeof(DRec),0);
00081                 
00082                 switch (drec->Type){
00083                 case LDATA_TYPE_STATISTICS:
00084                         {
00085                         DRecStat*       drecstat;
00086                         int                     Time;
00087                         
00088                         drecstat=(DRecStat*)Buff;
00089                         Time=ntohl(drecstat->Time);
00090                         tm=localtime((void*)&Time);
00091                         printf("Sensor statistics:\n");
00092                         printf("  Time %i/%i/%i %02i:%02i:%02i\n",tm->tm_mon+1, tm->tm_mday, tm->tm_year+1900, tm->tm_hour, tm->tm_min, tm->tm_sec);
00093                         printf("  Total Packets/sec %u\n",ntohs(drecstat->PacketCount));
00094                         printf("  TCP Packets/sec   %u\n",ntohs(drecstat->TCPCount));
00095                         printf("  UDP Packets/sec   %u\n",ntohs(drecstat->UDPCount));
00096                         }
00097                         break;
00098                 case LDATA_TYPE_ALERT:
00099                         {
00100                         DRecAlert*      alert;
00101                         
00102                         printf("Alert:\n");
00103                         printf("  Message %s\n",alert->Message);
00104                         }
00105                         break;
00106                 default:
00107                         printf("Unknown message type\n");
00108                 }
00109 
00110         }
00111 
00112         close(sockfd);
00113 
00114         return 0;       
00115 }

Generated on Sat Jul 7 23:33:10 2007 for HLBR by  doxygen 1.5.2