tcp_main.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2001-2003 FhG Fokus
  5. *
  6. * This file is part of ser, a free SIP server.
  7. *
  8. * ser 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. * For a license to use the ser software under conditions
  14. * other than those described here, or to purchase support for this
  15. * software, please contact iptel.org by e-mail at the following addresses:
  16. * [email protected]
  17. *
  18. * ser is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. */
  27. /*
  28. * History:
  29. * --------
  30. * 2002-11-29 created by andrei
  31. * 2002-12-11 added tcp_send (andrei)
  32. * 2003-01-20 locking fixes, hashtables (andrei)
  33. * 2003-02-20 s/lock_t/gen_lock_t/ to avoid a conflict on solaris (andrei)
  34. * 2003-02-25 Nagle is disabled if -DDISABLE_NAGLE (andrei)
  35. * 2003-03-29 SO_REUSEADDR before calling bind to allow
  36. * server restart, Nagle set on the (hopefuly)
  37. * correct socket (jiri)
  38. * 2003-03-31 always try to find the corresponding tcp listen socket for
  39. * a temp. socket and store in in *->bind_address: added
  40. * find_tcp_si, modified tcpconn_connect (andrei)
  41. * 2003-04-14 set sockopts to TOS low delay (andrei)
  42. * 2003-06-30 moved tcp new connect checking & handling to
  43. * handle_new_connect (andrei)
  44. * 2003-07-09 tls_close called before closing the tcp connection (andrei)
  45. * 2003-10-24 converted to the new socket_info lists (andrei)
  46. * 2003-10-27 tcp port aliases support added (andrei)
  47. * 2003-11-04 always lock before manipulating refcnt; sendchild
  48. * does not inc refcnt by itself anymore (andrei)
  49. * 2003-11-07 different unix sockets are used for fd passing
  50. * to/from readers/writers (andrei)
  51. * 2003-11-17 handle_new_connect & tcp_connect will close the
  52. * new socket if tcpconn_new return 0 (e.g. out of mem) (andrei)
  53. * 2003-11-28 tcp_blocking_write & tcp_blocking_connect added (andrei)
  54. * 2004-11-08 dropped find_tcp_si and replaced with find_si (andrei)
  55. * 2005-06-07 new tcp optimized code, supports epoll (LT), sigio + real time
  56. * signals, poll & select (andrei)
  57. * 2005-06-26 *bsd kqueue support (andrei)
  58. * 2005-07-04 solaris /dev/poll support (andrei)
  59. * 2005-07-08 tcp_max_connections, tcp_connection_lifetime, don't accept
  60. * more connections if tcp_max_connections is exceeded (andrei)
  61. * 2005-10-21 cleanup all the open connections on exit
  62. * decrement the no. of open connections on timeout too (andrei) * 2006-01-30 queue send_fd request and execute them at the end of the
  63. * poll loop (#ifdef) (andrei)
  64. * process all children requests, before attempting to send
  65. * them new stuff (fixes some deadlocks) (andrei)
  66. * 2006-02-03 timers are run only once per s (andrei)
  67. * tcp children fds can be non-blocking; send fds are queued on
  68. * EAGAIN; lots of bug fixes (andrei)
  69. * 2006-02-06 better tcp_max_connections checks, tcp_connections_no moved to
  70. * shm (andrei)
  71. * 2006-04-12 tcp_send() changed to use struct dest_info (andrei)
  72. */
  73. #ifdef USE_TCP
  74. #ifndef SHM_MEM
  75. #error "shared memory support needed (add -DSHM_MEM to Makefile.defs)"
  76. #endif
  77. #include <sys/time.h>
  78. #include <sys/types.h>
  79. #include <sys/select.h>
  80. #include <sys/socket.h>
  81. #include <netinet/in.h>
  82. #include <netinet/in_systm.h>
  83. #include <netinet/ip.h>
  84. #include <netinet/tcp.h>
  85. #include <sys/uio.h> /* writev*/
  86. #include <netdb.h>
  87. #include <stdlib.h> /*exit() */
  88. #include <unistd.h>
  89. #include <errno.h>
  90. #include <string.h>
  91. #ifdef HAVE_SELECT
  92. #include <sys/select.h>
  93. #endif
  94. #include <sys/poll.h>
  95. #include "ip_addr.h"
  96. #include "pass_fd.h"
  97. #include "tcp_conn.h"
  98. #include "globals.h"
  99. #include "pt.h"
  100. #include "locking.h"
  101. #include "mem/mem.h"
  102. #include "mem/shm_mem.h"
  103. #include "timer.h"
  104. #include "sr_module.h"
  105. #include "tcp_server.h"
  106. #include "tcp_init.h"
  107. #include "tsend.h"
  108. #include "timer_ticks.h"
  109. #ifdef USE_TLS
  110. #include "tls/tls_server.h"
  111. #endif
  112. #include "tcp_info.h"
  113. #define local_malloc pkg_malloc
  114. #define local_free pkg_free
  115. #define HANDLE_IO_INLINE
  116. #include "io_wait.h"
  117. #include <fcntl.h> /* must be included after io_wait.h if SIGIO_RT is used */
  118. #define TCP_PASS_NEW_CONNECTION_ON_DATA /* don't pass a new connection
  119. immediately to a child, wait for
  120. some data on it first */
  121. #define TCP_LISTEN_BACKLOG 1024
  122. #define SEND_FD_QUEUE /* queue send fd requests on EAGAIN, instead of sending
  123. them immediately */
  124. #define TCP_CHILD_NON_BLOCKING
  125. #ifdef SEND_FD_QUEUE
  126. #ifndef TCP_CHILD_NON_BLOCKING
  127. #define TCP_CHILD_NON_BLOCKING
  128. #endif
  129. #define MAX_SEND_FD_QUEUE_SIZE tcp_max_fd_no
  130. #define SEND_FD_QUEUE_SIZE 128 /* initial size */
  131. #define MAX_SEND_FD_RETRIES 96 /* FIXME: not used for now */
  132. #define SEND_FD_QUEUE_TIMEOUT MS_TO_TICKS(2000) /* 2 s */
  133. #endif
  134. enum fd_types { F_NONE, F_SOCKINFO /* a tcp_listen fd */,
  135. F_TCPCONN, F_TCPCHILD, F_PROC };
  136. struct tcp_child{
  137. pid_t pid;
  138. int proc_no; /* ser proc_no, for debugging */
  139. int unix_sock; /* unix "read child" sock fd */
  140. int busy;
  141. int n_reqs; /* number of requests serviced so far */
  142. };
  143. int tcp_accept_aliases=0; /* by default don't accept aliases */
  144. int tcp_connect_timeout=DEFAULT_TCP_CONNECT_TIMEOUT;
  145. int tcp_send_timeout=DEFAULT_TCP_SEND_TIMEOUT;
  146. int tcp_con_lifetime=DEFAULT_TCP_CONNECTION_LIFETIME;
  147. enum poll_types tcp_poll_method=0; /* by default choose the best method */
  148. int tcp_max_connections=DEFAULT_TCP_MAX_CONNECTIONS;
  149. int tcp_max_fd_no=0;
  150. static int* tcp_connections_no=0; /* current open connections */
  151. /* connection hash table (after ip&port) , includes also aliases */
  152. struct tcp_conn_alias** tcpconn_aliases_hash=0;
  153. /* connection hash table (after connection id) */
  154. struct tcp_connection** tcpconn_id_hash=0;
  155. gen_lock_t* tcpconn_lock=0;
  156. static struct tcp_child* tcp_children;
  157. static int* connection_id=0; /* unique for each connection, used for
  158. quickly finding the corresponding connection
  159. for a reply */
  160. int unix_tcp_sock;
  161. static int tcp_proto_no=-1; /* tcp protocol number as returned by
  162. getprotobyname */
  163. static io_wait_h io_h;
  164. /* set all socket/fd options: disable nagle, tos lowdelay, non-blocking
  165. * return -1 on error */
  166. static int init_sock_opt(int s)
  167. {
  168. int flags;
  169. int optval;
  170. #ifdef DISABLE_NAGLE
  171. flags=1;
  172. if ( (tcp_proto_no!=-1) && (setsockopt(s, tcp_proto_no , TCP_NODELAY,
  173. &flags, sizeof(flags))<0) ){
  174. LOG(L_WARN, "WARNING: init_sock_opt: could not disable Nagle: %s\n",
  175. strerror(errno));
  176. }
  177. #endif
  178. /* tos*/
  179. optval = tos;
  180. if (setsockopt(s, IPPROTO_IP, IP_TOS, (void*)&optval,sizeof(optval)) ==-1){
  181. LOG(L_WARN, "WARNING: init_sock_opt: setsockopt tos: %s\n",
  182. strerror(errno));
  183. /* continue since this is not critical */
  184. }
  185. /* non-blocking */
  186. flags=fcntl(s, F_GETFL);
  187. if (flags==-1){
  188. LOG(L_ERR, "ERROR: init_sock_opt: fnctl failed: (%d) %s\n",
  189. errno, strerror(errno));
  190. goto error;
  191. }
  192. if (fcntl(s, F_SETFL, flags|O_NONBLOCK)==-1){
  193. LOG(L_ERR, "ERROR: init_sock_opt: fcntl: set non-blocking failed:"
  194. " (%d) %s\n", errno, strerror(errno));
  195. goto error;
  196. }
  197. return 0;
  198. error:
  199. return -1;
  200. }
  201. /* blocking connect on a non-blocking fd; it will timeout after
  202. * tcp_connect_timeout
  203. * if BLOCKING_USE_SELECT and HAVE_SELECT are defined it will internally
  204. * use select() instead of poll (bad if fd > FD_SET_SIZE, poll is preferred)
  205. */
  206. static int tcp_blocking_connect(int fd, const struct sockaddr *servaddr,
  207. socklen_t addrlen)
  208. {
  209. int n;
  210. #if defined(HAVE_SELECT) && defined(BLOCKING_USE_SELECT)
  211. fd_set sel_set;
  212. fd_set orig_set;
  213. struct timeval timeout;
  214. #else
  215. struct pollfd pf;
  216. #endif
  217. int elapsed;
  218. int to;
  219. int ticks;
  220. int err;
  221. unsigned int err_len;
  222. int poll_err;
  223. poll_err=0;
  224. to=tcp_connect_timeout;
  225. ticks=get_ticks();
  226. again:
  227. n=connect(fd, servaddr, addrlen);
  228. if (n==-1){
  229. if (errno==EINTR){
  230. elapsed=(get_ticks()-ticks)*TIMER_TICK;
  231. if (elapsed<to) goto again;
  232. else goto error_timeout;
  233. }
  234. if (errno!=EINPROGRESS && errno!=EALREADY){
  235. LOG(L_ERR, "ERROR: tcp_blocking_connect: (%d) %s\n",
  236. errno, strerror(errno));
  237. goto error;
  238. }
  239. }else goto end;
  240. /* poll/select loop */
  241. #if defined(HAVE_SELECT) && defined(BLOCKING_USE_SELECT)
  242. FD_ZERO(&orig_set);
  243. FD_SET(fd, &orig_set);
  244. #else
  245. pf.fd=fd;
  246. pf.events=POLLOUT;
  247. #endif
  248. while(1){
  249. elapsed=(get_ticks()-ticks)*TIMER_TICK;
  250. if (elapsed<to)
  251. to-=elapsed;
  252. else
  253. goto error_timeout;
  254. #if defined(HAVE_SELECT) && defined(BLOCKING_USE_SELECT)
  255. sel_set=orig_set;
  256. timeout.tv_sec=to;
  257. timeout.tv_usec=0;
  258. n=select(fd+1, 0, &sel_set, 0, &timeout);
  259. #else
  260. n=poll(&pf, 1, to*1000);
  261. #endif
  262. if (n<0){
  263. if (errno==EINTR) continue;
  264. LOG(L_ERR, "ERROR: tcp_blocking_connect: poll/select failed:"
  265. " (%d) %s\n", errno, strerror(errno));
  266. goto error;
  267. }else if (n==0) /* timeout */ continue;
  268. #if defined(HAVE_SELECT) && defined(BLOCKING_USE_SELECT)
  269. if (FD_ISSET(fd, &sel_set))
  270. #else
  271. if (pf.revents&(POLLERR|POLLHUP|POLLNVAL)){
  272. LOG(L_ERR, "ERROR: tcp_blocking_connect: poll error: flags %x\n",
  273. pf.revents);
  274. poll_err=1;
  275. }
  276. #endif
  277. {
  278. err_len=sizeof(err);
  279. getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &err_len);
  280. if ((err==0) && (poll_err==0)) goto end;
  281. if (err!=EINPROGRESS && err!=EALREADY){
  282. LOG(L_ERR, "ERROR: tcp_blocking_connect: SO_ERROR (%d) %s\n",
  283. err, strerror(err));
  284. goto error;
  285. }
  286. }
  287. }
  288. error_timeout:
  289. /* timeout */
  290. LOG(L_ERR, "ERROR: tcp_blocking_connect: timeout %d s elapsed from %d s\n",
  291. elapsed, tcp_connect_timeout);
  292. error:
  293. return -1;
  294. end:
  295. return 0;
  296. }
  297. #if 0
  298. /* blocking write even on non-blocking sockets
  299. * if TCP_TIMEOUT will return with error */
  300. static int tcp_blocking_write(struct tcp_connection* c, int fd, char* buf,
  301. unsigned int len)
  302. {
  303. int n;
  304. fd_set sel_set;
  305. struct timeval timeout;
  306. int ticks;
  307. int initial_len;
  308. initial_len=len;
  309. again:
  310. n=send(fd, buf, len,
  311. #ifdef HAVE_MSG_NOSIGNAL
  312. MSG_NOSIGNAL
  313. #else
  314. 0
  315. #endif
  316. );
  317. if (n<0){
  318. if (errno==EINTR) goto again;
  319. else if (errno!=EAGAIN && errno!=EWOULDBLOCK){
  320. LOG(L_ERR, "tcp_blocking_write: failed to send: (%d) %s\n",
  321. errno, strerror(errno));
  322. goto error;
  323. }
  324. }else if (n<len){
  325. /* partial write */
  326. buf+=n;
  327. len-=n;
  328. }else{
  329. /* success: full write */
  330. goto end;
  331. }
  332. while(1){
  333. FD_ZERO(&sel_set);
  334. FD_SET(fd, &sel_set);
  335. timeout.tv_sec=tcp_send_timeout;
  336. timeout.tv_usec=0;
  337. ticks=get_ticks();
  338. n=select(fd+1, 0, &sel_set, 0, &timeout);
  339. if (n<0){
  340. if (errno==EINTR) continue; /* signal, ignore */
  341. LOG(L_ERR, "ERROR: tcp_blocking_write: select failed: "
  342. " (%d) %s\n", errno, strerror(errno));
  343. goto error;
  344. }else if (n==0){
  345. /* timeout */
  346. if (get_ticks()-ticks>=tcp_send_timeout){
  347. LOG(L_ERR, "ERROR: tcp_blocking_write: send timeout (%d)\n",
  348. tcp_send_timeout);
  349. goto error;
  350. }
  351. continue;
  352. }
  353. if (FD_ISSET(fd, &sel_set)){
  354. /* we can write again */
  355. goto again;
  356. }
  357. }
  358. error:
  359. return -1;
  360. end:
  361. return initial_len;
  362. }
  363. #endif
  364. struct tcp_connection* tcpconn_new(int sock, union sockaddr_union* su,
  365. struct socket_info* ba, int type,
  366. int state)
  367. {
  368. struct tcp_connection *c;
  369. c=(struct tcp_connection*)shm_malloc(sizeof(struct tcp_connection));
  370. if (c==0){
  371. LOG(L_ERR, "ERROR: tcpconn_new: mem. allocation failure\n");
  372. goto error;
  373. }
  374. memset(c, 0, sizeof(struct tcp_connection)); /* zero init */
  375. c->s=sock;
  376. c->fd=-1; /* not initialized */
  377. if (lock_init(&c->write_lock)==0){
  378. LOG(L_ERR, "ERROR: tcpconn_new: init lock failed\n");
  379. goto error;
  380. }
  381. c->rcv.src_su=*su;
  382. c->refcnt=0;
  383. su2ip_addr(&c->rcv.src_ip, su);
  384. c->rcv.src_port=su_getport(su);
  385. c->rcv.bind_address=ba;
  386. if (ba){
  387. c->rcv.dst_ip=ba->address;
  388. c->rcv.dst_port=ba->port_no;
  389. }
  390. print_ip("tcpconn_new: new tcp connection: ", &c->rcv.src_ip, "\n");
  391. DBG( "tcpconn_new: on port %d, type %d\n", c->rcv.src_port, type);
  392. init_tcp_req(&c->req);
  393. c->id=(*connection_id)++;
  394. c->rcv.proto_reserved1=0; /* this will be filled before receive_message*/
  395. c->rcv.proto_reserved2=0;
  396. c->state=state;
  397. c->extra_data=0;
  398. #ifdef USE_TLS
  399. if (type==PROTO_TLS){
  400. if (tls_tcpconn_init(c, sock)==-1) goto error;
  401. }else
  402. #endif /* USE_TLS*/
  403. {
  404. c->type=PROTO_TCP;
  405. c->rcv.proto=PROTO_TCP;
  406. c->timeout=get_ticks()+tcp_con_lifetime;
  407. }
  408. c->flags|=F_CONN_REMOVED;
  409. return c;
  410. error:
  411. if (c) shm_free(c);
  412. return 0;
  413. }
  414. struct tcp_connection* tcpconn_connect(union sockaddr_union* server, int type)
  415. {
  416. int s;
  417. struct socket_info* si;
  418. union sockaddr_union my_name;
  419. socklen_t my_name_len;
  420. struct tcp_connection* con;
  421. struct ip_addr ip;
  422. s=-1;
  423. if (*tcp_connections_no >= tcp_max_connections){
  424. LOG(L_ERR, "ERROR: tcpconn_connect: maximum number of connections"
  425. " exceeded (%d/%d)\n",
  426. *tcp_connections_no, tcp_max_connections);
  427. goto error;
  428. }
  429. s=socket(AF2PF(server->s.sa_family), SOCK_STREAM, 0);
  430. if (s==-1){
  431. LOG(L_ERR, "ERROR: tcpconn_connect: socket: (%d) %s\n",
  432. errno, strerror(errno));
  433. goto error;
  434. }
  435. if (init_sock_opt(s)<0){
  436. LOG(L_ERR, "ERROR: tcpconn_connect: init_sock_opt failed\n");
  437. goto error;
  438. }
  439. if (tcp_blocking_connect(s, &server->s, sockaddru_len(*server))<0){
  440. LOG(L_ERR, "ERROR: tcpconn_connect: tcp_blocking_connect failed\n");
  441. goto error;
  442. }
  443. my_name_len=sizeof(my_name);
  444. if (getsockname(s, &my_name.s, &my_name_len)!=0){
  445. LOG(L_ERR, "ERROR: tcp_connect: getsockname failed: %s(%d)\n",
  446. strerror(errno), errno);
  447. si=0; /* try to go on */
  448. }
  449. su2ip_addr(&ip, &my_name);
  450. #ifdef USE_TLS
  451. if (type==PROTO_TLS)
  452. si=find_si(&ip, 0, PROTO_TLS);
  453. else
  454. #endif
  455. si=find_si(&ip, 0, PROTO_TCP);
  456. if (si==0){
  457. LOG(L_ERR, "ERROR: tcp_connect: could not find corresponding"
  458. " listening socket, using default...\n");
  459. if (server->s.sa_family==AF_INET) si=sendipv4_tcp;
  460. #ifdef USE_IPV6
  461. else si=sendipv6_tcp;
  462. #endif
  463. }
  464. con=tcpconn_new(s, server, si, type, S_CONN_CONNECT);
  465. if (con==0){
  466. LOG(L_ERR, "ERROR: tcp_connect: tcpconn_new failed, closing the "
  467. " socket\n");
  468. goto error;
  469. }
  470. return con;
  471. /*FIXME: set sock idx! */
  472. error:
  473. if (s!=-1) close(s); /* close the opened socket */
  474. return 0;
  475. }
  476. struct tcp_connection* tcpconn_add(struct tcp_connection *c)
  477. {
  478. unsigned hash;
  479. if (c){
  480. TCPCONN_LOCK;
  481. /* add it at the begining of the list*/
  482. hash=tcp_id_hash(c->id);
  483. c->id_hash=hash;
  484. tcpconn_listadd(tcpconn_id_hash[hash], c, id_next, id_prev);
  485. hash=tcp_addr_hash(&c->rcv.src_ip, c->rcv.src_port);
  486. /* set the first alias */
  487. c->con_aliases[0].port=c->rcv.src_port;
  488. c->con_aliases[0].hash=hash;
  489. c->con_aliases[0].parent=c;
  490. tcpconn_listadd(tcpconn_aliases_hash[hash], &c->con_aliases[0],
  491. next, prev);
  492. c->aliases++;
  493. TCPCONN_UNLOCK;
  494. DBG("tcpconn_add: hashes: %d, %d\n", hash, c->id_hash);
  495. return c;
  496. }else{
  497. LOG(L_CRIT, "tcpconn_add: BUG: null connection pointer\n");
  498. return 0;
  499. }
  500. }
  501. /* unsafe tcpconn_rm version (nolocks) */
  502. void _tcpconn_rm(struct tcp_connection* c)
  503. {
  504. int r;
  505. tcpconn_listrm(tcpconn_id_hash[c->id_hash], c, id_next, id_prev);
  506. /* remove all the aliases */
  507. for (r=0; r<c->aliases; r++)
  508. tcpconn_listrm(tcpconn_aliases_hash[c->con_aliases[r].hash],
  509. &c->con_aliases[r], next, prev);
  510. lock_destroy(&c->write_lock);
  511. #ifdef USE_TLS
  512. if (c->type==PROTO_TLS) tls_tcpconn_clean(c);
  513. #endif
  514. shm_free(c);
  515. }
  516. void tcpconn_rm(struct tcp_connection* c)
  517. {
  518. int r;
  519. TCPCONN_LOCK;
  520. tcpconn_listrm(tcpconn_id_hash[c->id_hash], c, id_next, id_prev);
  521. /* remove all the aliases */
  522. for (r=0; r<c->aliases; r++)
  523. tcpconn_listrm(tcpconn_aliases_hash[c->con_aliases[r].hash],
  524. &c->con_aliases[r], next, prev);
  525. TCPCONN_UNLOCK;
  526. lock_destroy(&c->write_lock);
  527. #ifdef USE_TLS
  528. if ((c->type==PROTO_TLS)&&(c->extra_data)) tls_tcpconn_clean(c);
  529. #endif
  530. shm_free(c);
  531. }
  532. /* finds a connection, if id=0 uses the ip addr & port (host byte order)
  533. * WARNING: unprotected (locks) use tcpconn_get unless you really
  534. * know what you are doing */
  535. struct tcp_connection* _tcpconn_find(int id, struct ip_addr* ip, int port)
  536. {
  537. struct tcp_connection *c;
  538. struct tcp_conn_alias* a;
  539. unsigned hash;
  540. #ifdef EXTRA_DEBUG
  541. DBG("tcpconn_find: %d port %d\n",id, port);
  542. if (ip) print_ip("tcpconn_find: ip ", ip, "\n");
  543. #endif
  544. if (id){
  545. hash=tcp_id_hash(id);
  546. for (c=tcpconn_id_hash[hash]; c; c=c->id_next){
  547. #ifdef EXTRA_DEBUG
  548. DBG("c=%p, c->id=%d, port=%d\n",c, c->id, c->rcv.src_port);
  549. print_ip("ip=", &c->rcv.src_ip, "\n");
  550. #endif
  551. if ((id==c->id)&&(c->state!=S_CONN_BAD)) return c;
  552. }
  553. }else if (ip){
  554. hash=tcp_addr_hash(ip, port);
  555. for (a=tcpconn_aliases_hash[hash]; a; a=a->next){
  556. #ifdef EXTRA_DEBUG
  557. DBG("a=%p, c=%p, c->id=%d, alias port= %d port=%d\n", a, a->parent,
  558. a->parent->id, a->port, a->parent->rcv.src_port);
  559. print_ip("ip=",&a->parent->rcv.src_ip,"\n");
  560. #endif
  561. if ( (a->parent->state!=S_CONN_BAD) && (port==a->port) &&
  562. (ip_addr_cmp(ip, &a->parent->rcv.src_ip)) )
  563. return a->parent;
  564. }
  565. }
  566. return 0;
  567. }
  568. /* _tcpconn_find with locks and timeout */
  569. struct tcp_connection* tcpconn_get(int id, struct ip_addr* ip, int port,
  570. int timeout)
  571. {
  572. struct tcp_connection* c;
  573. TCPCONN_LOCK;
  574. c=_tcpconn_find(id, ip, port);
  575. if (c){
  576. c->refcnt++;
  577. c->timeout=get_ticks()+timeout;
  578. }
  579. TCPCONN_UNLOCK;
  580. return c;
  581. }
  582. /* add port as an alias for the "id" connection
  583. * returns 0 on success,-1 on failure */
  584. int tcpconn_add_alias(int id, int port, int proto)
  585. {
  586. struct tcp_connection* c;
  587. unsigned hash;
  588. struct tcp_conn_alias* a;
  589. a=0;
  590. /* fix the port */
  591. port=port?port:((proto==PROTO_TLS)?SIPS_PORT:SIP_PORT);
  592. TCPCONN_LOCK;
  593. /* check if alias already exists */
  594. c=_tcpconn_find(id, 0, 0);
  595. if (c){
  596. hash=tcp_addr_hash(&c->rcv.src_ip, port);
  597. /* search the aliases for an already existing one */
  598. for (a=tcpconn_aliases_hash[hash]; a; a=a->next){
  599. if ( (a->parent->state!=S_CONN_BAD) && (port==a->port) &&
  600. (ip_addr_cmp(&c->rcv.src_ip, &a->parent->rcv.src_ip)) ){
  601. /* found */
  602. if (a->parent!=c) goto error_sec;
  603. else goto ok;
  604. }
  605. }
  606. if (c->aliases>=TCP_CON_MAX_ALIASES) goto error_aliases;
  607. c->con_aliases[c->aliases].parent=c;
  608. c->con_aliases[c->aliases].port=port;
  609. c->con_aliases[c->aliases].hash=hash;
  610. tcpconn_listadd(tcpconn_aliases_hash[hash],
  611. &c->con_aliases[c->aliases], next, prev);
  612. c->aliases++;
  613. }else goto error_not_found;
  614. ok:
  615. TCPCONN_UNLOCK;
  616. #ifdef EXTRA_DEBUG
  617. if (a) DBG("tcpconn_add_alias: alias already present\n");
  618. else DBG("tcpconn_add_alias: alias port %d for hash %d, id %d\n",
  619. port, hash, c->id);
  620. #endif
  621. return 0;
  622. error_aliases:
  623. TCPCONN_UNLOCK;
  624. LOG(L_ERR, "ERROR: tcpconn_add_alias: too many aliases for connection %p"
  625. " (%d)\n", c, c->id);
  626. return -1;
  627. error_not_found:
  628. TCPCONN_UNLOCK;
  629. LOG(L_ERR, "ERROR: tcpconn_add_alias: no connection found for id %d\n",id);
  630. return -1;
  631. error_sec:
  632. TCPCONN_UNLOCK;
  633. LOG(L_ERR, "ERROR: tcpconn_add_alias: possible port hijack attempt\n");
  634. LOG(L_ERR, "ERROR: tcpconn_add_alias: alias already present and points"
  635. " to another connection (%d : %d and %d : %d)\n",
  636. a->parent->id, port, c->id, port);
  637. return -1;
  638. }
  639. void tcpconn_ref(struct tcp_connection* c)
  640. {
  641. TCPCONN_LOCK;
  642. c->refcnt++; /* FIXME: atomic_dec */
  643. TCPCONN_UNLOCK;
  644. }
  645. void tcpconn_put(struct tcp_connection* c)
  646. {
  647. TCPCONN_LOCK;
  648. c->refcnt--; /* FIXME: atomic_dec */
  649. TCPCONN_UNLOCK;
  650. }
  651. /* finds a tcpconn & sends on it
  652. * uses the dst members to, proto (TCP|TLS) and id
  653. * returns: number of bytes written (>=0) on success
  654. * <0 on error */
  655. int tcp_send(struct dest_info* dst, char* buf, unsigned len)
  656. {
  657. struct tcp_connection *c;
  658. struct tcp_connection *tmp;
  659. struct ip_addr ip;
  660. int port;
  661. int fd;
  662. long response[2];
  663. int n;
  664. port=su_getport(&dst->to);
  665. if (port){
  666. su2ip_addr(&ip, &dst->to);
  667. c=tcpconn_get(dst->id, &ip, port, tcp_con_lifetime);
  668. }else if (dst->id){
  669. c=tcpconn_get(dst->id, 0, 0, tcp_con_lifetime);
  670. }else{
  671. LOG(L_CRIT, "BUG: tcp_send called with null id & to\n");
  672. return -1;
  673. }
  674. if (dst->id){
  675. if (c==0) {
  676. if (port){
  677. /* try again w/o id */
  678. c=tcpconn_get(0, &ip, port, tcp_con_lifetime);
  679. goto no_id;
  680. }else{
  681. LOG(L_ERR, "ERROR: tcp_send: id %d not found, dropping\n",
  682. dst->id);
  683. return -1;
  684. }
  685. }else goto get_fd;
  686. }
  687. no_id:
  688. if (c==0){
  689. DBG("tcp_send: no open tcp connection found, opening new one\n");
  690. /* create tcp connection */
  691. if ((c=tcpconn_connect(&dst->to, dst->proto))==0){
  692. LOG(L_ERR, "ERROR: tcp_send: connect failed\n");
  693. return -1;
  694. }
  695. c->refcnt++; /* safe to do it w/o locking, it's not yet
  696. available to the rest of the world */
  697. fd=c->s;
  698. /* send the new tcpconn to "tcp main" */
  699. response[0]=(long)c;
  700. response[1]=CONN_NEW;
  701. n=send_fd(unix_tcp_sock, response, sizeof(response), c->s);
  702. if (n<=0){
  703. LOG(L_ERR, "BUG: tcp_send: failed send_fd: %s (%d)\n",
  704. strerror(errno), errno);
  705. n=-1;
  706. goto end;
  707. }
  708. goto send_it;
  709. }
  710. get_fd:
  711. /* todo: see if this is not the same process holding
  712. * c and if so send directly on c->fd */
  713. DBG("tcp_send: tcp connection found (%p), acquiring fd\n", c);
  714. /* get the fd */
  715. response[0]=(long)c;
  716. response[1]=CONN_GET_FD;
  717. n=send_all(unix_tcp_sock, response, sizeof(response));
  718. if (n<=0){
  719. LOG(L_ERR, "BUG: tcp_send: failed to get fd(write):%s (%d)\n",
  720. strerror(errno), errno);
  721. n=-1;
  722. goto release_c;
  723. }
  724. DBG("tcp_send, c= %p, n=%d\n", c, n);
  725. tmp=c;
  726. n=receive_fd(unix_tcp_sock, &c, sizeof(c), &fd, MSG_WAITALL);
  727. if (n<=0){
  728. LOG(L_ERR, "BUG: tcp_send: failed to get fd(receive_fd):"
  729. " %s (%d)\n", strerror(errno), errno);
  730. n=-1;
  731. goto release_c;
  732. }
  733. if (c!=tmp){
  734. LOG(L_CRIT, "BUG: tcp_send: get_fd: got different connection:"
  735. " %p (id= %d, refcnt=%d state=%d != "
  736. " %p (id= %d, refcnt=%d state=%d (n=%d)\n",
  737. c, c->id, c->refcnt, c->state,
  738. tmp, tmp->id, tmp->refcnt, tmp->state, n
  739. );
  740. n=-1; /* fail */
  741. goto end;
  742. }
  743. DBG("tcp_send: after receive_fd: c= %p n=%d fd=%d\n",c, n, fd);
  744. send_it:
  745. DBG("tcp_send: sending...\n");
  746. lock_get(&c->write_lock);
  747. #ifdef USE_TLS
  748. if (c->type==PROTO_TLS)
  749. n=tls_blocking_write(c, fd, buf, len);
  750. else
  751. #endif
  752. /* n=tcp_blocking_write(c, fd, buf, len); */
  753. n=tsend_stream(fd, buf, len, tcp_send_timeout*1000);
  754. lock_release(&c->write_lock);
  755. DBG("tcp_send: after write: c= %p n=%d fd=%d\n",c, n, fd);
  756. DBG("tcp_send: buf=\n%.*s\n", (int)len, buf);
  757. if (n<0){
  758. LOG(L_ERR, "ERROR: tcp_send: failed to send\n");
  759. /* error on the connection , mark it as bad and set 0 timeout */
  760. c->state=S_CONN_BAD;
  761. c->timeout=0;
  762. /* tell "main" it should drop this (optional it will t/o anyway?)*/
  763. response[0]=(long)c;
  764. response[1]=CONN_ERROR;
  765. if (send_all(unix_tcp_sock, response, sizeof(response))<=0){
  766. LOG(L_ERR, "BUG: tcp_send: error return failed (write):%s (%d)\n",
  767. strerror(errno), errno);
  768. tcpconn_put(c); /* deref. it manually */
  769. n=-1;
  770. }
  771. /* CONN_ERROR will auto-dec refcnt => we must not call tcpconn_put
  772. * if it succeeds */
  773. close(fd);
  774. return n; /* error return, no tcpconn_put */
  775. }
  776. end:
  777. close(fd);
  778. release_c:
  779. tcpconn_put(c); /* release c (lock; dec refcnt; unlock) */
  780. return n;
  781. }
  782. int tcp_init(struct socket_info* sock_info)
  783. {
  784. union sockaddr_union* addr;
  785. int optval;
  786. #ifdef DISABLE_NAGLE
  787. int flag;
  788. struct protoent* pe;
  789. if (tcp_proto_no==-1){ /* if not already set */
  790. pe=getprotobyname("tcp");
  791. if (pe==0){
  792. LOG(L_ERR, "ERROR: tcp_init: could not get TCP protocol number\n");
  793. tcp_proto_no=-1;
  794. }else{
  795. tcp_proto_no=pe->p_proto;
  796. }
  797. }
  798. #endif
  799. addr=&sock_info->su;
  800. /* sock_info->proto=PROTO_TCP; */
  801. if (init_su(addr, &sock_info->address, sock_info->port_no)<0){
  802. LOG(L_ERR, "ERROR: tcp_init: could no init sockaddr_union\n");
  803. goto error;
  804. }
  805. sock_info->socket=socket(AF2PF(addr->s.sa_family), SOCK_STREAM, 0);
  806. if (sock_info->socket==-1){
  807. LOG(L_ERR, "ERROR: tcp_init: socket: %s\n", strerror(errno));
  808. goto error;
  809. }
  810. #ifdef DISABLE_NAGLE
  811. flag=1;
  812. if ( (tcp_proto_no!=-1) &&
  813. (setsockopt(sock_info->socket, tcp_proto_no , TCP_NODELAY,
  814. &flag, sizeof(flag))<0) ){
  815. LOG(L_ERR, "ERROR: tcp_init: could not disable Nagle: %s\n",
  816. strerror(errno));
  817. }
  818. #endif
  819. #if !defined(TCP_DONT_REUSEADDR)
  820. /* Stevens, "Network Programming", Section 7.5, "Generic Socket
  821. * Options": "...server started,..a child continues..on existing
  822. * connection..listening server is restarted...call to bind fails
  823. * ... ALL TCP servers should specify the SO_REUSEADDRE option
  824. * to allow the server to be restarted in this situation
  825. *
  826. * Indeed, without this option, the server can't restart.
  827. * -jiri
  828. */
  829. optval=1;
  830. if (setsockopt(sock_info->socket, SOL_SOCKET, SO_REUSEADDR,
  831. (void*)&optval, sizeof(optval))==-1) {
  832. LOG(L_ERR, "ERROR: tcp_init: setsockopt %s\n",
  833. strerror(errno));
  834. goto error;
  835. }
  836. #endif
  837. /* tos */
  838. optval = tos;
  839. if (setsockopt(sock_info->socket, IPPROTO_IP, IP_TOS, (void*)&optval,
  840. sizeof(optval)) ==-1){
  841. LOG(L_WARN, "WARNING: tcp_init: setsockopt tos: %s\n", strerror(errno));
  842. /* continue since this is not critical */
  843. }
  844. if (bind(sock_info->socket, &addr->s, sockaddru_len(*addr))==-1){
  845. LOG(L_ERR, "ERROR: tcp_init: bind(%x, %p, %d) on %s:%d : %s\n",
  846. sock_info->socket, &addr->s,
  847. (unsigned)sockaddru_len(*addr),
  848. sock_info->address_str.s,
  849. sock_info->port_no,
  850. strerror(errno));
  851. goto error;
  852. }
  853. if (listen(sock_info->socket, TCP_LISTEN_BACKLOG)==-1){
  854. LOG(L_ERR, "ERROR: tcp_init: listen(%x, %p, %d) on %s: %s\n",
  855. sock_info->socket, &addr->s,
  856. (unsigned)sockaddru_len(*addr),
  857. sock_info->address_str.s,
  858. strerror(errno));
  859. goto error;
  860. }
  861. return 0;
  862. error:
  863. if (sock_info->socket!=-1){
  864. close(sock_info->socket);
  865. sock_info->socket=-1;
  866. }
  867. return -1;
  868. }
  869. /* used internally by tcp_main_loop() */
  870. static void tcpconn_destroy(struct tcp_connection* tcpconn)
  871. {
  872. int fd;
  873. TCPCONN_LOCK; /*avoid races w/ tcp_send*/
  874. tcpconn->refcnt--;
  875. if (tcpconn->refcnt==0){
  876. DBG("tcpconn_destroy: destroying connection %p, flags %04x\n",
  877. tcpconn, tcpconn->flags);
  878. fd=tcpconn->s;
  879. #ifdef USE_TLS
  880. /*FIXME: lock ->writelock ? */
  881. if (tcpconn->type==PROTO_TLS)
  882. tls_close(tcpconn, fd);
  883. #endif
  884. _tcpconn_rm(tcpconn);
  885. close(fd);
  886. (*tcp_connections_no)--;
  887. }else{
  888. /* force timeout */
  889. tcpconn->timeout=0;
  890. tcpconn->state=S_CONN_BAD;
  891. DBG("tcpconn_destroy: delaying (%p, flags %04x) ...\n",
  892. tcpconn, tcpconn->flags);
  893. }
  894. TCPCONN_UNLOCK;
  895. }
  896. #ifdef SEND_FD_QUEUE
  897. struct send_fd_info{
  898. struct tcp_connection* tcp_conn;
  899. ticks_t expire;
  900. int unix_sock;
  901. unsigned int retries; /* debugging */
  902. };
  903. struct tcp_send_fd_q{
  904. struct send_fd_info* data; /* buffer */
  905. struct send_fd_info* crt; /* pointer inside the buffer */
  906. struct send_fd_info* end; /* points after the last valid position */
  907. };
  908. static struct tcp_send_fd_q send2child_q;
  909. static int send_fd_queue_init(struct tcp_send_fd_q *q, unsigned int size)
  910. {
  911. q->data=pkg_malloc(size*sizeof(struct send_fd_info));
  912. if (q->data==0){
  913. LOG(L_ERR, "ERROR: send_fd_queue_init: out of memory\n");
  914. return -1;
  915. }
  916. q->crt=&q->data[0];
  917. q->end=&q->data[size];
  918. return 0;
  919. }
  920. static void send_fd_queue_destroy(struct tcp_send_fd_q *q)
  921. {
  922. if (q->data){
  923. pkg_free(q->data);
  924. q->data=0;
  925. q->crt=q->end=0;
  926. }
  927. }
  928. static int init_send_fd_queues()
  929. {
  930. if (send_fd_queue_init(&send2child_q, SEND_FD_QUEUE_SIZE)!=0)
  931. goto error;
  932. return 0;
  933. error:
  934. LOG(L_ERR, "ERROR: init_send_fd_queues: init failed\n");
  935. return -1;
  936. }
  937. static void destroy_send_fd_queues()
  938. {
  939. send_fd_queue_destroy(&send2child_q);
  940. }
  941. inline static int send_fd_queue_add( struct tcp_send_fd_q* q,
  942. int unix_sock,
  943. struct tcp_connection *t)
  944. {
  945. struct send_fd_info* tmp;
  946. unsigned long new_size;
  947. if (q->crt>=q->end){
  948. new_size=q->end-&q->data[0];
  949. if (new_size< MAX_SEND_FD_QUEUE_SIZE/2){
  950. new_size*=2;
  951. }else new_size=MAX_SEND_FD_QUEUE_SIZE;
  952. if (q->crt>=&q->data[new_size]){
  953. LOG(L_ERR, "ERROR: send_fd_queue_add: queue full: %ld/%ld\n",
  954. (long)(q->crt-&q->data[0]-1), new_size);
  955. goto error;
  956. }
  957. LOG(L_CRIT, "INFO: send_fd_queue: queue full: %ld, extending to %ld\n",
  958. (long)(q->end-&q->data[0]), new_size);
  959. tmp=pkg_realloc(q->data, new_size*sizeof(struct send_fd_info));
  960. if (tmp==0){
  961. LOG(L_ERR, "ERROR: send_fd_queue_add: out of memory\n");
  962. goto error;
  963. }
  964. q->crt=(q->crt-&q->data[0])+tmp;
  965. q->data=tmp;
  966. q->end=&q->data[new_size];
  967. }
  968. q->crt->tcp_conn=t;
  969. q->crt->unix_sock=unix_sock;
  970. q->crt->expire=get_ticks_raw()+SEND_FD_QUEUE_TIMEOUT;
  971. q->crt->retries=0;
  972. q->crt++;
  973. return 0;
  974. error:
  975. return -1;
  976. }
  977. inline static void send_fd_queue_run(struct tcp_send_fd_q* q)
  978. {
  979. struct send_fd_info* p;
  980. struct send_fd_info* t;
  981. for (p=t=&q->data[0]; p<q->crt; p++){
  982. if (send_fd(p->unix_sock, &(p->tcp_conn),
  983. sizeof(struct tcp_connection*), p->tcp_conn->s)<=0){
  984. if ( ((errno==EAGAIN)||(errno==EWOULDBLOCK)) &&
  985. ((s_ticks_t)(p->expire-get_ticks_raw())>0)){
  986. /* leave in queue for a future try */
  987. *t=*p;
  988. t->retries++;
  989. t++;
  990. }else{
  991. LOG(L_ERR, "ERROR: run_send_fd_queue: send_fd failed"
  992. " on socket %d , queue entry %ld, retries %d,"
  993. " connection %p, tcp socket %d, errno=%d (%s) \n",
  994. p->unix_sock, (long)(p-&q->data[0]), p->retries,
  995. p->tcp_conn, p->tcp_conn->s, errno,
  996. strerror(errno));
  997. tcpconn_destroy(p->tcp_conn);
  998. }
  999. }
  1000. }
  1001. q->crt=t;
  1002. }
  1003. #else
  1004. #define send_fd_queue_run(q)
  1005. #endif
  1006. /* handles io from a tcp child process
  1007. * params: tcp_c - pointer in the tcp_children array, to the entry for
  1008. * which an io event was detected
  1009. * fd_i - fd index in the fd_array (usefull for optimizing
  1010. * io_watch_deletes)
  1011. * returns: handle_* return convention: -1 on error, 0 on EAGAIN (no more
  1012. * io events queued), >0 on success. success/error refer only to
  1013. * the reads from the fd.
  1014. */
  1015. inline static int handle_tcp_child(struct tcp_child* tcp_c, int fd_i)
  1016. {
  1017. struct tcp_connection* tcpconn;
  1018. long response[2];
  1019. int cmd;
  1020. int bytes;
  1021. if (tcp_c->unix_sock<=0){
  1022. /* (we can't have a fd==0, 0 is never closed )*/
  1023. LOG(L_CRIT, "BUG: handle_tcp_child: fd %d for %d "
  1024. "(pid %d, ser no %d)\n", tcp_c->unix_sock,
  1025. (int)(tcp_c-&tcp_children[0]), tcp_c->pid, tcp_c->proc_no);
  1026. goto error;
  1027. }
  1028. /* read until sizeof(response)
  1029. * (this is a SOCK_STREAM so read is not atomic) */
  1030. bytes=recv_all(tcp_c->unix_sock, response, sizeof(response), MSG_DONTWAIT);
  1031. if (bytes<(int)sizeof(response)){
  1032. if (bytes==0){
  1033. /* EOF -> bad, child has died */
  1034. DBG("DBG: handle_tcp_child: dead tcp child %d (pid %d, no %d)"
  1035. " (shutting down?)\n", (int)(tcp_c-&tcp_children[0]),
  1036. tcp_c->pid, tcp_c->proc_no );
  1037. /* don't listen on it any more */
  1038. io_watch_del(&io_h, tcp_c->unix_sock, fd_i, 0);
  1039. goto error; /* eof. so no more io here, it's ok to return error */
  1040. }else if (bytes<0){
  1041. /* EAGAIN is ok if we try to empty the buffer
  1042. * e.g.: SIGIO_RT overflow mode or EPOLL ET */
  1043. if ((errno!=EAGAIN) && (errno!=EWOULDBLOCK)){
  1044. LOG(L_CRIT, "ERROR: handle_tcp_child: read from tcp child %ld "
  1045. " (pid %d, no %d) %s [%d]\n",
  1046. (long)(tcp_c-&tcp_children[0]), tcp_c->pid,
  1047. tcp_c->proc_no, strerror(errno), errno );
  1048. }else{
  1049. bytes=0;
  1050. }
  1051. /* try to ignore ? */
  1052. goto end;
  1053. }else{
  1054. /* should never happen */
  1055. LOG(L_CRIT, "BUG: handle_tcp_child: too few bytes received (%d)\n",
  1056. bytes );
  1057. bytes=0; /* something was read so there is no error; otoh if
  1058. receive_fd returned less then requested => the receive
  1059. buffer is empty => no more io queued on this fd */
  1060. goto end;
  1061. }
  1062. }
  1063. DBG("handle_tcp_child: reader response= %lx, %ld from %d \n",
  1064. response[0], response[1], (int)(tcp_c-&tcp_children[0]));
  1065. cmd=response[1];
  1066. tcpconn=(struct tcp_connection*)response[0];
  1067. if (tcpconn==0){
  1068. /* should never happen */
  1069. LOG(L_CRIT, "BUG: handle_tcp_child: null tcpconn pointer received"
  1070. " from tcp child %d (pid %d): %lx, %lx\n",
  1071. (int)(tcp_c-&tcp_children[0]), tcp_c->pid,
  1072. response[0], response[1]) ;
  1073. goto end;
  1074. }
  1075. switch(cmd){
  1076. case CONN_RELEASE:
  1077. tcp_c->busy--;
  1078. if (tcpconn->state==S_CONN_BAD){
  1079. tcpconn_destroy(tcpconn);
  1080. break;
  1081. }
  1082. /* update the timeout*/
  1083. tcpconn->timeout=get_ticks()+tcp_con_lifetime;
  1084. tcpconn_put(tcpconn);
  1085. /* must be after the de-ref*/
  1086. io_watch_add(&io_h, tcpconn->s, F_TCPCONN, tcpconn);
  1087. tcpconn->flags&=~F_CONN_REMOVED;
  1088. DBG("handle_tcp_child: CONN_RELEASE %p refcnt= %d\n",
  1089. tcpconn, tcpconn->refcnt);
  1090. break;
  1091. case CONN_ERROR:
  1092. case CONN_DESTROY:
  1093. case CONN_EOF:
  1094. /* WARNING: this will auto-dec. refcnt! */
  1095. tcp_c->busy--;
  1096. /* main doesn't listen on it => we don't have to delete it
  1097. if (tcpconn->s!=-1)
  1098. io_watch_del(&io_h, tcpconn->s, -1, IO_FD_CLOSING);
  1099. */
  1100. tcpconn_destroy(tcpconn); /* closes also the fd */
  1101. break;
  1102. default:
  1103. LOG(L_CRIT, "BUG: handle_tcp_child: unknown cmd %d"
  1104. " from tcp reader %d\n",
  1105. cmd, (int)(tcp_c-&tcp_children[0]));
  1106. }
  1107. end:
  1108. return bytes;
  1109. error:
  1110. return -1;
  1111. }
  1112. /* handles io from a "generic" ser process (get fd or new_fd from a tcp_send)
  1113. *
  1114. * params: p - pointer in the ser processes array (pt[]), to the entry for
  1115. * which an io event was detected
  1116. * fd_i - fd index in the fd_array (usefull for optimizing
  1117. * io_watch_deletes)
  1118. * returns: handle_* return convention:
  1119. * -1 on error reading from the fd,
  1120. * 0 on EAGAIN or when no more io events are queued
  1121. * (receive buffer empty),
  1122. * >0 on successfull reads from the fd (the receive buffer might
  1123. * be non-empty).
  1124. */
  1125. inline static int handle_ser_child(struct process_table* p, int fd_i)
  1126. {
  1127. struct tcp_connection* tcpconn;
  1128. long response[2];
  1129. int cmd;
  1130. int bytes;
  1131. int ret;
  1132. int fd;
  1133. ret=-1;
  1134. if (p->unix_sock<=0){
  1135. /* (we can't have a fd==0, 0 is never closed )*/
  1136. LOG(L_CRIT, "BUG: handle_ser_child: fd %d for %d "
  1137. "(pid %d)\n", p->unix_sock, (int)(p-&pt[0]), p->pid);
  1138. goto error;
  1139. }
  1140. /* get all bytes and the fd (if transmitted)
  1141. * (this is a SOCK_STREAM so read is not atomic) */
  1142. bytes=receive_fd(p->unix_sock, response, sizeof(response), &fd,
  1143. MSG_DONTWAIT);
  1144. if (bytes<(int)sizeof(response)){
  1145. /* too few bytes read */
  1146. if (bytes==0){
  1147. /* EOF -> bad, child has died */
  1148. DBG("DBG: handle_ser_child: dead child %d, pid %d"
  1149. " (shutting down?)\n", (int)(p-&pt[0]), p->pid);
  1150. /* don't listen on it any more */
  1151. io_watch_del(&io_h, p->unix_sock, fd_i, 0);
  1152. goto error; /* child dead => no further io events from it */
  1153. }else if (bytes<0){
  1154. /* EAGAIN is ok if we try to empty the buffer
  1155. * e.g: SIGIO_RT overflow mode or EPOLL ET */
  1156. if ((errno!=EAGAIN) && (errno!=EWOULDBLOCK)){
  1157. LOG(L_CRIT, "ERROR: handle_ser_child: read from child %d "
  1158. "(pid %d): %s [%d]\n", (int)(p-&pt[0]), p->pid,
  1159. strerror(errno), errno);
  1160. ret=-1;
  1161. }else{
  1162. ret=0;
  1163. }
  1164. /* try to ignore ? */
  1165. goto end;
  1166. }else{
  1167. /* should never happen */
  1168. LOG(L_CRIT, "BUG: handle_ser_child: too few bytes received (%d)\n",
  1169. bytes );
  1170. ret=0; /* something was read so there is no error; otoh if
  1171. receive_fd returned less then requested => the receive
  1172. buffer is empty => no more io queued on this fd */
  1173. goto end;
  1174. }
  1175. }
  1176. ret=1; /* something was received, there might be more queued */
  1177. DBG("handle_ser_child: read response= %lx, %ld, fd %d from %d (%d)\n",
  1178. response[0], response[1], fd, (int)(p-&pt[0]), p->pid);
  1179. cmd=response[1];
  1180. tcpconn=(struct tcp_connection*)response[0];
  1181. if (tcpconn==0){
  1182. LOG(L_CRIT, "BUG: handle_ser_child: null tcpconn pointer received"
  1183. " from child %d (pid %d): %lx, %lx\n",
  1184. (int)(p-&pt[0]), p->pid, response[0], response[1]) ;
  1185. goto end;
  1186. }
  1187. switch(cmd){
  1188. case CONN_ERROR:
  1189. if (!(tcpconn->flags & F_CONN_REMOVED) && (tcpconn->s!=-1)){
  1190. io_watch_del(&io_h, tcpconn->s, -1, IO_FD_CLOSING);
  1191. tcpconn->flags|=F_CONN_REMOVED;
  1192. }
  1193. tcpconn_destroy(tcpconn); /* will close also the fd */
  1194. break;
  1195. case CONN_GET_FD:
  1196. /* send the requested FD */
  1197. /* WARNING: take care of setting refcnt properly to
  1198. * avoid race condition */
  1199. if (send_fd(p->unix_sock, &tcpconn, sizeof(tcpconn),
  1200. tcpconn->s)<=0){
  1201. LOG(L_ERR, "ERROR: handle_ser_child: send_fd failed\n");
  1202. }
  1203. break;
  1204. case CONN_NEW:
  1205. /* update the fd in the requested tcpconn*/
  1206. /* WARNING: take care of setting refcnt properly to
  1207. * avoid race condition */
  1208. if (fd==-1){
  1209. LOG(L_CRIT, "BUG: handle_ser_child: CONN_NEW:"
  1210. " no fd received\n");
  1211. break;
  1212. }
  1213. (*tcp_connections_no)++;
  1214. tcpconn->s=fd;
  1215. /* add tcpconn to the list*/
  1216. tcpconn_add(tcpconn);
  1217. /* update the timeout*/
  1218. tcpconn->timeout=get_ticks()+tcp_con_lifetime;
  1219. io_watch_add(&io_h, tcpconn->s, F_TCPCONN, tcpconn);
  1220. tcpconn->flags&=~F_CONN_REMOVED;
  1221. break;
  1222. default:
  1223. LOG(L_CRIT, "BUG: handle_ser_child: unknown cmd %d\n", cmd);
  1224. }
  1225. end:
  1226. return ret;
  1227. error:
  1228. return -1;
  1229. }
  1230. /* sends a tcpconn + fd to a choosen child */
  1231. inline static int send2child(struct tcp_connection* tcpconn)
  1232. {
  1233. int i;
  1234. int min_busy;
  1235. int idx;
  1236. static int crt=0; /* current child */
  1237. int last;
  1238. min_busy=tcp_children[0].busy;
  1239. idx=0;
  1240. last=crt+tcp_children_no;
  1241. for (; crt<last; crt++){
  1242. i=crt%tcp_children_no;
  1243. if (!tcp_children[i].busy){
  1244. idx=i;
  1245. min_busy=0;
  1246. break;
  1247. }else if (min_busy>tcp_children[i].busy){
  1248. min_busy=tcp_children[i].busy;
  1249. idx=i;
  1250. }
  1251. }
  1252. crt=idx+1; /* next time we start with crt%tcp_children_no */
  1253. tcp_children[idx].busy++;
  1254. tcp_children[idx].n_reqs++;
  1255. if (min_busy){
  1256. DBG("WARNING: send2child: no free tcp receiver, "
  1257. " connection passed to the least busy one (%d)\n",
  1258. min_busy);
  1259. }
  1260. DBG("send2child: to tcp child %d %d(%d), %p\n", idx,
  1261. tcp_children[idx].proc_no,
  1262. tcp_children[idx].pid, tcpconn);
  1263. /* first make sure this child doesn't have pending request for
  1264. * tcp_main (to avoid a possible deadlock: e.g. child wants to
  1265. * send a release command, but the master fills its socket buffer
  1266. * with new connection commands => deadlock) */
  1267. /* answer tcp_send requests first */
  1268. while(handle_ser_child(&pt[tcp_children[idx].proc_no], -1)>0);
  1269. /* process tcp readers requests */
  1270. while(handle_tcp_child(&tcp_children[idx], -1)>0);
  1271. #ifdef SEND_FD_QUEUE
  1272. /* if queue full, try to queue the io */
  1273. if (send_fd(tcp_children[idx].unix_sock, &tcpconn, sizeof(tcpconn),
  1274. tcpconn->s)<=0){
  1275. if ((errno==EAGAIN)||(errno==EWOULDBLOCK)){
  1276. /* FIXME: remove after debugging */
  1277. LOG(L_CRIT, "INFO: tcp child %d, socket %d: queue full,"
  1278. " %d requests queued (total handled %d)\n",
  1279. idx, tcp_children[idx].unix_sock, min_busy,
  1280. tcp_children[idx].n_reqs-1);
  1281. if (send_fd_queue_add(&send2child_q, tcp_children[idx].unix_sock,
  1282. tcpconn)!=0){
  1283. LOG(L_ERR, "ERROR: send2child: queue send op. failed\n");
  1284. return -1;
  1285. }
  1286. }else{
  1287. LOG(L_ERR, "ERROR: send2child: send_fd failed\n");
  1288. return -1;
  1289. }
  1290. }
  1291. #else
  1292. if (send_fd(tcp_children[idx].unix_sock, &tcpconn, sizeof(tcpconn),
  1293. tcpconn->s)<=0){
  1294. LOG(L_ERR, "ERROR: send2child: send_fd failed\n");
  1295. return -1;
  1296. }
  1297. #endif
  1298. return 0;
  1299. }
  1300. /* handles a new connection, called internally by tcp_main_loop/handle_io.
  1301. * params: si - pointer to one of the tcp socket_info structures on which
  1302. * an io event was detected (connection attempt)
  1303. * returns: handle_* return convention: -1 on error, 0 on EAGAIN (no more
  1304. * io events queued), >0 on success. success/error refer only to
  1305. * the accept.
  1306. */
  1307. static inline int handle_new_connect(struct socket_info* si)
  1308. {
  1309. union sockaddr_union su;
  1310. struct tcp_connection* tcpconn;
  1311. socklen_t su_len;
  1312. int new_sock;
  1313. /* got a connection on r */
  1314. su_len=sizeof(su);
  1315. new_sock=accept(si->socket, &(su.s), &su_len);
  1316. if (new_sock==-1){
  1317. if ((errno==EAGAIN)||(errno==EWOULDBLOCK))
  1318. return 0;
  1319. LOG(L_ERR, "WARNING: handle_new_connect: error while accepting"
  1320. " connection(%d): %s\n", errno, strerror(errno));
  1321. return -1;
  1322. }
  1323. if (*tcp_connections_no>=tcp_max_connections){
  1324. LOG(L_ERR, "ERROR: maximum number of connections exceeded: %d/%d\n",
  1325. *tcp_connections_no, tcp_max_connections);
  1326. close(new_sock);
  1327. return 1; /* success, because the accept was succesfull */
  1328. }
  1329. if (init_sock_opt(new_sock)<0){
  1330. LOG(L_ERR, "ERROR: handle_new_connect: init_sock_opt failed\n");
  1331. close(new_sock);
  1332. return 1; /* success, because the accept was succesfull */
  1333. }
  1334. (*tcp_connections_no)++;
  1335. /* add socket to list */
  1336. tcpconn=tcpconn_new(new_sock, &su, si, si->proto, S_CONN_ACCEPT);
  1337. if (tcpconn){
  1338. #ifdef TCP_PASS_NEW_CONNECTION_ON_DATA
  1339. io_watch_add(&io_h, tcpconn->s, F_TCPCONN, tcpconn);
  1340. tcpconn->flags&=~F_CONN_REMOVED;
  1341. tcpconn_add(tcpconn);
  1342. #else
  1343. tcpconn->refcnt++; /* safe, not yet available to the
  1344. outside world */
  1345. tcpconn_add(tcpconn);
  1346. DBG("handle_new_connect: new connection: %p %d flags: %04x\n",
  1347. tcpconn, tcpconn->s, tcpconn->flags);
  1348. /* pass it to a child */
  1349. if(send2child(tcpconn)<0){
  1350. LOG(L_ERR,"ERROR: handle_new_connect: no children "
  1351. "available\n");
  1352. tcpconn_destroy(tcpconn);
  1353. }
  1354. #endif
  1355. }else{ /*tcpconn==0 */
  1356. LOG(L_ERR, "ERROR: handle_new_connect: tcpconn_new failed, "
  1357. "closing socket\n");
  1358. close(new_sock);
  1359. (*tcp_connections_no)--;
  1360. }
  1361. return 1; /* accept() was succesfull */
  1362. }
  1363. /* handles an io event on one of the watched tcp connections
  1364. *
  1365. * params: tcpconn - pointer to the tcp_connection for which we have an io ev.
  1366. * fd_i - index in the fd_array table (needed for delete)
  1367. * returns: handle_* return convention, but on success it always returns 0
  1368. * (because it's one-shot, after a succesfull execution the fd is
  1369. * removed from tcp_main's watch fd list and passed to a child =>
  1370. * tcp_main is not interested in further io events that might be
  1371. * queued for this fd)
  1372. */
  1373. inline static int handle_tcpconn_ev(struct tcp_connection* tcpconn, int fd_i)
  1374. {
  1375. /* is refcnt!=0 really necessary?
  1376. * No, in fact it's a bug: I can have the following situation: a send only
  1377. * tcp connection used by n processes simultaneously => refcnt = n. In
  1378. * the same time I can have a read event and this situation is perfectly
  1379. * valid. -- andrei
  1380. */
  1381. #if 0
  1382. if ((tcpconn->refcnt!=0)){
  1383. /* FIXME: might be valid for sigio_rt iff fd flags are not cleared
  1384. * (there is a short window in which it could generate a sig
  1385. * that would be catched by tcp_main) */
  1386. LOG(L_CRIT, "BUG: handle_tcpconn_ev: io event on referenced"
  1387. " tcpconn (%p), refcnt=%d, fd=%d\n",
  1388. tcpconn, tcpconn->refcnt, tcpconn->s);
  1389. return -1;
  1390. }
  1391. #endif
  1392. /* pass it to child, so remove it from the io watch list */
  1393. DBG("handle_tcpconn_ev: data available on %p %d\n", tcpconn, tcpconn->s);
  1394. if (io_watch_del(&io_h, tcpconn->s, fd_i, 0)==-1) goto error;
  1395. tcpconn->flags|=F_CONN_REMOVED;
  1396. tcpconn_ref(tcpconn); /* refcnt ++ */
  1397. if (send2child(tcpconn)<0){
  1398. LOG(L_ERR,"ERROR: handle_tcpconn_ev: no children available\n");
  1399. tcpconn_destroy(tcpconn);
  1400. #if 0
  1401. TCPCONN_LOCK;
  1402. tcpconn->refcnt--;
  1403. if (tcpconn->refcnt==0){
  1404. fd=tcpconn->s;
  1405. _tcpconn_rm(tcpconn);
  1406. close(fd);
  1407. }else tcpconn->timeout=0; /* force expire*/
  1408. TCPCONN_UNLOCK;
  1409. #endif
  1410. }
  1411. return 0; /* we are not interested in possibly queued io events,
  1412. the fd was either passed to a child, or closed */
  1413. error:
  1414. return -1;
  1415. }
  1416. /* generic handle io routine, it will call the appropiate
  1417. * handle_xxx() based on the fd_map type
  1418. *
  1419. * params: fm - pointer to a fd hash entry
  1420. * idx - index in the fd_array (or -1 if not known)
  1421. * return: -1 on error
  1422. * 0 on EAGAIN or when by some other way it is known that no more
  1423. * io events are queued on the fd (the receive buffer is empty).
  1424. * Usefull to detect when there are no more io events queued for
  1425. * sigio_rt, epoll_et, kqueue.
  1426. * >0 on successfull read from the fd (when there might be more io
  1427. * queued -- the receive buffer might still be non-empty)
  1428. */
  1429. inline static int handle_io(struct fd_map* fm, int idx)
  1430. {
  1431. int ret;
  1432. switch(fm->type){
  1433. case F_SOCKINFO:
  1434. ret=handle_new_connect((struct socket_info*)fm->data);
  1435. break;
  1436. case F_TCPCONN:
  1437. ret=handle_tcpconn_ev((struct tcp_connection*)fm->data, idx);
  1438. break;
  1439. case F_TCPCHILD:
  1440. ret=handle_tcp_child((struct tcp_child*)fm->data, idx);
  1441. break;
  1442. case F_PROC:
  1443. ret=handle_ser_child((struct process_table*)fm->data, idx);
  1444. break;
  1445. case F_NONE:
  1446. LOG(L_CRIT, "BUG: handle_io: empty fd map\n");
  1447. goto error;
  1448. default:
  1449. LOG(L_CRIT, "BUG: handle_io: uknown fd type %d\n", fm->type);
  1450. goto error;
  1451. }
  1452. return ret;
  1453. error:
  1454. return -1;
  1455. }
  1456. /* very inefficient for now - FIXME
  1457. * keep in sync with tcpconn_destroy, the "delete" part should be
  1458. * the same except for io_watch_del..*/
  1459. static inline void tcpconn_timeout(int force)
  1460. {
  1461. static int prev_ticks=0;
  1462. struct tcp_connection *c, *next;
  1463. unsigned int ticks;
  1464. unsigned h;
  1465. int fd;
  1466. ticks=get_ticks();
  1467. if ((ticks==prev_ticks) && !force) return;
  1468. prev_ticks=ticks;
  1469. TCPCONN_LOCK; /* fixme: we can lock only on delete IMO */
  1470. for(h=0; h<TCP_ID_HASH_SIZE; h++){
  1471. c=tcpconn_id_hash[h];
  1472. while(c){
  1473. next=c->id_next;
  1474. if (force ||((c->refcnt==0) && ((int)(ticks-c->timeout)>=0))){
  1475. if (!force)
  1476. DBG("tcpconn_timeout: timeout for hash=%d - %p"
  1477. " (%d > %d)\n", h, c, ticks, c->timeout);
  1478. fd=c->s;
  1479. #ifdef USE_TLS
  1480. if (c->type==PROTO_TLS)
  1481. tls_close(c, fd);
  1482. #endif
  1483. _tcpconn_rm(c);
  1484. if ((fd>0)&&(c->refcnt==0)) {
  1485. if (!(c->flags & F_CONN_REMOVED)){
  1486. io_watch_del(&io_h, fd, -1, IO_FD_CLOSING);
  1487. c->flags|=F_CONN_REMOVED;
  1488. }
  1489. close(fd);
  1490. }
  1491. (*tcp_connections_no)--;
  1492. }
  1493. c=next;
  1494. }
  1495. }
  1496. TCPCONN_UNLOCK;
  1497. }
  1498. /* tcp main loop */
  1499. void tcp_main_loop()
  1500. {
  1501. struct socket_info* si;
  1502. int r;
  1503. /* init send fd queues (here because we want mem. alloc only in the tcp
  1504. * process */
  1505. #ifdef SEND_FD_QUEUE
  1506. if (init_send_fd_queues()<0){
  1507. LOG(L_CRIT, "ERROR: init_tcp: could not init send fd queues\n");
  1508. goto error;
  1509. }
  1510. #endif
  1511. /* init io_wait (here because we want the memory allocated only in
  1512. * the tcp_main process) */
  1513. /* FIXME: TODO: make tcp_max_fd_no a config param */
  1514. if (init_io_wait(&io_h, tcp_max_fd_no, tcp_poll_method)<0)
  1515. goto error;
  1516. /* init: start watching all the fds*/
  1517. /* add all the sockets we listens on for connections */
  1518. for (si=tcp_listen; si; si=si->next){
  1519. if ((si->proto==PROTO_TCP) &&(si->socket!=-1)){
  1520. if (io_watch_add(&io_h, si->socket, F_SOCKINFO, si)<0){
  1521. LOG(L_CRIT, "ERROR: tcp_main_loop: init: failed to add "
  1522. "listen socket to the fd list\n");
  1523. goto error;
  1524. }
  1525. }else{
  1526. LOG(L_CRIT, "BUG: tcp_main_loop: non tcp address in tcp_listen\n");
  1527. }
  1528. }
  1529. #ifdef USE_TLS
  1530. if (!tls_disable){
  1531. for (si=tls_listen; si; si=si->next){
  1532. if ((si->proto==PROTO_TLS) && (si->socket!=-1)){
  1533. if (io_watch_add(&io_h, si->socket, F_SOCKINFO, si)<0){
  1534. LOG(L_CRIT, "ERROR: tcp_main_loop: init: failed to add "
  1535. "tls listen socket to the fd list\n");
  1536. goto error;
  1537. }
  1538. }else{
  1539. LOG(L_CRIT, "BUG: tcp_main_loop: non tls address"
  1540. " in tls_listen\n");
  1541. }
  1542. }
  1543. }
  1544. #endif
  1545. /* add all the unix sockets used for communcation with other ser processes
  1546. * (get fd, new connection a.s.o) */
  1547. for (r=1; r<process_no; r++){
  1548. if (pt[r].unix_sock>0) /* we can't have 0, we never close it!*/
  1549. if (io_watch_add(&io_h, pt[r].unix_sock, F_PROC, &pt[r])<0){
  1550. LOG(L_CRIT, "ERROR: tcp_main_loop: init: failed to add "
  1551. "process %d unix socket to the fd list\n", r);
  1552. goto error;
  1553. }
  1554. }
  1555. /* add all the unix sokets used for communication with the tcp childs */
  1556. for (r=0; r<tcp_children_no; r++){
  1557. if (tcp_children[r].unix_sock>0)/*we can't have 0, we never close it!*/
  1558. if (io_watch_add(&io_h, tcp_children[r].unix_sock, F_TCPCHILD,
  1559. &tcp_children[r]) <0){
  1560. LOG(L_CRIT, "ERROR: tcp_main_loop: init: failed to add "
  1561. "tcp child %d unix socket to the fd list\n", r);
  1562. goto error;
  1563. }
  1564. }
  1565. /* main loop */
  1566. switch(io_h.poll_method){
  1567. case POLL_POLL:
  1568. while(1){
  1569. /* wait and process IO */
  1570. io_wait_loop_poll(&io_h, TCP_MAIN_SELECT_TIMEOUT, 0);
  1571. send_fd_queue_run(&send2child_q); /* then new io */
  1572. /* remove old connections */
  1573. tcpconn_timeout(0);
  1574. }
  1575. break;
  1576. #ifdef HAVE_SELECT
  1577. case POLL_SELECT:
  1578. while(1){
  1579. io_wait_loop_select(&io_h, TCP_MAIN_SELECT_TIMEOUT, 0);
  1580. send_fd_queue_run(&send2child_q); /* then new io */
  1581. tcpconn_timeout(0);
  1582. }
  1583. break;
  1584. #endif
  1585. #ifdef HAVE_SIGIO_RT
  1586. case POLL_SIGIO_RT:
  1587. while(1){
  1588. io_wait_loop_sigio_rt(&io_h, TCP_MAIN_SELECT_TIMEOUT);
  1589. send_fd_queue_run(&send2child_q); /* then new io */
  1590. tcpconn_timeout(0);
  1591. }
  1592. break;
  1593. #endif
  1594. #ifdef HAVE_EPOLL
  1595. case POLL_EPOLL_LT:
  1596. while(1){
  1597. io_wait_loop_epoll(&io_h, TCP_MAIN_SELECT_TIMEOUT, 0);
  1598. send_fd_queue_run(&send2child_q); /* then new io */
  1599. tcpconn_timeout(0);
  1600. }
  1601. break;
  1602. case POLL_EPOLL_ET:
  1603. while(1){
  1604. io_wait_loop_epoll(&io_h, TCP_MAIN_SELECT_TIMEOUT, 1);
  1605. send_fd_queue_run(&send2child_q); /* then new io */
  1606. tcpconn_timeout(0);
  1607. }
  1608. break;
  1609. #endif
  1610. #ifdef HAVE_KQUEUE
  1611. case POLL_KQUEUE:
  1612. while(1){
  1613. io_wait_loop_kqueue(&io_h, TCP_MAIN_SELECT_TIMEOUT, 0);
  1614. send_fd_queue_run(&send2child_q); /* then new io */
  1615. tcpconn_timeout(0);
  1616. }
  1617. break;
  1618. #endif
  1619. #ifdef HAVE_DEVPOLL
  1620. case POLL_DEVPOLL:
  1621. while(1){
  1622. io_wait_loop_devpoll(&io_h, TCP_MAIN_SELECT_TIMEOUT, 0);
  1623. send_fd_queue_run(&send2child_q); /* then new io */
  1624. tcpconn_timeout(0);
  1625. }
  1626. break;
  1627. #endif
  1628. default:
  1629. LOG(L_CRIT, "BUG: tcp_main_loop: no support for poll method "
  1630. " %s (%d)\n",
  1631. poll_method_name(io_h.poll_method), io_h.poll_method);
  1632. goto error;
  1633. }
  1634. error:
  1635. #ifdef SEND_FD_QUEUE
  1636. destroy_send_fd_queues();
  1637. #endif
  1638. destroy_io_wait(&io_h);
  1639. LOG(L_CRIT, "ERROR: tcp_main_loop: exiting...");
  1640. exit(-1);
  1641. }
  1642. /* cleanup before exit */
  1643. void destroy_tcp()
  1644. {
  1645. if (tcpconn_id_hash){
  1646. tcpconn_timeout(1); /* force close/expire for all active tcpconns*/
  1647. shm_free(tcpconn_id_hash);
  1648. tcpconn_id_hash=0;
  1649. }
  1650. if (tcp_connections_no){
  1651. shm_free(tcp_connections_no);
  1652. tcp_connections_no=0;
  1653. }
  1654. if (connection_id){
  1655. shm_free(connection_id);
  1656. connection_id=0;
  1657. }
  1658. if (tcpconn_aliases_hash){
  1659. shm_free(tcpconn_aliases_hash);
  1660. tcpconn_aliases_hash=0;
  1661. }
  1662. if (tcpconn_lock){
  1663. lock_destroy(tcpconn_lock);
  1664. lock_dealloc((void*)tcpconn_lock);
  1665. tcpconn_lock=0;
  1666. }
  1667. if (tcp_children){
  1668. pkg_free(tcp_children);
  1669. tcp_children=0;
  1670. }
  1671. }
  1672. int init_tcp()
  1673. {
  1674. char* poll_err;
  1675. /* init lock */
  1676. tcpconn_lock=lock_alloc();
  1677. if (tcpconn_lock==0){
  1678. LOG(L_CRIT, "ERROR: init_tcp: could not alloc lock\n");
  1679. goto error;
  1680. }
  1681. if (lock_init(tcpconn_lock)==0){
  1682. LOG(L_CRIT, "ERROR: init_tcp: could not init lock\n");
  1683. lock_dealloc((void*)tcpconn_lock);
  1684. tcpconn_lock=0;
  1685. goto error;
  1686. }
  1687. /* init globals */
  1688. tcp_connections_no=shm_malloc(sizeof(int));
  1689. if (tcp_connections_no==0){
  1690. LOG(L_CRIT, "ERROR: init_tcp: could not alloc globals\n");
  1691. goto error;
  1692. }
  1693. *tcp_connections_no=0;
  1694. connection_id=shm_malloc(sizeof(int));
  1695. if (connection_id==0){
  1696. LOG(L_CRIT, "ERROR: init_tcp: could not alloc globals\n");
  1697. goto error;
  1698. }
  1699. *connection_id=1;
  1700. /* alloc hashtables*/
  1701. tcpconn_aliases_hash=(struct tcp_conn_alias**)
  1702. shm_malloc(TCP_ALIAS_HASH_SIZE* sizeof(struct tcp_conn_alias*));
  1703. if (tcpconn_aliases_hash==0){
  1704. LOG(L_CRIT, "ERROR: init_tcp: could not alloc address hashtable\n");
  1705. goto error;
  1706. }
  1707. tcpconn_id_hash=(struct tcp_connection**)shm_malloc(TCP_ID_HASH_SIZE*
  1708. sizeof(struct tcp_connection*));
  1709. if (tcpconn_id_hash==0){
  1710. LOG(L_CRIT, "ERROR: init_tcp: could not alloc id hashtable\n");
  1711. goto error;
  1712. }
  1713. /* init hashtables*/
  1714. memset((void*)tcpconn_aliases_hash, 0,
  1715. TCP_ALIAS_HASH_SIZE * sizeof(struct tcp_conn_alias*));
  1716. memset((void*)tcpconn_id_hash, 0,
  1717. TCP_ID_HASH_SIZE * sizeof(struct tcp_connection*));
  1718. /* fix config variables */
  1719. /* they can have only positive values due the config parser so we can
  1720. * ignore most of them */
  1721. poll_err=check_poll_method(tcp_poll_method);
  1722. /* set an appropiate poll method */
  1723. if (poll_err || (tcp_poll_method==0)){
  1724. tcp_poll_method=choose_poll_method();
  1725. if (poll_err){
  1726. LOG(L_ERR, "ERROR: init_tcp: %s, using %s instead\n",
  1727. poll_err, poll_method_name(tcp_poll_method));
  1728. }else{
  1729. LOG(L_INFO, "init_tcp: using %s as the io watch method"
  1730. " (auto detected)\n", poll_method_name(tcp_poll_method));
  1731. }
  1732. }else{
  1733. LOG(L_INFO, "init_tcp: using %s io watch method (config)\n",
  1734. poll_method_name(tcp_poll_method));
  1735. }
  1736. return 0;
  1737. error:
  1738. /* clean-up */
  1739. destroy_tcp();
  1740. return -1;
  1741. }
  1742. /* returns -1 on error */
  1743. static int set_non_blocking(int s)
  1744. {
  1745. int flags;
  1746. /* non-blocking */
  1747. flags=fcntl(s, F_GETFL);
  1748. if (flags==-1){
  1749. LOG(L_ERR, "ERROR: set_non_blocking: fnctl failed: (%d) %s\n",
  1750. errno, strerror(errno));
  1751. goto error;
  1752. }
  1753. if (fcntl(s, F_SETFL, flags|O_NONBLOCK)==-1){
  1754. LOG(L_ERR, "ERROR: set_non_blocking: fcntl: set non-blocking failed:"
  1755. " (%d) %s\n", errno, strerror(errno));
  1756. goto error;
  1757. }
  1758. return 0;
  1759. error:
  1760. return -1;
  1761. }
  1762. /* starts the tcp processes */
  1763. int tcp_init_children()
  1764. {
  1765. int r;
  1766. int sockfd[2];
  1767. int reader_fd[2]; /* for comm. with the tcp children read */
  1768. pid_t pid;
  1769. struct socket_info *si;
  1770. /* estimate max fd. no:
  1771. * 1 tcp send unix socket/all_proc,
  1772. * + 1 udp sock/udp proc + 1 tcp_child sock/tcp child*
  1773. * + no_listen_tcp */
  1774. for(r=0, si=tcp_listen; si; si=si->next, r++);
  1775. #ifdef USE_TLS
  1776. if (! tls_disable)
  1777. for (si=tls_listen; si; si=si->next, r++);
  1778. #endif
  1779. tcp_max_fd_no=process_count*2 +r-1 /* timer */ +3; /* stdin/out/err*/
  1780. /* max connections can be temporarily exceeded with process_count
  1781. * - tcp_main (tcpconn_connect called simultaneously in all all the
  1782. * processes) */
  1783. tcp_max_fd_no+=tcp_max_connections+process_count-1 /* tcp main */;
  1784. /* alloc the children array */
  1785. tcp_children=pkg_malloc(sizeof(struct tcp_child)*tcp_children_no);
  1786. if (tcp_children==0){
  1787. LOG(L_ERR, "ERROR: tcp_init_children: out of memory\n");
  1788. goto error;
  1789. }
  1790. /* create the tcp sock_info structures */
  1791. /* copy the sockets --moved to main_loop*/
  1792. /* fork children & create the socket pairs*/
  1793. for(r=0; r<tcp_children_no; r++){
  1794. if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfd)<0){
  1795. LOG(L_ERR, "ERROR: tcp_main: socketpair failed: %s\n",
  1796. strerror(errno));
  1797. goto error;
  1798. }
  1799. if (socketpair(AF_UNIX, SOCK_STREAM, 0, reader_fd)<0){
  1800. LOG(L_ERR, "ERROR: tcp_main: socketpair failed: %s\n",
  1801. strerror(errno));
  1802. goto error;
  1803. }
  1804. #ifdef TCP_CHILD_NON_BLOCKING
  1805. if ((set_non_blocking(reader_fd[0])<0) ||
  1806. (set_non_blocking(reader_fd[1])<0)){
  1807. LOG(L_ERR, "ERROR: tcp_main: failed to set non blocking"
  1808. "on child sockets\n");
  1809. /* continue, it's not critical (it will go slower under
  1810. * very high connection rates) */
  1811. }
  1812. #endif
  1813. process_no++;
  1814. child_rank++;
  1815. pid=fork();
  1816. if (pid<0){
  1817. LOG(L_ERR, "ERROR: tcp_main: fork failed: %s\n",
  1818. strerror(errno));
  1819. goto error;
  1820. }else if (pid>0){
  1821. /* parent */
  1822. close(sockfd[1]);
  1823. close(reader_fd[1]);
  1824. tcp_children[r].pid=pid;
  1825. tcp_children[r].proc_no=process_no;
  1826. tcp_children[r].busy=0;
  1827. tcp_children[r].n_reqs=0;
  1828. tcp_children[r].unix_sock=reader_fd[0];
  1829. pt[process_no].pid=pid;
  1830. pt[process_no].unix_sock=sockfd[0];
  1831. pt[process_no].idx=r;
  1832. strncpy(pt[process_no].desc, "tcp receiver", MAX_PT_DESC);
  1833. }else{
  1834. /* child */
  1835. close(sockfd[0]);
  1836. unix_tcp_sock=sockfd[1];
  1837. bind_address=0; /* force a SEGFAULT if someone uses a non-init.
  1838. bind address on tcp */
  1839. /* record pid twice to avoid the child using it, before
  1840. * parent gets a chance to set it*/
  1841. pt[process_no].pid=getpid();
  1842. if (init_child(child_rank) < 0) {
  1843. LOG(L_ERR, "init_children failed\n");
  1844. goto error;
  1845. }
  1846. tcp_receive_loop(reader_fd[1]);
  1847. }
  1848. }
  1849. return 0;
  1850. error:
  1851. return -1;
  1852. }
  1853. void tcp_get_info(struct tcp_gen_info *ti)
  1854. {
  1855. ti->tcp_readers=tcp_children_no;
  1856. ti->tcp_max_connections=tcp_max_connections;
  1857. ti->tcp_connections_no=*tcp_connections_no;
  1858. }
  1859. #endif