udp.c 5.2 KB

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