void processPS_PES_packet (u_int pid, long pkt_nr, u_char *buf, int len) { OPTION *opt = getOptionPtr(); char *strx = (opt->packet_mode == PES) ? "PES" : "PS"; indent (0); print_packet_header (opt, strx, opt->pid, pkt_nr, len); if (opt->buffer_hexdump) { printhex_buf (0, buf, len); out_NL(0); } // decode protocol if (opt->printdecode) { decodePS_PES_packet (buf, len ,opt->pid); out_nl (3,"=========================================================="); out_NL (3); } }
void processTS_packet (u_int pid, long pkt_nr, u_char *b, int len) { OPTION *opt = getOptionPtr(); // -- subdecode prev. collected TS data // -- push new data if (opt->printdecode && opt->ts_subdecode) { ts2SecPes_subdecode (b, len, pid); } // -- new packet, output header indent (0); print_packet_header (opt, "TS", opt->pid, pkt_nr, len); // hex output (also on wrong packets) if (opt->buffer_hexdump) { printhex_buf (0, b, len); out_NL(0); } // -- decode protocol (if ts packet) if (opt->printdecode) { decodeTS_packet (b, len); out_nl (3,"=========================================================="); out_NL (3); if (opt->ts_subdecode) { // -- check if stored packet(s) length is sufficient for output ts2SecPes_checkAndDo_PacketSubdecode_Output(); } } }
void processSI_packet (u_int pid, long pkt_nr, u_char *buf, int len) { OPTION *opt = getOptionPtr(); indent (0); print_packet_header (opt, "SECT", opt->pid, pkt_nr, len); if (opt->buffer_hexdump) { printhex_buf (0,buf, len); out_NL(0); } // -- decode protocol if (opt->printdecode) { decodeSI_packet (buf,len ,opt->pid); out_nl (3,"=========================================================="); out_NL (3); } }
void process_inbound_udp(int sock) { struct sockaddr_in from; socklen_t fromlen; char buf[MAX_PACKET_LENGTH]; fromlen = sizeof(from); int read_result = spiffy_recvfrom(sock, buf, MAX_PACKET_LENGTH, 0, (struct sockaddr *) &from, &fromlen); printf("read %d bytes\n", read_result); //printf("incoming message from %s:%d\n", inet_ntoa(from.sin_addr), ntohs(from.sin_port)); /* we just assume we got one packet everytime * if there are multiple packets in the buffer (whichi is rare), we just ignore them, * and everything will just work fine. */ struct network_packet_s* network_packet = (struct network_packet_s*)buf; net_to_host_header(& network_packet->header); print_packet_header(&network_packet->header); if(validate_packet(& network_packet->header) < 0){ printf("packet is invalid, skip\n"); return; } int packet_type = get_packet_type(& network_packet->header); /* get the peer who send this packet*/ bt_peer_t* peer = search_bt_peer(&config, (struct sockaddr *)&from); switch(packet_type) { case TYPE_WHOHAS: { struct network_packet_s* ihave_packet = malloc_ihave_packet(network_packet); /* this node has no such chunk */ if(ihave_packet == NULL){ return; } else{ //print_ihave_packet(ihave_packet); send_packet(ihave_packet, global_socket, (struct sockaddr *) &from); free(ihave_packet); } break; } case TYPE_IHAVE: { if(current_job == NULL){ return; } /* receiving a new IHAVE packet */ /* check whether there is alreay a connection with this peer */ struct receiver_connection_s* existing_connection = search_receiver_connection(receiver_connection_head, peer); if(existing_connection != NULL){ /* there is alreay a connection */ add_ihave_to_receiver_connection(existing_connection, network_packet); } /* there is no such connection */ else{ /* check if the number of connection reached the maximum */ if(get_receiver_connection_number(receiver_connection_head) >= max_receiver_connection){ return; } /* add add the hash whose chunk is in a CHUNK_STATUS_NOT_FOUND status * to a new connection */ else{ struct receiver_connection_s* new_connection = malloc_receiver_connection(network_packet, peer); /* if every chunk has a provider, the new_connection is NULL */ if(new_connection != NULL){ add_receiver_connection(new_connection); /* start the new connection */ struct network_packet_s* get_packet = start_connection(new_connection); //print_get_packet(get_packet); free(get_packet); } } } //print_receiver_connection_list(receiver_connection_head); break; } case TYPE_GET: { //print_get_packet(network_packet); /* check whether there is alreay a connection with this peer */ if(search_provider_connection(provider_connection_head, peer) != NULL){ printf("Provider connection already exists with peer %d , ignore GET\n", peer->id); return; } /* do nothing is the number of connection reached the maximum*/ if(get_provider_connection_number(provider_connection_head) >= max_provider_connection){ printf("Provider connection reached maximum with peer %d \n", peer->id); return; } struct provider_connection_s* new_connection = malloc_provider_connection(network_packet, peer); if(new_connection != NULL){ printf("Add new provider connection with peer %d \n", peer->id); add_provider_connection(new_connection); //print_provider_connection(new_connection); } //print_provider_connection_list(provider_connection_head); break; } case TYPE_DATA: { print_data_packet(network_packet); printf("received data packet, seq: %d, ack: %d\n ", network_packet->header.seq_number, network_packet->header.ack_number); /* check whether there is a connection with this peer */ struct receiver_connection_s* receiver_connection = search_receiver_connection(receiver_connection_head, peer); /* connection does not exist, ignore the data packet */ if(receiver_connection == NULL){ return; } int existing_seq_num = receiver_connection->chunk_list_head->chunk->received_seq_num; printf("expected data packet, seq: %d \n", 1 + existing_seq_num); int packet_seq_num = network_packet->header.seq_number; /* sequence number is illegal */ if(packet_seq_num < 0){ return; } /* old packet arrived, do nothing */ else if(packet_seq_num < (existing_seq_num + 1)){ return; } /* latter packet arrived first, send duplicate ACK */ else if(packet_seq_num > (existing_seq_num + 1)){ struct network_packet_s* ack_packet = malloc_ack_packet(existing_seq_num); send_packet(ack_packet, global_socket, (struct sockaddr *) &from); free(ack_packet); return; } /* the packet expected */ else{ gettimeofday(&receiver_connection->last_data_time, NULL); receiver_connection->status = RECEIVER_STATUS_RECEIVED_DATA; struct network_packet_s* ack_packet = malloc_ack_packet(1 + existing_seq_num); send_packet(ack_packet, global_socket, (struct sockaddr *) &from); free(ack_packet); /* save_data_packet */ save_data_packet(network_packet, receiver_connection); /* save the downloading chunk of the connnection, if it finihsed */ save_chunk(receiver_connection); /* continue to download next chunk, if finished first one */ reset_receiver_connection(receiver_connection); } break; } case TYPE_ACK: { //print_ack_packet(network_packet); struct provider_connection_s* provider_connection = search_provider_connection(provider_connection_head, peer); if(provider_connection == NULL){ return; } provider_control_by_ack(provider_connection, network_packet->header.ack_number); /* check if the data has been all send to a receiver */ if(provider_connection->last_packet_acked == CHUNK_DATA_NUM){ /* download finished */ finish_provider_connection(provider_connection); } break; } case TYPE_DENIED: { break; } default:{ break; } } }