/** * @brief Send a TCP segment to libntoh */ void send_tcp_segment ( struct ip *iphdr , pntoh_tcp_callback_t callback ) { ppeer_info_t pinfo; ntoh_tcp_tuple5_t tcpt5; pntoh_tcp_stream_t stream; struct tcphdr *tcp; size_t size_ip; size_t total_len; size_t size_tcp; size_t size_payload; unsigned char *payload; int ret; unsigned int error; size_ip = iphdr->ip_hl * 4; total_len = ntohs( iphdr->ip_len ); tcp = (struct tcphdr*)((unsigned char*)iphdr + size_ip); if ( (size_tcp = tcp->th_off * 4) < sizeof(struct tcphdr) ) return; payload = (unsigned char *)iphdr + size_ip + size_tcp; size_payload = total_len - ( size_ip + size_tcp ); ntoh_tcp_get_tuple5 ( (void*)iphdr , tcp , &tcpt5 ); /* find the stream or creates a new one */ if ( !( stream = ntoh_tcp_find_stream( tcp_session , &tcpt5 ) ) ) if ( ! ( stream = ntoh_tcp_new_stream( tcp_session , &tcpt5, callback , 0 , &error , 1 , 1 ) ) ) { fprintf ( stderr , "\n[e] Error %d creating new stream: %s" , error , ntoh_get_errdesc ( error ) ); return; } if ( size_payload > 0 ) pinfo = get_peer_info ( payload , size_payload , &tcpt5 ); else pinfo = 0; /* add this segment to the stream */ switch ( ( ret = ntoh_tcp_add_segment( tcp_session , stream, (void*)iphdr, total_len, (void*)pinfo ) ) ) { case NTOH_OK: break; case NTOH_SYNCHRONIZING: free_peer_info ( pinfo ); break; default: fprintf( stderr, "\n[e] Error %d adding segment: %s", ret, ntoh_get_retval_desc( ret ) ); free_peer_info ( pinfo ); break; } return; }
int validate() { if (!gUserPassword) { fprintf(stderr, "You'll need to specify a password, see the help for details\n"); return -1; } if (get_peer_info(gServerAddress, &gServerStruct)) { fprintf(stderr, "Server's ADDR:PORT pair '%s' seems to be invalid\n", gServerAddress); return -1; } return 0; }
int main(int argc, char **argv) { int i; char c; int maxfd; fd_set rset; fd_set allset; int num_ready_fds; struct timeval *time_out; struct timeval tv; bzero(&log_file_name, MAX_FILE_NAME_LEN); log_flag = 0; bzero(&room_file_name, MAX_FILE_NAME_LEN); time_out = (struct timeval *)NULL; sweep_int = 0; /* process arguments */ while((c = getopt(argc, argv, optstr)) != -1){ switch(c) { case 't': server_tcp_port = atoi(optarg); break; case 'u': server_udp_port = atoi(optarg); break; case 'f': strncpy(log_file_name, optarg, MAX_FILE_NAME_LEN); break; case 's': sweep_int = atoi(optarg); sweep_int *= 60; /* convert to seconds */ tv.tv_sec = sweep_int; tv.tv_usec = 0; if(sweep_int != 0) time_out = &tv; break; case 'r': strncpy(room_file_name, optarg, MAX_FILE_NAME_LEN); break; default: printf("invalid option\n"); break; } } if(server_tcp_port == 0 || server_udp_port == 0) { usage(argv); } if(log_file_name[0] != 0 ) { log_flag = 1; if( (logfp = fopen(log_file_name, "a+")) == NULL) { perror("fopen");; exit(1); } } /* initialize tcp and udp server; create rooms if config file present */ init_server(); /* usual preparation stuff for select() */ FD_ZERO(&allset); FD_SET(tcp_socket_fd, &allset); FD_SET(udp_socket_fd, &allset); maxfd = ((udp_socket_fd > tcp_socket_fd) ? udp_socket_fd : tcp_socket_fd); /* * server sits in an infinite loop waiting for events * * three things can happen: * 1. chat client connects to server through tcp and sends * control messages; * 2. chat client sends chat messages through udp * 3. server times out periodically to remove dormant/crashed * clients and rooms that do not have a member * if time_out == NULL, server will not time out, so 3. won't happen */ for( ; ; ) { rset = allset; if((num_ready_fds = select(maxfd+1, &rset, NULL, NULL, time_out)) < 0) { perror("select"); exit(1); } if(num_ready_fds <=0 ) { /* due to time out */ struct member_type *mt; struct room_type *rt; /* go through the member list and sweep members that are there for more than 1 sweep interval without messages */ mt = mem_list_hd; while(mt != NULL) { struct member_type *tmp_mt; tmp_mt=mt->next_member; if(mt->quiet_flag == 0) { /* active in the last time interval */ mt->quiet_flag ++ ; } else { /* remove this member */ remove_member(mt); if(log_flag) { char *tp; now = time(NULL); tp = ctime(&now); tp[strlen(tp)-1] = '\0'; fprintf(logfp, "%s member [%s] is removed from the session\n", tp, mt->member_name); fprintf(logfp, "Total number of members:%d\n", total_num_of_members); fflush(logfp); } free(mt); } mt = tmp_mt; } /* go through room list */ rt = room_list_hd; while(rt != NULL) { struct room_type *tmp_rt; tmp_rt = rt->next_room; if(rt->num_of_members == 0) { if(rt->empty_flag == 0 ) { rt->empty_flag ++; } else { /* remove this room */ remove_room(rt); total_num_of_rooms --; /* need to log this info */ if(log_flag) { char *tp; now = time(NULL); tp = ctime(&now); tp[strlen(tp)-1] = '\0'; fprintf(logfp, "%s room [%s] is removed from the session\n", tp, rt->room_name); fprintf(logfp, "Total number of rooms:%d\n", total_num_of_rooms); fflush(logfp); } free(rt); } } rt = tmp_rt; } /* reset the timer here */ if(sweep_int != 0) { tv.tv_sec = sweep_int; tv.tv_usec = 0; time_out = &tv; } continue; } if(FD_ISSET(udp_socket_fd, &rset)) { /* * message arrives at the udp server port * --> chat message */ process_chat_msg(udp_socket_fd); /* no more descriptors are ready, we go back to wait */ if( --num_ready_fds <= 0) continue; } if(FD_ISSET(tcp_socket_fd, &rset)) { /* * a request to set up tcp connection for control messages */ struct sockaddr_in client_addr; socklen_t client_addr_len = sizeof(struct sockaddr_in); int connect_fd; if( (connect_fd = accept(tcp_socket_fd, (struct sockaddr *)&client_addr, &client_addr_len)) < 0 ) { perror("accept"); } else { /* we accepted a new connection */ if(log_flag) { get_peer_info(connect_fd, info_str); fprintf(logfp, "%s connects successfully\n", info_str); fflush(logfp); } /* find a place in fd_table[] to store the accepted fd */ for(i=0; i< MAX_CONTROL_SESSIONS; i++) { if(fd_table[i] == -1){ fd_table[i] = connect_fd; break; } } if(i == MAX_CONTROL_SESSIONS) { if(log_flag) { fprintf(logfp, "too many connections\n"); fflush(logfp); } close(connect_fd); } if(i > max_fd_idx) max_fd_idx = i; if(connect_fd > maxfd) maxfd = connect_fd; FD_SET(connect_fd, &allset); } if( --num_ready_fds <= 0) continue; } /* * check which descriptor has data to read, and process * the control message */ /* tmp_fd_idx is used to denote the second highest fd number: * it is useful in the case that the max_fd_idx is removed. */ int tmp_fd_idx = -1; for(i=0; i<= max_fd_idx; i++) { if(fd_table[i] == -1) continue; if(FD_ISSET(fd_table[i], &rset)) { process_control_msg(fd_table[i]); /* * close connection after processing since * the semantics are that a connection is only * good for one control message */ close(fd_table[i]); FD_CLR(fd_table[i], &allset); fd_table[i] = -1; if( --num_ready_fds <=0 ) break; } else { tmp_fd_idx = i; } } if(i == max_fd_idx) max_fd_idx = tmp_fd_idx; } return 0; }
void torrent_handle::get_peer_info(std::vector<peer_info>& v) const { INVARIANT_CHECK; TORRENT_FORWARD(get_peer_info(v)); }
callable_obj_t *create_new_call (callable_type_t type, call_state_t state, const gchar* const callID, const gchar* const accountID, const gchar* const peer_name, const gchar* const peer_number) { GError *err1 = NULL ; callable_obj_t *obj; DEBUG ("CallableObj: Create new call"); DEBUG ("CallableObj: Account: %s", accountID); // Allocate memory obj = g_new0 (callable_obj_t, 1); obj->_error_dialogs = g_ptr_array_new(); // Set fields obj->_type = type; obj->_state = state; obj->_state_code = 0; obj->_state_code_description = NULL; if (g_strcasecmp (callID, "") == 0) { obj->_callID = g_new0 (gchar, 30); if (obj->_callID) g_sprintf (obj->_callID, "%d", rand()); } else obj->_callID = g_strdup (callID); obj->_confID = NULL; obj->_historyConfID = NULL; obj->_accountID = g_strdup (accountID); set_timestamp (& (obj->_time_start)); set_timestamp (& (obj->_time_current)); set_timestamp (& (obj->_time_stop)); obj->_srtp_cipher = NULL; obj->_sas = NULL; obj->_peer_name = g_strdup (peer_name); obj->_peer_number = g_strdup (peer_number); obj->_trsft_to = NULL; obj->_peer_info = get_peer_info (peer_name, peer_number); obj->_audio_codec = NULL; obj->_recordfile = NULL; obj->_record_is_playing = FALSE; obj->clockStarted = 1; if (obj->_type == CALL) { // pthread_create(&(obj->tid), NULL, threaded_clock_incrementer, obj); if ( (obj->tid = g_thread_create ( (GThreadFunc) threaded_clock_incrementer, (void *) obj, TRUE, &err1)) == NULL) { DEBUG ("Thread creation failed!"); g_error_free (err1) ; } } obj->_time_added = 0; return obj; }
/** * @brief Send a TCP segment to libntoh */ void send_tcp_segment ( struct ip *iphdr , pntoh_tcp_callback_t callback ) { ppeer_info_t pinfo; ntoh_tcp_tuple5_t tcpt5; pntoh_tcp_stream_t stream; struct tcphdr *tcp; size_t size_ip; size_t total_len; size_t size_tcp; size_t size_payload; unsigned char *payload; int32_t ret; unsigned int error; size_ip = iphdr->ip_hl * 4; total_len = ntohs( iphdr->ip_len ); tcp = (struct tcphdr*)((unsigned char*)iphdr + size_ip); if ( (size_tcp = tcp->th_off * 4) < sizeof(struct tcphdr) ) { return; } payload = (unsigned char *)iphdr + size_ip + size_tcp; size_payload = total_len - ( size_ip + size_tcp ); ntoh_tcp_get_tuple5 ( iphdr , tcp , &tcpt5 ); /* find the stream or creates a new one */ if ( !( stream = ntoh_tcp_find_stream( tcp_session , &tcpt5 ) ) ) { if ( ! ( stream = ntoh_tcp_new_stream( tcp_session , &tcpt5, callback , 0 , &error , 1 , 1 ) ) ) { if (DEBUG) { fprintf ( stderr , "\n[e] Error %d creating new stream: %s" , error , ntoh_get_errdesc ( error ) ); } return; } } if ( size_payload > 0 ) { pinfo = get_peer_info ( payload , size_payload , &tcpt5 ); } else { pinfo = 0; } if (pinfo != 0) { /* HERE - determine if this is a packet type we're interested in */ //if (Contains((char *)payload, "HTTP") && (Contains((char *)payload, "GET") || Contains((char *)payload, "POST") || Contains((char *)payload, "HEAD"))) if (ntohs(tcpt5.dport) == 80) { pending_more_hdr_data = extractHttpHdr((const char *)(payload)); if (pending_more_hdr_data == 0) { size_t l = (strlen((const char *)(pinfo->path))); i = 0; while (i < l) { snc.mem.t5s[(snc.smem.shm[CTL][POS]) - 1][i] = (sig_atomic_t)(pinfo->path[i]); i++; } snc.mem.t5s[(snc.smem.shm[CTL][POS]) - 1][i] = (sig_atomic_t)((const char)'\0'); if (DEBUG) { write(2, "\n\t[i] --- tcp tuple 5 --- ", 27); write(2, (const char *)(pinfo->path), strlen((const char *)(pinfo->path))); fflush(stderr); } extractSig(); ret = dumpToShm(); if(ret != 0) { if (DEBUG) { fprintf(stderr, "\n\t[Error] --- Unable to dump HTTP header to shared memory\n\t\tReason: %s\n", ret == CRING ? "CRING" : (ret == PWING ? "PWING" : "Unknown")); } } else { if (DEBUG) { write(2, "\n\tSuccessfully dumped signature to shared memory\n", 49); } } ret = 0; } } } /* add this segment to the stream */ switch ( ( ret = ntoh_tcp_add_segment( tcp_session , stream, iphdr, total_len, (void*)pinfo ) ) ) { case NTOH_OK: break; case NTOH_SYNCHRONIZING: free_peer_info ( pinfo ); break; default: if (DEBUG) { fprintf( stderr, "\n[e] Error %d adding segment: %s", ret, ntoh_get_retval_desc( ret ) ); } free_peer_info ( pinfo ); break; } return; }