📄 sipp.cpp
字号:
(L_poll_idx) < pollnfds; (L_poll_idx)++) { if(pollfiles[L_poll_idx].fd == twinSippSocket) { pollset_remove(L_poll_idx); } if(pollfiles[L_poll_idx].fd == localTwinSippSocket) { pollset_remove(L_poll_idx); } } if(twinSippSocket) { shutdown(twinSippSocket, SHUT_RDWR); close(twinSippSocket); twinSippSocket = 0 ; } if(localTwinSippSocket) { shutdown(localTwinSippSocket, SHUT_RDWR); close(localTwinSippSocket); localTwinSippSocket = 0 ; } }#endif nb_net_recv_errors++; } } } return recv_size;}int recv_tcp_message(int sock, char *buffer, int buffer_size, E_Alter_YesNo alter_msg, E_Alter_YesNo isControlMsg = E_ALTER_NO){ int len = 0; int recv_size; char * ctl_hdr; int content_length; // Try to read SIP Header Message only // or CMD Message while(1) { // Read one char on tcp socket recv_size = recv_all_tcp(sock, &(buffer[len]), 1, 1); // Check read problem return if(recv_size <= 0) { return recv_size; } len++; // Search the end Message condition if (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, "Content-Length:"); if(!ctl_hdr) ctl_hdr = strstr(buffer, "Content-length:"); if(!ctl_hdr) ctl_hdr = strstr(buffer, "content-Length:"); if(!ctl_hdr) ctl_hdr = strstr(buffer, "content-length:"); if(!ctl_hdr) ctl_hdr = strstr(buffer, "CONTENT-LENGTH:"); // Content Length was found // Read its value if((ctl_hdr) && (alter_msg==E_ALTER_YES)) { ctl_hdr += 15; 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) { TRACE_MSG((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", 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, sizeof(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 */ if (transport == T_TCP) { int sock_opt = 1; if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void *)&sock_opt, sizeof (sock_opt)) == -1) { ERROR("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("Unable to set SO_LINGER option"); } } } /* Increase buffer sizes for this sockets */ if(setsockopt(s, SOL_SOCKET, SO_SNDBUF, &buffsize, sizeof(buffsize))) { ERROR("Unable to set socket sndbuf"); } buffsize = 65535; if(setsockopt(s, SOL_SOCKET, SO_RCVBUF, &buffsize, sizeof(buffsize))) { ERROR("Unable to set socket rcvbuf"); } } int send_message(int s, void ** comp_state, char * msg){ struct sockaddr_in *L_dest = &remote_sockaddr; if(transport == T_TCP) { int rc; rc = send_nowait(s, msg, strlen(msg), 0); #ifdef EAGAIN if(errno == EAGAIN) { nb_net_cong++; return -1; }#endif if(errno == EWOULDBLOCK) { nb_net_cong++; return -1; } if(errno == EPIPE) { nb_net_send_errors++; ERROR("Broken pipe on TCP connection, remote peer " "probably closed the socket"); } if(rc <= 0) { nb_net_send_errors++; ERROR_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 *)&remote_sockaddr, (struct sockaddr *)(void *)L_dest, sizeof(remote_sockaddr)) == -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; for((*poll_idx) = 0; (*poll_idx) < pollnfds; (*poll_idx)++) { if(pollfiles[(*poll_idx)].revents) { call * recv_call = pollcalls[(*poll_idx)]; int s = pollfiles[(*poll_idx)].fd; 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 if(transport == T_TCP) { if(s == main_socket) { /* New incoming connection */ sipp_socklen_t len = sizeof(remote_sockaddr); int new_sock = accept(s, (sockaddr *)(void *)&remote_sockaddr, &len); pollset_add(0, new_sock); return -2; } size = recv_tcp_message(s, buffer, buffer_size, E_ALTER_YES); if(size <= 0) { /* Remote side closed TCP connection */ nb_net_recv_errors++; /* Preventive cleaning */ if(size < 0) { WARNING_P2("TCP recv error on socket %d, index = %d", s, *poll_idx); ERROR_NO("TCP recv_error"); } if(recv_call) { recv_call -> call_socket = 0; if(recv_call -> pollset_index) { recv_call -> pollset_index = 0; } } pollset_remove((*poll_idx)); shutdown(s, SHUT_RDWR); close(s); return 0; } } else { /* T_UDP */ if(toolMode == MODE_SERVER) { sipp_socklen_t len = sizeof(remote_sockaddr); size = recvfrom(s, buffer, buffer_size, 0, (sockaddr *)(void *)&remote_sockaddr, &len); } else { size = recvfrom(s, buffer, buffer_size, 0, NULL, NULL); } if(size < 0) { WARNING_P3("Unexpected UDP recv error, idx = %d, " "socket = %d, recv_call = 0x%08x", (*poll_idx), s, recv_call); ERROR_NO("Unexpected UDP recv error");#if 0 nb_net_recv_errors++; pollset_remove((*poll_idx)); shutdown(s, SHUT_RDWR); close(s);#endif return 0; } if (size > 0) { size = decompress_if_needed(s, buffer, size, ((recv_call) ? (&(recv_call -> comp_state)) : &monosocket_comp_state)); } } /* else ... T_UDP */ break;#ifdef __3PCC__ }#endif } /* if(pollfiles[(*poll_idx)].revents) */ } /* for((*poll_idx)) */ buffer[size] = 0; struct timeval currentTime; GET_TIME (¤tTime); TRACE_MSG((s, "----------------------------------------------- %s\n" "%s message received [%d] bytes :\n\n%s\n", CStat::instance()->formatTime(¤tTime),
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -