tcp_main.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681
  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. */
  60. #ifdef USE_TCP
  61. #ifndef SHM_MEM
  62. #error "shared memory support needed (add -DSHM_MEM to Makefile.defs)"
  63. #endif
  64. #include <sys/time.h>
  65. #include <sys/types.h>
  66. #include <sys/select.h>
  67. #include <sys/socket.h>
  68. #include <netinet/in.h>
  69. #include <netinet/in_systm.h>
  70. #include <netinet/ip.h>
  71. #include <netinet/tcp.h>
  72. #include <sys/uio.h> /* writev*/
  73. #include <netdb.h>
  74. #include <stdlib.h> /*exit() */
  75. #include <unistd.h>
  76. #include <errno.h>
  77. #include <string.h>
  78. #ifdef HAVE_SELECT
  79. #include <sys/select.h>
  80. #endif
  81. #include <sys/poll.h>
  82. #include "ip_addr.h"
  83. #include "pass_fd.h"
  84. #include "tcp_conn.h"
  85. #include "globals.h"
  86. #include "pt.h"
  87. #include "locking.h"
  88. #include "mem/mem.h"
  89. #include "mem/shm_mem.h"
  90. #include "timer.h"
  91. #include "sr_module.h"
  92. #include "tcp_server.h"
  93. #include "tcp_init.h"
  94. #include "tsend.h"
  95. #ifdef USE_TLS
  96. #include "tls/tls_server.h"
  97. #endif
  98. #define local_malloc pkg_malloc
  99. #define local_free pkg_free
  100. #define HANDLE_IO_INLINE
  101. #include "io_wait.h"
  102. #include <fcntl.h> /* must be included after io_wait.h if SIGIO_RT is used */
  103. #define MAX_TCP_CHILDREN 100
  104. enum fd_types { F_NONE, F_SOCKINFO /* a tcp_listen fd */,
  105. F_TCPCONN, F_TCPCHILD, F_PROC };
  106. struct tcp_child{
  107. pid_t pid;
  108. int proc_no; /* ser proc_no, for debugging */
  109. int unix_sock; /* unix "read child" sock fd */
  110. int busy;
  111. int n_reqs; /* number of requests serviced so far */
  112. };
  113. int tcp_accept_aliases=0; /* by default don't accept aliases */
  114. int tcp_connect_timeout=DEFAULT_TCP_CONNECT_TIMEOUT;
  115. int tcp_send_timeout=DEFAULT_TCP_SEND_TIMEOUT;
  116. enum poll_types tcp_poll_method=0; /* by default choose the best method */
  117. int tcp_max_fd_no=DEFAULT_TCP_MAX_FD_NO;
  118. /* connection hash table (after ip&port) , includes also aliases */
  119. struct tcp_conn_alias** tcpconn_aliases_hash=0;
  120. /* connection hash table (after connection id) */
  121. struct tcp_connection** tcpconn_id_hash=0;
  122. gen_lock_t* tcpconn_lock=0;
  123. struct tcp_child tcp_children[MAX_TCP_CHILDREN];
  124. static int* connection_id=0; /* unique for each connection, used for
  125. quickly finding the corresponding connection
  126. for a reply */
  127. int unix_tcp_sock;
  128. static int tcp_proto_no=-1; /* tcp protocol number as returned by
  129. getprotobyname */
  130. static io_wait_h io_h;
  131. /* set all socket/fd options: disable nagle, tos lowdelay, non-blocking
  132. * return -1 on error */
  133. static int init_sock_opt(int s)
  134. {
  135. int flags;
  136. int optval;
  137. #ifdef DISABLE_NAGLE
  138. flags=1;
  139. if ( (tcp_proto_no!=-1) && (setsockopt(s, tcp_proto_no , TCP_NODELAY,
  140. &flags, sizeof(flags))<0) ){
  141. LOG(L_WARN, "WARNING: init_sock_opt: could not disable Nagle: %s\n",
  142. strerror(errno));
  143. }
  144. #endif
  145. /* tos*/
  146. optval=IPTOS_LOWDELAY;
  147. if (setsockopt(s, IPPROTO_IP, IP_TOS, (void*)&optval,sizeof(optval)) ==-1){
  148. LOG(L_WARN, "WARNING: init_sock_opt: setsockopt tos: %s\n",
  149. strerror(errno));
  150. /* continue since this is not critical */
  151. }
  152. /* non-blocking */
  153. flags=fcntl(s, F_GETFL);
  154. if (flags==-1){
  155. LOG(L_ERR, "ERROR: init_sock_opt: fnctl failed: (%d) %s\n",
  156. errno, strerror(errno));
  157. goto error;
  158. }
  159. if (fcntl(s, F_SETFL, flags|O_NONBLOCK)==-1){
  160. LOG(L_ERR, "ERROR: init_sock_opt: fcntl: set non-blocking failed:"
  161. " (%d) %s\n", errno, strerror(errno));
  162. goto error;
  163. }
  164. return 0;
  165. error:
  166. return -1;
  167. }
  168. /* blocking connect on a non-blocking fd; it will timeout after
  169. * tcp_connect_timeout
  170. * if BLOCKING_USE_SELECT and HAVE_SELECT are defined it will internally
  171. * use select() instead of poll (bad if fd > FD_SET_SIZE, poll is preferred)
  172. */
  173. static int tcp_blocking_connect(int fd, const struct sockaddr *servaddr,
  174. socklen_t addrlen)
  175. {
  176. int n;
  177. #if defined(HAVE_SELECT) && defined(BLOCKING_USE_SELECT)
  178. fd_set sel_set;
  179. fd_set orig_set;
  180. struct timeval timeout;
  181. #else
  182. struct pollfd pf;
  183. #endif
  184. int elapsed;
  185. int to;
  186. int ticks;
  187. int err;
  188. unsigned int err_len;
  189. to=tcp_connect_timeout;
  190. ticks=get_ticks();
  191. again:
  192. n=connect(fd, servaddr, addrlen);
  193. if (n==-1){
  194. if (errno==EINTR){
  195. elapsed=(get_ticks()-ticks)*TIMER_TICK;
  196. if (elapsed<to) goto again;
  197. else goto error_timeout;
  198. }
  199. if (errno!=EINPROGRESS && errno!=EALREADY){
  200. LOG(L_ERR, "ERROR: tcp_blocking_connect: (%d) %s\n",
  201. errno, strerror(errno));
  202. goto error;
  203. }
  204. }else goto end;
  205. /* poll/select loop */
  206. #if defined(HAVE_SELECT) && defined(BLOCKING_USE_SELECT)
  207. FD_ZERO(&orig_set);
  208. FD_SET(fd, &orig_set);
  209. #else
  210. pf.fd=fd;
  211. pf.events=POLLOUT;
  212. #endif
  213. while(1){
  214. elapsed=(get_ticks()-ticks)*TIMER_TICK;
  215. if (elapsed<to)
  216. to-=elapsed;
  217. else
  218. goto error_timeout;
  219. #if defined(HAVE_SELECT) && defined(BLOCKING_USE_SELECT)
  220. sel_set=orig_set;
  221. timeout.tv_sec=to;
  222. timeout.tv_usec=0;
  223. n=select(fd+1, 0, &sel_set, 0, &timeout);
  224. #else
  225. n=poll(&pf, 1, to*1000);
  226. #endif
  227. if (n<0){
  228. if (errno==EINTR) continue;
  229. LOG(L_ERR, "ERROR: tcp_blocking_connect: poll/select failed:"
  230. " (%d) %s\n", errno, strerror(errno));
  231. goto error;
  232. }else if (n==0) /* timeout */ continue;
  233. #if defined(HAVE_SELECT) && defined(BLOCKING_USE_SELECT)
  234. if (FD_ISSET(fd, &sel_set)){
  235. #else
  236. if (pf.revents&(POLLERR|POLLHUP|POLLNVAL)){
  237. LOG(L_ERR, "ERROR: tcp_blocking_connect: bad poll flags %x\n",
  238. pf.revents);
  239. goto error;
  240. }else{
  241. #endif
  242. err_len=sizeof(err);
  243. getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &err_len);
  244. if (err==0) goto end;
  245. if (err!=EINPROGRESS && err!=EALREADY){
  246. LOG(L_ERR, "ERROR: tcp_blocking_connect: SO_ERROR (%d) %s\n",
  247. err, strerror(err));
  248. goto error;
  249. }
  250. }
  251. }
  252. error_timeout:
  253. /* timeout */
  254. LOG(L_ERR, "ERROR: tcp_blocking_connect: timeout %d s elapsed from %d s\n",
  255. elapsed, tcp_connect_timeout);
  256. error:
  257. return -1;
  258. end:
  259. return 0;
  260. }
  261. #if 0
  262. /* blocking write even on non-blocking sockets
  263. * if TCP_TIMEOUT will return with error */
  264. static int tcp_blocking_write(struct tcp_connection* c, int fd, char* buf,
  265. unsigned int len)
  266. {
  267. int n;
  268. fd_set sel_set;
  269. struct timeval timeout;
  270. int ticks;
  271. int initial_len;
  272. initial_len=len;
  273. again:
  274. n=send(fd, buf, len,
  275. #ifdef HAVE_MSG_NOSIGNAL
  276. MSG_NOSIGNAL
  277. #else
  278. 0
  279. #endif
  280. );
  281. if (n<0){
  282. if (errno==EINTR) goto again;
  283. else if (errno!=EAGAIN && errno!=EWOULDBLOCK){
  284. LOG(L_ERR, "tcp_blocking_write: failed to send: (%d) %s\n",
  285. errno, strerror(errno));
  286. goto error;
  287. }
  288. }else if (n<len){
  289. /* partial write */
  290. buf+=n;
  291. len-=n;
  292. }else{
  293. /* success: full write */
  294. goto end;
  295. }
  296. while(1){
  297. FD_ZERO(&sel_set);
  298. FD_SET(fd, &sel_set);
  299. timeout.tv_sec=tcp_send_timeout;
  300. timeout.tv_usec=0;
  301. ticks=get_ticks();
  302. n=select(fd+1, 0, &sel_set, 0, &timeout);
  303. if (n<0){
  304. if (errno==EINTR) continue; /* signal, ignore */
  305. LOG(L_ERR, "ERROR: tcp_blocking_write: select failed: "
  306. " (%d) %s\n", errno, strerror(errno));
  307. goto error;
  308. }else if (n==0){
  309. /* timeout */
  310. if (get_ticks()-ticks>=tcp_send_timeout){
  311. LOG(L_ERR, "ERROR: tcp_blocking_write: send timeout (%d)\n",
  312. tcp_send_timeout);
  313. goto error;
  314. }
  315. continue;
  316. }
  317. if (FD_ISSET(fd, &sel_set)){
  318. /* we can write again */
  319. goto again;
  320. }
  321. }
  322. error:
  323. return -1;
  324. end:
  325. return initial_len;
  326. }
  327. #endif
  328. struct tcp_connection* tcpconn_new(int sock, union sockaddr_union* su,
  329. struct socket_info* ba, int type,
  330. int state)
  331. {
  332. struct tcp_connection *c;
  333. c=(struct tcp_connection*)shm_malloc(sizeof(struct tcp_connection));
  334. if (c==0){
  335. LOG(L_ERR, "ERROR: tcpconn_new: mem. allocation failure\n");
  336. goto error;
  337. }
  338. memset(c, 0, sizeof(struct tcp_connection)); /* zero init */
  339. c->s=sock;
  340. c->fd=-1; /* not initialized */
  341. if (lock_init(&c->write_lock)==0){
  342. LOG(L_ERR, "ERROR: tcpconn_new: init lock failed\n");
  343. goto error;
  344. }
  345. c->rcv.src_su=*su;
  346. c->refcnt=0;
  347. su2ip_addr(&c->rcv.src_ip, su);
  348. c->rcv.src_port=su_getport(su);
  349. c->rcv.bind_address=ba;
  350. if (ba){
  351. c->rcv.dst_ip=ba->address;
  352. c->rcv.dst_port=ba->port_no;
  353. }
  354. print_ip("tcpconn_new: new tcp connection: ", &c->rcv.src_ip, "\n");
  355. DBG( "tcpconn_new: on port %d, type %d\n", c->rcv.src_port, type);
  356. init_tcp_req(&c->req);
  357. c->id=(*connection_id)++;
  358. c->rcv.proto_reserved1=0; /* this will be filled before receive_message*/
  359. c->rcv.proto_reserved2=0;
  360. c->state=state;
  361. c->extra_data=0;
  362. #ifdef USE_TLS
  363. if (type==PROTO_TLS){
  364. if (tls_tcpconn_init(c, sock)==-1) goto error;
  365. }else
  366. #endif /* USE_TLS*/
  367. {
  368. c->type=PROTO_TCP;
  369. c->rcv.proto=PROTO_TCP;
  370. c->flags=0;
  371. c->timeout=get_ticks()+TCP_CON_TIMEOUT;
  372. }
  373. return c;
  374. error:
  375. if (c) shm_free(c);
  376. return 0;
  377. }
  378. struct tcp_connection* tcpconn_connect(union sockaddr_union* server, int type)
  379. {
  380. int s;
  381. struct socket_info* si;
  382. union sockaddr_union my_name;
  383. socklen_t my_name_len;
  384. struct tcp_connection* con;
  385. struct ip_addr ip;
  386. s=socket(AF2PF(server->s.sa_family), SOCK_STREAM, 0);
  387. if (s==-1){
  388. LOG(L_ERR, "ERROR: tcpconn_connect: socket: (%d) %s\n",
  389. errno, strerror(errno));
  390. goto error;
  391. }
  392. if (init_sock_opt(s)<0){
  393. LOG(L_ERR, "ERROR: tcpconn_connect: init_sock_opt failed\n");
  394. goto error;
  395. }
  396. if (tcp_blocking_connect(s, &server->s, sockaddru_len(*server))<0){
  397. LOG(L_ERR, "ERROR: tcpconn_connect: tcp_blocking_connect failed\n");
  398. goto error;
  399. }
  400. my_name_len=sizeof(my_name);
  401. if (getsockname(s, &my_name.s, &my_name_len)!=0){
  402. LOG(L_ERR, "ERROR: tcp_connect: getsockname failed: %s(%d)\n",
  403. strerror(errno), errno);
  404. si=0; /* try to go on */
  405. }
  406. su2ip_addr(&ip, &my_name);
  407. #ifdef USE_TLS
  408. if (type==PROTO_TLS)
  409. si=find_si(&ip, 0, PROTO_TLS);
  410. else
  411. #endif
  412. si=find_si(&ip, 0, PROTO_TCP);
  413. if (si==0){
  414. LOG(L_ERR, "ERROR: tcp_connect: could not find corresponding"
  415. " listening socket, using default...\n");
  416. if (server->s.sa_family==AF_INET) si=sendipv4_tcp;
  417. #ifdef USE_IPV6
  418. else si=sendipv6_tcp;
  419. #endif
  420. }
  421. con=tcpconn_new(s, server, si, type, S_CONN_CONNECT);
  422. if (con==0){
  423. LOG(L_ERR, "ERROR: tcp_connect: tcpconn_new failed, closing the "
  424. " socket\n");
  425. goto error;
  426. }
  427. return con;
  428. /*FIXME: set sock idx! */
  429. error:
  430. if (s!=-1) close(s); /* close the opened socket */
  431. return 0;
  432. }
  433. struct tcp_connection* tcpconn_add(struct tcp_connection *c)
  434. {
  435. unsigned hash;
  436. if (c){
  437. TCPCONN_LOCK;
  438. /* add it at the begining of the list*/
  439. hash=tcp_id_hash(c->id);
  440. c->id_hash=hash;
  441. tcpconn_listadd(tcpconn_id_hash[hash], c, id_next, id_prev);
  442. hash=tcp_addr_hash(&c->rcv.src_ip, c->rcv.src_port);
  443. /* set the first alias */
  444. c->con_aliases[0].port=c->rcv.src_port;
  445. c->con_aliases[0].hash=hash;
  446. c->con_aliases[0].parent=c;
  447. tcpconn_listadd(tcpconn_aliases_hash[hash], &c->con_aliases[0],
  448. next, prev);
  449. c->aliases++;
  450. TCPCONN_UNLOCK;
  451. DBG("tcpconn_add: hashes: %d, %d\n", hash, c->id_hash);
  452. return c;
  453. }else{
  454. LOG(L_CRIT, "tcpconn_add: BUG: null connection pointer\n");
  455. return 0;
  456. }
  457. }
  458. /* unsafe tcpconn_rm version (nolocks) */
  459. void _tcpconn_rm(struct tcp_connection* c)
  460. {
  461. int r;
  462. tcpconn_listrm(tcpconn_id_hash[c->id_hash], c, id_next, id_prev);
  463. /* remove all the aliases */
  464. for (r=0; r<c->aliases; r++)
  465. tcpconn_listrm(tcpconn_aliases_hash[c->con_aliases[r].hash],
  466. &c->con_aliases[r], next, prev);
  467. lock_destroy(&c->write_lock);
  468. #ifdef USE_TLS
  469. if (c->type==PROTO_TLS) tls_tcpconn_clean(c);
  470. #endif
  471. shm_free(c);
  472. }
  473. void tcpconn_rm(struct tcp_connection* c)
  474. {
  475. int r;
  476. TCPCONN_LOCK;
  477. tcpconn_listrm(tcpconn_id_hash[c->id_hash], c, id_next, id_prev);
  478. /* remove all the aliases */
  479. for (r=0; r<c->aliases; r++)
  480. tcpconn_listrm(tcpconn_aliases_hash[c->con_aliases[r].hash],
  481. &c->con_aliases[r], next, prev);
  482. TCPCONN_UNLOCK;
  483. lock_destroy(&c->write_lock);
  484. #ifdef USE_TLS
  485. if ((c->type==PROTO_TLS)&&(c->extra_data)) tls_tcpconn_clean(c);
  486. #endif
  487. shm_free(c);
  488. }
  489. /* finds a connection, if id=0 uses the ip addr & port (host byte order)
  490. * WARNING: unprotected (locks) use tcpconn_get unless you really
  491. * know what you are doing */
  492. struct tcp_connection* _tcpconn_find(int id, struct ip_addr* ip, int port)
  493. {
  494. struct tcp_connection *c;
  495. struct tcp_conn_alias* a;
  496. unsigned hash;
  497. #ifdef EXTRA_DEBUG
  498. DBG("tcpconn_find: %d port %d\n",id, port);
  499. if (ip) print_ip("tcpconn_find: ip ", ip, "\n");
  500. #endif
  501. if (id){
  502. hash=tcp_id_hash(id);
  503. for (c=tcpconn_id_hash[hash]; c; c=c->id_next){
  504. #ifdef EXTRA_DEBUG
  505. DBG("c=%p, c->id=%d, port=%d\n",c, c->id, c->rcv.src_port);
  506. print_ip("ip=", &c->rcv.src_ip, "\n");
  507. #endif
  508. if ((id==c->id)&&(c->state!=S_CONN_BAD)) return c;
  509. }
  510. }else if (ip){
  511. hash=tcp_addr_hash(ip, port);
  512. for (a=tcpconn_aliases_hash[hash]; a; a=a->next){
  513. #ifdef EXTRA_DEBUG
  514. DBG("a=%p, c=%p, c->id=%d, alias port= %d port=%d\n", a, a->parent,
  515. a->parent->id, a->port, a->parent->rcv.src_port);
  516. print_ip("ip=",&a->parent->rcv.src_ip,"\n");
  517. #endif
  518. if ( (a->parent->state!=S_CONN_BAD) && (port==a->port) &&
  519. (ip_addr_cmp(ip, &a->parent->rcv.src_ip)) )
  520. return a->parent;
  521. }
  522. }
  523. return 0;
  524. }
  525. /* _tcpconn_find with locks and timeout */
  526. struct tcp_connection* tcpconn_get(int id, struct ip_addr* ip, int port,
  527. int timeout)
  528. {
  529. struct tcp_connection* c;
  530. TCPCONN_LOCK;
  531. c=_tcpconn_find(id, ip, port);
  532. if (c){
  533. c->refcnt++;
  534. c->timeout=get_ticks()+timeout;
  535. }
  536. TCPCONN_UNLOCK;
  537. return c;
  538. }
  539. /* add port as an alias for the "id" connection
  540. * returns 0 on success,-1 on failure */
  541. int tcpconn_add_alias(int id, int port, int proto)
  542. {
  543. struct tcp_connection* c;
  544. unsigned hash;
  545. struct tcp_conn_alias* a;
  546. a=0;
  547. /* fix the port */
  548. port=port?port:((proto==PROTO_TLS)?SIPS_PORT:SIP_PORT);
  549. TCPCONN_LOCK;
  550. /* check if alias already exists */
  551. c=_tcpconn_find(id, 0, 0);
  552. if (c){
  553. hash=tcp_addr_hash(&c->rcv.src_ip, port);
  554. /* search the aliases for an already existing one */
  555. for (a=tcpconn_aliases_hash[hash]; a; a=a->next){
  556. if ( (a->parent->state!=S_CONN_BAD) && (port==a->port) &&
  557. (ip_addr_cmp(&c->rcv.src_ip, &a->parent->rcv.src_ip)) ){
  558. /* found */
  559. if (a->parent!=c) goto error_sec;
  560. else goto ok;
  561. }
  562. }
  563. if (c->aliases>=TCP_CON_MAX_ALIASES) goto error_aliases;
  564. c->con_aliases[c->aliases].parent=c;
  565. c->con_aliases[c->aliases].port=port;
  566. c->con_aliases[c->aliases].hash=hash;
  567. tcpconn_listadd(tcpconn_aliases_hash[hash],
  568. &c->con_aliases[c->aliases], next, prev);
  569. c->aliases++;
  570. }else goto error_not_found;
  571. ok:
  572. TCPCONN_UNLOCK;
  573. #ifdef EXTRA_DEBUG
  574. if (a) DBG("tcpconn_add_alias: alias already present\n");
  575. else DBG("tcpconn_add_alias: alias port %d for hash %d, id %d\n",
  576. port, hash, c->id);
  577. #endif
  578. return 0;
  579. error_aliases:
  580. TCPCONN_UNLOCK;
  581. LOG(L_ERR, "ERROR: tcpconn_add_alias: too many aliases for connection %p"
  582. " (%d)\n", c, c->id);
  583. return -1;
  584. error_not_found:
  585. TCPCONN_UNLOCK;
  586. LOG(L_ERR, "ERROR: tcpconn_add_alias: no connection found for id %d\n",id);
  587. return -1;
  588. error_sec:
  589. TCPCONN_UNLOCK;
  590. LOG(L_ERR, "ERROR: tcpconn_add_alias: possible port hijack attempt\n");
  591. LOG(L_ERR, "ERROR: tcpconn_add_alias: alias already present and points"
  592. " to another connection (%d : %d and %d : %d)\n",
  593. a->parent->id, port, c->id, port);
  594. return -1;
  595. }
  596. void tcpconn_ref(struct tcp_connection* c)
  597. {
  598. TCPCONN_LOCK;
  599. c->refcnt++; /* FIXME: atomic_dec */
  600. TCPCONN_UNLOCK;
  601. }
  602. void tcpconn_put(struct tcp_connection* c)
  603. {
  604. TCPCONN_LOCK;
  605. c->refcnt--; /* FIXME: atomic_dec */
  606. TCPCONN_UNLOCK;
  607. }
  608. /* finds a tcpconn & sends on it */
  609. int tcp_send(int type, char* buf, unsigned len, union sockaddr_union* to,
  610. int id)
  611. {
  612. struct tcp_connection *c;
  613. struct tcp_connection *tmp;
  614. struct ip_addr ip;
  615. int port;
  616. int fd;
  617. long response[2];
  618. int n;
  619. port=0;
  620. if (to){
  621. su2ip_addr(&ip, to);
  622. port=su_getport(to);
  623. c=tcpconn_get(id, &ip, port, TCP_CON_SEND_TIMEOUT);
  624. }else if (id){
  625. c=tcpconn_get(id, 0, 0, TCP_CON_SEND_TIMEOUT);
  626. }else{
  627. LOG(L_CRIT, "BUG: tcp_send called with null id & to\n");
  628. return -1;
  629. }
  630. if (id){
  631. if (c==0) {
  632. if (to){
  633. /* try again w/o id */
  634. c=tcpconn_get(0, &ip, port, TCP_CON_SEND_TIMEOUT);
  635. goto no_id;
  636. }else{
  637. LOG(L_ERR, "ERROR: tcp_send: id %d not found, dropping\n",
  638. id);
  639. return -1;
  640. }
  641. }else goto get_fd;
  642. }
  643. no_id:
  644. if (c==0){
  645. DBG("tcp_send: no open tcp connection found, opening new one\n");
  646. /* create tcp connection */
  647. if ((c=tcpconn_connect(to, type))==0){
  648. LOG(L_ERR, "ERROR: tcp_send: connect failed\n");
  649. return -1;
  650. }
  651. c->refcnt++; /* safe to do it w/o locking, it's not yet
  652. available to the rest of the world */
  653. fd=c->s;
  654. /* send the new tcpconn to "tcp main" */
  655. response[0]=(long)c;
  656. response[1]=CONN_NEW;
  657. n=send_fd(unix_tcp_sock, response, sizeof(response), c->s);
  658. if (n<=0){
  659. LOG(L_ERR, "BUG: tcp_send: failed send_fd: %s (%d)\n",
  660. strerror(errno), errno);
  661. n=-1;
  662. goto end;
  663. }
  664. goto send_it;
  665. }
  666. get_fd:
  667. /* todo: see if this is not the same process holding
  668. * c and if so send directly on c->fd */
  669. DBG("tcp_send: tcp connection found (%p), acquiring fd\n", c);
  670. /* get the fd */
  671. response[0]=(long)c;
  672. response[1]=CONN_GET_FD;
  673. n=send_all(unix_tcp_sock, response, sizeof(response));
  674. if (n<=0){
  675. LOG(L_ERR, "BUG: tcp_send: failed to get fd(write):%s (%d)\n",
  676. strerror(errno), errno);
  677. n=-1;
  678. goto release_c;
  679. }
  680. DBG("tcp_send, c= %p, n=%d\n", c, n);
  681. tmp=c;
  682. n=receive_fd(unix_tcp_sock, &c, sizeof(c), &fd, MSG_WAITALL);
  683. if (n<=0){
  684. LOG(L_ERR, "BUG: tcp_send: failed to get fd(receive_fd):"
  685. " %s (%d)\n", strerror(errno), errno);
  686. n=-1;
  687. goto release_c;
  688. }
  689. if (c!=tmp){
  690. LOG(L_CRIT, "BUG: tcp_send: get_fd: got different connection:"
  691. " %p (id= %d, refcnt=%d state=%d != "
  692. " %p (id= %d, refcnt=%d state=%d (n=%d)\n",
  693. c, c->id, c->refcnt, c->state,
  694. tmp, tmp->id, tmp->refcnt, tmp->state, n
  695. );
  696. n=-1; /* fail */
  697. goto end;
  698. }
  699. DBG("tcp_send: after receive_fd: c= %p n=%d fd=%d\n",c, n, fd);
  700. send_it:
  701. DBG("tcp_send: sending...\n");
  702. lock_get(&c->write_lock);
  703. #ifdef USE_TLS
  704. if (c->type==PROTO_TLS)
  705. n=tls_blocking_write(c, fd, buf, len);
  706. else
  707. #endif
  708. /* n=tcp_blocking_write(c, fd, buf, len); */
  709. n=tsend_stream(fd, buf, len, tcp_send_timeout*1000);
  710. lock_release(&c->write_lock);
  711. DBG("tcp_send: after write: c= %p n=%d fd=%d\n",c, n, fd);
  712. DBG("tcp_send: buf=\n%.*s\n", (int)len, buf);
  713. if (n<0){
  714. LOG(L_ERR, "ERROR: tcp_send: failed to send\n");
  715. /* error on the connection , mark it as bad and set 0 timeout */
  716. c->state=S_CONN_BAD;
  717. c->timeout=0;
  718. /* tell "main" it should drop this (optional it will t/o anyway?)*/
  719. response[0]=(long)c;
  720. response[1]=CONN_ERROR;
  721. n=send_all(unix_tcp_sock, response, sizeof(response));
  722. /* CONN_ERROR will auto-dec refcnt => we must not call tcpconn_put !!*/
  723. if (n<=0){
  724. LOG(L_ERR, "BUG: tcp_send: error return failed (write):%s (%d)\n",
  725. strerror(errno), errno);
  726. n=-1;
  727. }
  728. close(fd);
  729. return n; /* error return, no tcpconn_put */
  730. }
  731. end:
  732. close(fd);
  733. release_c:
  734. tcpconn_put(c); /* release c (lock; dec refcnt; unlock) */
  735. return n;
  736. }
  737. int tcp_init(struct socket_info* sock_info)
  738. {
  739. union sockaddr_union* addr;
  740. int optval;
  741. #ifdef DISABLE_NAGLE
  742. int flag;
  743. struct protoent* pe;
  744. if (tcp_proto_no==-1){ /* if not already set */
  745. pe=getprotobyname("tcp");
  746. if (pe==0){
  747. LOG(L_ERR, "ERROR: tcp_init: could not get TCP protocol number\n");
  748. tcp_proto_no=-1;
  749. }else{
  750. tcp_proto_no=pe->p_proto;
  751. }
  752. }
  753. #endif
  754. addr=&sock_info->su;
  755. /* sock_info->proto=PROTO_TCP; */
  756. if (init_su(addr, &sock_info->address, sock_info->port_no)<0){
  757. LOG(L_ERR, "ERROR: tcp_init: could no init sockaddr_union\n");
  758. goto error;
  759. }
  760. sock_info->socket=socket(AF2PF(addr->s.sa_family), SOCK_STREAM, 0);
  761. if (sock_info->socket==-1){
  762. LOG(L_ERR, "ERROR: tcp_init: socket: %s\n", strerror(errno));
  763. goto error;
  764. }
  765. #ifdef DISABLE_NAGLE
  766. flag=1;
  767. if ( (tcp_proto_no!=-1) &&
  768. (setsockopt(sock_info->socket, tcp_proto_no , TCP_NODELAY,
  769. &flag, sizeof(flag))<0) ){
  770. LOG(L_ERR, "ERROR: tcp_init: could not disable Nagle: %s\n",
  771. strerror(errno));
  772. }
  773. #endif
  774. #if !defined(TCP_DONT_REUSEADDR)
  775. /* Stevens, "Network Programming", Section 7.5, "Generic Socket
  776. * Options": "...server started,..a child continues..on existing
  777. * connection..listening server is restarted...call to bind fails
  778. * ... ALL TCP servers should specify the SO_REUSEADDRE option
  779. * to allow the server to be restarted in this situation
  780. *
  781. * Indeed, without this option, the server can't restart.
  782. * -jiri
  783. */
  784. optval=1;
  785. if (setsockopt(sock_info->socket, SOL_SOCKET, SO_REUSEADDR,
  786. (void*)&optval, sizeof(optval))==-1) {
  787. LOG(L_ERR, "ERROR: tcp_init: setsockopt %s\n",
  788. strerror(errno));
  789. goto error;
  790. }
  791. #endif
  792. /* tos */
  793. optval=IPTOS_LOWDELAY;
  794. if (setsockopt(sock_info->socket, IPPROTO_IP, IP_TOS, (void*)&optval,
  795. sizeof(optval)) ==-1){
  796. LOG(L_WARN, "WARNING: tcp_init: setsockopt tos: %s\n", strerror(errno));
  797. /* continue since this is not critical */
  798. }
  799. if (bind(sock_info->socket, &addr->s, sockaddru_len(*addr))==-1){
  800. LOG(L_ERR, "ERROR: tcp_init: bind(%x, %p, %d) on %s:%d : %s\n",
  801. sock_info->socket, &addr->s,
  802. (unsigned)sockaddru_len(*addr),
  803. sock_info->address_str.s,
  804. sock_info->port_no,
  805. strerror(errno));
  806. goto error;
  807. }
  808. if (listen(sock_info->socket, 10)==-1){
  809. LOG(L_ERR, "ERROR: tcp_init: listen(%x, %p, %d) on %s: %s\n",
  810. sock_info->socket, &addr->s,
  811. (unsigned)sockaddru_len(*addr),
  812. sock_info->address_str.s,
  813. strerror(errno));
  814. goto error;
  815. }
  816. return 0;
  817. error:
  818. if (sock_info->socket!=-1){
  819. close(sock_info->socket);
  820. sock_info->socket=-1;
  821. }
  822. return -1;
  823. }
  824. static int send2child(struct tcp_connection* tcpconn)
  825. {
  826. int i;
  827. int min_busy;
  828. int idx;
  829. min_busy=tcp_children[0].busy;
  830. idx=0;
  831. for (i=0; i<tcp_children_no; i++){
  832. if (!tcp_children[i].busy){
  833. idx=i;
  834. min_busy=0;
  835. break;
  836. }else if (min_busy>tcp_children[i].busy){
  837. min_busy=tcp_children[i].busy;
  838. idx=i;
  839. }
  840. }
  841. tcp_children[idx].busy++;
  842. tcp_children[idx].n_reqs++;
  843. if (min_busy){
  844. DBG("WARNING: send2child: no free tcp receiver, "
  845. " connection passed to the least busy one (%d)\n",
  846. min_busy);
  847. }
  848. DBG("send2child: to tcp child %d %d(%d), %p\n", idx,
  849. tcp_children[idx].proc_no,
  850. tcp_children[idx].pid, tcpconn);
  851. if (send_fd(tcp_children[idx].unix_sock, &tcpconn, sizeof(tcpconn),
  852. tcpconn->s)<=0){
  853. LOG(L_ERR, "ERROR: send2child: send_fd failed\n");
  854. return -1;
  855. }
  856. return 0;
  857. }
  858. /* handles a new connection, called internally by tcp_main_loop/handle_io.
  859. * params: si - pointer to one of the tcp socket_info structures on which
  860. * an io event was detected (connection attempt)
  861. * returns: handle_* return convention: -1 on error, 0 on EAGAIN (no more
  862. * io events queued), >0 on success. success/error refer only to
  863. * the accept.
  864. */
  865. static inline int handle_new_connect(struct socket_info* si)
  866. {
  867. union sockaddr_union su;
  868. struct tcp_connection* tcpconn;
  869. socklen_t su_len;
  870. int new_sock;
  871. /* got a connection on r */
  872. su_len=sizeof(su);
  873. new_sock=accept(si->socket, &(su.s), &su_len);
  874. if (new_sock==-1){
  875. if ((errno==EAGAIN)||(errno==EWOULDBLOCK))
  876. return 0;
  877. LOG(L_ERR, "WARNING: handle_new_connect: error while accepting"
  878. " connection(%d): %s\n", errno, strerror(errno));
  879. return -1;
  880. }
  881. if (init_sock_opt(new_sock)<0){
  882. LOG(L_ERR, "ERROR: handle_new_connect: init_sock_opt failed\n");
  883. close(new_sock);
  884. return 1; /* success, because the accept was succesfull */
  885. }
  886. /* add socket to list */
  887. tcpconn=tcpconn_new(new_sock, &su, si, si->proto, S_CONN_ACCEPT);
  888. if (tcpconn){
  889. tcpconn->refcnt++; /* safe, not yet available to the
  890. outside world */
  891. tcpconn_add(tcpconn);
  892. DBG("handle_new_connect: new connection: %p %d\n",
  893. tcpconn, tcpconn->s);
  894. /* pass it to a child */
  895. if(send2child(tcpconn)<0){
  896. LOG(L_ERR,"ERROR: handle_new_connect: no children "
  897. "available\n");
  898. TCPCONN_LOCK;
  899. tcpconn->refcnt--;
  900. if (tcpconn->refcnt==0){
  901. close(tcpconn->s);
  902. _tcpconn_rm(tcpconn);
  903. }else tcpconn->timeout=0; /* force expire */
  904. TCPCONN_UNLOCK;
  905. }
  906. }else{ /*tcpconn==0 */
  907. LOG(L_ERR, "ERROR: handle_new_connect: tcpconn_new failed, "
  908. "closing socket\n");
  909. close(new_sock);
  910. }
  911. return 1; /* accept() was succesfull */
  912. }
  913. /* used internally by tcp_main_loop() */
  914. static void tcpconn_destroy(struct tcp_connection* tcpconn)
  915. {
  916. int fd;
  917. TCPCONN_LOCK; /*avoid races w/ tcp_send*/
  918. tcpconn->refcnt--;
  919. if (tcpconn->refcnt==0){
  920. DBG("tcpconn_destroy: destroying connection\n");
  921. fd=tcpconn->s;
  922. #ifdef USE_TLS
  923. /*FIXME: lock ->writelock ? */
  924. if (tcpconn->type==PROTO_TLS)
  925. tls_close(tcpconn, fd);
  926. #endif
  927. _tcpconn_rm(tcpconn);
  928. close(fd);
  929. }else{
  930. /* force timeout */
  931. tcpconn->timeout=0;
  932. tcpconn->state=S_CONN_BAD;
  933. DBG("tcpconn_destroy: delaying ...\n");
  934. }
  935. TCPCONN_UNLOCK;
  936. }
  937. /* handles an io event on one of the watched tcp connections
  938. *
  939. * params: tcpconn - pointer to the tcp_connection for which we have an io ev.
  940. * fd_i - index in the fd_array table (needed for delete)
  941. * returns: handle_* return convention, but on success it always returns 0
  942. * (because it's one-shot, after a succesfull execution the fd is
  943. * removed from tcp_main's watch fd list and passed to a child =>
  944. * tcp_main is not interested in further io events that might be
  945. * queued for this fd)
  946. */
  947. inline static int handle_tcpconn_ev(struct tcp_connection* tcpconn, int fd_i)
  948. {
  949. int fd;
  950. /* FIXME: is refcnt!=0 really necessary? */
  951. if ((tcpconn->refcnt!=0)){
  952. /* FIXME: might be valid for sigio_rt iff fd flags are not cleared
  953. * (there is a short window in which it could generate a sig
  954. * that would be catched by tcp_main) */
  955. LOG(L_CRIT, "BUG: handle_tcpconn_ev: io event on referenced"
  956. " tcpconn (%p), refcnt=%d, fd=%d\n",
  957. tcpconn, tcpconn->refcnt, tcpconn->s);
  958. return -1;
  959. }
  960. /* pass it to child, so remove it from the io watch list */
  961. DBG("handle_tcpconn_ev: data available on %p %d\n", tcpconn, tcpconn->s);
  962. if (io_watch_del(&io_h, tcpconn->s, fd_i, 0)==-1) goto error;
  963. tcpconn_ref(tcpconn); /* refcnt ++ */
  964. if (send2child(tcpconn)<0){
  965. LOG(L_ERR,"ERROR: handle_tcpconn_ev: no children available\n");
  966. TCPCONN_LOCK;
  967. tcpconn->refcnt--;
  968. if (tcpconn->refcnt==0){
  969. fd=tcpconn->s;
  970. _tcpconn_rm(tcpconn);
  971. close(fd);
  972. }else tcpconn->timeout=0; /* force expire*/
  973. TCPCONN_UNLOCK;
  974. }
  975. return 0; /* we are not interested in possibly queued io events,
  976. the fd was either passed to a child, or closed */
  977. error:
  978. return -1;
  979. }
  980. /* handles io from a tcp child process
  981. * params: tcp_c - pointer in the tcp_children array, to the entry for
  982. * which an io event was detected
  983. * fd_i - fd index in the fd_array (usefull for optimizing
  984. * io_watch_deletes)
  985. * returns: handle_* return convention: -1 on error, 0 on EAGAIN (no more
  986. * io events queued), >0 on success. success/error refer only to
  987. * the reads from the fd.
  988. */
  989. inline static int handle_tcp_child(struct tcp_child* tcp_c, int fd_i)
  990. {
  991. struct tcp_connection* tcpconn;
  992. long response[2];
  993. int cmd;
  994. int bytes;
  995. if (tcp_c->unix_sock<=0){
  996. /* (we can't have a fd==0, 0 is never closed )*/
  997. LOG(L_CRIT, "BUG: handle_tcp_child: fd %d for %d "
  998. "(pid %d, ser no %d)\n", tcp_c->unix_sock,
  999. (int)(tcp_c-&tcp_children[0]), tcp_c->pid, tcp_c->proc_no);
  1000. goto error;
  1001. }
  1002. /* read until sizeof(response)
  1003. * (this is a SOCK_STREAM so read is not atomic) */
  1004. bytes=recv_all(tcp_c->unix_sock, response, sizeof(response), MSG_DONTWAIT);
  1005. if (bytes<(int)sizeof(response)){
  1006. if (bytes==0){
  1007. /* EOF -> bad, child has died */
  1008. DBG("DBG: handle_tcp_child: dead tcp child %d (pid %d, no %d)"
  1009. " (shutting down?)\n", (int)(tcp_c-&tcp_children[0]),
  1010. tcp_c->pid, tcp_c->proc_no );
  1011. /* don't listen on it any more */
  1012. io_watch_del(&io_h, tcp_c->unix_sock, fd_i, 0);
  1013. goto error; /* eof. so no more io here, it's ok to return error */
  1014. }else if (bytes<0){
  1015. /* EAGAIN is ok if we try to empty the buffer
  1016. * e.g.: SIGIO_RT overflow mode or EPOLL ET */
  1017. if ((errno!=EAGAIN) && (errno!=EWOULDBLOCK)){
  1018. LOG(L_CRIT, "ERROR: handle_tcp_child: read from tcp child %ld "
  1019. " (pid %d, no %d) %s [%d]\n",
  1020. (long)(tcp_c-&tcp_children[0]), tcp_c->pid,
  1021. tcp_c->proc_no, strerror(errno), errno );
  1022. }else{
  1023. bytes=0;
  1024. }
  1025. /* try to ignore ? */
  1026. goto end;
  1027. }else{
  1028. /* should never happen */
  1029. LOG(L_CRIT, "BUG: handle_tcp_child: too few bytes received (%d)\n",
  1030. bytes );
  1031. bytes=0; /* something was read so there is no error; otoh if
  1032. receive_fd returned less then requested => the receive
  1033. buffer is empty => no more io queued on this fd */
  1034. goto end;
  1035. }
  1036. }
  1037. DBG("handle_tcp_child: reader response= %lx, %ld from %d \n",
  1038. response[0], response[1], (int)(tcp_c-&tcp_children[0]));
  1039. cmd=response[1];
  1040. tcpconn=(struct tcp_connection*)response[0];
  1041. if (tcpconn==0){
  1042. /* should never happen */
  1043. LOG(L_CRIT, "BUG: handle_tcp_child: null tcpconn pointer received"
  1044. " from tcp child %d (pid %d): %lx, %lx\n",
  1045. (int)(tcp_c-&tcp_children[0]), tcp_c->pid,
  1046. response[0], response[1]) ;
  1047. goto end;
  1048. }
  1049. switch(cmd){
  1050. case CONN_RELEASE:
  1051. tcp_c->busy--;
  1052. if (tcpconn->state==S_CONN_BAD){
  1053. tcpconn_destroy(tcpconn);
  1054. break;
  1055. }
  1056. io_watch_add(&io_h, tcpconn->s, F_TCPCONN, tcpconn);
  1057. /* update the timeout*/
  1058. tcpconn->timeout=get_ticks()+TCP_CON_TIMEOUT;
  1059. tcpconn_put(tcpconn);
  1060. DBG("handle_tcp_child: CONN_RELEASE %p refcnt= %d\n",
  1061. tcpconn, tcpconn->refcnt);
  1062. break;
  1063. case CONN_ERROR:
  1064. case CONN_DESTROY:
  1065. case CONN_EOF:
  1066. /* WARNING: this will auto-dec. refcnt! */
  1067. tcp_c->busy--;
  1068. /* main doesn't listen on it => we don't have to delete it
  1069. if (tcpconn->s!=-1)
  1070. io_watch_del(&io_h, tcpconn->s, -1, IO_FD_CLOSING);
  1071. */
  1072. tcpconn_destroy(tcpconn); /* closes also the fd */
  1073. break;
  1074. default:
  1075. LOG(L_CRIT, "BUG: handle_tcp_child: unknown cmd %d"
  1076. " from tcp reader %d\n",
  1077. cmd, (int)(tcp_c-&tcp_children[0]));
  1078. }
  1079. end:
  1080. return bytes;
  1081. error:
  1082. return -1;
  1083. }
  1084. /* handles io from a "generic" ser process (get fd or new_fd from a tcp_send)
  1085. *
  1086. * params: p - pointer in the ser processes array (pt[]), to the entry for
  1087. * which an io event was detected
  1088. * fd_i - fd index in the fd_array (usefull for optimizing
  1089. * io_watch_deletes)
  1090. * returns: handle_* return convention:
  1091. * -1 on error reading from the fd,
  1092. * 0 on EAGAIN or when no more io events are queued
  1093. * (receive buffer empty),
  1094. * >0 on successfull reads from the fd (the receive buffer might
  1095. * be non-empty).
  1096. */
  1097. inline static int handle_ser_child(struct process_table* p, int fd_i)
  1098. {
  1099. struct tcp_connection* tcpconn;
  1100. long response[2];
  1101. int cmd;
  1102. int bytes;
  1103. int ret;
  1104. int fd;
  1105. ret=-1;
  1106. if (p->unix_sock<=0){
  1107. /* (we can't have a fd==0, 0 is never closed )*/
  1108. LOG(L_CRIT, "BUG: handle_ser_child: fd %d for %d "
  1109. "(pid %d)\n", p->unix_sock, (int)(p-&pt[0]), p->pid);
  1110. goto error;
  1111. }
  1112. /* get all bytes and the fd (if transmitted)
  1113. * (this is a SOCK_STREAM so read is not atomic) */
  1114. bytes=receive_fd(p->unix_sock, response, sizeof(response), &fd,
  1115. MSG_DONTWAIT);
  1116. if (bytes<(int)sizeof(response)){
  1117. /* too few bytes read */
  1118. if (bytes==0){
  1119. /* EOF -> bad, child has died */
  1120. DBG("DBG: handle_ser_child: dead child %d, pid %d"
  1121. " (shutting down?)\n", (int)(p-&pt[0]), p->pid);
  1122. /* don't listen on it any more */
  1123. io_watch_del(&io_h, p->unix_sock, fd_i, 0);
  1124. goto error; /* child dead => no further io events from it */
  1125. }else if (bytes<0){
  1126. /* EAGAIN is ok if we try to empty the buffer
  1127. * e.g: SIGIO_RT overflow mode or EPOLL ET */
  1128. if ((errno!=EAGAIN) && (errno!=EWOULDBLOCK)){
  1129. LOG(L_CRIT, "ERROR: handle_ser_child: read from child %d "
  1130. "(pid %d): %s [%d]\n", (int)(p-&pt[0]), p->pid,
  1131. strerror(errno), errno);
  1132. ret=-1;
  1133. }else{
  1134. ret=0;
  1135. }
  1136. /* try to ignore ? */
  1137. goto end;
  1138. }else{
  1139. /* should never happen */
  1140. LOG(L_CRIT, "BUG: handle_ser_child: too few bytes received (%d)\n",
  1141. bytes );
  1142. ret=0; /* something was read so there is no error; otoh if
  1143. receive_fd returned less then requested => the receive
  1144. buffer is empty => no more io queued on this fd */
  1145. goto end;
  1146. }
  1147. }
  1148. ret=1; /* something was received, there might be more queued */
  1149. DBG("handle_ser_child: read response= %lx, %ld, fd %d from %d (%d)\n",
  1150. response[0], response[1], fd, (int)(p-&pt[0]), p->pid);
  1151. cmd=response[1];
  1152. tcpconn=(struct tcp_connection*)response[0];
  1153. if (tcpconn==0){
  1154. LOG(L_CRIT, "BUG: handle_ser_child: null tcpconn pointer received"
  1155. " from child %d (pid %d): %lx, %lx\n",
  1156. (int)(p-&pt[0]), p->pid, response[0], response[1]) ;
  1157. goto end;
  1158. }
  1159. switch(cmd){
  1160. case CONN_ERROR:
  1161. if (tcpconn->s!=-1)
  1162. io_watch_del(&io_h, tcpconn->s, -1, IO_FD_CLOSING);
  1163. tcpconn_destroy(tcpconn); /* will close also the fd */
  1164. break;
  1165. case CONN_GET_FD:
  1166. /* send the requested FD */
  1167. /* WARNING: take care of setting refcnt properly to
  1168. * avoid race condition */
  1169. if (send_fd(p->unix_sock, &tcpconn, sizeof(tcpconn),
  1170. tcpconn->s)<=0){
  1171. LOG(L_ERR, "ERROR: handle_ser_child: send_fd failed\n");
  1172. }
  1173. break;
  1174. case CONN_NEW:
  1175. /* update the fd in the requested tcpconn*/
  1176. /* WARNING: take care of setting refcnt properly to
  1177. * avoid race condition */
  1178. if (fd==-1){
  1179. LOG(L_CRIT, "BUG: handle_ser_child: CONN_NEW:"
  1180. " no fd received\n");
  1181. break;
  1182. }
  1183. tcpconn->s=fd;
  1184. /* add tcpconn to the list*/
  1185. tcpconn_add(tcpconn);
  1186. io_watch_add(&io_h, tcpconn->s, F_TCPCONN, tcpconn);
  1187. /* update the timeout*/
  1188. tcpconn->timeout=get_ticks()+TCP_CON_TIMEOUT;
  1189. break;
  1190. default:
  1191. LOG(L_CRIT, "BUG: handle_ser_child: unknown cmd %d\n", cmd);
  1192. }
  1193. end:
  1194. return ret;
  1195. error:
  1196. return -1;
  1197. }
  1198. /* generic handle io routine, it will call the appropiate
  1199. * handle_xxx() based on the fd_map type
  1200. *
  1201. * params: fm - pointer to a fd hash entry
  1202. * idx - index in the fd_array (or -1 if not known)
  1203. * return: -1 on error
  1204. * 0 on EAGAIN or when by some other way it is known that no more
  1205. * io events are queued on the fd (the receive buffer is empty).
  1206. * Usefull to detect when there are no more io events queued for
  1207. * sigio_rt, epoll_et, kqueue.
  1208. * >0 on successfull read from the fd (when there might be more io
  1209. * queued -- the receive buffer might still be non-empty)
  1210. */
  1211. inline static int handle_io(struct fd_map* fm, int idx)
  1212. {
  1213. int ret;
  1214. switch(fm->type){
  1215. case F_SOCKINFO:
  1216. ret=handle_new_connect((struct socket_info*)fm->data);
  1217. break;
  1218. case F_TCPCONN:
  1219. ret=handle_tcpconn_ev((struct tcp_connection*)fm->data, idx);
  1220. break;
  1221. case F_TCPCHILD:
  1222. ret=handle_tcp_child((struct tcp_child*)fm->data, idx);
  1223. break;
  1224. case F_PROC:
  1225. ret=handle_ser_child((struct process_table*)fm->data, idx);
  1226. break;
  1227. case F_NONE:
  1228. LOG(L_CRIT, "BUG: handle_io: empty fd map\n");
  1229. goto error;
  1230. default:
  1231. LOG(L_CRIT, "BUG: handle_io: uknown fd type %d\n", fm->type);
  1232. goto error;
  1233. }
  1234. return ret;
  1235. error:
  1236. return -1;
  1237. }
  1238. /* very inefficient for now - FIXME*/
  1239. static void tcpconn_timeout()
  1240. {
  1241. struct tcp_connection *c, *next;
  1242. int ticks;
  1243. unsigned h;
  1244. int fd;
  1245. ticks=get_ticks();
  1246. TCPCONN_LOCK; /* fixme: we can lock only on delete IMO */
  1247. for(h=0; h<TCP_ID_HASH_SIZE; h++){
  1248. c=tcpconn_id_hash[h];
  1249. while(c){
  1250. next=c->id_next;
  1251. if ((c->refcnt==0) && (ticks>c->timeout)) {
  1252. DBG("tcpconn_timeout: timeout for hash=%d - %p (%d > %d)\n",
  1253. h, c, ticks, c->timeout);
  1254. fd=c->s;
  1255. #ifdef USE_TLS
  1256. if (c->type==PROTO_TLS)
  1257. tls_close(c, fd);
  1258. #endif
  1259. _tcpconn_rm(c);
  1260. if (fd>0) {
  1261. io_watch_del(&io_h, fd, -1, IO_FD_CLOSING);
  1262. close(fd);
  1263. }
  1264. }
  1265. c=next;
  1266. }
  1267. }
  1268. TCPCONN_UNLOCK;
  1269. }
  1270. /* tcp main loop */
  1271. void tcp_main_loop()
  1272. {
  1273. struct socket_info* si;
  1274. int r;
  1275. /* init io_wait (here because we want the memory allocated only in
  1276. * the tcp_main process) */
  1277. /* FIXME: TODO: make tcp_max_fd_no a config param */
  1278. if (init_io_wait(&io_h, tcp_max_fd_no, tcp_poll_method)<0)
  1279. goto error;
  1280. /* init: start watching all the fds*/
  1281. /* add all the sockets we listens on for connections */
  1282. for (si=tcp_listen; si; si=si->next){
  1283. if ((si->proto==PROTO_TCP) &&(si->socket!=-1)){
  1284. if (io_watch_add(&io_h, si->socket, F_SOCKINFO, si)<0){
  1285. LOG(L_CRIT, "ERROR: tcp_main_loop: init: failed to add "
  1286. "listen socket to the fd list\n");
  1287. goto error;
  1288. }
  1289. }else{
  1290. LOG(L_CRIT, "BUG: tcp_main_loop: non tcp address in tcp_listen\n");
  1291. }
  1292. }
  1293. #ifdef USE_TLS
  1294. if (!tls_disable){
  1295. for (si=tls_listen; si; si=si->next){
  1296. if ((si->proto==PROTO_TLS) && (si->socket!=-1)){
  1297. if (io_watch_add(&io_h, si->socket, F_SOCKINFO, si)<0){
  1298. LOG(L_CRIT, "ERROR: tcp_main_loop: init: failed to add "
  1299. "tls listen socket to the fd list\n");
  1300. goto error;
  1301. }
  1302. }else{
  1303. LOG(L_CRIT, "BUG: tcp_main_loop: non tls address"
  1304. " in tls_listen\n");
  1305. }
  1306. }
  1307. }
  1308. #endif
  1309. /* add all the unix sockets used for communcation with other ser processes
  1310. * (get fd, new connection a.s.o) */
  1311. for (r=1; r<process_no; r++){
  1312. if (pt[r].unix_sock>0) /* we can't have 0, we never close it!*/
  1313. if (io_watch_add(&io_h, pt[r].unix_sock, F_PROC, &pt[r])<0){
  1314. LOG(L_CRIT, "ERROR: tcp_main_loop: init: failed to add "
  1315. "process %d unix socket to the fd list\n", r);
  1316. goto error;
  1317. }
  1318. }
  1319. /* add all the unix sokets used for communication with the tcp childs */
  1320. for (r=0; r<tcp_children_no; r++){
  1321. if (tcp_children[r].unix_sock>0)/*we can't have 0, we never close it!*/
  1322. if (io_watch_add(&io_h, tcp_children[r].unix_sock, F_TCPCHILD,
  1323. &tcp_children[r]) <0){
  1324. LOG(L_CRIT, "ERROR: tcp_main_loop: init: failed to add "
  1325. "tcp child %d unix socket to the fd list\n", r);
  1326. goto error;
  1327. }
  1328. }
  1329. /* main loop */
  1330. switch(io_h.poll_method){
  1331. case POLL_POLL:
  1332. while(1){
  1333. /* wait and process IO */
  1334. io_wait_loop_poll(&io_h, TCP_MAIN_SELECT_TIMEOUT, 0);
  1335. /* remove old connections */
  1336. tcpconn_timeout();
  1337. }
  1338. break;
  1339. #ifdef HAVE_SELECT
  1340. case POLL_SELECT:
  1341. while(1){
  1342. io_wait_loop_select(&io_h, TCP_MAIN_SELECT_TIMEOUT, 0);
  1343. tcpconn_timeout();
  1344. }
  1345. break;
  1346. #endif
  1347. #ifdef HAVE_SIGIO_RT
  1348. case POLL_SIGIO_RT:
  1349. while(1){
  1350. io_wait_loop_sigio_rt(&io_h, TCP_MAIN_SELECT_TIMEOUT);
  1351. tcpconn_timeout();
  1352. }
  1353. break;
  1354. #endif
  1355. #ifdef HAVE_EPOLL
  1356. case POLL_EPOLL_LT:
  1357. while(1){
  1358. io_wait_loop_epoll(&io_h, TCP_MAIN_SELECT_TIMEOUT, 0);
  1359. tcpconn_timeout();
  1360. }
  1361. break;
  1362. case POLL_EPOLL_ET:
  1363. while(1){
  1364. io_wait_loop_epoll(&io_h, TCP_MAIN_SELECT_TIMEOUT, 1);
  1365. tcpconn_timeout();
  1366. }
  1367. break;
  1368. #endif
  1369. #ifdef HAVE_KQUEUE
  1370. case POLL_KQUEUE:
  1371. while(1){
  1372. io_wait_loop_kqueue(&io_h, TCP_MAIN_SELECT_TIMEOUT, 0);
  1373. tcpconn_timeout();
  1374. }
  1375. break;
  1376. #endif
  1377. #ifdef HAVE_DEVPOLL
  1378. case POLL_DEVPOLL:
  1379. while(1){
  1380. io_wait_loop_devpoll(&io_h, TCP_MAIN_SELECT_TIMEOUT, 0);
  1381. tcpconn_timeout();
  1382. }
  1383. break;
  1384. #endif
  1385. default:
  1386. LOG(L_CRIT, "BUG: tcp_main_loop: no support for poll method "
  1387. " %s (%d)\n",
  1388. poll_method_name(io_h.poll_method), io_h.poll_method);
  1389. goto error;
  1390. }
  1391. error:
  1392. destroy_io_wait(&io_h);
  1393. LOG(L_CRIT, "ERROR: tcp_main_loop: exiting...");
  1394. exit(-1);
  1395. }
  1396. /* cleanup before exit */
  1397. void destroy_tcp()
  1398. {
  1399. if (tcpconn_id_hash){
  1400. shm_free(tcpconn_id_hash);
  1401. tcpconn_id_hash=0;
  1402. }
  1403. if (connection_id){
  1404. shm_free(connection_id);
  1405. connection_id=0;
  1406. }
  1407. if (tcpconn_aliases_hash){
  1408. shm_free(tcpconn_aliases_hash);
  1409. tcpconn_aliases_hash=0;
  1410. }
  1411. if (tcpconn_lock){
  1412. lock_destroy(tcpconn_lock);
  1413. lock_dealloc((void*)tcpconn_lock);
  1414. tcpconn_lock=0;
  1415. }
  1416. }
  1417. int init_tcp()
  1418. {
  1419. /* init lock */
  1420. tcpconn_lock=lock_alloc();
  1421. if (tcpconn_lock==0){
  1422. LOG(L_CRIT, "ERROR: init_tcp: could not alloc lock\n");
  1423. goto error;
  1424. }
  1425. if (lock_init(tcpconn_lock)==0){
  1426. LOG(L_CRIT, "ERROR: init_tcp: could not init lock\n");
  1427. lock_dealloc((void*)tcpconn_lock);
  1428. tcpconn_lock=0;
  1429. goto error;
  1430. }
  1431. /* init globals */
  1432. connection_id=(int*)shm_malloc(sizeof(int));
  1433. if (connection_id==0){
  1434. LOG(L_CRIT, "ERROR: init_tcp: could not alloc globals\n");
  1435. goto error;
  1436. }
  1437. *connection_id=1;
  1438. /* alloc hashtables*/
  1439. tcpconn_aliases_hash=(struct tcp_conn_alias**)
  1440. shm_malloc(TCP_ALIAS_HASH_SIZE* sizeof(struct tcp_conn_alias*));
  1441. if (tcpconn_aliases_hash==0){
  1442. LOG(L_CRIT, "ERROR: init_tcp: could not alloc address hashtable\n");
  1443. goto error;
  1444. }
  1445. tcpconn_id_hash=(struct tcp_connection**)shm_malloc(TCP_ID_HASH_SIZE*
  1446. sizeof(struct tcp_connection*));
  1447. if (tcpconn_id_hash==0){
  1448. LOG(L_CRIT, "ERROR: init_tcp: could not alloc id hashtable\n");
  1449. goto error;
  1450. }
  1451. /* init hashtables*/
  1452. memset((void*)tcpconn_aliases_hash, 0,
  1453. TCP_ALIAS_HASH_SIZE * sizeof(struct tcp_conn_alias*));
  1454. memset((void*)tcpconn_id_hash, 0,
  1455. TCP_ID_HASH_SIZE * sizeof(struct tcp_connection*));
  1456. return 0;
  1457. error:
  1458. /* clean-up */
  1459. destroy_tcp();
  1460. return -1;
  1461. }
  1462. /* starts the tcp processes */
  1463. int tcp_init_children()
  1464. {
  1465. int r;
  1466. int sockfd[2];
  1467. int reader_fd[2]; /* for comm. with the tcp children read */
  1468. pid_t pid;
  1469. /* create the tcp sock_info structures */
  1470. /* copy the sockets --moved to main_loop*/
  1471. /* fork children & create the socket pairs*/
  1472. for(r=0; r<tcp_children_no; r++){
  1473. if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfd)<0){
  1474. LOG(L_ERR, "ERROR: tcp_main: socketpair failed: %s\n",
  1475. strerror(errno));
  1476. goto error;
  1477. }
  1478. if (socketpair(AF_UNIX, SOCK_STREAM, 0, reader_fd)<0){
  1479. LOG(L_ERR, "ERROR: tcp_main: socketpair failed: %s\n",
  1480. strerror(errno));
  1481. goto error;
  1482. }
  1483. process_no++;
  1484. pid=fork();
  1485. if (pid<0){
  1486. LOG(L_ERR, "ERROR: tcp_main: fork failed: %s\n",
  1487. strerror(errno));
  1488. goto error;
  1489. }else if (pid>0){
  1490. /* parent */
  1491. close(sockfd[1]);
  1492. close(reader_fd[1]);
  1493. tcp_children[r].pid=pid;
  1494. tcp_children[r].proc_no=process_no;
  1495. tcp_children[r].busy=0;
  1496. tcp_children[r].n_reqs=0;
  1497. tcp_children[r].unix_sock=reader_fd[0];
  1498. pt[process_no].pid=pid;
  1499. pt[process_no].unix_sock=sockfd[0];
  1500. pt[process_no].idx=r;
  1501. strncpy(pt[process_no].desc, "tcp receiver", MAX_PT_DESC);
  1502. }else{
  1503. /* child */
  1504. close(sockfd[0]);
  1505. unix_tcp_sock=sockfd[1];
  1506. bind_address=0; /* force a SEGFAULT if someone uses a non-init.
  1507. bind address on tcp */
  1508. /* record pid twice to avoid the child using it, before
  1509. * parent gets a chance to set it*/
  1510. pt[process_no].pid=getpid();
  1511. if (init_child(r+children_no+1) < 0) {
  1512. LOG(L_ERR, "init_children failed\n");
  1513. goto error;
  1514. }
  1515. tcp_receive_loop(reader_fd[1]);
  1516. }
  1517. }
  1518. return 0;
  1519. error:
  1520. return -1;
  1521. }
  1522. #endif