1 Star 0 Fork 0

souhoiryo / opensips

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
tcp_main.c 66.89 KB
一键复制 编辑 原始数据 按行查看 历史
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533
/*
* $Id$
*
* Copyright (C) 2001-2003 FhG Fokus
*
* This file is part of opensips, a free SIP server.
*
* opensips is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* opensips is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* History:
* --------
* 2002-11-29 created by andrei
* 2002-12-11 added tcp_send (andrei)
* 2003-01-20 locking fixes, hashtables (andrei)
* 2003-02-20 s/lock_t/gen_lock_t/ to avoid a conflict on solaris (andrei)
* 2003-02-25 Nagle is disabled if -DDISABLE_NAGLE (andrei)
* 2003-03-29 SO_REUSEADDR before calling bind to allow
* server restart, Nagle set on the (hopefuly)
* correct socket (jiri)
* 2003-03-31 always try to find the corresponding tcp listen socket for
* a temp. socket and store in in *->bind_address: added
* find_tcp_si, modified tcpconn_connect (andrei)
* 2003-04-14 set sockopts to TOS low delay (andrei)
* 2003-06-30 moved tcp new connect checking & handling to
* handle_new_connect (andrei)
* 2003-07-09 tls_close called before closing the tcp connection (andrei)
* 2003-10-24 converted to the new socket_info lists (andrei)
* 2003-10-27 tcp port aliases support added (andrei)
* 2003-11-04 always lock before manipulating refcnt; sendchild
* does not inc refcnt by itself anymore (andrei)
* 2003-11-07 different unix sockets are used for fd passing
* to/from readers/writers (andrei)
* 2003-11-17 handle_new_connect & tcp_connect will close the
* new socket if tcpconn_new return 0 (e.g. out of mem) (andrei)
* 2003-11-28 tcp_blocking_write & tcp_blocking_connect added (andrei)
* 2004-11-08 dropped find_tcp_si and replaced with find_si (andrei)
* 2005-12-22 added tos configurability (thanks to Andreas Granig)
* 2005-06-07 new tcp optimized code, supports epoll (LT), sigio + real time
* signals, poll & select (andrei)
* 2005-06-26 *bsd kqueue support (andrei)
* 2005-07-04 solaris /dev/poll support (andrei)
* 2005-07-08 tcp_max_connections, tcp_connection_lifetime, don't accept
* more connections if tcp_max_connections is exceeded (andrei)
* 2005-10-21 cleanup all the open connections on exit
* decrement the no. of open connections on timeout too (andrei)
* 2012-01-19 added TCP keepalive support
*/
/*!
* \file
* \brief TCP connection support (main)
*/
#ifdef USE_TCP
#ifndef SHM_MEM
#error "shared memory support needed (add -DSHM_MEM to Makefile.defs)"
#endif
#include <sys/time.h>
#include <sys/types.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <sys/uio.h> /* writev*/
#include <netdb.h>
#include <stdlib.h> /*exit() */
#include <time.h> /*time() */
#include <unistd.h>
#include <errno.h>
#include <string.h>
#ifdef HAVE_SELECT
#include <sys/select.h>
#endif
#include <sys/poll.h>
#include "ip_addr.h"
#include "pass_fd.h"
#include "tcp_conn.h"
#include "globals.h"
#include "pt.h"
#include "locking.h"
#include "mem/mem.h"
#include "mem/shm_mem.h"
#include "timer.h"
#include "sr_module.h"
#include "tcp_server.h"
#include "tcp_init.h"
#include "tsend.h"
#include "ut.h"
#include "daemonize.h"
#ifdef USE_TLS
#include "tls/tls_server.h"
#endif
#define local_malloc pkg_malloc
#define local_free pkg_free
#define HANDLE_IO_INLINE
#include "io_wait.h"
#include <fcntl.h> /* must be included after io_wait.h if SIGIO_RT is used */
enum fd_types { F_NONE=0, F_SOCKINFO=1 /* a tcp_listen fd */,
F_TCPCONN=2, F_TCPCHILD=4, F_PROC=8};
struct tcp_child {
pid_t pid;
int proc_no; /*!< OpenSIPS proc_no, for debugging */
int unix_sock; /*!< unix "read child" sock fd */
int busy;
int n_reqs; /*!< number of requests serviced so far */
};
int tcp_accept_aliases=0; /*!< by default don't accept aliases */
int tcp_connect_timeout=DEFAULT_TCP_CONNECT_TIMEOUT;
int tcp_send_timeout=DEFAULT_TCP_SEND_TIMEOUT;
int tcp_con_lifetime=DEFAULT_TCP_CONNECTION_LIFETIME;
int tcp_listen_backlog=DEFAULT_TCP_LISTEN_BACKLOG;
enum poll_types tcp_poll_method=0; /*!< by default choose the best method */
int tcp_max_connections=DEFAULT_TCP_MAX_CONNECTIONS;
int tcp_max_fd_no=0;
#ifdef HAVE_SO_KEEPALIVE
int tcp_keepalive = 1;
#else
int tcp_keepalive = 0;
#endif
int tcp_keepcount = 0;
int tcp_keepidle = 0;
int tcp_keepinterval = 0;
static int tcp_connections_no=0; /*!< current number of open connections */
struct tcp_partition {
/*! \brief connection hash table (after ip&port), includes also aliases */
struct tcp_conn_alias** tcpconn_aliases_hash;
/*! \brief connection hash table (after connection id) */
struct tcp_connection** tcpconn_id_hash;
gen_lock_t* tcpconn_lock;
};
struct tcp_partition tcp_parts[TCP_PARTITION_SIZE];
struct tcp_child *tcp_children=0;
static int* connection_id=0; /*!< unique for each connection, used for
quickly finding the corresponding connection for a reply */
int unix_tcp_sock = -1;
static int tcp_proto_no=-1; /*!< tcp protocol number as returned by getprotobyname */
static io_wait_h io_h;
int tcp_no_new_conn_bflag = 0; /*!< should a new TCP conn be open if needed? - branch flag to be set in the SIP messages - configuration option */
int tcp_no_new_conn = 0; /*!< should a new TCP conn be open if needed? - variable used to used for signalizing between SIP layer (branch flag) and TCP layer (tcp_send function) */
#define get_su_info(_su, _ip_char, _port_no) \
do { \
struct ip_addr __ip; \
sockaddr2ip_addr( &__ip, (struct sockaddr*)_su ); \
_ip_char = ip_addr2a(&__ip); \
_port_no = su_getport( (union sockaddr_union*)_su); \
} while(0)
static inline int init_sock_keepalive(int s)
{
int optval;
if (tcp_keepinterval || tcp_keepidle || tcp_keepcount) {
tcp_keepalive = 1; /* force on */
}
#ifdef HAVE_SO_KEEPALIVE
if ((optval = tcp_keepalive)) {
if (setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, &optval, sizeof(optval)) < 0) {
LM_WARN("init_sock_keepalive: failed to enable SO_KEEPALIVE: %s\n", strerror(errno));
return -1;
}
LM_INFO("-- TCP keepalive enabled on socket\n");
}
#endif
#ifdef HAVE_TCP_KEEPINTVL
if ((optval = tcp_keepinterval)) {
if (setsockopt(s, IPPROTO_TCP, TCP_KEEPINTVL, &optval, sizeof(optval)) < 0) {
LM_WARN("init_sock_keepalive: failed to set keepalive probes interval: %s\n", strerror(errno));
}
}
#endif
#ifdef HAVE_TCP_KEEPIDLE
if ((optval = tcp_keepidle)) {
if (setsockopt(s, IPPROTO_TCP, TCP_KEEPIDLE, &optval, sizeof(optval)) < 0) {
LM_WARN("init_sock_keepalive: failed to set keepalive idle interval: %s\n", strerror(errno));
}
}
#endif
#ifdef HAVE_TCP_KEEPCNT
if ((optval = tcp_keepcount)) {
if (setsockopt(s, IPPROTO_TCP, TCP_KEEPCNT, &optval, sizeof(optval)) < 0) {
LM_WARN("init_sock_keepalive: failed to set maximum keepalive count: %s\n", strerror(errno));
}
}
#endif
return 0;
}
/*! \brief Set all socket/fd options: disable nagle, tos lowdelay, non-blocking
* \return -1 on error */
static int init_sock_opt(int s)
{
int flags;
int optval;
#ifdef DISABLE_NAGLE
flags=1;
if ( (tcp_proto_no!=-1) && (setsockopt(s, tcp_proto_no , TCP_NODELAY,
&flags, sizeof(flags))<0) ){
LM_WARN("could not disable Nagle: %s\n", strerror(errno));
}
#endif
/* tos*/
optval = tos;
if (setsockopt(s, IPPROTO_IP, IP_TOS, (void*)&optval,sizeof(optval)) ==-1){
LM_WARN("setsockopt tos: %s\n", strerror(errno));
/* continue since this is not critical */
}
if (probe_max_sock_buff(s,1,MAX_SEND_BUFFER_SIZE,BUFFER_INCREMENT))
{
LM_WARN("setsockopt tcp snd buff: %s\n", strerror(errno));
/* continue since this is not critical */
}
init_sock_keepalive(s);
/* non-blocking */
flags=fcntl(s, F_GETFL);
if (flags==-1){
LM_ERR("fnctl failed: (%d) %s\n", errno, strerror(errno));
goto error;
}
if (fcntl(s, F_SETFL, flags|O_NONBLOCK)==-1){
LM_ERR("set non-blocking failed: (%d) %s\n", errno, strerror(errno));
goto error;
}
return 0;
error:
return -1;
}
/*! \brief blocking connect on a non-blocking fd; it will timeout after
* tcp_connect_timeout
* if BLOCKING_USE_SELECT and HAVE_SELECT are defined it will internally
* use select() instead of poll (bad if fd > FD_SET_SIZE, poll is preferred)
*/
static int tcp_blocking_connect(int fd, const struct sockaddr *servaddr,
socklen_t addrlen)
{
int n;
#if defined(HAVE_SELECT) && defined(BLOCKING_USE_SELECT)
fd_set sel_set;
fd_set orig_set;
struct timeval timeout;
#else
struct pollfd pf;
#endif
int elapsed;
int to;
int ticks;
int err;
unsigned int err_len;
int poll_err;
char *ip;
unsigned short port;
poll_err=0;
to=tcp_connect_timeout;
ticks=get_ticks();
again:
n=connect(fd, servaddr, addrlen);
if (n==-1){
if (errno==EINTR){
elapsed=(get_ticks()-ticks)*TIMER_TICK;
if (elapsed<to) goto again;
else goto error_timeout;
}
if (errno!=EINPROGRESS && errno!=EALREADY){
get_su_info( servaddr, ip, port);
LM_ERR("[server=%s:%d] (%d) %s\n",ip, port, errno, strerror(errno));
goto error;
}
}else goto end;
/* poll/select loop */
#if defined(HAVE_SELECT) && defined(BLOCKING_USE_SELECT)
FD_ZERO(&orig_set);
FD_SET(fd, &orig_set);
#else
pf.fd=fd;
pf.events=POLLOUT;
#endif
while(1){
elapsed=(get_ticks()-ticks)*TIMER_TICK;
if (elapsed<to)
to-=elapsed;
else
goto error_timeout;
#if defined(HAVE_SELECT) && defined(BLOCKING_USE_SELECT)
sel_set=orig_set;
timeout.tv_sec=to;
timeout.tv_usec=0;
n=select(fd+1, 0, &sel_set, 0, &timeout);
#else
n=poll(&pf, 1, to*1000);
#endif
if (n<0){
if (errno==EINTR) continue;
get_su_info( servaddr, ip, port);
LM_ERR("poll/select failed:[server=%s:%d] (%d) %s\n",
ip, port, errno, strerror(errno));
goto error;
}else if (n==0) /* timeout */ continue;
#if defined(HAVE_SELECT) && defined(BLOCKING_USE_SELECT)
if (FD_ISSET(fd, &sel_set))
#else
if (pf.revents&(POLLERR|POLLHUP|POLLNVAL)){
LM_ERR("poll error: flags %d - %d %d %d %d \n", pf.revents,
POLLOUT,POLLERR,POLLHUP,POLLNVAL);
poll_err=1;
}
#endif
{
err_len=sizeof(err);
getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &err_len);
if ((err==0) && (poll_err==0)) goto end;
if (err!=EINPROGRESS && err!=EALREADY){
get_su_info( servaddr, ip, port);
LM_ERR("failed to retrieve SO_ERROR [server=%s:%d] (%d) %s\n",
ip, port, err, strerror(err));
goto error;
}
}
}
error_timeout:
/* timeout */
LM_ERR("timeout %d s elapsed from %d s\n", elapsed, tcp_connect_timeout);
error:
return -1;
end:
return 0;
}
#if 0
/*! \brief blocking write even on non-blocking sockets
* if TCP_TIMEOUT will return with error */
static int tcp_blocking_write(struct tcp_connection* c, int fd, char* buf,
unsigned int len)
{
int n;
fd_set sel_set;
struct timeval timeout;
int ticks;
int initial_len;
initial_len=len;
again:
n=send(fd, buf, len,
#ifdef HAVE_MSG_NOSIGNAL
MSG_NOSIGNAL
#else
0
#endif
);
if (n<0){
if (errno==EINTR) goto again;
else if (errno!=EAGAIN && errno!=EWOULDBLOCK){
LM_ERR("failed to send: (%d) %s\n", errno, strerror(errno));
goto error;
}
}else if (n<len){
/* partial write */
buf+=n;
len-=n;
}else{
/* success: full write */
goto end;
}
while(1){
FD_ZERO(&sel_set);
FD_SET(fd, &sel_set);
timeout.tv_sec=tcp_send_timeout;
timeout.tv_usec=0;
ticks=get_ticks();
n=select(fd+1, 0, &sel_set, 0, &timeout);
if (n<0){
if (errno==EINTR) continue; /* signal, ignore */
LM_ERR("select failed: (%d) %s\n", errno, strerror(errno));
goto error;
}else if (n==0){
/* timeout */
if (get_ticks()-ticks>=tcp_send_timeout){
LM_ERR("send timeout (%d)\n", tcp_send_timeout);
goto error;
}
continue;
}
if (FD_ISSET(fd, &sel_set)){
/* we can write again */
goto again;
}
}
error:
return -1;
end:
return initial_len;
}
#endif
struct tcp_connection* tcpconn_new(int sock, union sockaddr_union* su,
struct socket_info* ba, int type,
int state)
{
struct tcp_connection *c;
c=(struct tcp_connection*)shm_malloc(sizeof(struct tcp_connection));
if (c==0){
LM_ERR("shared memory allocation failure\n");
goto error;
}
memset(c, 0, sizeof(struct tcp_connection)); /* zero init */
c->s=sock;
c->fd=-1; /* not initialized */
if (lock_init(&c->write_lock)==0){
LM_ERR("init lock failed\n");
goto error;
}
c->rcv.src_su=*su;
c->refcnt=0;
su2ip_addr(&c->rcv.src_ip, su);
c->rcv.src_port=su_getport(su);
c->rcv.bind_address=ba;
if (ba){
c->rcv.dst_ip=ba->address;
c->rcv.dst_port=ba->port_no;
}
print_ip("tcpconn_new: new tcp connection to: ", &c->rcv.src_ip, "\n");
LM_DBG("on port %d, type %d\n", c->rcv.src_port, type);
c->id=(*connection_id)++;
c->rcv.proto_reserved1=0; /* this will be filled before receive_message*/
c->rcv.proto_reserved2=0;
c->state=state;
c->extra_data=0;
#ifdef USE_TLS
if (type==PROTO_TLS){
if (tls_tcpconn_init(c, sock)==-1) goto error;
}else
#endif /* USE_TLS*/
{
c->type=PROTO_TCP;
c->rcv.proto=PROTO_TCP;
c->timeout=get_ticks()+tcp_con_lifetime;
}
c->flags|=F_CONN_REMOVED;
if (tcp_async) {
c->async_chunks = shm_malloc(sizeof(struct tcp_send_chunk *) *
tcp_async_max_postponed_chunks);
if (c->async_chunks == NULL) {
LM_ERR("No more SHM for send chunks pointers \n");
goto error;
}
}
tcp_connections_no++;
return c;
error:
if (c) shm_free(c);
return 0;
}
/* returns :
* 0 - in case of success
* -1 - in case there was an internal error
* -2 - in case our chunks buffer is full
* and we need to let the connection go
*/
static inline int add_write_chunk(struct tcp_connection *con,char *buf,int len,
int lock)
{
struct tcp_send_chunk *c;
c = shm_malloc(sizeof(struct tcp_send_chunk) + len);
if (!c) {
LM_ERR("No more SHM\n");
return -1;
}
c->len = len;
c->ticks = get_ticks();
c->buf = (char *)(c+1);
memcpy(c->buf,buf,len);
c->pos = c->buf;
if (lock)
lock_get(&con->write_lock);
if (con->async_chunks_no == tcp_async_max_postponed_chunks) {
LM_ERR("We have reached the limit of max async postponed chunks\n");
if (lock)
lock_release(&con->write_lock);
shm_free(c);
return -2;
}
con->async_chunks[con->async_chunks_no++] = c;
if (con->async_chunks_no == 1)
con->oldest_chunk = c->ticks;
if (lock)
lock_release(&con->write_lock);
return 0;
}
#define ASYNC_TCP_CONN ((struct tcp_connection *)-1)
#define ASYNC_TCP_CONN_ERR ((struct tcp_connection *)-2)
static inline struct tcp_connection * async_connect_or_pass(int fd,
union sockaddr_union *server,socklen_t addrlen,
struct socket_info *send_sock,char *buf, int len,
int type,unsigned int max_us)
{
int n;
#if defined(HAVE_SELECT) && defined(BLOCKING_USE_SELECT)
fd_set sel_set;
fd_set orig_set;
struct timeval timeout;
#else
struct pollfd pf;
#endif
unsigned int elapsed,to;
int err;
unsigned int err_len;
int poll_err;
char *ip;
unsigned short port;
struct timeval begin;
struct tcp_connection* con=NULL;
long response[2];
poll_err=0;
elapsed = 0;
to = max_us;
if (gettimeofday(&(begin), NULL)) {
LM_ERR("Failed to get TCP connect start time\n");
goto pass_to_main;
}
again:
n=connect(fd, &server->s, addrlen);
if (n==-1) {
if (errno==EINTR){
elapsed=get_time_diff(&begin);
if (elapsed<max_us)
goto again;
else {
LM_DBG("Local connect attempt failed \n");
goto pass_to_main;
}
}
if (errno!=EINPROGRESS && errno!=EALREADY){
get_su_info(&server->s, ip, port);
LM_ERR("[server=%s:%d] (%d) %s\n",ip, port, errno, strerror(errno));
goto error;
}
} else goto local_success;
/* let's poll for a little */
#if defined(HAVE_SELECT) && defined(BLOCKING_USE_SELECT)
FD_ZERO(&orig_set);
FD_SET(fd, &orig_set);
#else
pf.fd=fd;
pf.events=POLLOUT;
#endif
while(1){
elapsed=get_time_diff(&begin);
if (elapsed<to)
to-=elapsed;
else {
LM_DBG("Polling is overdue \n");
goto pass_to_main;
}
#if defined(HAVE_SELECT) && defined(BLOCKING_USE_SELECT)
sel_set=orig_set;
timeout.tv_sec=to/1000000;
timeout.tv_usec=to%1000000;
n=select(fd+1, 0, &sel_set, 0, &timeout);
#else
n=poll(&pf, 1, to/1000);
#endif
if (n<0){
if (errno==EINTR) continue;
get_su_info(&server->s, ip, port);
LM_ERR("poll/select failed:[server=%s:%d] (%d) %s\n",
ip, port, errno, strerror(errno));
goto error;
}else if (n==0) /* timeout */ continue;
#if defined(HAVE_SELECT) && defined(BLOCKING_USE_SELECT)
if (FD_ISSET(fd, &sel_set))
#else
if (pf.revents&(POLLERR|POLLHUP|POLLNVAL)){
LM_ERR("poll error: flags %x\n", pf.revents);
poll_err=1;
}
#endif
{
err_len=sizeof(err);
getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &err_len);
if ((err==0) && (poll_err==0)) goto local_success;
if (err!=EINPROGRESS && err!=EALREADY){
get_su_info(&server->s, ip, port);
LM_ERR("failed to retrieve SO_ERROR [server=%s:%d] (%d) %s\n",
ip, port, err, strerror(err));
goto error;
}
}
}
pass_to_main:
LM_DBG("Should now pass the socket to TCP main \n");
/* create a new dummy connection */
con=tcpconn_new(fd, server, send_sock, type, S_CONN_INIT);
if (con == 0) {
LM_ERR("tcpconn_new failed, closing the socket\n");
goto error;
}
if (add_write_chunk(con,buf,len,0) < 0) {
LM_ERR("Failed to add the initial write chunk\n");
/* FIXME - seems no more SHM now ...
* continue the async connect process ? */
}
response[0]=(long)con;
response[1]=ASYNC_CONNECT;
n=send_fd(unix_tcp_sock, response, sizeof(response), con->s);
if (n<=0) {
LM_ERR("Failed to send the socket to main for async connection \n");
goto error;
}
return ASYNC_TCP_CONN;
local_success:
con=tcpconn_new(fd, server, send_sock, type, S_CONN_CONNECT);
if (con == 0) {
LM_ERR("tcpconn_new failed, closing the socket\n");
goto error;
}
return con;
error:
if (con)
shm_free(con);
return ASYNC_TCP_CONN_ERR;
}
struct tcp_connection* tcpconn_async_connect(struct socket_info* send_sock,
union sockaddr_union* server, int type,char *buf, unsigned len,
unsigned int max_us)
{
int s;
union sockaddr_union my_name;
socklen_t my_name_len;
struct tcp_connection* con;
s=socket(AF2PF(server->s.sa_family), SOCK_STREAM, 0);
if (s==-1){
LM_ERR("socket: (%d) %s\n", errno, strerror(errno));
return ASYNC_TCP_CONN_ERR;
}
if (init_sock_opt(s)<0){
LM_ERR("init_sock_opt failed\n");
close(s);
return ASYNC_TCP_CONN_ERR;
}
my_name_len = sockaddru_len(send_sock->su);
memcpy( &my_name, &send_sock->su, my_name_len);
su_setport( &my_name, 0);
if (bind(s, &my_name.s, my_name_len )!=0) {
LM_ERR("bind failed (%d) %s\n", errno,strerror(errno));
close(s);
return ASYNC_TCP_CONN_ERR;
}
con = async_connect_or_pass(s,server,sockaddru_len(*server),send_sock,
buf,len,type,max_us);
if (con == ASYNC_TCP_CONN_ERR) {
/* internal error */
LM_ERR("Internal error encountered when connecting\n");
close(s);
return con;
} else {
/* either we connected on our own - or we failed to connect
* but we succesfully passed socket to main */
return con;
}
}
struct tcp_connection* tcpconn_connect(struct socket_info* send_sock,
union sockaddr_union* server, int type)
{
int s;
union sockaddr_union my_name;
socklen_t my_name_len;
struct tcp_connection* con;
s=socket(AF2PF(server->s.sa_family), SOCK_STREAM, 0);
if (s==-1){
LM_ERR("socket: (%d) %s\n", errno, strerror(errno));
goto error;
}
if (init_sock_opt(s)<0){
LM_ERR("init_sock_opt failed\n");
goto error;
}
my_name_len = sockaddru_len(send_sock->su);
memcpy( &my_name, &send_sock->su, my_name_len);
su_setport( &my_name, 0);
if (bind(s, &my_name.s, my_name_len )!=0) {
LM_ERR("bind failed (%d) %s\n", errno,strerror(errno));
goto error;
}
if (tcp_blocking_connect(s, &server->s, sockaddru_len(*server))<0){
LM_ERR("tcp_blocking_connect failed\n");
goto error;
}
con=tcpconn_new(s, server, send_sock, type, S_CONN_CONNECT);
if (con==0){
LM_ERR("tcpconn_new failed, closing the socket\n");
goto error;
}
return con;
/*FIXME: set sock idx! */
error:
if (s!=-1) close(s); /* close the opened socket */
return 0;
}
struct tcp_connection* tcpconn_add(struct tcp_connection *c)
{
unsigned hash;
if (c){
TCPCONN_LOCK(c->id);
/* add it at the begining of the list*/
hash=tcp_id_hash(c->id);
c->id_hash=hash;
tcpconn_listadd(TCP_PART(c->id).tcpconn_id_hash[hash], c, id_next, id_prev);
hash=tcp_addr_hash(&c->rcv.src_ip, c->rcv.src_port);
/* set the first alias */
c->con_aliases[0].port=c->rcv.src_port;
c->con_aliases[0].hash=hash;
c->con_aliases[0].parent=c;
tcpconn_listadd(TCP_PART(c->id).tcpconn_aliases_hash[hash], &c->con_aliases[0],
next, prev);
c->aliases++;
TCPCONN_UNLOCK(c->id);
LM_DBG("hashes: %d, %d\n", hash, c->id_hash);
return c;
}else{
LM_CRIT("null connection pointer\n");
return 0;
}
}
/*! \brief unsafe tcpconn_rm version (nolocks) */
void _tcpconn_rm(struct tcp_connection* c)
{
int r;
tcpconn_listrm(TCP_PART(c->id).tcpconn_id_hash[c->id_hash], c,
id_next, id_prev);
/* remove all the aliases */
for (r=0; r<c->aliases; r++)
tcpconn_listrm(TCP_PART(c->id).tcpconn_aliases_hash[c->con_aliases[r].hash],
&c->con_aliases[r], next, prev);
lock_destroy(&c->write_lock);
#ifdef USE_TLS
if (c->type==PROTO_TLS) tls_tcpconn_clean(c);
#endif
for (r=0;r<c->async_chunks_no;r++) {
shm_free(c->async_chunks[r]);
}
shm_free(c);
}
void tcpconn_rm(struct tcp_connection* c)
{
int r;
TCPCONN_LOCK(c->id);
tcpconn_listrm(TCP_PART(c->id).tcpconn_id_hash[c->id_hash], c,
id_next, id_prev);
/* remove all the aliases */
for (r=0; r<c->aliases; r++)
tcpconn_listrm(TCP_PART(c->id).tcpconn_aliases_hash[c->con_aliases[r].hash],
&c->con_aliases[r], next, prev);
TCPCONN_UNLOCK(c->id);
lock_destroy(&c->write_lock);
#ifdef USE_TLS
if ((c->type==PROTO_TLS)&&(c->extra_data)) tls_tcpconn_clean(c);
#endif
shm_free(c);
}
/*! \brief finds a connection, if id=0 return NULL
* \note WARNING: unprotected (locks) use tcpconn_get unless you really
* know what you are doing */
static struct tcp_connection* _tcpconn_find(int id)
{
struct tcp_connection *c;
unsigned hash;
if (id){
hash=tcp_id_hash(id);
for (c=TCP_PART(id).tcpconn_id_hash[hash]; c; c=c->id_next){
#ifdef EXTRA_DEBUG
LM_DBG("c=%p, c->id=%d, port=%d\n",c, c->id, c->rcv.src_port);
print_ip("ip=", &c->rcv.src_ip, "\n");
#endif
if ((id==c->id)&&(c->state!=S_CONN_BAD)) return c;
}
}
return 0;
}
/*! \brief _tcpconn_find with locks and timeout */
struct tcp_connection* tcpconn_get(int id, struct ip_addr* ip, int port,
int timeout)
{
struct tcp_connection* c;
struct tcp_conn_alias* a;
unsigned hash;
int part;
if (id) {
part = id;
TCPCONN_LOCK(part);
if ( (c=_tcpconn_find(part))!=NULL )
goto found;
TCPCONN_UNLOCK(part);
}
/* continue search based on IP + port */
#ifdef EXTRA_DEBUG
LM_DBG("%d port %d\n",id, port);
if (ip) print_ip("tcpconn_find: ip ", ip, "\n");
#endif
if (ip){
hash=tcp_addr_hash(ip, port);
for( part=0 ; part<TCP_PARTITION_SIZE ; part++ ) {
TCPCONN_LOCK(part);
for (a=TCP_PART(part).tcpconn_aliases_hash[hash]; a; a=a->next) {
#ifdef EXTRA_DEBUG
LM_DBG("a=%p, c=%p, c->id=%d, alias port= %d port=%d\n",
a, a->parent, a->parent->id, a->port, a->parent->rcv.src_port);
print_ip("ip=",&a->parent->rcv.src_ip,"\n");
#endif
c = a->parent;
if ( (c->state!=S_CONN_BAD) && (port==a->port) &&
(ip_addr_cmp(ip, &c->rcv.src_ip)) )
goto found;
}
TCPCONN_UNLOCK(part);
}
}
return NULL;
found:
c->refcnt++;
c->timeout=get_ticks()+timeout;
TCPCONN_UNLOCK(part);
return c;
}
/*! \brief add port as an alias for the "id" connection
* \return 0 on success,-1 on failure */
int tcpconn_add_alias(int id, int port, int proto)
{
struct tcp_connection* c;
unsigned hash;
struct tcp_conn_alias* a;
a=0;
/* fix the port */
port=port?port:((proto==PROTO_TLS)?SIPS_PORT:SIP_PORT);
TCPCONN_LOCK(id);
/* check if alias already exists */
c=_tcpconn_find(id);
if (c){
hash=tcp_addr_hash(&c->rcv.src_ip, port);
/* search the aliases for an already existing one */
for (a=TCP_PART(id).tcpconn_aliases_hash[hash]; a; a=a->next){
if ( (a->parent->state!=S_CONN_BAD) && (port==a->port) &&
(ip_addr_cmp(&c->rcv.src_ip, &a->parent->rcv.src_ip)) ){
/* found */
if (a->parent!=c) goto error_sec;
else goto ok;
}
}
if (c->aliases>=TCP_CON_MAX_ALIASES) goto error_aliases;
c->con_aliases[c->aliases].parent=c;
c->con_aliases[c->aliases].port=port;
c->con_aliases[c->aliases].hash=hash;
tcpconn_listadd(TCP_PART(id).tcpconn_aliases_hash[hash],
&c->con_aliases[c->aliases], next, prev);
c->aliases++;
}else goto error_not_found;
ok:
TCPCONN_UNLOCK(id);
#ifdef EXTRA_DEBUG
if (a) LM_DBG("alias already present\n");
else LM_DBG("alias port %d for hash %d, id %d\n", port, hash, c->id);
#endif
return 0;
error_aliases:
TCPCONN_UNLOCK(id);
LM_ERR("too many aliases for connection %p (%d)\n", c, c->id);
return -1;
error_not_found:
TCPCONN_UNLOCK(id);
LM_ERR("no connection found for id %d\n",id);
return -1;
error_sec:
TCPCONN_UNLOCK(id);
LM_ERR("possible port hijack attempt\n");
LM_ERR("alias already present and points to another connection "
"(%d : %d and %d : %d)\n", a->parent->id, port, c->id, port);
return -1;
}
void tcpconn_ref(struct tcp_connection* c)
{
TCPCONN_LOCK(c->id);
c->refcnt++; /* FIXME: atomic_dec */
TCPCONN_UNLOCK(c->id);
}
void tcpconn_put(struct tcp_connection* c)
{
TCPCONN_LOCK(c->id);
c->refcnt--; /* FIXME: atomic_dec */
TCPCONN_UNLOCK(c->id);
}
/* called under the TCP connection write lock */
int async_tsend_stream(struct tcp_connection *c,
int fd, char* buf, unsigned int len, int timeout)
{
int written;
int n;
struct pollfd pf;
long response[2];
pf.fd=fd;
pf.events=POLLOUT;
written=0;
again:
n=send(fd, buf, len,
#ifdef HAVE_MSG_NOSIGNAL
MSG_NOSIGNAL
#else
0
#endif
);
if (n<0){
if (errno==EINTR) goto again;
else if (errno!=EAGAIN && errno!=EWOULDBLOCK) {
LM_ERR("Failed first TCP async send : (%d) %s\n",
errno, strerror(errno));
return -1;
} else
goto poll_loop;
}
written+=n;
if (n<len) {
/* partial write */
buf+=n;
len-=n;
} else {
/* succesful write from the first try */
LM_DBG("Async succesful write from first try on %p\n",c);
return len;
}
poll_loop:
n=poll(&pf,1,timeout/1000);
if (n<0) {
if (errno==EINTR)
goto poll_loop;
LM_ERR("Polling while trying to async send failed %s [%d]\n",
strerror(errno), errno);
return -1;
} else if (n==0) {
LM_DBG("timeout - preparing to send to main\n");
/* timeout - let's just pass to main */
if (add_write_chunk(c,buf,len,0) < 0) {
LM_ERR("Failed to add write chunk to connection \n");
return -1;
} else {
/* we have succesfully added async write chunk
* tell MAIN to poll out for us */
response[0]=(long)c;
response[1]=ASYNC_WRITE;
n=send_all(unix_tcp_sock, response, sizeof(response));
if (n<=0){
LM_ERR("Failed to tell main to poll out for us :%s (%d)\n",
strerror(errno), errno);
return -1;
}
LM_DBG("Succesfully told main to pollout for conn %p\n",c);
return len;
}
}
if (pf.events&POLLOUT)
goto again;
/* some other events triggered by poll - treat as errors */
return -1;
}
/*! \brief Finds a tcpconn & sends on it */
int tcp_send(struct socket_info* send_sock, int type, char* buf, unsigned len,
union sockaddr_union* to, int id)
{
struct tcp_connection *c;
struct tcp_connection *tmp;
struct ip_addr ip;
int port;
int fd;
long response[2];
int n;
struct timeval get,rcv,snd;
port=0;
reset_tcp_vars(tcpthreshold);
start_expire_timer(get,tcpthreshold);
if (to){
su2ip_addr(&ip, to);
port=su_getport(to);
c=tcpconn_get(id, &ip, port, tcp_con_lifetime);
}else if (id){
c=tcpconn_get(id, 0, 0, tcp_con_lifetime);
}else{
LM_CRIT("tcp_send called with null id & to\n");
get_time_difference(get,tcpthreshold,tcp_timeout_con_get);
return -1;
}
if (id){
if (c==0) {
if (to){
/* try again w/o id */
c=tcpconn_get(0, &ip, port, tcp_con_lifetime);
goto no_id;
}else{
LM_ERR("id %d not found, dropping\n", id);
get_time_difference(get,tcpthreshold,tcp_timeout_con_get);
return -1;
}
}else goto get_fd;
}
no_id:
if (c==0){
if (tcp_no_new_conn) {
return -1;
}
LM_DBG("no open tcp connection found, opening new one\n");
/* create tcp connection */
if (tcp_async && type==PROTO_TCP) {
c=tcpconn_async_connect(send_sock, to, type,buf,len,
tcp_async_local_connect_timeout);
if (c == ASYNC_TCP_CONN_ERR) {
LM_ERR("async TCP connect failed\n");
get_time_difference(get,tcpthreshold,tcp_timeout_con_get);
return -1;
}
/* if we failed to connect right away, break the sending
* flow now and return when TCP main says connect was
* succesful */
if (c == ASYNC_TCP_CONN) {
LM_DBG("Succesfully passed FD to TCP main for "
"async connection \n");
return len;
}
LM_DBG("First connect attempt succeded in %d us "
"proceed to writing \n",tcp_async_local_connect_timeout);
/* our first connect attempt succeeded - go ahead as normal */
} else if ((c=tcpconn_connect(send_sock, to, type))==0){
LM_ERR("connect failed\n");
get_time_difference(get,tcpthreshold,tcp_timeout_con_get);
return -1;
}
c->refcnt++; /* safe to do it w/o locking, it's not yet
available to the rest of the world */
fd=c->s;
/* send the new tcpconn to "tcp main" */
response[0]=(long)c;
response[1]=CONN_NEW;
n=send_fd(unix_tcp_sock, response, sizeof(response), c->s);
get_time_difference(get,tcpthreshold,tcp_timeout_con_get);
if (n<=0){
LM_ERR("failed send_fd: %s (%d)\n", strerror(errno), errno);
n=-1;
goto end;
}
goto send_it;
}
get_fd:
if (c->flags & F_CONN_NOT_CONNECTED) {
/* the connection is currently in the process of getting
* connected - let's append our send chunk as well - just in
* case we ever manage to get through */
LM_DBG("We have acquired a TCP connection which is still pending to connect - delaying write \n");
n = add_write_chunk(c,buf,len,1);
if (n < 0) {
LM_ERR("Failed to add another write chunk to %p\n",c);
if (n == -2) {
/* write chunk buffer reached max - close this
* connection now */
tcpconn_put(c);
c->state=S_CONN_BAD;
c->timeout=0;
/* tell "main" it should drop this */
response[0]=(long)c;
response[1]=CONN_ERROR;
n=send_all(unix_tcp_sock, response, sizeof(response));
if (n<=0){
LM_ERR("return failed (write):%s (%d)\n",
strerror(errno), errno);
}
return -1;
} else {
/* we failed due to internal errors - put the
* connection back */
tcpconn_put(c);
return -1;
}
}
/* we succesfully added our write chunk - success */
tcpconn_put(c);
return len;
}
get_time_difference(get,tcpthreshold,tcp_timeout_con_get);
/* todo: see if this is not the same process holding
* c and if so send directly on c->fd */
LM_DBG("tcp connection found (%p), acquiring fd\n", c);
/* get the fd */
response[0]=(long)c;
response[1]=CONN_GET_FD;
start_expire_timer(rcv,tcpthreshold);
n=send_all(unix_tcp_sock, response, sizeof(response));
if (n<=0){
LM_ERR("failed to get fd(write):%s (%d)\n",
strerror(errno), errno);
n=-1;
get_time_difference(rcv,tcpthreshold,tcp_timeout_receive_fd);
goto release_c;
}
LM_DBG("c= %p, n=%d\n", c, n);
tmp=c;
n=receive_fd(unix_tcp_sock, &c, sizeof(c), &fd, MSG_WAITALL);
get_time_difference(rcv,tcpthreshold,tcp_timeout_receive_fd);
if (n<=0){
LM_ERR("failed to get fd(receive_fd):"
" %s (%d)\n", strerror(errno), errno);
n=-1;
goto release_c;
}
if (c!=tmp){
LM_CRIT("got different connection:"
" %p (id= %d, refcnt=%d state=%d != "
" %p (id= %d, refcnt=%d state=%d (n=%d)\n",
c, c->id, c->refcnt, c->state,
tmp, tmp->id, tmp->refcnt, tmp->state, n
);
n=-1; /* fail */
goto end;
}
LM_DBG("after receive_fd: c= %p n=%d fd=%d\n",c, n, fd);
send_it:
LM_DBG("sending...\n");
lock_get(&c->write_lock);
#ifdef USE_TLS
if (c->type==PROTO_TLS)
n=tls_blocking_write(c, fd, buf, len);
else
#endif
{
/* n=tcp_blocking_write(c, fd, buf, len); */
start_expire_timer(snd,tcpthreshold);
if (tcp_async) {
n=async_tsend_stream(c,fd,buf,len,tcp_async_local_write_timeout);
} else {
n=tsend_stream(fd, buf, len, tcp_send_timeout*1000);
get_time_difference(snd,tcpthreshold,tcp_timeout_send);
stop_expire_timer(get,tcpthreshold,"tcp ops",buf,(int)len,1);
}
}
lock_release(&c->write_lock);
LM_DBG("after write: c= %p n=%d fd=%d\n",c, n, fd);
LM_DBG("buf=\n%.*s\n", (int)len, buf);
if (n<0){
LM_ERR("failed to send\n");
/* error on the connection , mark it as bad and set 0 timeout */
c->state=S_CONN_BAD;
c->timeout=0;
/* tell "main" it should drop this (optional it will t/o anyway?)*/
response[0]=(long)c;
response[1]=CONN_ERROR;
n=send_all(unix_tcp_sock, response, sizeof(response));
/* CONN_ERROR will auto-dec refcnt => we must not call tcpconn_put !!*/
if (n<=0){
LM_ERR("return failed (write):%s (%d)\n",
strerror(errno), errno);
}
close(fd);
return -1; /* error return, no tcpconn_put */
}
end:
close(fd);
release_c:
tcpconn_put(c); /* release c (lock; dec refcnt; unlock) */
return n;
}
int tcp_init(struct socket_info* sock_info)
{
union sockaddr_union* addr;
int optval;
#ifdef DISABLE_NAGLE
int flag;
struct protoent* pe;
if (tcp_proto_no==-1){ /* if not already set */
pe=getprotobyname("tcp");
if (pe==0){
LM_ERR("could not get TCP protocol number\n");
tcp_proto_no=-1;
}else{
tcp_proto_no=pe->p_proto;
}
}
#endif
addr=&sock_info->su;
/* sock_info->proto=PROTO_TCP; */
if (init_su(addr, &sock_info->address, sock_info->port_no)<0){
LM_ERR("could no init sockaddr_union\n");
goto error;
}
sock_info->socket=socket(AF2PF(addr->s.sa_family), SOCK_STREAM, 0);
if (sock_info->socket==-1){
LM_ERR("socket: %s\n", strerror(errno));
goto error;
}
#ifdef DISABLE_NAGLE
flag=1;
if ( (tcp_proto_no!=-1) &&
(setsockopt(sock_info->socket, tcp_proto_no , TCP_NODELAY,
&flag, sizeof(flag))<0) ){
LM_ERR("could not disable Nagle: %s\n", strerror(errno));
}
#endif
#if !defined(TCP_DONT_REUSEADDR)
/* Stevens, "Network Programming", Section 7.5, "Generic Socket
* Options": "...server started,..a child continues..on existing
* connection..listening server is restarted...call to bind fails
* ... ALL TCP servers should specify the SO_REUSEADDRE option
* to allow the server to be restarted in this situation
*
* Indeed, without this option, the server can't restart.
* -jiri
*/
optval=1;
if (setsockopt(sock_info->socket, SOL_SOCKET, SO_REUSEADDR,
(void*)&optval, sizeof(optval))==-1) {
LM_ERR("setsockopt %s\n", strerror(errno));
goto error;
}
#endif
/* tos */
optval = tos;
if (setsockopt(sock_info->socket, IPPROTO_IP, IP_TOS, (void*)&optval,
sizeof(optval)) ==-1){
LM_WARN("setsockopt tos: %s\n", strerror(errno));
/* continue since this is not critical */
}
if (probe_max_sock_buff(sock_info->socket,1,MAX_SEND_BUFFER_SIZE,
BUFFER_INCREMENT))
{
LM_WARN("setsockopt tcp snd buff: %s\n", strerror(errno));
/* continue since this is not critical */
}
init_sock_keepalive(sock_info->socket);
if (bind(sock_info->socket, &addr->s, sockaddru_len(*addr))==-1){
LM_ERR("bind(%x, %p, %d) on %s:%d : %s\n",
sock_info->socket, &addr->s,
(unsigned)sockaddru_len(*addr),
sock_info->address_str.s,
sock_info->port_no,
strerror(errno));
goto error;
}
if (listen(sock_info->socket, tcp_listen_backlog)==-1){
LM_ERR("listen(%x, %p, %d) on %s: %s\n",
sock_info->socket, &addr->s,
(unsigned)sockaddru_len(*addr),
sock_info->address_str.s,
strerror(errno));
goto error;
}
return 0;
error:
if (sock_info->socket!=-1){
close(sock_info->socket);
sock_info->socket=-1;
}
return -1;
}
static int send2child(struct tcp_connection* tcpconn,int rw)
{
int i;
int min_busy;
int idx;
long response[2];
min_busy=tcp_children[0].busy;
idx=0;
for (i=0; i<tcp_children_no; i++){
if (!tcp_children[i].busy){
idx=i;
min_busy=0;
break;
}else if (min_busy>tcp_children[i].busy){
min_busy=tcp_children[i].busy;
idx=i;
}
}
tcp_children[idx].busy++;
tcp_children[idx].n_reqs++;
if (min_busy){
LM_INFO("no free tcp receiver, connection passed to the least"
" busy one (%d)\n", min_busy);
}
LM_DBG("to tcp child %d %d(%d), %p rw %d\n", idx, tcp_children[idx].proc_no,
tcp_children[idx].pid, tcpconn,rw);
response[0]=(long)tcpconn;
response[1]=rw;
if (send_fd(tcp_children[idx].unix_sock, response, sizeof(response),
tcpconn->s)<=0){
LM_ERR("send_fd failed\n");
return -1;
}
return 0;
}
/*! \brief
* handles a new connection, called internally by tcp_main_loop/handle_io.
* \param si - pointer to one of the tcp socket_info structures on which
* an io event was detected (connection attempt)
* \return handle_* return convention: -1 on error, 0 on EAGAIN (no more
* io events queued), >0 on success. success/error refer only to
* the accept.
*/
static inline int handle_new_connect(struct socket_info* si)
{
union sockaddr_union su;
struct tcp_connection* tcpconn;
socklen_t su_len;
int new_sock;
int id;
/* got a connection on r */
su_len=sizeof(su);
new_sock=accept(si->socket, &(su.s), &su_len);
if (new_sock==-1){
if ((errno==EAGAIN)||(errno==EWOULDBLOCK))
return 0;
LM_ERR("failed to accept connection(%d): %s\n", errno, strerror(errno));
return -1;
}
if (tcp_connections_no>=tcp_max_connections){
LM_ERR("maximum number of connections exceeded: %d/%d\n",
tcp_connections_no, tcp_max_connections);
close(new_sock);
return 1; /* success, because the accept was succesfull */
}
if (init_sock_opt(new_sock)<0){
LM_ERR("init_sock_opt failed\n");
close(new_sock);
return 1; /* success, because the accept was succesfull */
}
/* add socket to list */
tcpconn=tcpconn_new(new_sock, &su, si, si->proto, S_CONN_ACCEPT);
if (tcpconn){
tcpconn->refcnt++; /* safe, not yet available to the
outside world */
tcpconn_add(tcpconn);
LM_DBG("new connection: %p %d flags: %04x\n",
tcpconn, tcpconn->s, tcpconn->flags);
/* pass it to a child */
if(send2child(tcpconn,IO_WATCH_READ)<0){
LM_ERR("no children available\n");
id = tcpconn->id;
TCPCONN_LOCK(id);
tcpconn->refcnt--;
if (tcpconn->refcnt==0){
close(tcpconn->s);
_tcpconn_rm(tcpconn);
}else tcpconn->timeout=0; /* force expire */
TCPCONN_UNLOCK(id);
}
}else{ /*tcpconn==0 */
LM_ERR("tcpconn_new failed, closing socket\n");
close(new_sock);
}
return 1; /* accept() was succesfull */
}
/*! \brief used internally by tcp_main_loop() */
static void tcpconn_destroy(struct tcp_connection* tcpconn)
{
int fd;
int id = tcpconn->id;
TCPCONN_LOCK(id); /*avoid races w/ tcp_send*/
tcpconn->refcnt--;
if (tcpconn->refcnt==0){
LM_DBG("destroying connection %p, flags %04x\n",
tcpconn, tcpconn->flags);
fd=tcpconn->s;
#ifdef USE_TLS
/*FIXME: lock ->writelock ? */
if (tcpconn->type==PROTO_TLS)
tls_close(tcpconn, fd);
#endif
_tcpconn_rm(tcpconn);
close(fd);
tcp_connections_no--;
}else{
/* force timeout */
tcpconn->timeout=0;
tcpconn->state=S_CONN_BAD;
LM_DBG("delaying (%p, flags %04x) ref = %d ...\n",
tcpconn, tcpconn->flags, tcpconn->refcnt);
}
TCPCONN_UNLOCK(id);
}
/*! \brief
* handles an io event on one of the watched tcp connections
*
* \param tcpconn - pointer to the tcp_connection for which we have an io ev.
* \param fd_i - index in the fd_array table (needed for delete)
* \return handle_* return convention, but on success it always returns 0
* (because it's one-shot, after a succesfull execution the fd is
* removed from tcp_main's watch fd list and passed to a child =>
* tcp_main is not interested in further io events that might be
* queued for this fd)
*/
inline static int handle_tcpconn_ev(struct tcp_connection* tcpconn, int fd_i,int event_type)
{
int fd;
int err;
int id;
unsigned int err_len;
/* is refcnt!=0 really necessary?
* No, in fact it's a bug: I can have the following situation: a send only
* tcp connection used by n processes simultaneously => refcnt = n. In
* the same time I can have a read event and this situation is perfectly
* valid. -- andrei
*/
#if 0
if ((tcpconn->refcnt!=0)){
/* FIXME: might be valid for sigio_rt iff fd flags are not cleared
* (there is a short window in which it could generate a sig
* that would be catched by tcp_main) */
LM_CRIT("io event on referenced tcpconn (%p), refcnt=%d, fd=%d\n",
tcpconn, tcpconn->refcnt, tcpconn->s);
return -1;
}
#endif
if (event_type == IO_WATCH_READ) {
/* pass it to child, so remove it from the io watch list */
LM_DBG("data available on %p %d\n", tcpconn, tcpconn->s);
if (io_watch_del(&io_h, tcpconn->s, fd_i, 0,IO_WATCH_READ)==-1)
return -1;
tcpconn->flags|=F_CONN_REMOVED;
tcpconn_ref(tcpconn); /* refcnt ++ */
if (send2child(tcpconn,IO_WATCH_READ)<0){
LM_ERR("no children available\n");
id = tcpconn->id;
TCPCONN_LOCK(id);
tcpconn->refcnt--;
if (tcpconn->refcnt==0){
fd=tcpconn->s;
_tcpconn_rm(tcpconn);
close(fd);
}else tcpconn->timeout=0; /* force expire*/
TCPCONN_UNLOCK(id);
}
return 0; /* we are not interested in possibly queued io events,
the fd was either passed to a child, or closed */
} else {
LM_DBG("connection %p fd %d is now writable\n", tcpconn, tcpconn->s);
/* we received a write event */
if (tcpconn->flags & F_CONN_NOT_CONNECTED) {
/* we're coming from an async connect & write
* let's see if we connected succesfully*/
err_len=sizeof(err);
getsockopt(tcpconn->s, SOL_SOCKET, SO_ERROR, &err, &err_len);
if (err != 0) {
LM_DBG("Failed connection attempt\n");
tcpconn_ref(tcpconn);
io_watch_del(&io_h, tcpconn->s, -1, IO_FD_CLOSING,
IO_WATCH_READ|IO_WATCH_WRITE);
tcpconn->flags|=F_CONN_REMOVED;
tcpconn_destroy(tcpconn);
return 0;
}
/* we succesfully connected - further treat this case as if we
* were coming from an async write */
tcpconn->flags &=~F_CONN_NOT_CONNECTED;
LM_DBG("Succesfully completed previous async connect\n");
goto async_write;
} else {
/* we're coming from an async write -
* just pass to child and have it write
* our TCP chunks */
async_write:
/* no more write events for now */
if (io_watch_del(&io_h, tcpconn->s, fd_i, 0,IO_WATCH_WRITE)==-1)
return -1;
tcpconn->flags|=F_CONN_REMOVED;
tcpconn_ref(tcpconn); /* refcnt ++ */
if (send2child(tcpconn,IO_WATCH_WRITE)<0){
LM_ERR("no children available\n");
id = tcpconn->id;
TCPCONN_LOCK(id);
tcpconn->refcnt--;
if (tcpconn->refcnt==0){
fd=tcpconn->s;
_tcpconn_rm(tcpconn);
close(fd);
}else tcpconn->timeout=0; /* force expire*/
TCPCONN_UNLOCK(id);
}
return 0;
}
}
}
void force_tcp_conn_lifetime(struct receive_info *rcv, unsigned int timeout)
{
struct tcp_connection* con;
unsigned int lifetime = get_ticks() + timeout;
TCPCONN_LOCK(rcv->proto_reserved1);
con =_tcpconn_find(rcv->proto_reserved1);
if (!con) {
LM_ERR("Strange, tcp conn not found (id=%d)\n",rcv->proto_reserved1);
} else {
con->lifetime = lifetime;
}
TCPCONN_UNLOCK(rcv->proto_reserved1);
}
static inline void set_tcp_timeout(struct tcp_connection *c)
{
unsigned int timeout = get_ticks() + tcp_con_lifetime;
if (c->lifetime) {
if ( c->lifetime < timeout ) {
c->timeout = timeout;
c->lifetime = 0;
} else
c->timeout = c->lifetime;
} else {
c->timeout = timeout;
}
}
/*! \brief handles io from a tcp child process
* \param tcp_c - pointer in the tcp_children array, to the entry for
* which an io event was detected
* \param fd_i - fd index in the fd_array (usefull for optimizing
* io_watch_deletes)
* \return handle_* return convention: -1 on error, 0 on EAGAIN (no more
* io events queued), >0 on success. success/error refer only to
* the reads from the fd.
*/
inline static int handle_tcp_child(struct tcp_child* tcp_c, int fd_i)
{
struct tcp_connection* tcpconn;
long response[2];
int cmd;
int bytes;
if (tcp_c->unix_sock<=0){
/* (we can't have a fd==0, 0 is never closed )*/
LM_CRIT("fd %d for %d (pid %d, ser no %d)\n", tcp_c->unix_sock,
(int)(tcp_c-&tcp_children[0]), tcp_c->pid, tcp_c->proc_no);
goto error;
}
/* read until sizeof(response)
* (this is a SOCK_STREAM so read is not atomic) */
bytes=recv_all(tcp_c->unix_sock, response, sizeof(response), MSG_DONTWAIT);
if (bytes<(int)sizeof(response)){
if (bytes==0){
/* EOF -> bad, child has died */
LM_DBG("dead tcp child %d (pid %d, no %d)"
" (shutting down?)\n", (int)(tcp_c-&tcp_children[0]),
tcp_c->pid, tcp_c->proc_no );
/* don't listen on it any more */
io_watch_del(&io_h, tcp_c->unix_sock, fd_i, 0,IO_WATCH_READ);
goto error; /* eof. so no more io here, it's ok to return error */
}else if (bytes<0){
/* EAGAIN is ok if we try to empty the buffer
* e.g.: SIGIO_RT overflow mode or EPOLL ET */
if ((errno!=EAGAIN) && (errno!=EWOULDBLOCK)){
LM_CRIT("read from tcp child %ld (pid %d, no %d) %s [%d]\n",
(long)(tcp_c-&tcp_children[0]), tcp_c->pid,
tcp_c->proc_no, strerror(errno), errno );
}else{
bytes=0;
}
/* try to ignore ? */
goto end;
}else{
/* should never happen */
LM_CRIT("too few bytes received (%d)\n", bytes );
bytes=0; /* something was read so there is no error; otoh if
receive_fd returned less then requested => the receive
buffer is empty => no more io queued on this fd */
goto end;
}
}
LM_DBG("reader response= %lx, %ld from %d \n",
response[0], response[1], (int)(tcp_c-&tcp_children[0]));
cmd=response[1];
tcpconn=(struct tcp_connection*)response[0];
if (tcpconn==0){
/* should never happen */
LM_CRIT("null tcpconn pointer received from tcp child %d (pid %d):"
"%lx, %lx\n", (int)(tcp_c-&tcp_children[0]), tcp_c->pid,
response[0], response[1]) ;
goto end;
}
switch(cmd){
case CONN_RELEASE:
tcp_c->busy--;
if (tcpconn->state==S_CONN_BAD){
tcpconn_destroy(tcpconn);
break;
}
/* update the timeout (lifetime) */
set_tcp_timeout( tcpconn );
tcpconn_put(tcpconn);
/* must be after the de-ref*/
io_watch_add(&io_h, tcpconn->s, F_TCPCONN, tcpconn,IO_WATCH_READ);
tcpconn->flags&=~F_CONN_REMOVED;
break;
case ASYNC_WRITE:
tcp_c->busy--;
if (tcpconn->state==S_CONN_BAD){
tcpconn_destroy(tcpconn);
break;
}
/* update the timeout (lifetime) */
set_tcp_timeout( tcpconn );
tcpconn_put(tcpconn);
/* must be after the de-ref*/
io_watch_add(&io_h, tcpconn->s, F_TCPCONN, tcpconn,IO_WATCH_WRITE);
tcpconn->flags&=~F_CONN_REMOVED;
break;
case CONN_ERROR:
case CONN_DESTROY:
case CONN_EOF:
/* WARNING: this will auto-dec. refcnt! */
tcp_c->busy--;
/* main doesn't listen on it => we don't have to delete it
if (tcpconn->s!=-1)
io_watch_del(&io_h, tcpconn->s, -1, IO_FD_CLOSING);
*/
tcpconn_destroy(tcpconn); /* closes also the fd */
break;
default:
LM_CRIT("unknown cmd %d from tcp reader %d\n",
cmd, (int)(tcp_c-&tcp_children[0]));
}
end:
return bytes;
error:
return -1;
}
/*! \brief handles io from a "generic" ser process (get fd or new_fd from a tcp_send)
*
* \param p - pointer in the ser processes array (pt[]), to the entry for
* which an io event was detected
* \param fd_i - fd index in the fd_array (usefull for optimizing
* io_watch_deletes)
* \return handle_* return convention:
* - -1 on error reading from the fd,
* - 0 on EAGAIN or when no more io events are queued
* (receive buffer empty),
* - >0 on successfull reads from the fd (the receive buffer might
* be non-empty).
*/
inline static int handle_ser_child(struct process_table* p, int fd_i)
{
struct tcp_connection* tcpconn;
long response[2];
int cmd;
int bytes;
int ret;
int fd;
ret=-1;
if (p->unix_sock<=0){
/* (we can't have a fd==0, 0 is never closed )*/
LM_CRIT("fd %d for %d (pid %d)\n",
p->unix_sock, (int)(p-&pt[0]), p->pid);
goto error;
}
/* get all bytes and the fd (if transmitted)
* (this is a SOCK_STREAM so read is not atomic) */
bytes=receive_fd(p->unix_sock, response, sizeof(response), &fd,
MSG_DONTWAIT);
if (bytes<(int)sizeof(response)){
/* too few bytes read */
if (bytes==0){
/* EOF -> bad, child has died */
LM_DBG("dead child %d, pid %d"
" (shutting down?)\n", (int)(p-&pt[0]), p->pid);
/* don't listen on it any more */
io_watch_del(&io_h, p->unix_sock, fd_i, 0,IO_WATCH_READ);
goto error; /* child dead => no further io events from it */
}else if (bytes<0){
/* EAGAIN is ok if we try to empty the buffer
* e.g: SIGIO_RT overflow mode or EPOLL ET */
if ((errno!=EAGAIN) && (errno!=EWOULDBLOCK)){
LM_CRIT("read from child %d (pid %d): %s [%d]\n",
(int)(p-&pt[0]), p->pid, strerror(errno), errno);
ret=-1;
}else{
ret=0;
}
/* try to ignore ? */
goto end;
}else{
/* should never happen */
LM_CRIT("too few bytes received (%d)\n", bytes );
ret=0; /* something was read so there is no error; otoh if
receive_fd returned less then requested => the receive
buffer is empty => no more io queued on this fd */
goto end;
}
}
ret=1; /* something was received, there might be more queued */
LM_DBG("read response= %lx, %ld, fd %d from %d (%d)\n",
response[0], response[1], fd, (int)(p-&pt[0]), p->pid);
cmd=response[1];
tcpconn=(struct tcp_connection*)response[0];
if (tcpconn==0){
LM_CRIT("null tcpconn pointer received from child %d (pid %d)"
"%lx, %lx\n", (int)(p-&pt[0]), p->pid, response[0], response[1]) ;
goto end;
}
switch(cmd){
case CONN_ERROR:
if (!(tcpconn->flags & F_CONN_REMOVED) && (tcpconn->s!=-1)){
io_watch_del(&io_h, tcpconn->s, -1, IO_FD_CLOSING,
IO_WATCH_READ|IO_WATCH_WRITE);
tcpconn->flags|=F_CONN_REMOVED;
}
tcpconn_destroy(tcpconn); /* will close also the fd */
break;
case CONN_GET_FD:
/* send the requested FD */
/* WARNING: take care of setting refcnt properly to
* avoid race condition */
if (send_fd(p->unix_sock, &tcpconn, sizeof(tcpconn),
tcpconn->s)<=0){
LM_ERR("send_fd failed\n");
}
break;
case CONN_NEW:
/* update the fd in the requested tcpconn*/
/* WARNING: take care of setting refcnt properly to
* avoid race condition */
if (fd==-1){
LM_CRIT(" cmd CONN_NEW: no fd received\n");
break;
}
tcpconn->s=fd;
/* add tcpconn to the list*/
tcpconn_add(tcpconn);
/* update the timeout*/
tcpconn->timeout=get_ticks()+tcp_con_lifetime;
io_watch_add(&io_h, tcpconn->s, F_TCPCONN, tcpconn,IO_WATCH_READ);
tcpconn->flags&=~F_CONN_REMOVED;
break;
case ASYNC_CONNECT:
/* connection is not yet linked to hash = not yet
* available to the outside world */
if (fd==-1){
LM_CRIT(" cmd CONN_NEW: no fd received\n");
break;
}
tcpconn->flags|=F_CONN_NOT_CONNECTED;
tcpconn->s=fd;
/* add tcpconn to the list*/
tcpconn_add(tcpconn);
/* update the timeout*/
tcpconn->timeout=get_ticks()+tcp_con_lifetime;
/* only maintain the socket in the IO_WATCH_WRITE watcher
* while we have stuff to write - otherwise we're going to get
* useless events */
io_watch_add(&io_h, tcpconn->s, F_TCPCONN, tcpconn,IO_WATCH_WRITE);
tcpconn->flags&=~F_CONN_REMOVED;
break;
case ASYNC_WRITE:
if (tcpconn->state==S_CONN_BAD){
tcpconn_destroy(tcpconn);
break;
}
/* update the timeout (lifetime) */
set_tcp_timeout( tcpconn );
/* must be after the de-ref*/
io_watch_add(&io_h, tcpconn->s, F_TCPCONN, tcpconn,IO_WATCH_WRITE);
tcpconn->flags&=~F_CONN_REMOVED;
break;
default:
LM_CRIT("unknown cmd %d\n", cmd);
}
end:
return ret;
error:
return -1;
}
/*! \brief generic handle io routine, it will call the appropiate
* handle_xxx() based on the fd_map type
*
* \param fm - pointer to a fd hash entry
* \param idx - index in the fd_array (or -1 if not known)
* \return -1 on error
* 0 on EAGAIN or when by some other way it is known that no more
* io events are queued on the fd (the receive buffer is empty).
* Usefull to detect when there are no more io events queued for
* sigio_rt, epoll_et, kqueue.
* >0 on successfull read from the fd (when there might be more io
* queued -- the receive buffer might still be non-empty)
*/
inline static int handle_io(struct fd_map* fm, int idx,int event_type)
{
int ret;
switch(fm->type){
case F_SOCKINFO:
ret=handle_new_connect((struct socket_info*)fm->data);
break;
case F_TCPCONN:
ret=handle_tcpconn_ev((struct tcp_connection*)fm->data, idx,event_type);
break;
case F_TCPCHILD:
ret=handle_tcp_child((struct tcp_child*)fm->data, idx);
break;
case F_PROC:
ret=handle_ser_child((struct process_table*)fm->data, idx);
break;
case F_NONE:
LM_CRIT("empty fd map\n");
goto error;
default:
LM_CRIT("uknown fd type %d\n", fm->type);
goto error;
}
return ret;
error:
return -1;
}
/*
* iterates through all TCP connections and closes expired ones
*
* Note: runs once per second at most
*/
#define tcpconn_timeout(last_sec, close_all) \
do { \
int now; \
now = get_ticks(); \
if (last_sec != now) { \
last_sec = now; \
__tcpconn_timeout(close_all); \
} \
} while (0)
/*! \brief very inefficient for now - FIXME
* keep in sync with tcpconn_destroy, the "delete" part should be
* the same except for io_watch_del..
* \todo FIXME (very inefficient for now)
*/
static inline void __tcpconn_timeout(int force)
{
struct tcp_connection *c, *next;
unsigned int ticks,part;
unsigned h;
int fd;
ticks=get_ticks();
for( part=0 ; part<TCP_PARTITION_SIZE ; part++ ) {
TCPCONN_LOCK(part); /* fixme: we can lock only on delete IMO */
for(h=0; h<TCP_ID_HASH_SIZE; h++){
c=TCP_PART(part).tcpconn_id_hash[h];
while(c){
next=c->id_next;
if (force ||((c->refcnt==0) && (ticks>c->timeout))) {
if (!force)
LM_DBG("timeout for hash=%d - %p"
" (%d > %d)\n", h, c, ticks, c->timeout);
fd=c->s;
#ifdef USE_TLS
if (c->type==PROTO_TLS)
tls_close(c, fd);
#endif
_tcpconn_rm(c);
if ((!force)&&(fd>0)&&(c->refcnt==0)) {
if (!(c->flags & F_CONN_REMOVED)){
io_watch_del(&io_h, fd, -1, IO_FD_CLOSING,
IO_WATCH_READ|IO_WATCH_WRITE);
c->flags|=F_CONN_REMOVED;
}
close(fd);
}
tcp_connections_no--;
}
c=next;
}
}
TCPCONN_UNLOCK(part);
}
}
/*! \brief tcp main loop */
void tcp_main_loop(void)
{
static unsigned int last_sec = 0;
int flags;
struct socket_info* si;
int r;
/* init io_wait (here because we want the memory allocated only in
* the tcp_main process) */
/*! \todo FIXME: TODO: make tcp_max_fd_no a config param */
if (init_io_wait(&io_h, tcp_max_fd_no, tcp_poll_method)<0)
goto error;
/* init: start watching all the fds*/
/* add all the sockets we listens on for connections */
for (si=tcp_listen; si; si=si->next){
if ((si->proto==PROTO_TCP) &&(si->socket!=-1)){
if (io_watch_add(&io_h, si->socket, F_SOCKINFO, si,IO_WATCH_READ)<0){
LM_CRIT("failed to add listen socket to the fd list\n");
goto error;
}
}else{
LM_CRIT("non tcp address in tcp_listen\n");
}
}
#ifdef USE_TLS
if (!tls_disable){
for (si=tls_listen; si; si=si->next){
if ((si->proto==PROTO_TLS) && (si->socket!=-1)){
if (io_watch_add(&io_h, si->socket, F_SOCKINFO, si,IO_WATCH_READ)<0){
LM_CRIT("failed to add tls listen socket to the fd list\n");
goto error;
}
}else{
LM_CRIT("non tls address in tls_listen\n");
}
}
}
#endif
/* add all the unix sockets used for communcation with other opensips
* processes (get fd, new connection a.s.o) */
for (r=1; r<counted_processes; r++){
/* skip myslef (as process) and -1 socks (disabled)
(we can't have 0, we never close it!) */
if (r!=process_no && pt[r].unix_sock>0)
if (io_watch_add(&io_h, pt[r].unix_sock, F_PROC, &pt[r],IO_WATCH_READ)<0){
LM_CRIT("failed to add process %d (%s) unix socket "
"to the fd list\n", r, pt[r].desc);
goto error;
}
}
/* add all the unix sokets used for communication with the tcp childs */
for (r=0; r<tcp_children_no; r++){
/*we can't have 0, we never close it!*/
if (tcp_children[r].unix_sock>0) {
/* make socket non-blocking */
flags=fcntl(tcp_children[r].unix_sock, F_GETFL);
if (flags==-1){
LM_ERR("fnctl failed: (%d) %s\n", errno, strerror(errno));
goto error;
}
if (fcntl(tcp_children[r].unix_sock,F_SETFL,flags|O_NONBLOCK)==-1){
LM_ERR("set non-blocking failed: (%d) %s\n",
errno, strerror(errno));
goto error;
}
/* add socket for listening */
if (io_watch_add(&io_h, tcp_children[r].unix_sock, F_TCPCHILD,
&tcp_children[r],IO_WATCH_READ) <0){
LM_CRIT("failed to add tcp child %d unix socket to "
"the fd list\n", r);
goto error;
}
}
}
/* main loop */
switch(io_h.poll_method){
case POLL_POLL:
while(1){
/* wait and process IO */
io_wait_loop_poll(&io_h, TCP_MAIN_SELECT_TIMEOUT, 0);
/* remove old connections */
tcpconn_timeout(last_sec, 0);
}
break;
#ifdef HAVE_SELECT
case POLL_SELECT:
while(1){
io_wait_loop_select(&io_h, TCP_MAIN_SELECT_TIMEOUT, 0);
tcpconn_timeout(last_sec, 0);
}
break;
#endif
#ifdef HAVE_SIGIO_RT
case POLL_SIGIO_RT:
while(1){
io_wait_loop_sigio_rt(&io_h, TCP_MAIN_SELECT_TIMEOUT);
tcpconn_timeout(last_sec, 0);
}
break;
#endif
#ifdef HAVE_EPOLL
case POLL_EPOLL_LT:
while(1){
io_wait_loop_epoll(&io_h, TCP_MAIN_SELECT_TIMEOUT, 0);
tcpconn_timeout(last_sec, 0);
}
break;
case POLL_EPOLL_ET:
while(1){
io_wait_loop_epoll(&io_h, TCP_MAIN_SELECT_TIMEOUT, 1);
tcpconn_timeout(last_sec, 0);
}
break;
#endif
#ifdef HAVE_KQUEUE
case POLL_KQUEUE:
while(1){
io_wait_loop_kqueue(&io_h, TCP_MAIN_SELECT_TIMEOUT, 0);
tcpconn_timeout(last_sec, 0);
}
break;
#endif
#ifdef HAVE_DEVPOLL
case POLL_DEVPOLL:
while(1){
io_wait_loop_devpoll(&io_h, TCP_MAIN_SELECT_TIMEOUT, 0);
tcpconn_timeout(last_sec, 0);
}
break;
#endif
default:
LM_CRIT("no support for poll method %s (%d)\n",
poll_method_name(io_h.poll_method), io_h.poll_method);
goto error;
}
error:
destroy_io_wait(&io_h);
LM_CRIT("exiting...");
exit(-1);
}
/*! \brief cleanup before exit */
void destroy_tcp(void)
{
int part;
if (tcp_parts[0].tcpconn_id_hash)
__tcpconn_timeout(1); /* force close/expire for all active tcpconns*/
if (connection_id){
shm_free(connection_id);
connection_id=0;
}
for ( part=0 ; part<TCP_PARTITION_SIZE ; part++ ) {
if (tcp_parts[part].tcpconn_id_hash){
shm_free(tcp_parts[part].tcpconn_id_hash);
tcp_parts[part].tcpconn_id_hash=0;
}
if (tcp_parts[part].tcpconn_aliases_hash){
shm_free(tcp_parts[part].tcpconn_aliases_hash);
tcp_parts[part].tcpconn_aliases_hash=0;
}
if (tcp_parts[part].tcpconn_lock){
lock_destroy(tcp_parts[part].tcpconn_lock);
lock_dealloc((void*)tcp_parts[part].tcpconn_lock);
tcp_parts[part].tcpconn_lock=0;
}
}
}
int init_tcp(void)
{
char* poll_err;
unsigned int i;
/* init tcp children array */
tcp_children = (struct tcp_child*)pkg_malloc
( tcp_children_no*sizeof(struct tcp_child) );
if (tcp_children==0) {
LM_CRIT("could not alloc tcp_children array in pkg memory\n");
goto error;
}
memset( tcp_children, 0, tcp_children_no*sizeof(struct tcp_child));
/* init globals */
connection_id=(int*)shm_malloc(sizeof(int));
if (connection_id==0){
LM_CRIT("could not alloc globals in shm memory\n");
goto error;
}
*connection_id=1;
memset( &tcp_parts, 0, TCP_PARTITION_SIZE*sizeof(struct tcp_partition));
/* init partitions */
for( i=0 ; i<TCP_PARTITION_SIZE ; i++ ) {
/* init lock */
tcp_parts[i].tcpconn_lock=lock_alloc();
if (tcp_parts[i].tcpconn_lock==0){
LM_CRIT("could not alloc lock\n");
goto error;
}
if (lock_init(tcp_parts[i].tcpconn_lock)==0){
LM_CRIT("could not init lock\n");
lock_dealloc((void*)tcp_parts[i].tcpconn_lock);
tcp_parts[i].tcpconn_lock=0;
goto error;
}
/* alloc hashtables*/
tcp_parts[i].tcpconn_aliases_hash=(struct tcp_conn_alias**)
shm_malloc(TCP_ALIAS_HASH_SIZE* sizeof(struct tcp_conn_alias*));
if (tcp_parts[i].tcpconn_aliases_hash==0){
LM_CRIT("could not alloc address hashtable in shm memory\n");
goto error;
}
tcp_parts[i].tcpconn_id_hash=(struct tcp_connection**)shm_malloc(TCP_ID_HASH_SIZE*
sizeof(struct tcp_connection*));
if (tcp_parts[i].tcpconn_id_hash==0){
LM_CRIT("could not alloc id hashtable in shm memory\n");
goto error;
}
/* init hashtables*/
memset((void*)tcp_parts[i].tcpconn_aliases_hash, 0,
TCP_ALIAS_HASH_SIZE * sizeof(struct tcp_conn_alias*));
memset((void*)tcp_parts[i].tcpconn_id_hash, 0,
TCP_ID_HASH_SIZE * sizeof(struct tcp_connection*));
}
/* fix config variables */
/* they can have only positive values due the config parser so we can
* ignore most of them */
poll_err=check_poll_method(tcp_poll_method);
/* set an appropiate poll method */
if (poll_err || (tcp_poll_method==0)){
tcp_poll_method=choose_poll_method();
if (poll_err){
LM_ERR("%s, using %s instead\n",
poll_err, poll_method_name(tcp_poll_method));
}else{
LM_INFO("using %s as the TCP io watch method"
" (auto detected)\n", poll_method_name(tcp_poll_method));
}
}else{
LM_INFO("using %s as the TCP io watch method (config)\n",
poll_method_name(tcp_poll_method));
}
return 0;
error:
/* clean-up */
destroy_tcp();
return -1;
}
/*! \brief starts the tcp processes */
int tcp_init_children(int *chd_rank, int *startup_done)
{
int r;
//int sockfd[2];
int reader_fd[2]; /* for comm. with the tcp children read */
pid_t pid;
struct socket_info *si;
stat_var *load_p = NULL;
/* estimate max fd. no:
* 1 tcp send unix socket/all_proc,
* + 1 udp sock/udp proc + 1 tcp_child sock/tcp child*
* + no_listen_tcp */
for(r=0, si=tcp_listen; si; si=si->next, r++);
#ifdef USE_TLS
if (! tls_disable)
for (si=tls_listen; si; si=si->next, r++);
#endif
tcp_max_fd_no=counted_processes*2 +r-1 /* timer */ +3; /* stdin/out/err*/
tcp_max_fd_no+=tcp_max_connections;
/* create the tcp sock_info structures */
/* copy the sockets --moved to main_loop*/
if (register_tcp_load_stat( &load_p )!=0) {
LM_ERR("failed to init tcp load statistics\n");
goto error;
}
/* fork children & create the socket pairs*/
for(r=0; r<tcp_children_no; r++){
/*if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfd)<0){
LM_ERR("socketpair failed: %s\n", strerror(errno));
goto error;
}*/
if (socketpair(AF_UNIX, SOCK_STREAM, 0, reader_fd)<0){
LM_ERR("socketpair failed: %s\n", strerror(errno));
goto error;
}
(*chd_rank)++;
pid=internal_fork("SIP receiver TCP");
if (pid<0){
LM_ERR("fork failed\n");
goto error;
}else if (pid>0){
/* parent */
close(reader_fd[1]);
tcp_children[r].pid=pid;
tcp_children[r].proc_no=process_no;
tcp_children[r].busy=0;
tcp_children[r].n_reqs=0;
tcp_children[r].unix_sock=reader_fd[0];
}else{
/* child */
set_proc_attrs("TCP receiver");
pt[process_no].idx=r;
pt[process_no].load = load_p;
bind_address=0; /* force a SEGFAULT if someone uses a non-init.
bind address on tcp */
if (init_child(*chd_rank) < 0) {
LM_ERR("init_children failed\n");
if (send_status_code(-1) < 0)
LM_ERR("failed to send status code\n");
clean_write_pipeend();
*startup_done = -1;
exit(-1);
}
/* was startup route executed so far ? */
if (startup_done!=NULL && *startup_done==0 && r==0) {
LM_DBG("runing startup for first TCP\n");
if(run_startup_route()< 0) {
*startup_done = -1;
if (send_status_code(-1) < 0)
LM_ERR("failed to send status code\n");
clean_write_pipeend();
LM_ERR("Startup route processing failed\n");
exit(-1);
}
*startup_done = 1;
}
if (send_status_code(0) < 0)
LM_ERR("failed to send status code\n");
clean_write_pipeend();
tcp_receive_loop(reader_fd[1]);
exit(-1);
}
}
return 0;
error:
return -1;
}
struct mi_root *mi_list_tcp_conns(struct mi_root *cmd, void *param)
{
struct mi_root *rpl_tree;
struct mi_node* node;
struct mi_attr *attr;
struct tcp_connection *conn;
time_t _ts;
char date_buf[MI_DATE_BUF_LEN];
int date_buf_len;
unsigned int i,n,part;
char proto[4];
char *p;
int len;
if (tcp_disable)
return init_mi_tree( 404, MI_SSTR("TCP support disabled"));
rpl_tree = init_mi_tree( 200, MI_SSTR(MI_OK));
if (rpl_tree==NULL)
return 0;
for( part=0 ; part<TCP_PARTITION_SIZE ; part++) {
TCPCONN_LOCK(part);
for( i=0,n=0 ; i<TCP_ID_HASH_SIZE ; i++ ) {
for( conn=TCP_PART(part).tcpconn_id_hash[i] ; conn ; conn=conn->id_next ) {
/* add one node for each conn */
node = add_mi_node_child(&rpl_tree->node, 0,
MI_SSTR("Connection"), 0, 0 );
if (node==0)
goto error;
/* add ID */
p = int2str((unsigned long)conn->id, &len);
attr = add_mi_attr( node, MI_DUP_VALUE, MI_SSTR("ID"), p, len);
if (attr==0)
goto error;
/* add type/proto */
p = proto2str(conn->type, proto);
attr = add_mi_attr( node, MI_DUP_VALUE, MI_SSTR("Type"),
proto, (int)(long)(p-proto));
if (attr==0)
goto error;
/* add state */
p = int2str((unsigned long)conn->state, &len);
attr = add_mi_attr( node, MI_DUP_VALUE, MI_SSTR("State"), p, len);
if (attr==0)
goto error;
/* add Source */
attr = addf_mi_attr( node, MI_DUP_VALUE, MI_SSTR("Source"),
"%s:%d",ip_addr2a(&conn->rcv.src_ip), conn->rcv.src_port);
if (attr==0)
goto error;
/* add Destination */
attr = addf_mi_attr( node, MI_DUP_VALUE, MI_SSTR("Destination"),
"%s:%d",ip_addr2a(&conn->rcv.dst_ip), conn->rcv.dst_port);
if (attr==0)
goto error;
/* add timeout */
_ts = (time_t)conn->timeout + startup_time;
date_buf_len = strftime(date_buf, MI_DATE_BUF_LEN - 1,
"%Y-%m-%d %H:%M:%S", localtime(&_ts));
if (date_buf_len != 0) {
attr = add_mi_attr( node, MI_DUP_VALUE, MI_SSTR("Timeout"),
date_buf, date_buf_len);
} else {
p = int2str((unsigned long)_ts, &len);
attr = add_mi_attr( node, MI_DUP_VALUE, MI_SSTR("Timeout"), p,len);
}
if (attr==0)
goto error;
/* add lifetime */
p = int2str((unsigned long)conn->lifetime, &len);
attr = add_mi_attr( node, MI_DUP_VALUE,
MI_SSTR("Pending lifetime"), p, len);
if (attr==0)
goto error;
n++;
/* at each 50 conns, flush the tree */
if ( (n % 50) == 0 )
flush_mi_tree(rpl_tree);
}
}
TCPCONN_UNLOCK(part);
}
return rpl_tree;
error:
TCPCONN_UNLOCK(part);
LM_ERR("failed to add node\n");
free_mi_tree(rpl_tree);
return 0;
}
#endif
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/maplerain/opensips.git
git@gitee.com:maplerain/opensips.git
maplerain
opensips
opensips
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891