patch-proxy-client_c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. $OpenBSD: patch-proxy-client_c,v 1.1 2002/08/10 01:14:04 naddy Exp $
  2. --- proxy-client.c.orig Fri Aug 2 22:15:44 2002
  3. +++ proxy-client.c Tue Mar 6 00:49:53 2001
  4. @@ -0,0 +1,179 @@
  5. +/* Copyright (C) 2000-1 drscholl@users.sourceforge.net
  6. + This is free software distributed under the terms of the
  7. + GNU Public License. See the file COPYING for details.
  8. +
  9. + proxy-client.c,v 1.3 2001/03/06 06:49:53 drscholl Exp */
  10. +
  11. +/* a simple proxy server to spy on the traffic between clients. this
  12. + is a lot easier than using tcpdump. */
  13. +
  14. +#ifdef HAVE_CONFIG_H
  15. +#include "config.h"
  16. +#endif
  17. +
  18. +#include <sys/socket.h>
  19. +#include <netinet/in.h>
  20. +#include <unistd.h>
  21. +#include <stdio.h>
  22. +#include <stdlib.h>
  23. +#include <arpa/inet.h>
  24. +#include <errno.h>
  25. +
  26. +unsigned char buf[2048];
  27. +
  28. +int
  29. +pass_message (const char *id, int s, int d)
  30. +{
  31. + int len;
  32. + int i;
  33. +
  34. + len = read (s, buf, sizeof (buf));
  35. + if (len <= 0)
  36. + {
  37. + if (len == -1)
  38. + printf ("%s: %s\n", id, strerror (errno));
  39. + else
  40. + printf ("%s: EOF\n", id);
  41. + return -1;
  42. + }
  43. + buf[len] = 0;
  44. +
  45. + for (i = 0; i < len; i++)
  46. + if (buf[i] == '\r')
  47. + buf[i] = 'M';
  48. +
  49. + printf ("%s: len=%d, data=%s\n", id, len, buf);
  50. +
  51. + write (d, buf, len);
  52. +
  53. + return 0;
  54. +}
  55. +
  56. +static void
  57. +usage (void)
  58. +{
  59. + puts ("usage: spyserv [ -s client ] [ -p clientport ] [ -l localport ]");
  60. + exit (0);
  61. +}
  62. +
  63. +int
  64. +main (int argc, char **argv)
  65. +{
  66. + int s;
  67. + int c;
  68. + int r;
  69. + int localport = 6699;
  70. + size_t sinsize;
  71. + struct sockaddr_in sin;
  72. + fd_set fds;
  73. + char *host = "192.168.0.101";
  74. + int port = 6699;
  75. +
  76. + while ((r = getopt (argc, argv, "hs:p:l:")) != EOF)
  77. + {
  78. + switch (r)
  79. + {
  80. + case 'l':
  81. + localport = atoi (optarg);
  82. + break;
  83. + case 's':
  84. + host = optarg;
  85. + break;
  86. + case 'p':
  87. + port = atoi (optarg);
  88. + break;
  89. + default:
  90. + usage ();
  91. + }
  92. + }
  93. +
  94. + /* accept connection from client */
  95. + s = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
  96. + if (s < 0)
  97. + {
  98. + perror ("socket");
  99. + exit (1);
  100. + }
  101. + c = 1;
  102. + if (setsockopt (s, SOL_SOCKET, SO_REUSEADDR, &c, sizeof (c)) != 0)
  103. + {
  104. + perror ("setsockopt");
  105. + exit (1);
  106. + }
  107. + memset (&sin, 0, sizeof (sin));
  108. + sin.sin_port = htons (localport);
  109. + sin.sin_family = AF_INET;
  110. + sin.sin_addr.s_addr = INADDR_ANY;
  111. + if (bind (s, &sin, sizeof (sin)) < 0)
  112. + {
  113. + perror ("bind");
  114. + exit (1);
  115. + }
  116. + if (listen (s, 1) < 0)
  117. + {
  118. + perror ("listen");
  119. + exit (1);
  120. + }
  121. +
  122. + for (;;)
  123. + {
  124. + puts ("waiting for connection");
  125. + sinsize = sizeof (sin);
  126. + c = accept (s, &sin, &sinsize);
  127. + if (c < 0)
  128. + {
  129. + perror ("accept");
  130. + exit (1);
  131. + }
  132. + puts ("got incoming connection");
  133. +
  134. + /* make connection to server */
  135. + printf ("connecting to client...");
  136. + fflush (stdout);
  137. + r = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
  138. + if (r < 0)
  139. + {
  140. + perror ("socket");
  141. + exit (1);
  142. + }
  143. + memset (&sin, 0, sizeof (sin));
  144. + sin.sin_port = htons (port);
  145. + sin.sin_family = AF_INET;
  146. + sin.sin_addr.s_addr = inet_addr (host);
  147. + if (connect (r, &sin, sizeof (sin)) < 0)
  148. + {
  149. + perror ("connect");
  150. + exit (1);
  151. + }
  152. + puts ("connected");
  153. +
  154. + for (;;)
  155. + {
  156. + FD_ZERO (&fds);
  157. + FD_SET (r, &fds);
  158. + FD_SET (c, &fds);
  159. + puts ("Waiting for input");
  160. + if (select (((r > c) ? r : c) + 1, &fds, 0, 0, 0) < 0)
  161. + {
  162. + perror ("select");
  163. + break;
  164. + }
  165. + if (FD_ISSET (c, &fds))
  166. + {
  167. + if (pass_message ("remote", c, r) != 0)
  168. + break;
  169. + }
  170. + if (FD_ISSET (r, &fds))
  171. + {
  172. + if (pass_message ("local", r, c) != 0)
  173. + break;
  174. + }
  175. + }
  176. +
  177. + close (r);
  178. + close (c);
  179. + }
  180. + close (s);
  181. +
  182. + exit (0);
  183. +}