/* * A V6 RST packet arrives. */ static bool test_tcp_trans_state_handle_v6rst(void) { struct session_entry *session; struct expire_timer *expirer; struct packet pkt; struct sk_buff *skb; bool success = true; /* Prepare */ session = session_create_str_tcp("1::2", 1212, "3::4", 3434, "5.6.7.8", 5678, "8.7.6.5", 8765, TRANS); if (!session) return false; if (is_error(create_tcp_packet(&skb, L3PROTO_IPV6, false, true, false))) return false; if (is_error(pkt_init_ipv6(&pkt, skb))) return false; /* Evaluate */ success &= assert_equals_int(0, tcp_trans_state_handle(&pkt, session, &expirer), "V6 rst-result"); success &= assert_equals_u8(TRANS, session->state, "V6 rst-state"); success &= assert_null(session->expirer, "null expirer"); kfree_skb(skb); return success; }
/* * Something else arrives. */ static bool test_tcp_trans_state_handle_else(void) { struct session_entry *session; struct expire_timer *expirer; struct packet pkt; struct sk_buff *skb; unsigned long timeout = 0; bool success = true; /* Prepare */ session = session_create_str_tcp("1::2", 1212, "3::4", 3434, "5.6.7.8", 5678, "8.7.6.5", 8765, TRANS); if (!session) return false; if (is_error(create_tcp_packet(&skb, L3PROTO_IPV4, true, false, false))) return false; if (is_error(pkt_init_ipv4(&pkt, skb))) return false; /* Evaluate */ success &= assert_equals_int(0, tcp_trans_state_handle(&pkt, session, &expirer), "else-result"); success &= assert_equals_u8(ESTABLISHED, session->state, "else-state"); success &= assert_equals_int(0, sessiondb_get_timeout(session, &timeout), "else-toresult"); success &= assert_equals_ulong(TCPEST_TIMEOUT, timeout, "else-lifetime"); kfree_skb(skb); return success; }
/* * A V4 FIN packet arrives. */ static bool test_tcp_established_state_handle_v4fin(void) { struct session_entry *session; struct expire_timer *expirer; struct packet pkt; struct sk_buff *skb; bool success = true; /* Prepare */ session = session_create_str_tcp("1::2", 1212, "3::4", 3434, "5.6.7.8", 5678, "8.7.6.5", 8765, ESTABLISHED); if (!session) return false; if (is_error(create_tcp_packet(&skb, L3PROTO_IPV4, false, false, true))) return false; if (is_error(pkt_init_ipv4(&pkt, skb))) return false; /* Evaluate */ success &= assert_equals_int(0, tcp_established_state_handle(&pkt, session, &expirer), "result"); success &= assert_equals_u8(V4_FIN_RCV, session->state, "V4 fin-state"); success &= assert_null(session->expirer, "null expirer"); kfree_skb(skb); return success; }
/* * A V6 FIN packet arrives. */ static bool test_tcp_v4_fin_rcv_state_handle_v6fin(void) { struct session_entry *session; struct expire_timer *expirer; struct packet pkt; struct sk_buff *skb; unsigned long timeout = 0; bool success = true; /* Prepare */ session = session_create_str_tcp("1::2", 1212, "3::4", 3434, "5.6.7.8", 5678, "8.7.6.5", 8765, V4_FIN_RCV); if (!session) return false; if (is_error(create_tcp_packet(&skb, L3PROTO_IPV6, false, false, true))) return false; if (is_error(pkt_init_ipv6(&pkt, skb))) return false; /* Evaluate */ success &= assert_equals_int(0, tcp_v4_fin_rcv_state_handle(&pkt, session, &expirer), "V6 fin-result"); success &= assert_equals_u8(V4_FIN_V6_FIN_RCV, session->state, "V6 fin-state"); success &= assert_equals_int(0, sessiondb_get_timeout(session, &timeout), "V6 fin-toresult"); success &= assert_equals_ulong(TCPTRANS_TIMEOUT, timeout, "V6 fin-lifetime"); kfree_skb(skb); return success; }
void handle_tcp(const struct ip* args,const struct pcap_pkthdr* pkthdr,const u_char* packet){ char * payload; u_int p_size, tcp_hsize; u_int ip_hlen = (args->ip_hl)*4; //header length of ip header(byte) struct tcphdr2* tcp_header; struct ip* ip_header; u_int16_t src_port, dst_port; printf("TCP: "); /* jump pass the ip & ether header */ ip_header = (struct ip*)(packet + sizeof(struct ether_header)); tcp_header = (struct tcphdr2*)(packet + sizeof(struct ether_header) + ip_hlen); src_port = ntohs(tcp_header->source); dst_port = ntohs(tcp_header->dest); printf("src port: %d, des port: %d", src_port, dst_port); payload = (char *)(tcp_header+(tcp_header->doff)*4); tcp_hsize = (tcp_header->doff)*4; p_size = ntohs(args->ip_len)-ip_hlen-(tcp_header->doff)*4; printf(" tcp_hsize = %d, payload size: %d\n", tcp_hsize, p_size); create_tcp_packet(tcp_header, ip_header, p_size,packet); return; }
/* * A V6 RST packet arrives. */ static bool test_tcp_established_state_handle_v6rst(void) { struct session_entry *session; struct expire_timer *expirer; struct sk_buff *skb; unsigned long timeout; bool success = true; /* Prepare */ session = session_create_str_tcp("1::2", 1212, "3::4", 3434, "5.6.7.8", 5678, "8.7.6.5", 8765, ESTABLISHED); if (!session) return false; if (is_error(create_tcp_packet(&skb, L3PROTO_IPV6, false, true, false))) return false; /* Evaluate */ success &= assert_equals_int(0, tcp_established_state_handle(skb, session, &expirer), "result"); success &= assert_equals_u8(TRANS, session->state, "V6 rst-state"); success &= assert_equals_int(0, sessiondb_get_timeout(session, &timeout), "V6 rst-toresult"); success &= assert_equals_ulong(TCPTRANS_TIMEOUT, timeout, "V6 rst-lifetime"); kfree_skb(skb); return success; }
/* * Something else arrives. */ static bool test_tcp_v4_init_state_handle_else(void) { struct session_entry *session; struct expire_timer *expirer; struct sk_buff *skb; bool success = true; /* Prepare */ session = session_create_str_tcp("1::2", 1212, "3::4", 3434, "5.6.7.8", 5678, "8.7.6.5", 8765, V4_INIT); if (!session) return false; if (is_error(create_tcp_packet(&skb, L3PROTO_IPV6, false, true, false))) return false; /* Evaluate */ success &= assert_equals_int(0, tcp_v4_init_state_handle(skb, session, &expirer), "else-result"); success &= assert_null(session->expirer, "null expirer"); kfree_skb(skb); return success; }
//---------------------------------------------------------------------------- //HTTP Request an einen Webserver stelle void http_request (void) { unsigned long index = MAX_TCP_ENTRY; if (http_get_state > 1 && http_get_state < 20) http_get_state++; if (http_get_state == 0) { //offnet eine Verbindung zu meinem Webserver HTTPC_DEBUG("ARP Request to HTTP URL\n\r"); unsigned int my_http_cp = 2354; add_tcp_app (my_http_cp, (void(*)(unsigned char))test); //ARP Request senden if(arp_request (WEATHER_SERVER_IP)) { for(unsigned long a=0;a<2000000;a++){asm("nop");}; tcp_port_open (WEATHER_SERVER_IP,HTONS(80),HTONS(my_http_cp)); unsigned char tmp_counter = 0; while((index >= MAX_ARP_ENTRY) && (tcp_entry[index].app_status != 1)) { index = tcp_entry_search (WEATHER_SERVER_IP,HTONS(80)); if (tmp_counter++ > 30) { HTTPC_DEBUG("TCP Eintrag nicht gefunden (HTTP_CLIENT)!\r\n"); return; } } HTTPC_DEBUG("TCP Eintrag gefunden (HTTP_CLIENT)!\r\n"); tcp_entry[index].first_ack = 1; ///tcp_entry[index].dest_port = ; http_get_state = 2; } else { http_get_state = 1; } } //if (http_get_state == 10) if (http_get_state > 10 && http_get_state < 20) { usart_write ("\n\rHTTP GET sent \r\n"); index = tcp_entry_search (WEATHER_SERVER_IP,HTONS(80)); create_tcp_packet ( 0,//unsigned char index, 0,//unsigned char ip_adr[], mymac,//unsigned char mac_adr_my[], myLAN,//unsigned char mac_adr_LAN[], 80, //usigned int src_port, 80,//unsigned int dest_port WEATHER_GET_STRING, sizeof(WEATHER_GET_STRING) ); //memcpy_P(ð_buffer[TCP_DATA_START],WEATHER_GET_STRING,(sizeof(WEATHER_GET_STRING)-1)); tcp_entry[index].status = ACK_FLAG | PSH_FLAG; //create_new_tcp_packet((sizeof(WEATHER_GET_STRING)-1),index); } }
int tx_switch(struct cli_def *cli) { // These handles are only used when creating L3 and above packets. libnet_t *l; // the context libnet_ptag_t t2=0, t3=0, t4=0; // handles to layers double cpu_time_used; switch (mode) { case BYTE_STREAM: send_eth(); break; case ARP: if (send_arp()==-1) return 0; break; case BPDU: if (send_bpdu()==-1) return 0; break; case CDP: if (send_cdp()==-1) return 0; break; case IP: // From now on a new much more modular method is used: l = get_link_context(); t3 = create_ip_packet(l); // t3 can be used for later header changes if (!quiet) complexity(); if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes else send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly break; case ICMP: tx.ip_proto = 1; l = get_link_context(); t4 = create_icmp_packet(l); // t4 can be used for later header changes if (t4==-1) return 0; t3 = create_ip_packet(l); // t3 can be used for later header changes if (!quiet) complexity(); if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes else send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly break; case UDP: tx.ip_proto = 17; l = get_link_context(); t4 = create_udp_packet(l); // t4 can be used for later header changes if (t4==-1) return 0; t3 = create_ip_packet(l); // t3 can be used for later header changes if (!quiet) complexity(); if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes else send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly break; case TCP: tx.ip_proto = 6; l = get_link_context(); t4 = create_tcp_packet(l); // t4 can be used for later header changes if (t4==-1) return 0; t3 = create_ip_packet(l); // t3 can be used for later header changes if (!quiet) complexity(); if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes else send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly break; case DNS: tx.ip_proto = 17; l = get_link_context(); if (create_dns_packet()==-1) return 0; t4 = create_udp_packet(l); // t4 can be used for later header changes t3 = create_ip_packet(l); // t3 can be used for later header changes if (!quiet) complexity(); if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes else send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly break; case RTP: tx.ip_proto = 17; l = get_link_context(); if (create_rtp_packet()==-1) return 0; cli_print(cli, "RTP mode! (count=%u, delay=%u usec)\n", tx.count, tx.delay); t4 = create_udp_packet(l); // t4 can be used for later header changes t3 = create_ip_packet(l); // t3 can be used for later header changes if (!quiet) complexity(); if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes else send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly break; case RX_RTP: // Receive RTP packets rcv_rtp_init(); rcv_rtp(); break; case SYSLOG: tx.ip_proto = 17; l = get_link_context(); if (create_syslog_packet()==-1) return 0; t4 = create_udp_packet(l); // t4 can be used for later header changes t3 = create_ip_packet(l); // t3 can be used for later header changes if (!quiet) complexity(); if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes else send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly break; case LLDP: // start with a new concept here //l = get_link_context(); //(void) create_lldp_packet(); // // // printf("SIZE=%lu\n",sizeof(struct tx_struct)); break; default: cli_print(cli,"Unknown mode!\n"); return (1); } // ***** Re-init packet functions: ***** tx.ip_payload_s = 0; tx.udp_len = 0; tx.tcp_payload_s = 0; tx.icmp_payload_s = 0; tx.cdp_sum = 0; mode = 0; // ************************************** mz_stop = clock(); cpu_time_used = ((double) (mz_stop - mz_start)) / CLOCKS_PER_SEC; if (cpu_time_used > 0) { total_d /= cpu_time_used; cli_print(cli, "%.2f seconds (%.Lf packets per second)\n",cpu_time_used,total_d); } return 0; }
/** * We'll just chain a handful of packets, since testing every combination would * take forever and the inner functions are tested in session db anyway. * The chain is V6 SYN --> V4 SYN --> V6 RST --> V6 SYN. */ static bool test_tcp(void) { struct xlation state = { .jool = jool }; struct sk_buff *skb; bool success = true; log_debug("== V6 SYN =="); if (init_tuple6(&state.in.tuple, "1::2", 1212, "3::4", 3434, L4PROTO_TCP)) return false; if (create_tcp_packet(&skb, L3PROTO_IPV6, true, false, false)) return false; if (pkt_init_ipv6(&state, skb)) return false; success &= ASSERT_VERDICT(CONTINUE, ipv6_tcp(&state), "Closed-result"); success &= assert_bib_count(1, L4PROTO_TCP); success &= assert_bib_exists("1::2", 1212, "192.0.2.128", 1024, L4PROTO_TCP, 1); success &= assert_session_count(1, L4PROTO_TCP); success &= assert_session_exists("1::2", 1212, "3::4", 3434, "192.0.2.128", 1024, "0.0.0.4", 3434, L4PROTO_TCP, V6_INIT, SESSION_TIMER_TRANS, TCP_TRANS); kfree_skb(skb); log_debug("== V4 SYN =="); if (invert_tuple(&state)) return false; if (create_tcp_packet(&skb, L3PROTO_IPV4, true, false, false)) return false; if (pkt_init_ipv4(&state, skb)) return false; success &= ASSERT_VERDICT(CONTINUE, ipv4_tcp(&state), "V6 init-result"); success &= assert_bib_count(1, L4PROTO_TCP); success &= assert_bib_exists("1::2", 1212, "192.0.2.128", 1024, L4PROTO_TCP, 1); success &= assert_session_count(1, L4PROTO_TCP); success &= assert_session_exists("1::2", 1212, "3::4", 3434, "192.0.2.128", 1024, "0.0.0.4", 3434, L4PROTO_TCP, ESTABLISHED, SESSION_TIMER_EST, TCP_EST); kfree_skb(skb); log_debug("== V6 RST =="); if (init_tuple6(&state.in.tuple, "1::2", 1212, "3::4", 3434, L4PROTO_TCP)) return false; if (create_tcp_packet(&skb, L3PROTO_IPV6, false, true, false)) return false; if (pkt_init_ipv6(&state, skb)) return false; success &= ASSERT_VERDICT(CONTINUE, ipv6_tcp(&state), "Established-result"); success &= assert_bib_count(1, L4PROTO_TCP); success &= assert_bib_exists("1::2", 1212, "192.0.2.128", 1024, L4PROTO_TCP, 1); success &= assert_session_count(1, L4PROTO_TCP); success &= assert_session_exists("1::2", 1212, "3::4", 3434, "192.0.2.128", 1024, "0.0.0.4", 3434, L4PROTO_TCP, TRANS, SESSION_TIMER_TRANS, TCP_TRANS); kfree_skb(skb); log_debug("== V6 SYN =="); if (create_tcp_packet(&skb, L3PROTO_IPV6, true, false, false)) return false; if (pkt_init_ipv6(&state, skb)) return false; success &= ASSERT_VERDICT(CONTINUE, ipv6_tcp(&state), "Trans-result"); success &= assert_bib_count(1, L4PROTO_TCP); success &= assert_bib_exists("1::2", 1212, "192.0.2.128", 1024, L4PROTO_TCP, 1); success &= assert_session_count(1, L4PROTO_TCP); success &= assert_session_exists("1::2", 1212, "3::4", 3434, "192.0.2.128", 1024, "0.0.0.4", 3434, L4PROTO_TCP, ESTABLISHED, SESSION_TIMER_EST, TCP_EST); kfree_skb(skb); return success; }
int main(int argc, char **argv) { // These handles are only used when creating L3 and above packets. libnet_t *l; // the context libnet_ptag_t t2=0, t3=0, t4=0; // handles to layers double cpu_time_used; reset(); if ( getopts(argc, argv) ) { (void) fprintf(stderr, " Invalid command line parameters!\n"); help(); } // Check whether hires timers are supported or not: (void) check_timer(); signal(SIGINT, signal_handler); // to close all file pointers etc upon SIGINT switch (mode) { case BYTE_STREAM: send_eth(); break; case ARP: (void) send_arp(); break; case BPDU: (void) send_bpdu(); break; case CDP: (void) send_cdp(); break; case IP: // From now on a new much more modular method is used: l = get_link_context(); t3 = create_ip_packet(l); // t3 can be used for later header changes if (!quiet) complexity(); if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes else send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly break; case ICMP: tx.ip_proto = 1; l = get_link_context(); t4 = create_icmp_packet(l); // t4 can be used for later header changes t3 = create_ip_packet(l); // t3 can be used for later header changes if (!quiet) complexity(); if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes else send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly break; case ICMP6: tx.ip_proto = 58; l = get_link_context(); t4 = create_icmp6_packet(l); // t4 can be used for later header changes t3 = create_ip_packet(l); // t3 can be used for later header changes if (ipv6_mode) update_ISUM(l, t4); if (!quiet) complexity(); if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes else send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly break; case UDP: tx.ip_proto = 17; l = get_link_context(); t4 = create_udp_packet(l); // t4 can be used for later header changes t3 = create_ip_packet(l); // t3 can be used for later header changes if (ipv6_mode) update_USUM(l, t4); if (!quiet) complexity(); if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes else send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly break; case TCP: tx.ip_proto = 6; l = get_link_context(); t4 = create_tcp_packet(l); // t4 can be used for later header changes t3 = create_ip_packet(l); // t3 can be used for later header changes if (ipv6_mode) update_TSUM(l, t4); if (!quiet) complexity(); if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes else send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly break; case DNS: tx.ip_proto = 17; l = get_link_context(); (void) create_dns_packet(); t4 = create_udp_packet(l); // t4 can be used for later header changes t3 = create_ip_packet(l); // t3 can be used for later header changes if (!quiet) complexity(); if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes else send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly break; case RTP: tx.ip_proto = 17; l = get_link_context(); if (!quiet) fprintf(stderr, " mz: RTP mode! (count=%u, delay=%u usec)\n\n", tx.count, tx.delay); (void) create_rtp_packet(); t4 = create_udp_packet(l); // t4 can be used for later header changes t3 = create_ip_packet(l); // t3 can be used for later header changes if (!quiet) complexity(); if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes else send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly break; case RX_RTP: // Receive RTP packets rcv_rtp_init(); rcv_rtp(); break; case SYSLOG: tx.ip_proto = 17; l = get_link_context(); (void) create_syslog_packet(); t4 = create_udp_packet(l); // t4 can be used for later header changes t3 = create_ip_packet(l); // t3 can be used for later header changes if (!quiet) complexity(); if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes else send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly break; case LLDP: // start with a new concept here //l = get_link_context(); //(void) create_lldp_packet(); // // // printf("SIZE=%lu\n",sizeof(struct tx_struct)); fprintf(stderr, "LLDP is currently only supported via the interactive mode\n"); exit(1); break; default: (void) fprintf(stderr," mz/main: unknown mode! Stop.\n"); return (1); } if (!quiet) { mz_stop = clock(); cpu_time_used = ((double) (mz_stop - mz_start)) / CLOCKS_PER_SEC; if (cpu_time_used > 0) { total_d /= cpu_time_used; fprintf(stderr, "%.2f seconds (%.Lf packets per second)\n",cpu_time_used,total_d); } else { fprintf(stderr, "\n"); } } return(0); }