void test_LengthNotMultipleOfFour(void) { TEST_ASSERT_EQUAL(PACKET_UNUSEABLE, process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC + 6, MODE_SERVER, &testspkt, "UnitTest")); TEST_ASSERT_EQUAL(PACKET_UNUSEABLE, process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC + 3, MODE_BROADCAST, &testspkt, "UnitTest")); }
void test_TooShortLength(void) { TEST_ASSERT_EQUAL(PACKET_UNUSEABLE, process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC - 1, MODE_SERVER, &testspkt, "UnitTest")); TEST_ASSERT_EQUAL(PACKET_UNUSEABLE, process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC - 1, MODE_BROADCAST, &testspkt, "UnitTest")); }
void test_CorrectUnauthenticatedPacket(void) { TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION)); TEST_ASSERT_EQUAL(LEN_PKT_NOMAC, process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC, MODE_SERVER, &testspkt, "UnitTest")); }
void test_KoDDeny(void) { TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION)); testpkt.stratum = STRATUM_PKT_UNSPEC; memcpy(&testpkt.refid, "DENY", 4); TEST_ASSERT_EQUAL(KOD_DEMOBILIZE, process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC, MODE_SERVER, &testspkt, "UnitTest")); }
void test_RejectUnsyncedServer(void) { TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION)); testpkt.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOTINSYNC, NTP_VERSION, MODE_SERVER); TEST_ASSERT_EQUAL(SERVER_UNUSEABLE, process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC, MODE_SERVER, &testspkt, "UnitTest")); }
void test_CryptoNAKPacketReject(void) { // Activate authentication option ActivateOption("-a", "123"); TEST_ASSERT_TRUE(ENABLED_OPT(AUTHENTICATION)); int pkt_len = LEN_PKT_NOMAC + 4; // + 4 byte MAC = Crypto-NAK TEST_ASSERT_EQUAL(SERVER_AUTH_FAIL, process_pkt(&testpkt, &testsock, pkt_len, MODE_SERVER, &testspkt, "UnitTest")); }
void test_AcceptNoSentPacketBroadcastMode(void) { TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION)); testpkt.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING, NTP_VERSION, MODE_BROADCAST); TEST_ASSERT_EQUAL(LEN_PKT_NOMAC, process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC, MODE_BROADCAST, NULL, "UnitTest")); }
void test_NonWantedMode(void) { TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION)); testpkt.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING, NTP_VERSION, MODE_CLIENT); // The packet has a mode of MODE_CLIENT, but process_pkt expects MODE_SERVER TEST_ASSERT_EQUAL(SERVER_UNUSEABLE, process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC, MODE_SERVER, &testspkt, "UnitTest")); }
void test_UnauthenticatedPacketReject(void) { //sntptest(); // Activate authentication option ActivateOption("-a", "123"); TEST_ASSERT_TRUE(ENABLED_OPT(AUTHENTICATION)); int pkt_len = LEN_PKT_NOMAC; // We demand authentication, but no MAC header is present. TEST_ASSERT_EQUAL(SERVER_AUTH_FAIL, process_pkt(&testpkt, &testsock, pkt_len, MODE_SERVER, &testspkt, "UnitTest")); }
void test_ServerVersionTooNew(void) { TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION)); testpkt.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING, NTP_VERSION + 1, MODE_CLIENT); TEST_ASSERT_TRUE(PKT_VERSION(testpkt.li_vn_mode) > NTP_VERSION); int pkt_len = LEN_PKT_NOMAC; TEST_ASSERT_EQUAL(SERVER_UNUSEABLE, process_pkt(&testpkt, &testsock, pkt_len, MODE_SERVER, &testspkt, "UnitTest")); }
void test_TooShortExtensionFieldLength(void) { /* The lower 16-bits are the length of the extension field. * This lengths must be multiples of 4 bytes, which gives * a minimum of 4 byte extension field length. */ testpkt.exten[7] = htonl(3); // 3 bytes is too short. /* We send in a pkt_len of header size + 4 byte extension * header + 24 byte MAC, this prevents the length error to * be caught at an earlier stage */ int pkt_len = LEN_PKT_NOMAC + 4 + 24; TEST_ASSERT_EQUAL(PACKET_UNUSEABLE, process_pkt(&testpkt, &testsock, pkt_len, MODE_SERVER, &testspkt, "UnitTest")); }
void test_RejectWrongResponseServerMode(void) { TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION)); l_fp tmp; tmp.l_ui = 1000UL; tmp.l_uf = 0UL; HTONL_FP(&tmp, &testpkt.org); tmp.l_ui = 2000UL; tmp.l_uf = 0UL; HTONL_FP(&tmp, &testspkt.xmt); TEST_ASSERT_EQUAL(PACKET_UNUSEABLE, process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC, MODE_SERVER, &testspkt, "UnitTest")); }
int recv_bcst_pkt ( SOCKET rsock, struct pkt *rpkt, unsigned int rsize, sockaddr_u *sas ) { sockaddr_u sender; int pkt_len = recv_bcst_data(rsock, (char *)rpkt, rsize, sas, &sender); if (pkt_len < 0) { return BROADCAST_FAILED; } pkt_len = process_pkt(rpkt, sas, pkt_len, MODE_BROADCAST, NULL, "recv_bcst_pkt"); return pkt_len; }
void deserialize_pkt(void * packet){ uint16_t sender_id; sender_id=process_pkt(packet); if(servers[sender_id-1].is_alive==1 && servers[sender_id-1].is_neighbor==1){ // accept packet only if its from an active and neighnor server printf("RECEIVED A MESSAGE FROM SERVER %d\n",sender_id); bellman_ford(); num_of_pkts_received++; reset_skip_flag(sender_id); } else{ //discard packet printf("PACKET FROM SERVER %d DISCARDED\n",sender_id); } }
void test_CorrectAuthenticatedPacketSHA1(void) { PrepareAuthenticationTest(20, 15, "SHA1", "abcdefghijklmno"); TEST_ASSERT_TRUE(ENABLED_OPT(AUTHENTICATION)); int pkt_len = LEN_PKT_NOMAC; // Prepare the packet. testpkt.exten[0] = htonl(20); int mac_len = make_mac((char*)&testpkt, pkt_len, MAX_MAC_LEN, key_ptr, (char*)&testpkt.exten[1]); pkt_len += 4 + mac_len; TEST_ASSERT_EQUAL(pkt_len, process_pkt(&testpkt, &testsock, pkt_len, MODE_SERVER, &testspkt, "UnitTest")); }
/* Fetch data, check if it's data for us and whether it's useable or not. If not, return * a failure code so we can delete this server from our list and continue with another one. */ int recvpkt ( SOCKET rsock, struct pkt *rpkt, /* received packet (response) */ unsigned int rsize, /* size of rpkt buffer */ struct pkt *spkt /* sent packet (request) */ ) { int rdy_socks; int pkt_len; sockaddr_u sender; struct timeval timeout_tv; fd_set recv_fd; FD_ZERO(&recv_fd); FD_SET(rsock, &recv_fd); if (ENABLED_OPT(TIMEOUT)) timeout_tv.tv_sec = (int) atol(OPT_ARG(TIMEOUT)); else timeout_tv.tv_sec = 68; /* ntpd broadcasts every 64s */ timeout_tv.tv_usec = 0; rdy_socks = select(rsock + 1, &recv_fd, 0, 0, &timeout_tv); switch (rdy_socks) { case -1: if (ENABLED_OPT(NORMALVERBOSE)) perror("sntp recvpkt: select()"); return PACKET_UNUSEABLE; break; case 0: if (ENABLED_OPT(NORMALVERBOSE)) printf("sntp recvpkt: select() reached timeout (%u sec), aborting.\n", (unsigned)timeout_tv.tv_sec); return PACKET_UNUSEABLE; break; default: break; } pkt_len = recvdata(rsock, &sender, (char *)rpkt, rsize); if (pkt_len > 0) pkt_len = process_pkt(rpkt, &sender, pkt_len, MODE_SERVER, spkt, "recvpkt"); return pkt_len; }
void test_AuthenticatedPacketUnknownKey(void) { // Activate authentication option PrepareAuthenticationTestMD5(30, 9, "123456789"); TEST_ASSERT_TRUE(ENABLED_OPT(AUTHENTICATION)); // Prepare the packet. Observe that the Key-ID expected is 30, // but the packet has a key id of 50. int pkt_len = LEN_PKT_NOMAC; testpkt.exten[0] = htonl(50); int mac_len = make_mac((char*)&testpkt, pkt_len, MAX_MD5_LEN, key_ptr, (char*)&testpkt.exten[1]); pkt_len += 4 + mac_len; TEST_ASSERT_EQUAL(SERVER_AUTH_FAIL, process_pkt(&testpkt, &testsock, pkt_len, MODE_SERVER, &testspkt, "UnitTest")); }
int bb_rx() { int avail; int processed; unsigned int rx_status; int rx_size, pkt_size, ring_offset; int i; unsigned char *pkt; processed = 0; /* While we have frames left to process... */ while (!(nic8[RT_CHIPCMD] & 1)) { /* Get frame size and status */ rx_status = mem16[0x0000/2 + rtl.cur_rx/2]; rx_size = mem16[0x0000/2 + rtl.cur_rx/2 + 1]; /* apparently this means the rtl8139 is still copying */ if (rx_size == 0xfff0) { break; } pkt_size = rx_size - 4; if (rx_status & 1) { pkt = (unsigned char*)(mem8 + 0x0000 + rtl.cur_rx + 4); pktcpy(current_pkt, pkt, pkt_size); } rtl.cur_rx = (rtl.cur_rx + rx_size + 4 + 3) & ~3; if (rtl.cur_rx >= 16384) rtl.cur_rx = rtl.cur_rx - 16384; nic16[RT_RXBUFTAIL/2] = rtl.cur_rx - 16; if (rx_status & 1) { if (pkt_size <= 1514) process_pkt(current_pkt, pkt_size); } processed++; } return processed; }
void test_CorrectAuthenticatedPacketMD5(void) { PrepareAuthenticationTestMD5(10, 15, "123456789abcdef"); TEST_ASSERT_TRUE(ENABLED_OPT(AUTHENTICATION)); int pkt_len = LEN_PKT_NOMAC; /* Prepare the packet. */ testpkt.exten[0] = htonl(10); int mac_len = make_mac(&testpkt, pkt_len, MAX_MD5_LEN, key_ptr, &testpkt.exten[1]); pkt_len += 4 + mac_len; TEST_ASSERT_EQUAL(pkt_len, process_pkt(&testpkt, &testsock, pkt_len, MODE_SERVER, &testspkt, "UnitTest")); }
void test_AuthenticatedPacketInvalid(void) { // Activate authentication option PrepareAuthenticationTestMD5(50, 9, "123456789"); TEST_ASSERT_TRUE(ENABLED_OPT(AUTHENTICATION)); // Prepare the packet. int pkt_len = LEN_PKT_NOMAC; testpkt.exten[0] = htonl(50); int mac_len = make_mac((char*)&testpkt, pkt_len, MAX_MD5_LEN, key_ptr, (char*)&testpkt.exten[1]); pkt_len += 4 + mac_len; // Now, alter the MAC so it becomes invalid. testpkt.exten[1] += 1; TEST_ASSERT_EQUAL(SERVER_AUTH_FAIL, process_pkt(&testpkt, &testsock, pkt_len, MODE_SERVER, &testspkt, "UnitTest")); }
void * packet_processing_task (void *unused) { int len = 0; int sock_id = -1; int ifport = (int)unused; sock_id = (int)port_cdb[ifport].platform; if (sock_id < 0) sleep (sock_id); while (1) { char *buf = malloc (MAX_MTU); len = rcv_pkt (sock_id, buf); if (len > 0) { #ifdef PKT_DBG printf (" Index : %d\n", linux_if_map[ifport].linux_ifIndex); #endif process_pkt (buf, len, ifport + 1); } } return 0; }
int main(void) { t_addr* next_packet; int i = 0; int mytid = nf_tid(); if(mytid != 0) { nf_stall_a_bit(); nf_lock(LOCK_INIT); // should not get it } else { nf_lock(LOCK_INIT); // should get it immediately nf_pktout_init(); nf_pktin_init(); } nf_unlock(LOCK_INIT); while(1) { next_packet = nf_pktin_pop(); if (nf_pktin_is_valid(next_packet)) { i++; iddle_time(NUMBER_OF_LOOPS); log("thread %d sending packet %d from %p\n", nf_tid(), i, next_packet); process_pkt(next_packet); } } while(1); return 0; }
void rdt(){ printf("## starting rdt() ###\n"); int pkti; // sequence no of the packet to sent, index within the current cwnd-long window int r; // no of bytes of the received message (ACK) int m; // the # of bytes read from the file int N = MAX_SEQ_N; // pkt buffer length int start = 1; // boolean indicator to indicate first buffer fill int end = MAX_SEQ_N + 1; // end = last packet to send outside the buffer until we meet file end; // int prev_cwnd; //used to keep the value of the last cwnd on filling the buffer [0--> N - prev_cwnd] int EOF_reached = 0; // boolean indicator to indicate EOF reached int data_offset = 0; // data offset from the start of the file int pkt_ack_no; ack_t pkt_ack; timeval timer_difference; // first time = true fill all the buffer m = fread(file_buff, PKT_DATA_SIZE, N, file); if (m < 0) perror("couldn't read from file"); if(m < N) end = m; for (pkti = win_base; ; pkti = (pkti + 1) % N) { // printf("cwnd = %d seqno = %d base = %d end = %d\n", cwnd, seqno, buff_base, end); // send packet process_pkt(pkti, data_offset); if ( (rand()%100) > loss_cnt){ // if the random num gen [0-->10] is > than the number of packets to drop sendto(worker_sock, (void *)&(packet_buff[pkti]), sizeof(pkt_t), 0, (struct sockaddr*) &client_addr, client_addr_len); }else { // loss occured // printf("loss \n"); } // start timer timers_status[pkti] = 1; if (active_timers > 0){ // set the timer value = timout - (current_timer_max_val - curren_timer); getitimer(ITIMER_REAL, &itimer); timer_difference.tv_sec = itimer.it_interval.tv_sec; timer_difference.tv_usec = itimer.it_interval.tv_usec; timers[pkti] = TIME_OUT_VAL - timeval2long(timer_difference); }else { timers[pkti] = TIME_OUT_VAL; itimer.it_interval = timeout_tv; itimer.it_value = timeout_tv; setitimer(ITIMER_REAL, &itimer, NULL); } active_timers++; //the breaking condition is added inside the loop (after packet send) rather than in the for definition //to avoid the special case when end = N-1; in this case we would go into infinite loop (seqno<=end) //or don't send the last packet (end). if (pkti == end ){ printf("Transmission Complete :D \n"); break; } // recieve acks (n acks) and update window boundries r = recvfrom(worker_sock, &pkt_ack, sizeof(ack_t), 0, (struct sockaddr*) &client_addr, &client_addr_len); while (r > 0){ pkt_ack_no = (pkt_ack.seqno / PKT_DATA_SIZE) % MAX_SEQ_N; // packet_received_report(); timers_status[pkt_ack_no] = 0; if (pkt_ack_no == win_base) while (timers_status[pkt_ack_no++]==0){ // if a valid timer ==0 then the timer is off timers_status[win_base] = -1; // setting the status of this packet to not set bec it's outside the window win_base++; } r = recvfrom(worker_sock, &pkt_ack, sizeof(ack_t), MSG_DONTWAIT, (struct sockaddr*) &client_addr, &client_addr_len); } // copy the last window of the buffer (was kept while the whole buffer was copied) [N-cwnd-->N] if (win_base == N ) { data_offset+=BUFF_SIZE; if ( !EOF_reached && !start){ m = fread(file_buff+(N-prev_cwnd)*PKT_DATA_SIZE, PKT_DATA_SIZE, prev_cwnd, file); if(m < prev_cwnd){ EOF_reached = 1; end = N-prev_cwnd + m; } } } // copy from the start of the buffer except the last cwnd items // the window is at the end of buffer (next base++ --> goes to start) if (win_base + cwnd == N && !EOF_reached) { // prev_cwnd = cwnd; start = 0; m = fread(file_buff, PKT_DATA_SIZE, N-cwnd, file); // printf("buffer = %s\n",file_buff); if(m < N-cwnd){ EOF_reached = 1; end = m; } } // printf("\n"); if (pkti == (win_base+cwnd)%N && !start){ sleep(1000000); } win_base %= N; } printf("## return rdt() ###\n"); }
// // Supernode udp user request // int snode_udp_reqsearch(char *destip, u16 destport, char *our_public_ip, char *skypeuser) { u16 seqnum; u32 rnd; char resp[0x1000]; int resp_len; char pkt[0x1000]; int pkt_len; int retcode; char *mainbuf; u32 mainbuf_len; u32 mainbuf_alloc; mainbuf=malloc(0x1000); mainbuf_len=0; mainbuf_alloc=0x1000; seqnum=rand() % 0x10000; rnd=rand() % 0x10000; rnd+=(rand() % 0x10000)*0x10000; // pkt1 retcode=make_udp_profile_pkt1(our_public_ip,destip,seqnum,rnd,skypeuser,(char *)pkt,&pkt_len); if (retcode==-1) { //printf("prepare error\n"); free(mainbuf); return -1; }; resp_len=udp_talk(destip,destport,pkt,pkt_len,resp,sizeof(resp)); if (resp_len<0) { //printf("socket comm error\n"); free(mainbuf); return -1; }; if (resp_len==0) { //printf("timeout\n"); free(mainbuf); return -2; }; retcode=process_udp_search_pkt1(resp,resp_len,our_public_ip,destip, mainbuf, &mainbuf_len); if (retcode==-1) { //printf("not skype\n"); free(mainbuf); return -3; }; mainbuf_alloc+=0x1000; mainbuf=realloc(mainbuf,mainbuf_alloc); do { resp_len=udp_recv(destip,destport,resp,sizeof(resp)); if (resp_len<0) { //printf("socket comm error\n"); //return -1; break; }; if (resp_len==0) { //printf("timeout\n"); //return -2; if (mainbuf_len<0x188){ free(mainbuf); return -2; }; break; }; retcode=process_udp_search_pkt1(resp,resp_len,our_public_ip,destip, mainbuf, &mainbuf_len); if (retcode==-1) { //printf("not skype\n"); //return -3; break; }; mainbuf_alloc+=0x1000; mainbuf=realloc(mainbuf,mainbuf_alloc); } while(resp_len>0); get_profiles(mainbuf, mainbuf_len); main_unpack(mainbuf, mainbuf_len); if (0){ u32 last_recv_pkt_num; process_pkt(mainbuf, mainbuf_len, 1, &last_recv_pkt_num); }; free(mainbuf); //printf("our public ip: %s\n",our_public_ip); //printf("this is supernode\n"); return 1; }
/* ** Socket readable/timeout Callback: ** Read in the packet ** Unicast: ** - close socket ** - decrement n_pending_ntp ** - If packet is good, set the time and "exit" ** Broadcast: ** - If packet is good, set the time and "exit" */ void sock_cb( evutil_socket_t fd, short what, void *ptr ) { sockaddr_u sender; sockaddr_u * psau; sent_pkt ** p_pktlist; sent_pkt * spkt; int rpktl; int rc; INSIST(sock4 == fd || sock6 == fd); TRACE(3, ("sock_cb: event on sock%s:%s%s%s%s\n", (fd == sock6) ? "6" : "4", (what & EV_TIMEOUT) ? " timeout" : "", (what & EV_READ) ? " read" : "", (what & EV_WRITE) ? " write" : "", (what & EV_SIGNAL) ? " signal" : "")); if (!(EV_READ & what)) { if (EV_TIMEOUT & what) timeout_queries(); return; } /* Read in the packet */ rpktl = recvdata(fd, &sender, &rbuf, sizeof(rbuf)); if (rpktl < 0) { msyslog(LOG_DEBUG, "recvfrom error %m"); return; } if (sock6 == fd) p_pktlist = &v6_pkts_list; else p_pktlist = &v4_pkts_list; for (spkt = *p_pktlist; spkt != NULL; spkt = spkt->link) { psau = &spkt->addr; if (SOCK_EQ(&sender, psau)) break; } if (NULL == spkt) { msyslog(LOG_WARNING, "Packet from unexpected source %s dropped", sptoa(&sender)); return; } TRACE(1, ("sock_cb: %s %s\n", spkt->dctx->name, sptoa(&sender))); rpktl = process_pkt(&r_pkt, &sender, rpktl, MODE_SERVER, &spkt->x_pkt, "sock_cb"); TRACE(2, ("sock_cb: process_pkt returned %d\n", rpktl)); /* If this is a Unicast packet, one down ... */ if (!spkt->done && (CTX_UCST & spkt->dctx->flags)) { dec_pending_ntp(spkt->dctx->name, &spkt->addr); spkt->done = TRUE; } /* If the packet is good, set the time and we're all done */ rc = handle_pkt(rpktl, &r_pkt, &spkt->addr, spkt->dctx->name); if (0 != rc) TRACE(1, ("sock_cb: handle_pkt() returned %d\n", rc)); check_exit_conditions(); }