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); init_tcp_req(&c->req); 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; tcp_connections_no++; return c; error: if (c) shm_free(c); return 0; }
static int hep_tcp_read_req(struct tcp_connection* con, int* bytes_read) { int bytes; int total_bytes; struct tcp_req *req; bytes = -1; total_bytes = 0; if (con->con_req) { req = con->con_req; LM_DBG("Using the per connection buff \n"); } else { LM_DBG("Using the global ( per process ) buff \n"); init_tcp_req(&hep_current_req, 0); req = &hep_current_req; } again: if(req->error == TCP_REQ_OK){ /* if we still have some unparsed part, parse it first, * don't do the read*/ if (req->parsed < req->pos){ bytes=0; } else { bytes=tcp_read(con,req); if (bytes < 0) { LM_ERR("failed to read \n"); goto error; } } hep_parse_headers(req); total_bytes+=bytes; /* eof check: * is EOF if eof on fd and req. not complete yet, * if req. is complete we might have a second unparsed * request after it, so postpone release_with_eof */ if ((con->state==S_CONN_EOF) && (req->complete==0)) { LM_DBG("EOF received\n"); goto done; } } if (req->error!=TCP_REQ_OK){ LM_ERR("bad request, state=%d, error=%d " "buf:\n%.*s\nparsed:\n%.*s\n", req->state, req->error, (int)(req->pos-req->buf), req->buf, (int)(req->parsed-req->start), req->start); LM_DBG("- received from: port %d\n", con->rcv.src_port); print_ip("- received from: ip ",&con->rcv.src_ip, "\n"); goto error; } switch (hep_handle_req(req, con, hep_max_msg_chunks) ) { case 1: goto again; case -1: goto error; } LM_DBG("tcp_read_req end\n"); done: if (bytes_read) *bytes_read=total_bytes; /* connection will be released */ return 0; error: /* connection will be released as ERROR */ return -1; }
static int smpp_handle_req(struct tcp_req *req, struct tcp_connection *con) { long size; struct receive_info local_rcv; if (req->complete){ /* update the timeout - we successfully read the request */ tcp_conn_set_lifetime( con, tcp_con_lifetime); con->timeout = con->lifetime; LM_DBG("completely received a message\n"); /* rcv.bind_address should always be !=0 */ /* just for debugging use sendipv4 as receiving socket FIXME*/ con->rcv.proto_reserved1=con->id; /* copy the id */ /* prepare for next request */ size=req->pos - req->parsed; if (!size) { /* did not read any more things - we can release * the connection */ LM_DBG("Nothing more to read on TCP conn %p, currently in state %d \n", con,con->state); if (req != &smpp_current_req) { /* we have the buffer in the connection tied buff - * detach it , release the conn and free it afterwards */ con->con_req = NULL; } } else { LM_DBG("We still have things on the pipe - " "keeping connection \n"); } local_rcv = con->rcv; /* give the message to the registered functions */ handle_smpp_msg(req->buf, (smpp_session_t *)con->proto_data, &local_rcv); if (!size && req != &smpp_current_req) { /* if we no longer need this tcp_req * we can free it now */ pkg_free(req); } con->msg_attempts = 0; if (size) { memmove(req->buf, req->parsed, size); init_tcp_req(req, size); /* if we still have some unparsed bytes, try to parse them too*/ return 1; } } else { con->msg_attempts ++; if (con->msg_attempts == smpp_max_msg_chunks) { LM_ERR("Made %u read attempts but message is not complete yet - " "closing connection \n",con->msg_attempts); return -1; } if (req == &smpp_current_req) { /* let's duplicate this - most likely another conn will come in */ LM_DBG("We didn't manage to read a full request\n"); con->con_req = pkg_malloc(sizeof(struct tcp_req)); if (con->con_req == NULL) { LM_ERR("No more mem for dynamic con request buffer\n"); return -1; } if (req->pos != req->buf) { /* we have read some bytes */ memcpy(con->con_req->buf,req->buf,req->pos-req->buf); con->con_req->pos = con->con_req->buf + (req->pos-req->buf); } else { con->con_req->pos = con->con_req->buf; } if (req->parsed != req->buf) con->con_req->parsed =con->con_req->buf+(req->parsed-req->buf); else con->con_req->parsed = con->con_req->buf; con->con_req->complete=req->complete; con->con_req->content_len=req->content_len; con->con_req->error = req->error; } } return 0; }
/*! \brief * handle io routine, based on the fd_map type * (it will be called from io_wait_loop* ) * params: fm - pointer to a fd hash entry * idx - index in the fd_array (or -1 if not known) * return: -1 on error, or when we are not interested any more on reads * from this fd (e.g.: we are closing it ) * 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 ret; int n; struct tcp_connection* con; int s; long resp; switch(fm->type){ case F_TCPMAIN: again: ret=n=receive_fd(fm->fd, &con, sizeof(con), &s, 0); LM_DBG("received n=%d con=%p, fd=%d\n", n, con, s); if (n<0){ if (errno == EWOULDBLOCK || errno == EAGAIN){ ret=0; break; }else if (errno == EINTR) goto again; else{ LM_CRIT("read_fd: %s \n", strerror(errno)); abort(); /* big error*/ } } if (n==0){ LM_WARN("0 bytes read\n"); break; } if (con==0){ LM_CRIT("null pointer\n"); break; } con->fd=s; if (s==-1) { LM_ERR("read_fd:no fd read\n"); goto con_error; } if (con==tcp_conn_lst){ LM_CRIT("duplicate" " connection received: %p, id %d, fd %d, refcnt %d" " state %d (n=%d)\n", con, con->id, con->fd, con->refcnt, con->state, n); release_tcpconn(con, CONN_ERROR, tcpmain_sock); break; /* try to recover */ } /* reset the per process TCP req struct */ init_tcp_req(¤t_req); /* 0 attempts so far for this SIP MSG */ con->msg_attempts = 0; /* must be before io_watch_add, io_watch_add might catch some * already existing events => might call handle_io and * handle_io might decide to del. the new connection => * must be in the list */ tcpconn_listadd(tcp_conn_lst, con, c_next, c_prev); con->timeout=get_ticks()+TCP_CHILD_MAX_MSG_TIME; if (io_watch_add(&io_w, s, F_TCPCONN, con)<0){ LM_CRIT("failed to add new socket to the fd list\n"); tcpconn_listrm(tcp_conn_lst, con, c_next, c_prev); goto con_error; } break; case F_TCPCONN: con=(struct tcp_connection*)fm->data; resp=tcp_read_req(con, &ret); if (resp<0) { ret=-1; /* some error occured */ io_watch_del(&io_w, con->fd, idx, IO_FD_CLOSING); tcpconn_listrm(tcp_conn_lst, con, c_next, c_prev); con->state=S_CONN_BAD; release_tcpconn(con, resp, tcpmain_sock); } break; case F_NONE: LM_CRIT("empty fd map %p (%d): " "{%d, %d, %p}\n", fm, (int)(fm-io_w.fd_hash), fm->fd, fm->type, fm->data); goto error; default: LM_CRIT("uknown fd type %d\n", fm->type); goto error; } return ret; con_error: con->state=S_CONN_BAD; release_tcpconn(con, CONN_ERROR, fm->fd); return ret; error: return -1; }
/* Responsible for reading the request * * if returns >= 0 : it keeps the connection for further usage * or releases it manually * * if returns < 0 : the connection should be released by the * upper layer */ int tcp_read_req(struct tcp_connection* con, int* bytes_read) { int bytes; int total_bytes; int resp; long size; struct tcp_req* req; char c; struct receive_info local_rcv; char *msg_buf; int msg_len; bytes=-1; total_bytes=0; resp=CONN_RELEASE; if (con->con_req) { req=con->con_req; LM_DBG("Using the per connection buff \n"); } else { LM_DBG("Using the global ( per process ) buff \n"); req=¤t_req; } #ifdef USE_TLS if (con->type==PROTO_TLS){ if (tls_fix_read_conn(con)!=0){ resp=CONN_ERROR; goto end_req; } if(con->state!=S_CONN_OK) goto end_req; /* not enough data */ } #endif again: if(req->error==TCP_REQ_OK){ bytes=tcp_read_headers(con,req); //#ifdef EXTRA_DEBUG /* if timeout state=0; goto end__req; */ LM_DBG("read= %d bytes, parsed=%d, state=%d, error=%d\n", bytes, (int)(req->parsed-req->start), req->state, req->error ); LM_DBG("last char=0x%02X, parsed msg=\n%.*s\n", *(req->parsed-1), (int)(req->parsed-req->start), req->start); //#endif if (bytes==-1){ LM_ERR("failed to read \n"); resp=CONN_ERROR; goto end_req; } total_bytes+=bytes; /* eof check: * is EOF if eof on fd and req. not complete yet, * if req. is complete we might have a second unparsed * request after it, so postpone release_with_eof */ if ((con->state==S_CONN_EOF) && (req->complete==0)) { LM_DBG("EOF\n"); resp=CONN_EOF; goto end_req; } } if (req->error!=TCP_REQ_OK){ LM_ERR("bad request, state=%d, error=%d " "buf:\n%.*s\nparsed:\n%.*s\n", req->state, req->error, (int)(req->pos-req->buf), req->buf, (int)(req->parsed-req->start), req->start); LM_DBG("- received from: port %d\n", con->rcv.src_port); print_ip("- received from: ip ",&con->rcv.src_ip, "\n"); resp=CONN_ERROR; goto end_req; } if (req->complete){ #ifdef EXTRA_DEBUG LM_DBG("end of header part\n"); LM_DBG("- received from: port %d\n", con->rcv.src_port); print_ip("- received from: ip ", &con->rcv.src_ip, "\n"); LM_DBG("headers:\n%.*s.\n",(int)(req->body-req->start), req->start); #endif if (req->has_content_len){ LM_DBG("content-length= %d\n", req->content_len); #ifdef EXTRA_DEBUG LM_DBG("body:\n%.*s\n", req->content_len,req->body); #endif }else{ req->error=TCP_REQ_BAD_LEN; LM_ERR("content length not present or unparsable\n"); resp=CONN_ERROR; goto end_req; } /* update the timeout - we succesfully read the request */ con->timeout=get_ticks()+TCP_CHILD_MAX_MSG_TIME; /* if we are here everything is nice and ok*/ update_stat( pt[process_no].load, +1 ); resp=CONN_RELEASE; #ifdef EXTRA_DEBUG LM_DBG("calling receive_msg(%p, %d, )\n", req->start, (int)(req->parsed-req->start)); #endif /* rcv.bind_address should always be !=0 */ bind_address=con->rcv.bind_address; /* just for debugging use sendipv4 as receiving socket FIXME*/ /* if (con->rcv.dst_ip.af==AF_INET6){ bind_address=sendipv6_tcp; }else{ bind_address=sendipv4_tcp; } */ con->rcv.proto_reserved1=con->id; /* copy the id */ c=*req->parsed; /* ugly hack: zero term the msg & save the previous char, req->parsed should be ok because we always alloc BUF_SIZE+1 */ *req->parsed=0; /* prepare for next request */ size=req->pos-req->parsed; if (req->state==H_PING_CRLFCRLF) { /* we send the reply */ if (tcp_send( con->rcv.bind_address, con->rcv.proto,CRLF, CRLF_LEN, &(con->rcv.src_su), con->rcv.proto_reserved1) < 0) { LM_ERR("CRLF pong - tcp_send() failed\n"); } if (!size) { /* we can release the connection */ io_watch_del(&io_w, con->fd, -1, IO_FD_CLOSING); tcpconn_listrm(tcp_conn_lst, con, c_next, c_prev); if (con->state==S_CONN_EOF) release_tcpconn(con, CONN_EOF, tcpmain_sock); else release_tcpconn(con, CONN_RELEASE, tcpmain_sock); } } else { msg_buf = req->start; msg_len = req->parsed-req->start; local_rcv = con->rcv; if (!size) { /* did not read any more things - we can release the connection */ LM_DBG("We're releasing the connection in state %d \n",con->state); if (req != ¤t_req) { /* we have the buffer in the connection tied buff - * detach it , release the conn and free it afterwards */ con->con_req = NULL; } io_watch_del(&io_w, con->fd, -1, IO_FD_CLOSING); tcpconn_listrm(tcp_conn_lst, con, c_next, c_prev); /* if we have EOF, signal that to MAIN as well * otherwise - just pass it back */ if (con->state==S_CONN_EOF) release_tcpconn(con, CONN_EOF, tcpmain_sock); else release_tcpconn(con, CONN_RELEASE, tcpmain_sock); } else { LM_DBG("We still have things on the pipe - keeping connection \n"); } if (receive_msg(msg_buf, msg_len, &local_rcv) <0) LM_ERR("receive_msg failed \n"); if (req != ¤t_req) pkg_free(req); } *req->parsed=c; update_stat( pt[process_no].load, -1 ); if (size) memmove(req->buf, req->parsed, size); #ifdef EXTRA_DEBUG LM_DBG("preparing for new request, kept %ld bytes\n", size); #endif req->pos=req->buf+size; req->parsed=req->buf; req->start=req->buf; req->body=0; req->error=TCP_REQ_OK; req->state=H_SKIP_EMPTY; req->complete=req->content_len=req->has_content_len=0; req->bytes_to_go=0; con->msg_attempts = 0; /* if we still have some unparsed bytes, try to parse them too*/ if (size) goto again; } else { /* request not complete - check the if the thresholds are exceeded */ con->msg_attempts ++; if (con->msg_attempts == TCP_CHILD_MAX_MSG_CHUNK) { LM_ERR("Made %u read attempts but message is not complete yet - " "closing connection \n",con->msg_attempts); resp = CONN_ERROR; goto end_req; } if (req == ¤t_req) { /* let's duplicate this - most likely another conn will come in */ LM_DBG("We didn't manage to read a full request. Back to child poll\n"); /* FIXME - PKG or SHM ? */ con->con_req = pkg_malloc(sizeof(struct tcp_req)); if (con->con_req == NULL) { LM_ERR("No more mem for dynamic con request buffer\n"); resp = CONN_ERROR; goto end_req; } con->con_req->content_len = req->content_len; con->con_req->bytes_to_go = req->bytes_to_go; con->con_req->error = req->error; con->con_req->state = req->state; if (req->pos != req->buf) { /* we have read some bytes */ memcpy(con->con_req->buf,req->buf,req->pos-req->buf); con->con_req->pos = con->con_req->buf + (req->pos-req->buf); } else { con->con_req->pos = con->con_req->buf; } if (req->start != req->buf) con->con_req->start = con->con_req->buf + (req->start-req->buf); else con->con_req->start = con->con_req->buf; if (req->parsed != req->buf) con->con_req->parsed = con->con_req->buf + (req->parsed-req->buf); else con->con_req->parsed = con->con_req->buf; /* zero out the per process req for the future SIP msg */ init_tcp_req(¤t_req); } } LM_DBG("tcp_read_req end\n"); end_req: if (bytes_read) *bytes_read=total_bytes; return resp; }
static int tls_read_req(struct tcp_connection* con, int* bytes_read) { int ret; int bytes; int total_bytes; struct tcp_req* req; struct tls_data* data; bytes=-1; total_bytes=0; if (con->con_req) { req=con->con_req; LM_DBG("Using the per connection buff \n"); } else { LM_DBG("Using the global ( per process ) buff \n"); init_tcp_req(&tls_current_req, 0); req=&tls_current_req; } /* do this trick in order to trace whether if it's an error or not */ ret=tls_fix_read_conn(con); /* if there is pending tracing data on an accepted connection, flush it * As this is a read op, we look only for accepted conns, not to conflict * with connected conns (flushed on write op) */ if ( con->flags&F_CONN_ACCEPTED && con->proto_flags & F_TLS_TRACE_READY ) { data = con->proto_data; /* send the message if set from tls_mgm */ if ( data->message ) { send_trace_message( data->message, t_dst); data->message = NULL; } /* don't allow future traces for this connection */ data->tprot = 0; data->dest = 0; con->proto_flags &= ~( F_TLS_TRACE_READY ); } if ( ret != 0 ) { LM_ERR("failed to do pre-tls reading\n"); goto error; } if(con->state!=S_CONN_OK) goto done; /* not enough data */ again: if(req->error==TCP_REQ_OK){ /* if we still have some unparsed part, parse it first, * don't do the read*/ if (req->parsed<req->pos){ bytes=0; }else{ bytes=tls_read(con,req); if (bytes<0) { LM_ERR("failed to read \n"); goto error; } } tcp_parse_headers(req, tls_crlf_pingpong, tls_crlf_drop); #ifdef EXTRA_DEBUG /* if timeout state=0; goto end__req; */ LM_DBG("read= %d bytes, parsed=%d, state=%d, error=%d\n", bytes, (int)(req->parsed-req->start), req->state, req->error ); LM_DBG("last char=0x%02X, parsed msg=\n%.*s\n", *(req->parsed-1), (int)(req->parsed-req->start), req->start); #endif total_bytes+=bytes; /* eof check: * is EOF if eof on fd and req. not complete yet, * if req. is complete we might have a second unparsed * request after it, so postpone release_with_eof */ if ((con->state==S_CONN_EOF) && (req->complete==0)) { LM_DBG("EOF received\n"); goto done; } } if (req->error!=TCP_REQ_OK){ LM_ERR("bad request, state=%d, error=%d " "buf:\n%.*s\nparsed:\n%.*s\n", req->state, req->error, (int)(req->pos-req->buf), req->buf, (int)(req->parsed-req->start), req->start); LM_DBG("- received from: port %d\n", con->rcv.src_port); print_ip("- received from: ip ",&con->rcv.src_ip, "\n"); goto error; } switch (tcp_handle_req(req, con, tls_max_msg_chunks) ) { case 1: goto again; case -1: goto error; } LM_DBG("tls_read_req end\n"); done: if (bytes_read) *bytes_read=total_bytes; /* connection will be released */ return 0; error: /* connection will be released as ERROR */ return -1; }
static int tls_read_req(struct tcp_connection* con, int* bytes_read) { int bytes; int total_bytes; struct tcp_req* req; bytes=-1; total_bytes=0; if (con->con_req) { req=con->con_req; LM_DBG("Using the per connection buff \n"); } else { LM_DBG("Using the global ( per process ) buff \n"); init_tcp_req(&tls_current_req, 0); req=&tls_current_req; } if (tls_fix_read_conn(con)!=0) { LM_ERR("failed to do pre-tls reading\n"); goto error; } if(con->state!=S_CONN_OK) goto done; /* not enough data */ again: if(req->error==TCP_REQ_OK){ /* if we still have some unparsed part, parse it first, * don't do the read*/ if (req->parsed<req->pos){ bytes=0; }else{ bytes=tls_read(con,req); if (bytes<0) { LM_ERR("failed to read \n"); goto error; } } tcp_parse_headers(req, tls_crlf_pingpong, tls_crlf_drop); #ifdef EXTRA_DEBUG /* if timeout state=0; goto end__req; */ LM_DBG("read= %d bytes, parsed=%d, state=%d, error=%d\n", bytes, (int)(req->parsed-req->start), req->state, req->error ); LM_DBG("last char=0x%02X, parsed msg=\n%.*s\n", *(req->parsed-1), (int)(req->parsed-req->start), req->start); #endif total_bytes+=bytes; /* eof check: * is EOF if eof on fd and req. not complete yet, * if req. is complete we might have a second unparsed * request after it, so postpone release_with_eof */ if ((con->state==S_CONN_EOF) && (req->complete==0)) { LM_DBG("EOF received\n"); goto done; } } if (req->error!=TCP_REQ_OK){ LM_ERR("bad request, state=%d, error=%d " "buf:\n%.*s\nparsed:\n%.*s\n", req->state, req->error, (int)(req->pos-req->buf), req->buf, (int)(req->parsed-req->start), req->start); LM_DBG("- received from: port %d\n", con->rcv.src_port); print_ip("- received from: ip ",&con->rcv.src_ip, "\n"); goto error; } switch (tcp_handle_req(req, con, tls_max_msg_chunks) ) { case 1: goto again; case -1: goto error; } LM_DBG("tls_read_req end\n"); done: if (bytes_read) *bytes_read=total_bytes; /* connection will be released */ return 0; error: /* connection will be released as ERROR */ return -1; }