📄 sipp.cpp
字号:
// Check read problem return if(recv_size <= 0) { return recv_size; } len++; // Search the end Message condition if ((len > 3) && (isControlMsg == E_ALTER_NO)) { // In case of SIP Message \r\n follow by // \r\n is header end if((buffer[len-1] == '\n') && (buffer[len-2] == '\r') && (buffer[len-3] == '\n') && (buffer[len-4] == '\r')) { /* CRLF CRLF Detected */ buffer[len] = 0; break; } } else { // In case of CMD Message // escape char is the end of message if((alter_msg==E_ALTER_NO) && (buffer[len-1] == 27)) { /* End delimitor detected, stop receiving */ buffer[len-1] = 0; return (len - 1); } } } if(len >= buffer_size) { ERROR("TCP msg too big"); } // Now search the content length of the body // part of SIP or CMD Message ctl_hdr = strstr(buffer, "\r\nContent-Length:"); if(!ctl_hdr) {ctl_hdr = strstr(buffer, "\r\nContent-length:"); } if(!ctl_hdr) {ctl_hdr = strstr(buffer, "\r\ncontent-Length:"); } if(!ctl_hdr) {ctl_hdr = strstr(buffer, "\r\ncontent-length:"); } if(!ctl_hdr) {ctl_hdr = strstr(buffer, "\r\nCONTENT-LENGTH:"); } if(!ctl_hdr) {ctl_hdr = strstr(buffer, "\r\nl:"); short_form = true; } // Content Length was found // Read its value if((ctl_hdr) && (alter_msg==E_ALTER_YES)) { if (short_form) { ctl_hdr += 4; } else { ctl_hdr += 17; } content_length = atoi(ctl_hdr); } else { content_length = 0; } // If a body exist read it if(content_length) { /* Ensure remaining content will fit in remaining buffer size */ if(content_length > (buffer_size - len)) { ERROR("TCP msg too big"); } // Read Body part do { recv_size = recv_all_tcp(sock, &(buffer[len]), content_length, 2); if(recv_size <= 0) { return recv_size; } len += recv_size; content_length -= recv_size; } while(content_length); } // Add the final '\0' buffer[len] = 0; return len;}int decompress_if_needed(int sock, char *buff, int len, void **st){ if(compression && len) { if (useMessagef == 1) { struct timeval currentTime; GET_TIME (¤tTime); TRACE_MSG((s, "----------------------------------------------- %s\n" "Compressed message received, header :\n" "0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x " "0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n", CStat::instance()->formatTime(¤tTime), buff[0] , buff[1] , buff[2] , buff[3], buff[4] , buff[5] , buff[6] , buff[7], buff[8] , buff[9] , buff[10], buff[11], buff[12], buff[13], buff[14], buff[15])); } int rc = comp_uncompress(st, buff, (unsigned int *)&len); switch(rc) { case COMP_OK: TRACE_MSG((s,"Compressed message decompressed properly.\n")); break; case COMP_REPLY: TRACE_MSG((s, "Compressed message KO, sending a reply (resynch).\n")); sendto(sock, buff, len, 0, (sockaddr *)(void *)&remote_sockaddr, SOCK_ADDR_SIZE(&remote_sockaddr)); resynch_send++; return 0; case COMP_DISCARD: TRACE_MSG((s, "Compressed message discarded by pluggin.\n")); resynch_recv++; return 0; default: case COMP_KO: ERROR("Compression pluggin error"); return 0; } } return len;}void sipp_customize_socket(int s){ unsigned int buffsize = 65535; /* Allows fast TCP reuse of the socket */#ifdef _USE_OPENSSL if (transport == T_TCP || transport == T_TLS ) { #else if (transport == T_TCP) { #endif int sock_opt = 1; if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void *)&sock_opt, sizeof (sock_opt)) == -1) { ERROR_NO("setsockopt(SO_REUSEADDR) failed"); }#ifndef SOL_TCP#define SOL_TCP 6#endif if (setsockopt (s, SOL_TCP, TCP_NODELAY, (void *)&sock_opt, sizeof (sock_opt)) == -1) { { ERROR_NO("setsockopt(TCP_NODELAY) failed"); } } { struct linger linger; linger.l_onoff = 1; linger.l_linger = 1; if (setsockopt (s, SOL_SOCKET, SO_LINGER, &linger, sizeof (linger)) < 0) { ERROR_NO("Unable to set SO_LINGER option"); } } } /* Increase buffer sizes for this sockets */ if(setsockopt(s, SOL_SOCKET, SO_SNDBUF, &buffsize, sizeof(buffsize))) { ERROR_NO("Unable to set socket sndbuf"); } buffsize = 65535; if(setsockopt(s, SOL_SOCKET, SO_RCVBUF, &buffsize, sizeof(buffsize))) { ERROR_NO("Unable to set socket rcvbuf"); } } #ifdef _USE_OPENSSLint send_message_tls(SSL *ssl, void ** comp_state, char * msg){ int rc; rc = send_nowait_tls(ssl, msg, strlen(msg), 0); if(rc == 0) { nb_net_send_errors++; WARNING_NO("Unable to send TLS message"); return -2; } return rc;}#endifint send_message(int s, void ** comp_state, char * msg){ struct sockaddr_storage *L_dest = &remote_sockaddr; if(transport == T_TCP) { int rc; rc = send_nowait(s, msg, strlen(msg), 0); if (rc >= 0 && rc != strlen(msg)) { int idx; /* Truncated message sent ... we need to store pending msg */ for(idx = 0; idx < pollnfds; idx++) { if (pollfiles[idx].fd == s) { pending_msg[idx] = strdup(msg+rc); return 0; } } } if(rc <= 0) { TRACE_MSG((s,"Error send\n")); #ifdef EAGAIN if((ctrlEWGlobal == false) && (errno == EAGAIN)) { int L_idx ; TRACE_MSG((s,"problem EAGAIN \n")); if (multisocket) { char * L_call_id; call * L_call_ptr; L_call_id = get_call_id(msg); L_call_ptr = get_call(L_call_id); L_call_ptr -> poll_flag_write = true ; } else { ctrlEW = true ; } ctrlEWGlobal = true ; for(L_idx = 0; L_idx < pollnfds; L_idx++) { if (pollfiles[L_idx].fd == s) { TRACE_MSG((s,"problem EAGAIN on socket %d and poll_idx is %d \n", pollfiles[L_idx].fd, L_idx)); pollfiles[L_idx].events = POLLOUT ; } } nb_net_cong++; return 0; }#endif if((ctrlEWGlobal == false) && (errno == EWOULDBLOCK)) { int L_idx ; TRACE_MSG((s,"problem EWOULDBLOCK \n")); if (multisocket) { char * L_call_id; call * L_call_ptr; L_call_id = get_call_id(msg); L_call_ptr = get_call(L_call_id); L_call_ptr -> poll_flag_write = true ; } else { ctrlEW = true ; } ctrlEWGlobal = true ; for(L_idx = 0; L_idx < pollnfds; L_idx++) { if (pollfiles[L_idx].fd == s) { TRACE_MSG((s,"problem EWOULDBLOCK on socket %d and poll_idx is %d \n", pollfiles[L_idx].fd, L_idx)); pollfiles[L_idx].events = POLLOUT ; } } nb_net_cong++; return 0; } if(errno == EPIPE) { nb_net_send_errors++; if (reset_number > 0) { WARNING("Broken pipe on TCP connection, remote peer " "probably closed the socket"); start_calls = 1; return -2; } else { ERROR("Broken pipe on TCP connection, remote peer " "probably closed the socket"); } } nb_net_send_errors++; WARNING_NO("Unable to send TCP message"); return -2; } } else { /* UDP */ unsigned int len = strlen(msg); if(compression) { static char comp_msg[SIPP_MAX_MSG_SIZE]; strcpy(comp_msg, msg); if(comp_compress(comp_state, comp_msg, &len) != COMP_OK) { ERROR("Compression pluggin error"); } msg = (char *)comp_msg; TRACE_MSG((s, "---\nCompressed message len: %d\n", len)); } // different remote sending address from received if (use_remote_sending_addr) { L_dest = &remote_sending_sockaddr ; } if(sendto(s, msg, len, 0, (struct sockaddr *)(void *)L_dest, SOCK_ADDR_SIZE(L_dest)) == -1) { nb_net_send_errors++; ERROR_NO("Unable to send UDP message"); return -2; } } return 0;}/****************************** Network Interface *******************/int recv_message(char * buffer, int buffer_size, int * poll_idx){ int size = 0;#ifdef _USE_OPENSSL BIO *bio; SSL *ssl;#endif int err; for((*poll_idx) = 0; (*poll_idx) < pollnfds; (*poll_idx)++) { if((pollfiles[(*poll_idx)].revents & POLLOUT) != 0 ) { TRACE_MSG((s,"exit problem event %d on socket %d \n", pollfiles[(*poll_idx)].revents,pollfiles[(*poll_idx)].fd)); if (multisocket) { call * L_recv_call = pollcalls[(*poll_idx)]; if(L_recv_call) { L_recv_call -> poll_flag_write = false ; TRACE_MSG((s,"exit problem EAGAIN on socket %d \n", L_recv_call -> call_socket)); } } else { ctrlEW = false ; } if (pending_msg[(*poll_idx)] != NULL) { char * VP_tmp = strdup(pending_msg[(*poll_idx)]); free(pending_msg[(*poll_idx)]); pending_msg[(*poll_idx)] = NULL; send_message(pollfiles[(*poll_idx)].fd, NULL, VP_tmp); free(VP_tmp); } pollfiles[(*poll_idx)].revents = 0; pollfiles[(*poll_idx)].events = POLLIN | POLLERR; return 0 ; } else { if((pollfiles[(*poll_idx)].revents & POLLIN) != 0) { call * recv_call = pollcalls[(*poll_idx)]; int s = pollfiles[(*poll_idx)].fd; int ss = s; pollfiles[(*poll_idx)].revents = 0;#ifdef __3PCC__ if(s == localTwinSippSocket) { sipp_socklen_t len = sizeof(twinSipp_sockaddr); twinSippSocket = accept(s, (sockaddr *)(void *)&twinSipp_sockaddr, &len); pollset_add(0, twinSippSocket); return(-2); } else if (s == twinSippSocket) { size = recv_tcp_message(s, buffer, buffer_size, E_ALTER_NO, E_ALTER_YES); if(size >= 0) { buffer[size] = 0; } else buffer[0] = 0; return size; } else {#endif #ifdef _USE_OPENSSL if(transport == T_TCP || transport == T_TLS ) {#else if(transport == T_TCP ) {#endif if(s == main_socket) { /* New incoming connection */ sipp_socklen_t len = SOCK_ADDR_SIZE(&remote_sockaddr); int new_sock = accept(s, (sockaddr *)(void *)&remote_sockaddr, &len); #ifdef _USE_OPENSSL if (transport == T_TLS ) { /* Create a SSL object */ if (!(ssl = SSL_new(sip_trp_ssl_ctx))){ ERROR("Unable to create new SSL context recv_message: Fail SSL_new\n"); } // if ( (bio = BIO_new_socket(new_sock,BIO_NOCLOSE)) == NULL) { if ( (bio = BIO_new_socket(new_sock,BIO_CLOSE)) == NULL) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -