2
0

udp.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (C) 2002-2003 FhG Fokus
  3. *
  4. * This file is sipsak, a free sip testing tool.
  5. *
  6. * sipsak is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * sipsak is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * Copyright (C) 2001-2003 FhG Fokus
  17. *
  18. * This file is part of Kamailio, a free SIP server.
  19. *
  20. * Kamailio is free software; you can redistribute it and/or modify
  21. * it under the terms of the GNU General Public License as published by
  22. * the Free Software Foundation; either version 2 of the License, or
  23. * (at your option) any later version
  24. *
  25. * Kamailio is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU General Public License
  31. * along with this program; if not, write to the Free Software
  32. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  33. */
  34. /* sipsak written by nils ohlmeier ([email protected]).
  35. based up on a modified version of shoot.
  36. set DEBUG on compile will produce much more output. primarily
  37. it will print out the sent and received messages before or after
  38. every network action.
  39. */
  40. /* changes by [email protected]; now messages can be really received;
  41. status code returned is 2 for some local errors , 0 for success
  42. and 1 for remote error -- ICMP/timeout; can be used to test if
  43. a server is alive; 1xx messages are now ignored; windows support
  44. dropped
  45. */
  46. /*
  47. shot written by ashhar farhan, is not bound by any licensing at all.
  48. you are free to use this code as you deem fit. Just don't blame the author
  49. for any problems you may have using it.
  50. bouquets and brickbats to [email protected]
  51. */
  52. /* TO-DO:
  53. - support for short notation
  54. - support for IPv6
  55. */
  56. //set ts=4 :-)
  57. #include <stdlib.h>
  58. #include <stdio.h>
  59. #include <sys/types.h>
  60. #include <sys/time.h>
  61. #include <string.h>
  62. #include <ctype.h>
  63. #include <time.h>
  64. #include <unistd.h>
  65. #include <netdb.h>
  66. #include <sys/socket.h>
  67. #include <sys/utsname.h>
  68. #include <regex.h>
  69. #include <netinet/in.h>
  70. #include <arpa/inet.h>
  71. #include <sys/poll.h>
  72. #include <errno.h>
  73. /* this is the main function with the loops and modes */
  74. void shoot()
  75. {
  76. struct sockaddr_in addr, sockname;
  77. struct timeval tv, sendtime, recvtime, firstsendt;
  78. struct timezone tz;
  79. struct pollfd sockerr;
  80. int ssock, redirected, retryAfter;
  81. int sock, i, len, ret, usrlocstep, randretrys;
  82. int dontsend, cseqcmp, cseqtmp;
  83. int rem_rand, rem_namebeg;
  84. char *contact, *crlf, *foo, *bar;
  85. fd_set fd;
  86. socklen_t slen;
  87. regex_t redexp, proexp, okexp, tmhexp, errexp;
  88. int bsd_compat, opt_size;
  89. int nretries=3;
  90. char *buff="MiniTest";
  91. /* create a sending socket */
  92. sock = (int)socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
  93. if (sock==-1) {
  94. perror("no client socket");
  95. exit(2);
  96. }
  97. #ifndef _NO_LISTENER
  98. /* create a listening socket */
  99. ssock = (int)socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
  100. if (ssock==-1) {
  101. perror("no server socket");
  102. exit(2);
  103. }
  104. sockname.sin_family=AF_INET;
  105. sockname.sin_addr.s_addr = htonl( INADDR_ANY );
  106. sockname.sin_port = htons((short)47437);
  107. if (bind( ssock, (struct sockaddr *) &sockname, sizeof(sockname) )==-1) {
  108. perror("no bind");
  109. exit(2);
  110. }
  111. #endif
  112. /* destination socket init here because it could be changed in a
  113. case of a redirect */
  114. addr.sin_addr.s_addr = inet_addr("192.168.99.100");
  115. addr.sin_port = htons((short)888);
  116. addr.sin_family = AF_INET;
  117. /* if we don't connect, even on Linux, nothing will happen */
  118. #ifdef CONNECTED
  119. /* we connect as per the RFC 2543 recommendations
  120. modified from sendto/recvfrom */
  121. ret = connect(sock, (struct sockaddr *)&addr, sizeof(addr));
  122. if (ret==-1) {
  123. perror("no connect");
  124. exit(2);
  125. }
  126. #endif
  127. if (getsockopt( sock, SOL_SOCKET, SO_BSDCOMPAT, &bsd_compat, &opt_size )==-1) {
  128. perror("ERROR");
  129. exit(1);
  130. }
  131. printf("BSD compat: %d\n", bsd_compat);
  132. /* here we go for the number of nretries which heavily depends on the
  133. mode */
  134. for (i = 0; i <= nretries; i++)
  135. {
  136. /* lets fire the request to the server and store when we did */
  137. /* if we send too fast, ICMP will arrive back when we are already
  138. done and we won't be able to recognize an error
  139. */
  140. #ifdef CONNECTED
  141. ret = send(sock, buff, strlen(buff), 0);
  142. #else
  143. ret=sendto(sock, buff, strlen(buff), 0, (struct sockaddr *)&addr, sizeof(addr));
  144. #endif
  145. /* wait 1/10 sec to be safe we receive ICMP */
  146. usleep(100000);
  147. if (ret==-1) {
  148. perror("send failure");
  149. exit( 1 );
  150. }
  151. }
  152. exit(0);
  153. }
  154. int main(int argc, char *argv[])
  155. {
  156. shoot();
  157. }