/* used to tune the tcp_connection attributes - not to be used inside the network layer, but onlu from the above layer (otherwise we may end up in strange deadlocks!) */ int tcp_conn_fcntl(struct receive_info *rcv, int attr, void *value) { struct tcp_connection *con; switch (attr) { case DST_FCNTL_SET_LIFETIME: /* set connection 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 { tcp_conn_set_lifetime( con, (int)(long)(value)); } TCPCONN_UNLOCK(rcv->proto_reserved1); return 0; default: LM_ERR("unsupported operation %d on conn\n",attr); return -1; } return -1; }
static inline int hep_handle_req(struct tcp_req *req, struct tcp_connection *con, int _max_msg_chunks) { struct receive_info local_rcv; struct hep_desc h; char *msg_buf; int msg_len; long size; int ret=0; if (req->complete){ /* update the timeout - we successfully read the request */ tcp_conn_set_lifetime( con, tcp_con_lifetime); con->timeout=con->lifetime; /* 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; 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("Nothing more to read on TCP conn %p, currently in state %d \n", con,con->state); if (req != &hep_current_req) { /* we have the buffer in the connection tied buff - * detach it , release the conn and free it afterwards */ con->con_req = NULL; } /* TODO - we could indicate to the TCP net layer to release * the connection -> other worker may read the next available * message on the pipe */ } else { LM_DBG("We still have things on the pipe - " "keeping connection \n"); } if( msg_buf[0] == 'H' && msg_buf[1] == 'E' && msg_buf[2] == 'P' ) { /* HEP related */ if (unpack_hepv3(msg_buf, msg_len, &h)) { LM_ERR("failed to unpack hepV3\n"); return -1; } ret=run_hep_cbs(&h, &local_rcv); if (ret < 0) { LM_ERR("failed to run hep callbacks\n"); return -1; } msg_len = ntohs(h.u.hepv3.payload_chunk.chunk.length) - sizeof(hep_chunk_payload_t); /* remove the hep header; leave only the payload */ msg_buf = h.u.hepv3.payload_chunk.data; } /* skip receive msg if we were told so from at least one callback */ if (ret != HEP_SCRIPT_SKIP && receive_msg(msg_buf, msg_len, &local_rcv) <0) LM_ERR("receive_msg failed \n"); if (!size && req != &hep_current_req) { /* if we no longer need this tcp_req * we can free it now */ pkg_free(req); } if (size) { memmove(req->buf, req->parsed, size); req->pos = req->buf + size; /* anything that was parsed was already processed */ req->parsed = req->buf; } /* if we still have some unparsed bytes, try to parse them too*/ if (size) return 1; } else { /* request not complete - check the if the thresholds are exceeded */ if (con->msg_attempts==0) /* if first iteration, set a short timeout for reading * a whole SIP message */ con->timeout = get_ticks() + tcp_max_msg_time; con->msg_attempts ++; if (con->msg_attempts == _max_msg_chunks) { LM_ERR("Made %u read attempts but message is not complete yet - " "closing connection \n",con->msg_attempts); goto error; } if (req == &hep_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"); goto error; } 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; /* req will be reset on the next usage */ } } /* everything ok */ return 0; error: /* report error */ return -1; }
static int hep_tcp_send (struct socket_info* send_sock, char *buf, unsigned int len, union sockaddr_union *to, int id) { struct tcp_connection *c; int port=0; struct ip_addr ip; int fd, n; if (to) { su2ip_addr(&ip, to); port=su_getport(to); n = tcp_conn_get(id,&ip, port, &c, &fd); } else if (id) { n = tcp_conn_get(id, 0, 0, &c, &fd); } else { LM_CRIT("tcp_send called with null id & to\n"); return -1; } if (n < 0) { /* error during conn get, return with error too */ LM_ERR("failed to aquire connection\n"); return -1; } /* was connection found ?? */ if (c==0) { if (tcp_no_new_conn) { return -1; } if (!to) { LM_ERR("Unknown destination - cannot open new tcp connection\n"); return -1; } LM_DBG("no open tcp connection found, opening new one, async = %d\n",hep_async); /* create tcp connection */ if (hep_async) { n = tcpconn_async_connect(send_sock, to, buf, len, &c, &fd); if ( n<0 ) { LM_ERR("async TCP connect failed\n"); return -1; } /* connect succeded, we have a connection */ if (n==0) { /* connect is still in progress, break the sending * flow now (the actual write will be done when * connect will be completed */ LM_DBG("Succesfully started async connection \n"); tcp_conn_release(c, 0); return len; } /* our first connect attempt succeeded - go ahead as normal */ } else if ((c=hep_sync_connect(send_sock, to, &fd))==0) { LM_ERR("connect failed\n"); return -1; } goto send_it; } /* now we have a connection, let's see what we can do with it */ /* BE CAREFUL now as we need to release the conn before exiting !!! */ if (fd==-1) { /* connection is not writable because of its state - can we append * data to it for later writting (async writting)? */ if (c->state==S_CONN_CONNECTING) { /* 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); /* we failed due to internal errors - put the * connection back */ tcp_conn_release(c, 0); return -1; } /* we succesfully added our write chunk - success */ tcp_conn_release(c, 0); return len; } else { /* return error, nothing to do about it */ tcp_conn_release(c, 0); return -1; } } send_it: LM_DBG("sending via fd %d...\n",fd); n = _hep_write_on_socket(c, fd, buf, len); tcp_conn_set_lifetime( c, tcp_con_lifetime); 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"); c->state=S_CONN_BAD; if (c->proc_id != process_no) close(fd); tcp_conn_release(c, 0); return -1; } /* only close the FD if not already in the context of our process either we just connected, or main sent us the FD */ if (c->proc_id != process_no) close(fd); tcp_conn_release(c, (n<len)?1:0/*pending data in async mode?*/ ); return n; }
/*! \brief Finds a tcpconn & sends on it */ static int proto_ws_send(struct socket_info* send_sock, char* buf, unsigned int len, union sockaddr_union* to, int id) { struct tcp_connection *c; struct timeval get; struct ip_addr ip; int port = 0; int fd, n; reset_tcp_vars(tcpthreshold); start_expire_timer(get,tcpthreshold); if (to){ su2ip_addr(&ip, to); port=su_getport(to); n = tcp_conn_get(id, &ip, port, &c, &fd); }else if (id){ n = tcp_conn_get(id, 0, 0, &c, &fd); }else{ LM_CRIT("prot_tls_send called with null id & to\n"); get_time_difference(get,tcpthreshold,tcp_timeout_con_get); return -1; } if (n<0) { /* error during conn get, return with error too */ LM_ERR("failed to aquire connection\n"); get_time_difference(get,tcpthreshold,tcp_timeout_con_get); return -1; } /* was connection found ?? */ 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 ((c=ws_connect(send_sock, to, &fd))==0) { LM_ERR("connect failed\n"); return -1; } goto send_it; } get_time_difference(get, tcpthreshold, tcp_timeout_con_get); /* now we have a connection, let's what we can do with it */ /* BE CAREFUL now as we need to release the conn before exiting !!! */ if (fd==-1) { /* connection is not writable because of its state */ /* return error, nothing to do about it */ tcp_conn_release(c, 0); return -1; } send_it: LM_DBG("sending via fd %d...\n",fd); n = ws_req_write(c, fd, buf, len); stop_expire_timer(get, tcpthreshold, "WS ops",buf,(int)len,1); tcp_conn_set_lifetime( c, tcp_con_lifetime); LM_DBG("after write: c= %p n=%d fd=%d\n",c, n, fd); if (n<0){ LM_ERR("failed to send\n"); c->state=S_CONN_BAD; if (c->proc_id != process_no) close(fd); tcp_conn_release(c, 0); return -1; } /* only close the FD if not already in the context of our process either we just connected, or main sent us the FD */ if (c->proc_id != process_no) close(fd); tcp_conn_release(c, 0); return n; }
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; }
int ws_process(struct tcp_connection *con) { struct ws_req *req; struct ws_req *newreq; long size = 0; enum ws_close_code ret_code = WS_ERR_NONE; unsigned char bk; char *msg_buf; int msg_len; struct receive_info local_rcv; if (con->con_req) { req=(struct ws_req *)con->con_req; LM_DBG("Using the per connection buff \n"); } else { LM_DBG("Using the global ( per process ) buff \n"); init_ws_req(&ws_current_req, 0); req=&ws_current_req; } again: if (req->tcp.error == TCP_REQ_OK) { if (req->tcp.parsed >= req->tcp.pos) { if (ws_raw_read(con, &req->tcp) < 0) { LM_ERR("failed to read %d:%s\n", errno, strerror(errno)); goto error; } } ret_code = ws_parse(req); if (ret_code) goto error; /* eof check: * is EOF if eof on fd and r. not complete yet, * if r. is complete we might have a second unparsed * request after it, so postpone release_with_eof */ if ((con->state==S_CONN_EOF) && (req->tcp.complete==0)) { LM_DBG("EOF received\n"); goto done; } /* sanity mask checks */ if ((WS_TYPE(con) == WS_CLIENT && req->is_masked) || (WS_TYPE(con) == WS_SERVER && !req->is_masked)) { LM_DBG("malformed WS msg - %s %s\n", req->is_masked ? "masked" : "not masked", WS_TYPE(con) == WS_CLIENT ? "client" : "server"); ret_code = WS_ERR_BADDATA; goto error; } } if (req->tcp.complete) { /* update the timeout - we succesfully read the request */ tcp_conn_set_lifetime(con, ws_send_timeout); con->timeout=con->lifetime; /* if we are here everything is nice and ok*/ update_stat( pt[process_no].load, +1 ); /* rcv.bind_address should always be !=0 */ bind_address=con->rcv.bind_address; con->rcv.proto_reserved1=con->id; /* copy the id */ size=req->tcp.pos-req->tcp.parsed; switch (req->op) { case WS_OP_CLOSE: if (req->tcp.content_len) { /* for now we are only interested in the code, not the reason */ ret_code = WS_CLOSE_CODE(req); switch(ret_code) { case WS_ERR_NORMAL: LM_DBG("Normal WebSocket close\n"); break; case WS_ERR_CLIENT: LM_DBG("Client error close\n"); break; case WS_ERR_PROTO: LM_DBG("WebSocket protocol error\n"); break; case WS_ERR_BADDATA: LM_DBG("Data type not consistent\n"); break; case WS_ERR_POLICY: LM_DBG("Bad policy close\n"); break; case WS_ERR_TOO_BIG: LM_DBG("Packet too big close\n"); break; case WS_ERR_BADEXT: LM_DBG("Bad extension close\n"); break; case WS_ERR_UNEXPECT: LM_DBG("Unexpected condition close\n"); break; default: LM_DBG("Unknown WebSocket close: %d\n", ret_code); } } else { ret_code = WS_ERR_NORMAL; } /* respond to close */ WS_CODE(con) = ret_code; ws_send_close(con); WS_CODE(con) = WS_ERR_NOSEND; /* release the connextion */ con->state = S_CONN_EOF; goto done; case WS_OP_PING: if (ws_send_pong(con, req) < 0) LM_ERR("cannot send PONG msg\n"); break; case WS_OP_PONG: LM_DBG("Received WebSocket PONG\n"); break; case WS_OP_TEXT: case WS_OP_BIN: bk = *req->tcp.parsed; *req->tcp.parsed = 0; msg_buf = req->tcp.body; msg_len = req->tcp.parsed-req->tcp.body; 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 != &ws_current_req) { /* we have the buffer in the connection tied buff - * detach it , release the conn and free it afterwards */ con->con_req = NULL; } /* TODO - we could indicate to the TCP net layer to release * the connection -> other worker may read the next available * message on the pipe */ } 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"); *req->tcp.parsed = bk; break; default: LM_BUG("Can't handle %d\n", req->op); goto error; } update_stat( pt[process_no].load, -1 ); if (size) memmove(req->tcp.buf, req->tcp.parsed, size); #ifdef EXTRA_DEBUG LM_DBG("preparing for new request, kept %ld bytes\n", size); #endif init_ws_req(req, size); con->msg_attempts = 0; /* if we still have some unparsed bytes, try to parse them too*/ if (size) goto again; /* cleanup the existing request */ if (req != &ws_current_req) pkg_free(req); } else { /* request not complete - check the if the thresholds are exceeded */ con->msg_attempts++; if (con->msg_attempts == ws_max_msg_chunks) { LM_ERR("Made %u read attempts but message is not complete yet - " "closing connection \n",con->msg_attempts); goto error; } if (req == &ws_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"); newreq = pkg_malloc(sizeof(struct ws_req)); if (newreq == NULL) { LM_ERR("No more mem for dynamic con request buffer\n"); goto error; } if (req->tcp.pos != req->tcp.buf) { /* we have read some bytes */ memcpy(newreq->tcp.buf,req->tcp.buf,req->tcp.pos-req->tcp.buf); newreq->tcp.pos = newreq->tcp.buf + (req->tcp.pos-req->tcp.buf); } else { newreq->tcp.pos = newreq->tcp.buf; } if (req->tcp.start != req->tcp.buf) newreq->tcp.start = newreq->tcp.buf +(req->tcp.start-req->tcp.buf); else newreq->tcp.start = newreq->tcp.buf; if (req->tcp.parsed != req->tcp.buf) newreq->tcp.parsed =newreq->tcp.buf+(req->tcp.parsed-req->tcp.buf); else newreq->tcp.parsed = newreq->tcp.buf; if (req->tcp.body != 0) { newreq->tcp.body = newreq->tcp.buf + (req->tcp.body-req->tcp.buf); } else newreq->tcp.body = 0; newreq->tcp.complete=req->tcp.complete; newreq->tcp.has_content_len=req->tcp.has_content_len; newreq->tcp.content_len=req->tcp.content_len; newreq->tcp.bytes_to_go=req->tcp.bytes_to_go; newreq->tcp.error = req->tcp.error; newreq->tcp.state = req->tcp.state; newreq->op = req->op; newreq->mask = req->mask; newreq->is_masked = req->is_masked; con->con_req = (struct tcp_req *)newreq; } } LM_DBG("ws_read end\n"); done: /* connection will be released */ return size; error: WS_CODE(con) = ret_code; if (WS_CODE(con) != WS_ERR_NONE) { ws_send_close(con); WS_CODE(con) = WS_ERR_NOSEND; } return -1; }
static int proto_tls_send(struct socket_info* send_sock, char* buf, unsigned int len, union sockaddr_union* to, int id) { struct tcp_connection *c; struct ip_addr ip; int port; int fd, n; struct tls_data* data; if (to){ su2ip_addr(&ip, to); port=su_getport(to); n = tcp_conn_get(id, &ip, port, PROTO_TLS, &c, &fd); }else if (id){ n = tcp_conn_get(id, 0, 0, PROTO_NONE, &c, &fd); }else{ LM_CRIT("prot_tls_send called with null id & to\n"); return -1; } if (n<0) { /* error during conn get, return with error too */ LM_ERR("failed to acquire connection\n"); return -1; } /* was connection found ?? */ 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 ((c=tls_sync_connect(send_sock, to, &fd))==0) { LM_ERR("connect failed\n"); return -1; } goto send_it; } /* now we have a connection, let's what we can do with it */ /* BE CAREFUL now as we need to release the conn before exiting !!! */ if (fd==-1) { /* connection is not writable because of its state */ /* return error, nothing to do about it */ tcp_conn_release(c, 0); return -1; } send_it: /* if there is pending tracing data on a connection startet by us * (connected) -> flush it * As this is a write op, we look only for connected conns, not to conflict * with accepted conns (flushed on read op) */ if ( (c->flags&F_CONN_ACCEPTED)==0 && c->proto_flags & F_TLS_TRACE_READY ) { data = c->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; c->proto_flags &= ~( F_TLS_TRACE_READY ); } LM_DBG("sending via fd %d...\n",fd); lock_get(&c->write_lock); n = tls_blocking_write(c, fd, buf, len, &tls_mgm_api); lock_release(&c->write_lock); tcp_conn_set_lifetime( c, tcp_con_lifetime); 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"); c->state=S_CONN_BAD; if (c->proc_id != process_no) close(fd); tcp_conn_release(c, 0); return -1; } /* only close the FD if not already in the context of our process either we just connected, or main sent us the FD */ if (c->proc_id != process_no) close(fd); /* mark the ID of the used connection (tracing purposes) */ last_outgoing_tcp_id = c->id; tcp_conn_release(c, 0); return n; }
/*! \brief Finds a tcpconn & sends on it */ static int proto_wss_send(struct socket_info* send_sock, char* buf, unsigned int len, union sockaddr_union* to, int id) { struct tcp_connection *c; struct timeval get; struct ip_addr ip; int port = 0; int fd, n; struct ws_data* d; reset_tcp_vars(tcpthreshold); start_expire_timer(get,tcpthreshold); if (to){ su2ip_addr(&ip, to); port=su_getport(to); n = tcp_conn_get(id, &ip, port, PROTO_WSS, &c, &fd); }else if (id){ n = tcp_conn_get(id, 0, 0, PROTO_NONE, &c, &fd); }else{ LM_CRIT("prot_tls_send called with null id & to\n"); get_time_difference(get,tcpthreshold,tcp_timeout_con_get); return -1; } if (n<0) { /* error during conn get, return with error too */ LM_ERR("failed to acquire connection\n"); get_time_difference(get,tcpthreshold,tcp_timeout_con_get); return -1; } /* was connection found ?? */ if (c==0) { if (tcp_no_new_conn) { return -1; } if (!to) { LM_ERR("Unknown destination - cannot open new tcp connection\n"); return -1; } LM_DBG("no open tcp connection found, opening new one\n"); /* create tcp connection */ if ((c=ws_connect(send_sock, to, &fd))==0) { LM_ERR("connect failed\n"); return -1; } goto send_it; } get_time_difference(get, tcpthreshold, tcp_timeout_con_get); /* now we have a connection, let's what we can do with it */ /* BE CAREFUL now as we need to release the conn before exiting !!! */ if (fd==-1) { /* connection is not writable because of its state */ /* return error, nothing to do about it */ tcp_conn_release(c, 0); return -1; } send_it: LM_DBG("sending via fd %d...\n",fd); n = ws_req_write(c, fd, buf, len); stop_expire_timer(get, tcpthreshold, "WSS ops",buf,(int)len,1); tcp_conn_set_lifetime( c, tcp_con_lifetime); /* only here we will have all tracing data TLS + WS */ d = c->proto_data; if ( (c->flags&F_CONN_ACCEPTED)==0 && d && d->dest && d->tprot ) { if ( d->message ) { send_trace_message( d->message, t_dst); d->message = NULL; } /* don't allow future traces for this cnection */ d->tprot = 0; d->dest = 0; } LM_DBG("after write: c= %p n=%d fd=%d\n",c, n, fd); if (n<0){ LM_ERR("failed to send\n"); c->state=S_CONN_BAD; if (c->proc_id != process_no) close(fd); tcp_conn_release(c, 0); return -1; } /* only close the FD if not already in the context of our process either we just connected, or main sent us the FD */ if (c->proc_id != process_no) close(fd); /* mark the ID of the used connection (tracing purposes) */ last_outgoing_tcp_id = c->id; tcp_conn_release(c, 0); return n; }