int rudp_recv(int sock, char *receive_buf, struct sockaddr_in *self_addr,\ struct sockaddr *src_addr, unsigned *src_addr_len, size_t *recv_size) { size_t size; char *p=receive_buf; char buffer[PACKET_SIZE]; header_t head; enum recPkt sw_return; do { size = recvfrom(sock, (void *)buffer, PACKET_SIZE, 0, src_addr, src_addr_len); assert (size==PACKET_SIZE); assert (add_checksum(size, (u_char *)&(((struct sockaddr_in *)src_addr)->sin_addr.s_addr), (u_char *)&(self_addr->sin_addr.s_addr), size%2, (u_short *)buffer) == 0); printf("checksum correct\n"); read_header(&head,(packet_t *)buffer); assert (head.offset==PAYLOAD_SIZE || head.flag==FIN); printf("-----------recevie packet %d\n",head.seq); if (packet_lost(drop_p) && head.flag!=FIN) { // next_byte_expected--; //p+=head.offset; printf("packet %u dropped\n",head.seq); continue; } sw_return = receiver_receive_packet(head.seq); if (sw_return == dropPkt) { if(head.seq>next_byte_expected) { send_ack(next_byte_expected-1); printf("seq wrong. drop packet %d. send ack for %d\n",head.seq,next_byte_expected); } continue; } //printf("Seq %d Ack %d Offset %d, Flag %d \n",head.seq,head.ack,head.offset,head.flag); *recv_size += head.offset; read_packet((u_char *)p,(packet_t*)buffer,(u_short)head.offset); p+=head.offset; send_ack(head.seq); if (head.flag==FIN) { if (delay_p != 0) { while (DELAY_HEAD->next!=NULL) { traverse_dh(); } } return (int)head.flag; } } while (*recv_size < BUFFERSIZE); return (int)head.flag; }
static void print_stats(pcap_t* pcap, uint64_t packets_captured, struct packet_pool* pool) { struct pcap_stat stat; if (0 > pcap_stats(pcap, &stat)) { msg(MSG_INFO, "Could not get pcap stats!"); return; } uint32_t recv_this_interval = stat.ps_recv - prev_recv; uint32_t drop_this_interval = stat.ps_drop - prev_drop; //double ratio = stat.ps_recv?(double)stat.ps_drop/(double)stat.ps_recv*100:0; //msg(MSG_STATS, "%llu packets captured, %u received by filter, %u dropped by kernel, %f%% packet drop", packets_captured, stat.ps_recv, stat.ps_drop, ratio); double ratio = recv_this_interval?(double)drop_this_interval/(double)recv_this_interval*100:0; msg(MSG_STATS, "%llu packets captured, %u received by filter, %u dropped by kernel, %f%% packet drop in kernel, %llu packets lost in app", packets_captured, recv_this_interval, drop_this_interval, ratio, packet_lost(pool) - prev_app); struct connection_stats* conn_stats = connection_get_stats(); msg(MSG_STATS, "%llu free, %llu used, %llu active, %llu unecessary kept, %llu active timed_out", conn_stats->free_conns, conn_stats->used_conns, conn_stats->active_conns, conn_stats->used_conns - conn_stats->active_conns, conn_stats->active_conns_timed_out); prev_recv = stat.ps_recv; prev_drop = stat.ps_drop; prev_app = packet_lost(pool); }