static uint32_t read_random(fs_node_t *node, uint32_t offset, uint32_t size, uint8_t *buffer) { uint32_t s = 0; while (s < size) { buffer[s] = krand() % 0xFF; offset++; s++; } return size; }
static void rtl_netd(void * data, char * name) { fs_node_t * tty = data; { fprintf(tty, "Sending DNS query...\n"); uint8_t queries[] = { 3,'i','r','c', 8,'f','r','e','e','n','o','d','e', 3,'n','e','t', 0, 0x00, 0x01, /* A */ 0x00, 0x01, /* IN */ }; int my_tx = next_tx_buf(); size_t packet_size = write_dns_packet(rtl_tx_buffer[my_tx], sizeof(queries), queries); outportl(rtl_iobase + RTL_PORT_TXBUF + 4 * my_tx, rtl_tx_phys[my_tx]); outportl(rtl_iobase + RTL_PORT_TXSTAT + 4 * my_tx, packet_size); } sleep_on(rx_wait); parse_dns_response(tty, last_packet); { fprintf(tty, "Sending DNS query...\n"); uint8_t queries[] = { 7,'n','y','a','n','c','a','t', 5,'d','a','k','k','o', 2,'u','s', 0, 0x00, 0x01, /* A */ 0x00, 0x01, /* IN */ }; int my_tx = next_tx_buf(); size_t packet_size = write_dns_packet(rtl_tx_buffer[my_tx], sizeof(queries), queries); outportl(rtl_iobase + RTL_PORT_TXBUF + 4 * my_tx, rtl_tx_phys[my_tx]); outportl(rtl_iobase + RTL_PORT_TXSTAT + 4 * my_tx, packet_size); } sleep_on(rx_wait); parse_dns_response(tty, last_packet); seq_no = krand(); { fprintf(tty, "Sending TCP syn\n"); int my_tx = next_tx_buf(); uint8_t payload[] = { 0 }; size_t packet_size = write_tcp_packet(rtl_tx_buffer[my_tx], payload, 0, (TCP_FLAGS_SYN | DATA_OFFSET_5)); outportl(rtl_iobase + RTL_PORT_TXBUF + 4 * my_tx, rtl_tx_phys[my_tx]); outportl(rtl_iobase + RTL_PORT_TXSTAT + 4 * my_tx, packet_size); seq_no += 1; ack_no = 0; } { struct ethernet_packet * eth = net_receive(); uint16_t eth_type = ntohs(eth->type); fprintf(tty, "Ethernet II, Src: (%2x:%2x:%2x:%2x:%2x:%2x), Dst: (%2x:%2x:%2x:%2x:%2x:%2x) [type=%4x)\n", eth->source[0], eth->source[1], eth->source[2], eth->source[3], eth->source[4], eth->source[5], eth->destination[0], eth->destination[1], eth->destination[2], eth->destination[3], eth->destination[4], eth->destination[5], eth_type); struct ipv4_packet * ipv4 = (struct ipv4_packet *)eth->payload; uint32_t src_addr = ntohl(ipv4->source); uint32_t dst_addr = ntohl(ipv4->destination); uint16_t length = ntohs(ipv4->length); char src_ip[16]; char dst_ip[16]; ip_ntoa(src_addr, src_ip); ip_ntoa(dst_addr, dst_ip); fprintf(tty, "IP packet [%s → %s] length=%d bytes\n", src_ip, dst_ip, length); struct tcp_header * tcp = (struct tcp_header *)ipv4->payload; if (seq_no != ntohl(tcp->ack_number)) { fprintf(tty, "[eth] Expected ack number of 0x%x, got 0x%x\n", seq_no, ntohl(tcp->ack_number)); fprintf(tty, "[eth] Bailing...\n"); return; } ack_no = ntohl(tcp->seq_number) + 1; free(eth); } { fprintf(tty, "Sending TCP ack\n"); int my_tx = next_tx_buf(); uint8_t payload[] = { 0 }; size_t packet_size = write_tcp_packet(rtl_tx_buffer[my_tx], payload, 0, (TCP_FLAGS_ACK | DATA_OFFSET_5)); outportl(rtl_iobase + RTL_PORT_TXBUF + 4 * my_tx, rtl_tx_phys[my_tx]); outportl(rtl_iobase + RTL_PORT_TXSTAT + 4 * my_tx, packet_size); } fprintf(tty, "[eth] s-next=0x%x, r-next=0x%x\n", seq_no, ack_no); }