A messenger application for Raspberry Pi Zerofor A.U.TH (Real time Embedded systems).
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

232 lines
6.2 KiB

  1. #include <pthread.h>
  2. #include "listener.h"
  3. #include "client.h"
  4. #include "msg_impl.h"
  5. settings_t settings_init (settings);
  6. //status_t client (char_t* ip) {
  7. // int sockid;
  8. // struct sockaddr_in srvAddPort;
  9. //// struct sockaddr_in clntAddr;
  10. //// char buffer[256];
  11. //
  12. // memset(&srvAddPort, 0, sizeof(srvAddPort));
  13. // srvAddPort.sin_family= AF_INET;
  14. // srvAddPort.sin_port= htons(2288);
  15. // srvAddPort.sin_addr.s_addr = inet_addr(ip);
  16. //
  17. // if ((sockid = socket (PF_INET, SOCK_STREAM, 0)) == -1)
  18. // return MSG_ERROR;
  19. //
  20. // if (connect(sockid, (struct sockaddr*)&srvAddPort, sizeof(srvAddPort)) == -1) {
  21. // close (sockid);
  22. // return MSG_ERROR;
  23. // }
  24. //
  25. // if (send(sockid, sms, strlen(sms), MSG_CONFIRM) == -1) {
  26. // close(sockid);
  27. // return MSG_ERROR;
  28. // }
  29. //
  30. // close(sockid);
  31. // return MSG_OK;
  32. //}
  33. #include <sys/socket.h>
  34. #include <arpa/inet.h>
  35. #include <netinet/ip_icmp.h>
  36. #include <stdio.h>
  37. #include <unistd.h>
  38. //// make a ping request
  39. //void send_ping(int ping_sockfd, struct sockaddr_in *ping_addr,
  40. // char *ping_dom, char *ping_ip, char *rev_host)
  41. //{
  42. // int ttl_val=64, msg_count=0, i, addr_len, flag=1,
  43. // msg_received_count=0;
  44. //
  45. // struct ping_pkt pckt;
  46. // struct sockaddr_in r_addr;
  47. // struct timespec time_start, time_end, tfs, tfe;
  48. // long double rtt_msec=0, total_msec=0;
  49. // struct timeval tv_out;
  50. // int failure_cnt= 0;
  51. // int cnt;
  52. //
  53. // tv_out.tv_sec = RECV_TIMEOUT;
  54. // tv_out.tv_usec = 0;
  55. //
  56. // clock_gettime(CLOCK_MONOTONIC, &tfs);
  57. //
  58. //
  59. // // set socket options at ip to TTL and value to 64,
  60. // // change to what you want by setting ttl_val
  61. // if (setsockopt(ping_sockfd, SOL_IP, IP_TTL,
  62. // &ttl_val, sizeof(ttl_val)) != 0)
  63. // {
  64. // printf("\nSetting socket options to TTL failed!\n");
  65. // return;
  66. // }
  67. //
  68. // else
  69. // {
  70. // printf("\nSocket set to TTL..\n");
  71. // }
  72. //
  73. // // setting timeout of recv setting
  74. // setsockopt(ping_sockfd, SOL_SOCKET, SO_RCVTIMEO,
  75. // (const char*)&tv_out, sizeof tv_out);
  76. //
  77. // // send icmp packet in an infinite loop
  78. // while(pingloop)
  79. // {
  80. // // flag is whether packet was sent or not
  81. // flag=1;
  82. //
  83. // //filling packet
  84. // bzero(&pckt, sizeof(pckt));
  85. //
  86. // pckt.hdr.type = ICMP_ECHO;
  87. // pckt.hdr.un.echo.id = getpid();
  88. //
  89. // for ( i = 0; i < sizeof(pckt.msg)-1; i++ )
  90. // pckt.msg[i] = i+'0';
  91. //
  92. // pckt.msg[i] = 0;
  93. // pckt.hdr.un.echo.sequence = msg_count++;
  94. // pckt.hdr.checksum = checksum(&pckt, sizeof(pckt));
  95. //
  96. //
  97. // usleep(PING_SLEEP_RATE);
  98. //
  99. // //send packet
  100. // clock_gettime(CLOCK_MONOTONIC, &time_start);
  101. // if ( sendto(ping_sockfd, &pckt, sizeof(pckt), 0,
  102. // (struct sockaddr*) ping_addr,
  103. // sizeof(*ping_addr)) <= 0)
  104. // {
  105. // printf("\nPacket Sending Failed!\n");
  106. // flag=0;
  107. // }
  108. //
  109. // //receive packet
  110. // addr_len=sizeof(r_addr);
  111. //REC:
  112. // cnt = recvfrom(ping_sockfd, &pckt, sizeof(pckt), 0,
  113. // (struct sockaddr*)&r_addr, &addr_len);
  114. // if ( cnt <= 0 )
  115. // {
  116. // printf("\nPacket receive failed!\n");
  117. // failure_cnt++;
  118. // if(failure_cnt > 5){
  119. // break;
  120. // }
  121. // }
  122. //
  123. // else
  124. // {
  125. // clock_gettime(CLOCK_MONOTONIC, &time_end);
  126. //
  127. // double timeElapsed = ((double)(time_end.tv_nsec -
  128. // time_start.tv_nsec))/1000000.0;
  129. // rtt_msec = (time_end.tv_sec-
  130. // time_start.tv_sec) * 1000.0
  131. // + timeElapsed;
  132. //
  133. // // if packet was not sent, don't receive
  134. // if(flag)
  135. // {
  136. // struct icmp* icmp_hdr;
  137. // if (cnt >= 76) {
  138. // struct iphdr *iphdr = (struct iphdr *) &pckt;
  139. // /* skip ip hdr */
  140. // icmp_hdr = (struct icmp *) (((char* )&pckt) + (iphdr->ihl << 2));
  141. // }
  142. // if(icmp_hdr->icmp_type == ICMP_ECHO){
  143. // goto REC;
  144. // }
  145. //
  146. // if(!(icmp_hdr->icmp_type !=ICMP_ECHOREPLY) )
  147. // {
  148. // printf("Error..Packet received with ICMP"
  149. // "type %d code %d\n",
  150. // icmp_hdr->icmp_type, icmp_hdr->icmp_code);
  151. // }
  152. // else
  153. // {
  154. // printf("%d bytes from %s (h: %s)"
  155. // "(%s) msg_seq=%d ttl=%d "
  156. // "rtt = %Lf ms.\n",
  157. // PING_PKT_S, ping_dom, rev_host,
  158. // ping_ip, msg_count,
  159. // ttl_val, rtt_msec);
  160. //
  161. // msg_received_count++;
  162. // }
  163. // }
  164. // }
  165. // }
  166. // clock_gettime(CLOCK_MONOTONIC, &tfe);
  167. // double timeElapsed = ((double)(tfe.tv_nsec -
  168. // tfs.tv_nsec))/1000000.0;
  169. //
  170. // total_msec = (tfe.tv_sec-tfs.tv_sec)*1000.0+
  171. // timeElapsed;
  172. //
  173. // printf("\n===%s ping statistics===\n", ping_ip);
  174. // printf("\n%d packets sent, %d packets received, %f percent "
  175. // "packet loss. Total time: %Lf ms.\n\n",
  176. // msg_count, msg_received_count,
  177. // ((msg_count - msg_received_count)/msg_count) * 100.0,
  178. // total_msec);
  179. //}
  180. int main(int argc, char const *argv[]) {
  181. pthread_t tL;
  182. pthread_create( &tL, NULL, thListener, NULL);
  183. log_init ();
  184. msgList_init (&msgList);
  185. bool b =true;
  186. while (b) {
  187. device_t d = { 0, NULL };
  188. d.id = 43;
  189. if (ping (&d)) printf ("found: 10.0.0.43\n");
  190. else printf ("not found: 10.0.0.43\n");
  191. d.id = 8997;
  192. if (ping (&d)) printf ("found: 10.0.89.97\n");
  193. else printf ("not found 10.0.89.97\n");
  194. d.id = 7;
  195. if (ping (&d)) printf ("found: 10.0.0.7\n");
  196. else printf ("not found: 10.0.0.7\n");
  197. d.id = 1;
  198. if (ping (&d)) printf ("found: 10.0.0.1\n");
  199. else printf ("not found: 10.0.0.1\n");
  200. ping (NULL);
  201. sleep (10);
  202. }
  203. while (b) { }
  204. pthread_join( tL, NULL);
  205. return 0;
  206. }