net: silence the network stack unless MISAKA_DEBUG_NET is defined

This commit is contained in:
K. Lange 2021-06-15 19:29:57 +09:00
parent e3d0126829
commit 87d6784633
4 changed files with 29 additions and 13 deletions

View File

@ -11,6 +11,10 @@
#include <sys/socket.h>
#ifndef MISAKA_DEBUG_NET
#define printf(...)
#endif
struct arp_header {
uint16_t arp_htype;
uint16_t arp_ptype;

View File

@ -14,6 +14,10 @@
#include <sys/socket.h>
#ifndef MISAKA_DEBUG_NET
#define printf(...)
#endif
struct ethernet_packet {
uint8_t destination[6];
uint8_t source[6];
@ -46,7 +50,7 @@ void net_eth_handle(struct ethernet_packet * frame, fs_node_t * nic) {
break;
case ETHERNET_TYPE_IPV4: {
struct ipv4_packet * packet = (struct ipv4_packet*)&frame->payload;
//printf("net: eth: %s: rx ipv4 packet\n", nic->name);
printf("net: eth: %s: rx ipv4 packet\n", nic->name);
if (packet->source != 0xFFFFFFFF) {
net_arp_cache_add(nic->device, packet->source, frame->source, 0);
}

View File

@ -20,6 +20,10 @@
#include <sys/socket.h>
#ifndef MISAKA_DEBUG_NET
#define printf(...)
#endif
static void ip_ntoa(const uint32_t src_addr, char * out) {
snprintf(out, 16, "%d.%d.%d.%d",
(src_addr & 0xFF000000) >> 24,
@ -227,9 +231,9 @@ void net_ipv4_handle(struct ipv4_packet * packet, fs_node_t * nic) {
}
case IPV4_PROT_TCP: {
uint16_t dest_port = ntohs(((uint16_t*)&packet->payload)[1]);
//printf("net: ipv4: %s: %s -> %s tcp %d to %d\n", nic->name, src, dest, ntohs(((uint16_t*)&packet->payload)[0]), dest_port);
printf("net: ipv4: %s: %s -> %s tcp %d to %d\n", nic->name, src, dest, ntohs(((uint16_t*)&packet->payload)[0]), dest_port);
if (tcp_sockets && hashmap_has(tcp_sockets, (void*)(uintptr_t)dest_port)) {
//printf("net: tcp: received and have a waiting endpoint!\n");
printf("net: tcp: received and have a waiting endpoint!\n");
/* What kind of packet is this? Is it something we were expecting? */
sock_t * sock = hashmap_get(tcp_sockets, (void*)(uintptr_t)dest_port);
struct tcp_header * tcp = (struct tcp_header*)&packet->payload;
@ -237,7 +241,7 @@ void net_ipv4_handle(struct ipv4_packet * packet, fs_node_t * nic) {
if (sock->priv[1] == 1) {
/* Awaiting SYN ACK, is this one? */
if ((ntohs(tcp->flags) & (TCP_FLAGS_SYN | TCP_FLAGS_ACK)) == (TCP_FLAGS_SYN | TCP_FLAGS_ACK)) {
//printf("tcp: synack\n");
printf("tcp: synack\n");
tcp_ack(nic, sock, packet, 1, 1);
net_sock_add(sock, packet, ntohs(packet->length));
}
@ -246,7 +250,7 @@ void net_ipv4_handle(struct ipv4_packet * packet, fs_node_t * nic) {
size_t hlen = ((ntohs(tcp->flags) & 0xF000) >> 12) * 4;
size_t payload_len = packet_len - hlen;
if (payload_len) {
//printf("acking because payload_len = %zu (hlen=%zu, packet_len=%zu)\n", payload_len, hlen, packet_len);
printf("tcp: acking because payload_len = %zu (hlen=%zu, packet_len=%zu)\n", payload_len, hlen, packet_len);
tcp_ack(nic, sock, packet, 0, payload_len);
net_sock_add(sock, packet, ntohs(packet->length));
} else if (ntohs(tcp->flags) & TCP_FLAGS_FIN) {
@ -494,7 +498,7 @@ static long sock_tcp_connect(sock_t * sock, const struct sockaddr *addr, socklen
const struct sockaddr_in * dest = (const struct sockaddr_in *)addr;
char deststr[16];
ip_ntoa(ntohl(dest->sin_addr.s_addr), deststr);
//printf("tcp: connect requested to %s port %d\n", deststr, ntohs(dest->sin_port));
printf("tcp: connect requested to %s port %d\n", deststr, ntohs(dest->sin_port));
if (sock->priv[1] != 0) {
printf("tcp: socket is already connected?\n");
@ -503,7 +507,7 @@ static long sock_tcp_connect(sock_t * sock, const struct sockaddr *addr, socklen
/* Get a port */
tcp_get_port(sock);
//printf("tcp: connecting from ephemeral port %d\n", (int)sock->priv[0]);
printf("tcp: connecting from ephemeral port %d\n", (int)sock->priv[0]);
/* Mark as awaiting connection, send initial SYN */
sock->priv[1] = 1;
@ -555,18 +559,18 @@ static long sock_tcp_connect(sock_t * sock, const struct sockaddr *addr, socklen
free(response);
//printf("tcp: waiting for connect to finish\n");
printf("tcp: waiting for connect to finish\n");
/* wait for signal that we connected or timed out */
struct ipv4_packet * data = net_sock_get(sock);
//printf("tcp: connect complete\n");
printf("tcp: connect complete\n");
free(data);
return 0;
}
ssize_t sock_tcp_read(fs_node_t *node, off_t offset, size_t size, uint8_t *buffer) {
//printf("tcp: read into buffer of %zu bytes\n", size);
printf("tcp: read into buffer of %zu bytes\n", size);
struct iovec _iovec = {
buffer, size
};
@ -583,7 +587,7 @@ ssize_t sock_tcp_read(fs_node_t *node, off_t offset, size_t size, uint8_t *buffe
}
static long sock_tcp_send(sock_t * sock, const struct msghdr *msg, int flags) {
//printf("tcp: send called\n");
printf("tcp: send called\n");
if (msg->msg_iovlen > 1) {
printf("net: todo: can't send multiple iovs\n");
return -ENOTSUP;
@ -638,7 +642,7 @@ static long sock_tcp_send(sock_t * sock, const struct msghdr *msg, int flags) {
}
ssize_t sock_tcp_write(fs_node_t *node, off_t offset, size_t size, uint8_t *buffer) {
//printf("tcp: write of %zu bytes\n", size);
printf("tcp: write of %zu bytes\n", size);
struct iovec _iovec = {
(void*)buffer, size
};
@ -655,7 +659,7 @@ ssize_t sock_tcp_write(fs_node_t *node, off_t offset, size_t size, uint8_t *buff
}
static int tcp_socket(void) {
//printf("tcp socket...\n");
printf("tcp socket...\n");
sock_t * sock = net_sock_create();
sock->sock_recv = sock_tcp_recv;
sock->sock_send = sock_tcp_send;

View File

@ -20,6 +20,10 @@
#include <sys/socket.h>
#ifndef MISAKA_DEBUG_NET
#define printf(...)
#endif
/**
* TODO: Should we have an interface for modules to install protocol handlers?
* Thinking this should work like the VFS, with method tables for different