/* Responsible for reading the request * * if returns >= 0 : the connection will be released * * if returns < 0 : the connection will be released as BAD / broken */ static int wss_read_req(struct tcp_connection* con, int* bytes_read) { int size; /* we need to fix the SSL connection before doing anything */ if (tls_fix_read_conn(con) < 0) { LM_ERR("cannot fix read connection\n"); goto error; } if (WS_STATE(con) != WS_CON_HANDSHAKE_DONE) { size = ws_server_handshake(con); if (size < 0) { LM_ERR("cannot complete WebSocket handshake\n"); goto error; } if (size == 0) goto done; } if (WS_STATE(con) == WS_CON_HANDSHAKE_DONE && ws_process(con) < 0) goto error; done: return 0; error: /* connection will be released as 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; }
int tcp_read_req(struct tcp_connection* con, int* bytes_read) { int bytes; int total_bytes; int resp; long size; struct tcp_req* req; /* int s; */ char c; bytes=-1; total_bytes=0; resp=CONN_RELEASE; /* s=con->fd; */ req=&con->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); #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; } /* 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; if (req->state==H_PING_CRLFCRLF) { 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"); } } else if (receive_msg(req->start, req->parsed-req->start, &con->rcv)<0) { *req->parsed=c; resp=CONN_ERROR; update_stat( pt[process_no].load, -1 ); goto end_req; } *req->parsed=c; update_stat( pt[process_no].load, -1 ); /* prepare for next request */ size=req->pos-req->parsed; 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; /* if we still have some unparsed bytes, try to parse them too*/ if (size) goto again; else if (con->state==S_CONN_EOF){ LM_DBG("EOF after reading complete request\n"); resp=CONN_EOF; } } end_req: if (bytes_read) *bytes_read=total_bytes; return resp; }
/* Responsible for reading the request * * if returns >= 0 : the connection will be released * * if returns < 0 : the connection will be released as BAD / broken */ static int wss_read_req(struct tcp_connection* con, int* bytes_read) { int size; struct ws_data* d; /* we need to fix the SSL connection before doing anything */ if (tls_fix_read_conn(con) < 0) { LM_ERR("cannot fix read connection\n"); if ( (d=con->proto_data) && d->dest && d->tprot ) { if ( d->message ) { send_trace_message( d->message, t_dst); d->message = NULL; /* don't allow future traces for this connection */ d->tprot = 0; d->dest = 0; } } goto error; } d=con->proto_data; if (WS_STATE(con) != WS_CON_HANDSHAKE_DONE) { size = ws_server_handshake(con); if (size < 0) { LM_ERR("cannot complete WebSocket handshake\n"); goto error; } d = con->proto_data; /* there is a corner case when the TLS handhskae is traced * but the connection is closed with * EOF before reaching this code if the certificate is not * validated by the client */ if ( con->flags&F_CONN_ACCEPTED && (WS_STATE(con)==WS_CON_HANDSHAKE_DONE || con->state==S_CONN_EOF) && 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 connection */ d->tprot = 0; d->dest = 0; } if (size == 0) goto done; } if (WS_STATE(con) == WS_CON_HANDSHAKE_DONE && ws_process(con) < 0) goto error; done: 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; }