httpap.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. /**
  2. * httpap.c
  3. * HTTP Application Proxy for Onion Routing
  4. *
  5. * Matej Pfajfar <mp292@cam.ac.uk>
  6. */
  7. /*
  8. * Changes :
  9. * $Log$
  10. * Revision 1.1 2002/06/26 22:45:50 arma
  11. * Initial revision
  12. *
  13. * Revision 1.4 2002/06/14 20:45:26 mp292
  14. * Extra debugging message.
  15. *
  16. * Revision 1.3 2002/04/02 14:27:33 badbytes
  17. * Final finishes.
  18. *
  19. * Revision 1.2 2002/03/12 23:40:58 mp292
  20. * Tested.
  21. *
  22. * Revision 1.1 2002/03/11 00:21:53 mp292
  23. * Coding completed. Pending testing.
  24. *
  25. */
  26. #include <sys/types.h>
  27. #include <sys/socket.h>
  28. #include <sys/time.h>
  29. #include <netinet/in.h>
  30. #include <netdb.h>
  31. #include <arpa/inet.h>
  32. #include <errno.h>
  33. #include <ctype.h>
  34. #include <stdio.h>
  35. #include <unistd.h>
  36. #include <signal.h>
  37. #include <wait.h>
  38. #include <stdarg.h>
  39. #include <ctype.h>
  40. #include <stdint.h>
  41. #include <string.h>
  42. #include <stdlib.h>
  43. #include <time.h>
  44. #include "../common/log.h"
  45. #include "../common/config.h"
  46. #include "../common/ss.h"
  47. #include "../common/utils.h"
  48. #include "../common/version.h"
  49. #include "httpap.h"
  50. #include "http.h"
  51. int loglevel = LOG_ERR;
  52. struct timeval conn_tout;
  53. struct timeval *conn_toutp = &conn_tout;
  54. /* valid command-line options */
  55. static const char *args = "hf:p:l:";
  56. /* valid config file options */
  57. static config_opt_t options[] =
  58. {
  59. {"OnionProxy", CONFIG_TYPE_INT, {0}, 0},
  60. {"MaxConn", CONFIG_TYPE_INT, {0}, 0},
  61. {"Anonimize", CONFIG_TYPE_INT, {0}, 0},
  62. {"ConnTimeout", CONFIG_TYPE_INT, {0}, 0},
  63. {0}
  64. };
  65. enum opts {
  66. OnionProxy=0,MaxConn, Anonimize, ConnTimeout
  67. };
  68. /* number of open connections */
  69. int connections=0;
  70. /* prints help on using httpap */
  71. void print_usage()
  72. {
  73. char *program = "httpap";
  74. printf("\n%s - HTTP application proxy for Onion Routing.\nUsage : %s -f config [-p port -l loglevel -h]\n-h : display this help\n-f config : config file\n-p port : port number which %s should bind to\n-l loglevel : logging threshold; one of alert|crit|err|warning|notice|info|debug\n\n", program,program,program);
  75. }
  76. /* used for reaping zombie processes */
  77. void sigchld_handler(int s)
  78. {
  79. while (wait(NULL) > 0);
  80. connections--;
  81. }
  82. int handle_connection(int new_sock, struct hostent *local, struct sockaddr_in remote, uint16_t op_port)
  83. {
  84. int retval = 0;
  85. int i;
  86. char islocal = 0; /* is the accepted connection local? */
  87. char *cp; /* character pointer used for checking whether the connection is local */
  88. unsigned char *line; /* one line of input */
  89. int len; /* length of the line */
  90. unsigned char *http_ver; /* HTTP version of the incoming request */
  91. unsigned char *addr; /* destination address */
  92. unsigned char *port; /* destination port */
  93. unsigned char *header_name; /* name of a request header */
  94. uint16_t portn; /* destination port converted into an integer */
  95. char *errtest; /* error check when converting the port into an integer */
  96. ss_t ss; /* standard structure */
  97. unsigned char errcode; /* error code returned by the onion proxy */
  98. int sop; /* socket for connecting to the onion proxy */
  99. struct sockaddr_in op_addr; /* onion proxy address */
  100. /* for use with select() */
  101. fd_set mask,rmask;
  102. int maxfd;
  103. unsigned char buf[1024]; /* data buffer */
  104. log(LOG_DEBUG, "handle_connection() : Local address = %s.", inet_ntoa(*(struct in_addr *)local->h_addr));
  105. log(LOG_DEBUG, "handle_connection() : Remote address = %s.", inet_ntoa(remote.sin_addr));
  106. /* first check that the connection is from the local host, otherwise it will be rejected */
  107. if (*(uint32_t *)&remote.sin_addr == inet_addr("127.0.0.1"))
  108. islocal = 1;
  109. for (i=0; (local->h_addr_list[i] != NULL) && (!islocal); i++)
  110. {
  111. cp = local->h_addr_list[i];
  112. log(LOG_DEBUG,"handle_connection() : Checking if connection is from address %s.",inet_ntoa(*(struct in_addr *)cp));
  113. if (!memcmp(&remote.sin_addr, cp, sizeof(struct in_addr)))
  114. islocal = 1;
  115. }
  116. /* bypass this check for testing purposes */
  117. islocal = 1;
  118. /* reject a non-local connection */
  119. if (!islocal)
  120. {
  121. close(new_sock);
  122. return 0;
  123. }
  124. /* get the request-line */
  125. retval = http_get_line(new_sock, &line, &len, conn_toutp);
  126. if (retval == -1)
  127. {
  128. log(LOG_DEBUG,"handle_connection : Malformed input or connection lost.");
  129. write_tout(new_sock, HTTPAP_STATUS_LINE_BAD_REQUEST, strlen(HTTPAP_STATUS_LINE_BAD_REQUEST), conn_toutp);
  130. close(new_sock);
  131. return -1;
  132. }
  133. log(LOG_DEBUG,"handle_connection : Received this from client : %s.", line);
  134. /* check the HTTP version */
  135. retval = http_get_version(line, &http_ver);
  136. if (retval == -1)
  137. {
  138. log(LOG_DEBUG,"handle_connection : Unable to extract the HTTP version of the incoming request.");
  139. write_tout(new_sock, HTTPAP_STATUS_LINE_BAD_REQUEST, strlen(HTTPAP_STATUS_LINE_BAD_REQUEST), conn_toutp);
  140. return -1;
  141. }
  142. log(LOG_DEBUG,"handle_connection : Client's version is : %s.",http_ver);
  143. if (strcmp(http_ver, HTTPAP_VERSION)) /* not supported */
  144. {
  145. log(LOG_DEBUG,"handle_connection : Client's version is %s, I only support HTTP/1.0.",http_ver);
  146. write_tout(new_sock, HTTPAP_STATUS_LINE_VERSION_NOT_SUPPORTED, strlen(HTTPAP_STATUS_LINE_VERSION_NOT_SUPPORTED), conn_toutp);
  147. return -1;
  148. }
  149. free((void *)http_ver);
  150. /* extract the destination address and port */
  151. retval = http_get_dest(line, &addr, &port);
  152. if (retval == -1)
  153. {
  154. log(LOG_DEBUG,"handle_connection : Unable to extract destination address and port number.");
  155. write_tout(new_sock, HTTPAP_STATUS_LINE_BAD_REQUEST, strlen(HTTPAP_STATUS_LINE_BAD_REQUEST), conn_toutp);
  156. return -1;
  157. }
  158. if (!port) /* no destination port specified, assume the default */
  159. {
  160. port = (unsigned char *)malloc(6);
  161. if (!port)
  162. {
  163. log(LOG_ERR,"Insufficient memory.");
  164. write_tout(new_sock, HTTPAP_STATUS_LINE_UNEXPECTED, strlen(HTTPAP_STATUS_LINE_UNEXPECTED), conn_toutp);
  165. return -1;
  166. }
  167. snprintf(port,6,"%u",htons(HTTPAP_DEFAULT_HTTP_PORT));
  168. }
  169. else
  170. {
  171. log(LOG_DEBUG,"handle_connection() : Destination address is %s.",addr);
  172. log(LOG_DEBUG,"handle_connection() : Destination port is %s.",port);
  173. /* conver the port to an integer */
  174. portn = (uint16_t)strtoul(port,&errtest,0);
  175. if ((*port == '\0') || (*errtest != '\0')) /* port conversion was unsuccessful */
  176. {
  177. log(LOG_DEBUG,"handle_connection : Unable to convert destination port.");
  178. write_tout(new_sock, HTTPAP_STATUS_LINE_BAD_REQUEST, strlen(HTTPAP_STATUS_LINE_BAD_REQUEST), conn_toutp);
  179. return -1;
  180. }
  181. /* convert to network order and write back to a string */
  182. free((void *)port);
  183. port = (unsigned char *)malloc(6);
  184. if (!port)
  185. {
  186. log(LOG_ERR,"Insufficient memory.");
  187. write_tout(new_sock, HTTPAP_STATUS_LINE_UNEXPECTED, strlen(HTTPAP_STATUS_LINE_UNEXPECTED), conn_toutp);
  188. return -1;
  189. }
  190. snprintf(port,6,"%u",htons(portn));
  191. }
  192. /* create a standard structure */
  193. ss.version = VERSION;
  194. ss.protocol = SS_PROTOCOL_HTTP;
  195. ss.retry_count = 0;
  196. ss.addr_fmt = SS_ADDR_FMT_ASCII_HOST_PORT;
  197. /* open a socket for connecting to the proxy */
  198. sop = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
  199. if (sop < 0)
  200. {
  201. log(LOG_DEBUG,"handle_connection() : Error opening socket.");
  202. write_tout(new_sock, HTTPAP_STATUS_LINE_UNEXPECTED, strlen(HTTPAP_STATUS_LINE_UNEXPECTED), conn_toutp);
  203. return -1;
  204. }
  205. log(LOG_DEBUG,"handle_connection() : Socket opened.");
  206. memset((void *)&op_addr,0,sizeof(op_addr)); /* clear the structure first */
  207. /* set up the sockaddr_in structure */
  208. op_addr.sin_family=AF_INET;
  209. op_addr.sin_port=htons(op_port);
  210. memcpy((void *)&op_addr.sin_addr,local->h_addr,local->h_length);
  211. log(LOG_DEBUG,"handle_connection() : Trying to connect to %s at port %u.",inet_ntoa(*((struct in_addr *)local->h_addr)),op_port);
  212. /* try to connect */
  213. retval = connect(sop,(struct sockaddr *)&op_addr,sizeof(op_addr));
  214. if (retval == -1)
  215. {
  216. log(LOG_DEBUG,"handle_connection() : Connection to the onion proxy failed.");
  217. write_tout(new_sock, HTTPAP_STATUS_LINE_UNAVAILABLE, strlen(HTTPAP_STATUS_LINE_UNAVAILABLE), conn_toutp);
  218. close(sop);
  219. return -1;
  220. }
  221. /* send the standard structure and the destination address+port */
  222. retval = write_tout(sop,(unsigned char *)&ss, sizeof(ss), conn_toutp);
  223. if (retval < sizeof(ss))
  224. {
  225. write_tout(new_sock, HTTPAP_STATUS_LINE_UNAVAILABLE, strlen(HTTPAP_STATUS_LINE_UNAVAILABLE), conn_toutp);
  226. close(sop);
  227. return -1;
  228. }
  229. retval = write_tout(sop,addr,strlen(addr)+1, conn_toutp);
  230. if (retval < strlen(addr)+1)
  231. {
  232. write_tout(new_sock, HTTPAP_STATUS_LINE_UNAVAILABLE, strlen(HTTPAP_STATUS_LINE_UNAVAILABLE), conn_toutp);
  233. close(sop);
  234. return -1;
  235. }
  236. retval = write_tout(sop,port,strlen(port)+1, conn_toutp);
  237. if (retval < strlen(port)+1)
  238. {
  239. write_tout(new_sock, HTTPAP_STATUS_LINE_UNAVAILABLE, strlen(HTTPAP_STATUS_LINE_UNAVAILABLE), conn_toutp);
  240. close(sop);
  241. return -1;
  242. }
  243. /* wait for a return code */
  244. retval = read_tout(sop, &errcode, 1, MSG_WAITALL, conn_toutp);
  245. if (retval < 1)
  246. {
  247. write_tout(new_sock, HTTPAP_STATUS_LINE_UNAVAILABLE, strlen(HTTPAP_STATUS_LINE_UNAVAILABLE), conn_toutp);
  248. close(sop);
  249. return -1;
  250. }
  251. if (!errcode) /* onion proxy says OK */
  252. {
  253. /* send the request-line */
  254. retval = write_tout(sop, line, strlen(line), conn_toutp);
  255. if (retval < strlen(line))
  256. {
  257. write_tout(new_sock, HTTPAP_STATUS_LINE_UNAVAILABLE, strlen(HTTPAP_STATUS_LINE_UNAVAILABLE), conn_toutp);
  258. close(new_sock);
  259. return -1;
  260. }
  261. free((void *)line);
  262. /* read the request headers (if any) and sanitize if necessary */
  263. while(1)
  264. {
  265. retval = http_get_line(new_sock, &line, &len, conn_toutp);
  266. if (retval == -1)
  267. {
  268. log(LOG_DEBUG,"handle_connection() : Malformed input or connection lost.");
  269. write_tout(new_sock, HTTPAP_STATUS_LINE_BAD_REQUEST, strlen(HTTPAP_STATUS_LINE_BAD_REQUEST), conn_toutp);
  270. close(new_sock);
  271. return -1;
  272. }
  273. log(LOG_DEBUG,"handle_connection() : Received this from client : %s.", line);
  274. if (len == 2) /* empty line (CRLF only) signifying the end of headers */
  275. {
  276. log(LOG_DEBUG,"handle_connection() : Empty line received.");
  277. retval = write_tout(sop,line,strlen(line),conn_toutp);
  278. if (retval < strlen(line))
  279. {
  280. write_tout(new_sock, HTTPAP_STATUS_LINE_UNAVAILABLE, strlen(HTTPAP_STATUS_LINE_UNAVAILABLE), conn_toutp);
  281. close(new_sock);
  282. return -1;
  283. }
  284. free((void *)line);
  285. break;
  286. }
  287. else /* process the header */
  288. {
  289. retval = http_get_header_name(line, &header_name);
  290. if (retval == -1)
  291. {
  292. log(LOG_DEBUG,"handle_connection : Unable to extract header name.");
  293. write_tout(new_sock, HTTPAP_STATUS_LINE_BAD_REQUEST, strlen(HTTPAP_STATUS_LINE_BAD_REQUEST), conn_toutp);
  294. return -1;
  295. }
  296. log(LOG_DEBUG,"handle_connection : Identified the header as %s.", header_name);
  297. /* discard the Proxy-Connection header */
  298. if (!strcmp(header_name,HTTPAP_HEADER_PROXY_CONNECTION))
  299. free((void *)line);
  300. else if (options[Anonimize].r.i) /* did the user request anonimization? */
  301. {
  302. if (!strcmp(header_name,HTTPAP_HEADER_USER_AGENT))
  303. free((void *)line);
  304. else if (!strcmp(header_name, HTTPAP_HEADER_REFERER))
  305. free((void *)line);
  306. else
  307. {
  308. retval = write_tout(sop, line, strlen(line), conn_toutp);
  309. if (retval < strlen(line))
  310. {
  311. write_tout(new_sock, HTTPAP_STATUS_LINE_UNAVAILABLE, strlen(HTTPAP_STATUS_LINE_UNAVAILABLE), conn_toutp);
  312. close(new_sock);
  313. return -1;
  314. }
  315. }
  316. }
  317. else
  318. {
  319. retval = write_tout(sop, line, strlen(line), conn_toutp);
  320. if (retval < strlen(line))
  321. {
  322. write_tout(new_sock, HTTPAP_STATUS_LINE_UNAVAILABLE, strlen(HTTPAP_STATUS_LINE_UNAVAILABLE), conn_toutp);
  323. close(new_sock);
  324. return -1;
  325. }
  326. }
  327. free((void *)header_name);
  328. }
  329. }
  330. /* forward data in both directions until one of the principals closes it */
  331. /* set up for select() */
  332. log(LOG_DEBUG,"Header processed, forwarding data in both directions.");
  333. FD_ZERO(&mask);
  334. FD_ZERO(&rmask);
  335. FD_SET(new_sock, &mask);
  336. FD_SET(sop, &mask);
  337. if (sop > new_sock)
  338. maxfd = sop;
  339. else
  340. maxfd = new_sock;
  341. while(1)
  342. {
  343. rmask = mask;
  344. retval = select(maxfd+1,&rmask,NULL,NULL,NULL);
  345. if (retval < 0)
  346. {
  347. log(LOG_DEBUG,"handle_connection() : select() returned a negative integer");
  348. break;
  349. }
  350. if (FD_ISSET(sop,&rmask)) /* data from the onion proxy */
  351. {
  352. retval = read_tout(sop,buf,1024,0,conn_toutp);
  353. if (retval <= 0)
  354. {
  355. log(LOG_DEBUG,"handle_connection : Conection to the onion proxy lost.");
  356. close(sop);
  357. close(new_sock);
  358. break;
  359. }
  360. log(LOG_DEBUG,"handle_connection() : Received %u bytes from the onion proxy.",retval);
  361. retval = write_tout(new_sock, buf, retval, conn_toutp);
  362. if (retval <= 0)
  363. {
  364. log(LOG_DEBUG, "handle_connection : Connection to the client lost.");
  365. close(sop);
  366. close(new_sock);
  367. break;
  368. }
  369. }
  370. if (FD_ISSET(new_sock, &rmask))
  371. {
  372. retval = read_tout(new_sock,buf,1024,0,conn_toutp);
  373. if (retval <= 0)
  374. {
  375. log(LOG_DEBUG,"handle_connection : Conection to the client lost.");
  376. close(sop);
  377. close(new_sock);
  378. break;
  379. }
  380. log(LOG_DEBUG,"handle_connection() : Received %u bytes from the client.",retval);
  381. retval = write_tout(sop, buf, retval, conn_toutp);
  382. if (retval <= 0)
  383. {
  384. log(LOG_DEBUG, "handle_connection : Connection to the onion proxy lost.");
  385. close(sop);
  386. close(new_sock);
  387. break;
  388. }
  389. }
  390. }
  391. }
  392. else
  393. {
  394. log(LOG_DEBUG,"handle_connection() : Onion proxy returned a non-zero error code (%d)!", errcode);
  395. write_tout(new_sock, HTTPAP_STATUS_LINE_UNEXPECTED, strlen(HTTPAP_STATUS_LINE_UNEXPECTED), conn_toutp);
  396. close(sop);
  397. return -1;
  398. }
  399. return 0;
  400. }
  401. int main(int argc, char *argv[])
  402. {
  403. int retval = 0;
  404. char c; /* command-line option */
  405. /* configuration file */
  406. char *conf_filename = NULL;
  407. FILE *cf = NULL;
  408. struct hostent *local_host;
  409. char local_hostname[512];
  410. struct sockaddr_in local, remote; /* local and remote address info */
  411. int request_sock; /* where we listen for connections */
  412. int new_sock; /* for accepted connections */
  413. size_t sin_size; /* for accept() calls */
  414. u_short p; /* http proxy port */
  415. u_short op_port; /* onion proxy port */
  416. /* used for reaping zombie processes */
  417. struct sigaction sa;
  418. char *errtest = NULL; /* for detecting strtoul() errors */
  419. /* set default listening port */
  420. p = htons(HTTPAP_LISTEN_PORT);
  421. /* deal with program arguments */
  422. if ((argc < 2) && (argc > 5)) /* to few or too many arguments*/
  423. {
  424. print_usage();
  425. return -1;
  426. }
  427. opterr = 0;
  428. while ((c = getopt(argc,argv,args)) != -1)
  429. {
  430. switch(c)
  431. {
  432. case 'f': /* config file */
  433. conf_filename = optarg;
  434. break;
  435. case 'p':
  436. p = htons((u_short)strtoul(optarg,&errtest,0));
  437. if (errtest == optarg) /* error */
  438. {
  439. log(LOG_ERR,"Error : -p must be followed by an unsigned positive integer value.");
  440. print_usage();
  441. return -1;
  442. }
  443. break;
  444. case 'h':
  445. print_usage();
  446. return 0;
  447. break;
  448. case 'l':
  449. if (!strcmp(optarg,"emerg"))
  450. loglevel = LOG_EMERG;
  451. else if (!strcmp(optarg,"alert"))
  452. loglevel = LOG_ALERT;
  453. else if (!strcmp(optarg,"crit"))
  454. loglevel = LOG_CRIT;
  455. else if (!strcmp(optarg,"err"))
  456. loglevel = LOG_ERR;
  457. else if (!strcmp(optarg,"warning"))
  458. loglevel = LOG_WARNING;
  459. else if (!strcmp(optarg,"notice"))
  460. loglevel = LOG_NOTICE;
  461. else if (!strcmp(optarg,"info"))
  462. loglevel = LOG_INFO;
  463. else if (!strcmp(optarg,"debug"))
  464. loglevel = LOG_DEBUG;
  465. else
  466. {
  467. log(LOG_ERR,"Error : argument to -l must be one of alert|crit|err|warning|notice|info|debug.");
  468. print_usage();
  469. return -1;
  470. }
  471. break;
  472. case '?':
  473. if (isprint(c))
  474. log(LOG_ERR,"Missing argument or unknown option '-%c'.",optopt);
  475. else
  476. log(LOG_ERR,"Unknown option character 'x%x'.",optopt);
  477. print_usage();
  478. return -1;
  479. break;
  480. default:
  481. abort();
  482. }
  483. }
  484. /* the -f option is mandatory */
  485. if (conf_filename == NULL)
  486. {
  487. log(LOG_ERR,"You must specify a config file with the -f option. See help (-h).");
  488. return -1;
  489. }
  490. /* load config file */
  491. cf = open_config(conf_filename);
  492. if (!cf)
  493. {
  494. log(LOG_ERR,"Could not open configuration file %s.",conf_filename);
  495. return -1;
  496. }
  497. retval = parse_config(cf,options);
  498. if (retval)
  499. return -1;
  500. if (options[OnionProxy].err != 1)
  501. {
  502. log(LOG_ERR,"The OnionProxy option is mandatory.");
  503. return -1;
  504. }
  505. if (options[MaxConn].err != 1)
  506. {
  507. log(LOG_ERR,"The MaxConn option is mandatory.");
  508. return -1;
  509. }
  510. if (options[Anonimize].err != 1)
  511. {
  512. log(LOG_ERR,"The Anonimize option is mandatory.");
  513. return -1;
  514. }
  515. else if ((options[Anonimize].r.i != 0) && (options[Anonimize].r.i != 1))
  516. {
  517. log(LOG_ERR,"The Anonimize option takes the values 1 or 0.");
  518. return -1;
  519. }
  520. if (options[ConnTimeout].err != 1)
  521. {
  522. conn_tout.tv_sec = HTTPAP_DEFAULT_CONN_TIMEOUT;
  523. }
  524. else
  525. {
  526. if (!options[ConnTimeout].r.i)
  527. conn_toutp = NULL;
  528. else
  529. conn_tout.tv_sec = options[ConnTimeout].r.i;
  530. }
  531. conn_tout.tv_usec = 0;
  532. op_port = (u_short)options[OnionProxy].r.i;
  533. /* get local address so that we know where to get the onion proxy when we need it */
  534. retval = gethostname(local_hostname, (size_t)512);
  535. if (retval < 0)
  536. {
  537. log(LOG_ERR,"Error getting local hostname");
  538. return -1;
  539. }
  540. local_host = gethostbyname(local_hostname);
  541. if (!local_host)
  542. {
  543. log(LOG_ERR,"Error getting local address.");
  544. return -1;
  545. }
  546. log(LOG_DEBUG,"main() : Got local address : %s.",local_hostname);
  547. /* get the server up and running */
  548. request_sock = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
  549. if (request_sock < 0)
  550. {
  551. log(LOG_ERR,"Error opening socket.");
  552. return -1;
  553. }
  554. log(LOG_DEBUG,"Socket opened.");
  555. memset((void *)&local,0,sizeof(local)); /* clear the structure first */
  556. /* set up the sockaddr_in structure */
  557. local.sin_family=AF_INET;
  558. local.sin_addr.s_addr = INADDR_ANY;
  559. local.sin_port=p;
  560. /* bind it to the socket */
  561. retval = bind(request_sock,(struct sockaddr *)&local, sizeof(local));
  562. if (retval < 0)
  563. {
  564. log(LOG_ERR,"Error binding socket to local port %d.",ntohs(p));
  565. return retval;
  566. }
  567. log(LOG_DEBUG,"Socket bound to port %d.",ntohs(p));
  568. /* listen for connections */
  569. retval = listen(request_sock,SOMAXCONN);
  570. if (retval < 0)
  571. {
  572. log(LOG_ERR,"Could not listen for connections.");
  573. return retval;
  574. }
  575. log(LOG_DEBUG,"Listening for connections.");
  576. /* server should now be up and running */
  577. /* install the signal handler for making sure zombie processes are killed */
  578. sa.sa_handler = sigchld_handler;
  579. sigemptyset(&sa.sa_mask);
  580. sa.sa_flags = SA_RESTART;
  581. retval = sigaction(SIGCHLD,&sa,NULL);
  582. if (retval < 0)
  583. {
  584. log(LOG_ERR,"Could not install a signal handler.");
  585. return -1;
  586. }
  587. /* main server loop */
  588. /* I use a forking server technique - this isn't the most efficient way to do it,
  589. * but it is simpler. */
  590. while(1)
  591. {
  592. sin_size = sizeof(struct sockaddr_in);
  593. new_sock = accept(request_sock,(struct sockaddr *)&remote,&sin_size);
  594. if (new_sock == -1)
  595. {
  596. if (errno != EINTR)
  597. log(LOG_ERR,"Could not accept socket connection.");
  598. else
  599. log(LOG_DEBUG,"Interrupt received.");
  600. continue;
  601. }
  602. if (connections >= options[MaxConn].r.i)
  603. {
  604. log(LOG_NOTICE,"Number of maximum connections reached. Rejecting incoming request.");
  605. close(new_sock);
  606. continue;
  607. }
  608. log(LOG_DEBUG,"Accepted a connection from %s.",inet_ntoa(remote.sin_addr));
  609. connections++;
  610. if (!fork()) /* this is the child process */
  611. {
  612. close(request_sock); /* the child doesn't need the request socket anymore */
  613. /* Main logic of httpap. */
  614. retval = handle_connection(new_sock, local_host, remote, op_port);
  615. /* End main logic */
  616. exit(retval); /* done, exit */
  617. }
  618. close(new_sock); /* don't need this anymore */
  619. }
  620. return retval;
  621. }