#include #include "listener.h" #include "client.h" #include "msg_impl.h" settings_t settings_init (settings); //status_t client (char_t* ip) { // int sockid; // struct sockaddr_in srvAddPort; //// struct sockaddr_in clntAddr; //// char buffer[256]; // // memset(&srvAddPort, 0, sizeof(srvAddPort)); // srvAddPort.sin_family= AF_INET; // srvAddPort.sin_port= htons(2288); // srvAddPort.sin_addr.s_addr = inet_addr(ip); // // if ((sockid = socket (PF_INET, SOCK_STREAM, 0)) == -1) // return MSG_ERROR; // // if (connect(sockid, (struct sockaddr*)&srvAddPort, sizeof(srvAddPort)) == -1) { // close (sockid); // return MSG_ERROR; // } // // if (send(sockid, sms, strlen(sms), MSG_CONFIRM) == -1) { // close(sockid); // return MSG_ERROR; // } // // close(sockid); // return MSG_OK; //} #include #include #include #include #include //// make a ping request //void send_ping(int ping_sockfd, struct sockaddr_in *ping_addr, // char *ping_dom, char *ping_ip, char *rev_host) //{ // int ttl_val=64, msg_count=0, i, addr_len, flag=1, // msg_received_count=0; // // struct ping_pkt pckt; // struct sockaddr_in r_addr; // struct timespec time_start, time_end, tfs, tfe; // long double rtt_msec=0, total_msec=0; // struct timeval tv_out; // int failure_cnt= 0; // int cnt; // // tv_out.tv_sec = RECV_TIMEOUT; // tv_out.tv_usec = 0; // // clock_gettime(CLOCK_MONOTONIC, &tfs); // // // // set socket options at ip to TTL and value to 64, // // change to what you want by setting ttl_val // if (setsockopt(ping_sockfd, SOL_IP, IP_TTL, // &ttl_val, sizeof(ttl_val)) != 0) // { // printf("\nSetting socket options to TTL failed!\n"); // return; // } // // else // { // printf("\nSocket set to TTL..\n"); // } // // // setting timeout of recv setting // setsockopt(ping_sockfd, SOL_SOCKET, SO_RCVTIMEO, // (const char*)&tv_out, sizeof tv_out); // // // send icmp packet in an infinite loop // while(pingloop) // { // // flag is whether packet was sent or not // flag=1; // // //filling packet // bzero(&pckt, sizeof(pckt)); // // pckt.hdr.type = ICMP_ECHO; // pckt.hdr.un.echo.id = getpid(); // // for ( i = 0; i < sizeof(pckt.msg)-1; i++ ) // pckt.msg[i] = i+'0'; // // pckt.msg[i] = 0; // pckt.hdr.un.echo.sequence = msg_count++; // pckt.hdr.checksum = checksum(&pckt, sizeof(pckt)); // // // usleep(PING_SLEEP_RATE); // // //send packet // clock_gettime(CLOCK_MONOTONIC, &time_start); // if ( sendto(ping_sockfd, &pckt, sizeof(pckt), 0, // (struct sockaddr*) ping_addr, // sizeof(*ping_addr)) <= 0) // { // printf("\nPacket Sending Failed!\n"); // flag=0; // } // // //receive packet // addr_len=sizeof(r_addr); //REC: // cnt = recvfrom(ping_sockfd, &pckt, sizeof(pckt), 0, // (struct sockaddr*)&r_addr, &addr_len); // if ( cnt <= 0 ) // { // printf("\nPacket receive failed!\n"); // failure_cnt++; // if(failure_cnt > 5){ // break; // } // } // // else // { // clock_gettime(CLOCK_MONOTONIC, &time_end); // // double timeElapsed = ((double)(time_end.tv_nsec - // time_start.tv_nsec))/1000000.0; // rtt_msec = (time_end.tv_sec- // time_start.tv_sec) * 1000.0 // + timeElapsed; // // // if packet was not sent, don't receive // if(flag) // { // struct icmp* icmp_hdr; // if (cnt >= 76) { // struct iphdr *iphdr = (struct iphdr *) &pckt; // /* skip ip hdr */ // icmp_hdr = (struct icmp *) (((char* )&pckt) + (iphdr->ihl << 2)); // } // if(icmp_hdr->icmp_type == ICMP_ECHO){ // goto REC; // } // // if(!(icmp_hdr->icmp_type !=ICMP_ECHOREPLY) ) // { // printf("Error..Packet received with ICMP" // "type %d code %d\n", // icmp_hdr->icmp_type, icmp_hdr->icmp_code); // } // else // { // printf("%d bytes from %s (h: %s)" // "(%s) msg_seq=%d ttl=%d " // "rtt = %Lf ms.\n", // PING_PKT_S, ping_dom, rev_host, // ping_ip, msg_count, // ttl_val, rtt_msec); // // msg_received_count++; // } // } // } // } // clock_gettime(CLOCK_MONOTONIC, &tfe); // double timeElapsed = ((double)(tfe.tv_nsec - // tfs.tv_nsec))/1000000.0; // // total_msec = (tfe.tv_sec-tfs.tv_sec)*1000.0+ // timeElapsed; // // printf("\n===%s ping statistics===\n", ping_ip); // printf("\n%d packets sent, %d packets received, %f percent " // "packet loss. Total time: %Lf ms.\n\n", // msg_count, msg_received_count, // ((msg_count - msg_received_count)/msg_count) * 100.0, // total_msec); //} int main(int argc, char const *argv[]) { pthread_t tL; pthread_create( &tL, NULL, thListener, NULL); log_init (); msgList_init (&msgList); bool b =true; while (b) { device_t d = { 0, NULL }; d.id = 43; if (ping (&d)) printf ("found: 10.0.0.43\n"); else printf ("not found: 10.0.0.43\n"); d.id = 8997; if (ping (&d)) printf ("found: 10.0.89.97\n"); else printf ("not found 10.0.89.97\n"); d.id = 7; if (ping (&d)) printf ("found: 10.0.0.7\n"); else printf ("not found: 10.0.0.7\n"); d.id = 1; if (ping (&d)) printf ("found: 10.0.0.1\n"); else printf ("not found: 10.0.0.1\n"); ping (NULL); sleep (10); } while (b) { } pthread_join( tL, NULL); return 0; }