From: <ljs...@us...> - 2006-09-14 03:04:57
|
Revision: 354 http://svn.sourceforge.net/cadcdev/?rev=354&view=rev Author: ljsebald Date: 2006-09-13 20:04:48 -0700 (Wed, 13 Sep 2006) Log Message: ----------- Another networking change coming up. This is a bit of cleaning in the net_ipv4.c file. Modified Paths: -------------- kos/include/kos/net.h kos/kernel/net/net_ipv4.c Modified: kos/include/kos/net.h =================================================================== --- kos/include/kos/net.h 2006-09-14 02:39:59 UTC (rev 353) +++ kos/include/kos/net.h 2006-09-14 03:04:48 UTC (rev 354) @@ -198,6 +198,10 @@ within the ip. */ uint32 net_ipv4_address(const uint8 addr[4]); +/* Parse an IP address that is packet into a uint32 into an array of the + individual bytes */ +void net_ipv4_parse_address(uint32 addr, uint8 out[4]); + /***** net_udp.c **********************************************************/ /* Init */ Modified: kos/kernel/net/net_ipv4.c =================================================================== --- kos/kernel/net/net_ipv4.c 2006-09-14 02:39:59 UTC (rev 353) +++ kos/kernel/net/net_ipv4.c 2006-09-14 03:04:48 UTC (rev 354) @@ -2,7 +2,7 @@ kernel/net/net_ipv4.c - Copyright (C) 2005 Lawrence Sebald + Copyright (C) 2005, 2006 Lawrence Sebald Portions adapted from KOS' old net_icmp.c file: Copyright (c) 2002 Dan Potter @@ -11,6 +11,8 @@ #include <stdio.h> #include <string.h> +#include <errno.h> +#include <arpa/inet.h> #include <kos/net.h> #include "net_ipv4.h" #include "net_icmp.h" @@ -22,7 +24,7 @@ uint32 net_ntohl(uint32 n) { return ((n & 0xFF) << 24) | ((n & 0xFF00) << 8) | - ((n >> 8) & 0xFF00) | ((n >> 24) & 0xFF); + ((n >> 8) & 0xFF00) | ((n >> 24) & 0xFF); } /* Perform an IP-style checksum on a block of data */ @@ -43,7 +45,8 @@ } /* Determine if a given IP is in the current network */ -static int is_in_network(const uint8 src[4], const uint8 dest[4], const uint8 netmask[4]) { +static int is_in_network(const uint8 src[4], const uint8 dest[4], + const uint8 netmask[4]) { int i; for(i = 0; i < 4; i++) { @@ -55,19 +58,21 @@ } /* Send a packet on the specified network adaptor */ -int net_ipv4_send_packet(netif_t *net, ip_hdr_t *hdr, const uint8 *data, int size) { +int net_ipv4_send_packet(netif_t *net, ip_hdr_t *hdr, const uint8 *data, + int size) { uint8 dest_ip[4]; uint8 dest_mac[6]; uint8 pkt[size + sizeof(ip_hdr_t) + sizeof(eth_hdr_t)]; eth_hdr_t *ehdr; - dest_ip[0] = (uint8)(hdr->dest & 0xFF); - dest_ip[1] = (uint8)(hdr->dest >> 8); - dest_ip[2] = (uint8)(hdr->dest >> 16); - dest_ip[3] = (uint8)(hdr->dest >> 24); + if(net == NULL) { + net = net_default_dev; + } - /* Is this the loopback address? */ - if(dest_ip[0] == 127 && dest_ip[1] == 0 && dest_ip[2] == 0 && dest_ip[3] == 1) { + net_ipv4_parse_address(ntohl(hdr->dest), dest_ip); + + /* Is this the loopback address (127.0.0.1)? */ + if(ntohl(hdr->dest) == 0x7F000001) { /* Fill in the ethernet header */ ehdr = (eth_hdr_t *)pkt; memset(ehdr->dest, 0, 6); @@ -80,24 +85,29 @@ ehdr->type[1] = 0x00; /* Put the IP header / data into our ethernet packet */ - memcpy(pkt + sizeof(eth_hdr_t), hdr, 4 * (hdr->version_ihl & 0x0f)); - memcpy(pkt + sizeof(eth_hdr_t) + 4 * (hdr->version_ihl & 0x0f), data, size); + memcpy(pkt + sizeof(eth_hdr_t), hdr, + 4 * (hdr->version_ihl & 0x0f)); + memcpy(pkt + sizeof(eth_hdr_t) + 4 * (hdr->version_ihl & 0x0f), + data, size); /* Send it away */ - net_input(NULL, pkt, sizeof(eth_hdr_t) + sizeof(ip_hdr_t) + size); + net_input(NULL, pkt, sizeof(eth_hdr_t) + sizeof(ip_hdr_t) + + size); return 0; } - /* Is it in our network? */ - if(!is_in_network(net->ip_addr, dest_ip, net->netmask)) + /* Is it in our network? */ + if(!is_in_network(net->ip_addr, dest_ip, net->netmask)) { memcpy(dest_ip, net->gateway, 4); + } /* Get our destination's MAC address */ - if(net_arp_lookup(net, dest_ip, dest_mac) == -1) + if(net_arp_lookup(net, dest_ip, dest_mac) == -1) { + errno = ENETUNREACH; return -1; + } - /* Fill in the ethernet header */ ehdr = (eth_hdr_t *)pkt; memcpy(ehdr->dest, dest_mac, 6); @@ -107,10 +117,12 @@ /* Put the IP header / data into our ethernet packet */ memcpy(pkt + sizeof(eth_hdr_t), hdr, 4 * (hdr->version_ihl & 0x0f)); - memcpy(pkt + sizeof(eth_hdr_t) + 4 * (hdr->version_ihl & 0x0f), data, size); + memcpy(pkt + sizeof(eth_hdr_t) + 4 * (hdr->version_ihl & 0x0f), data, + size); /* Send it away */ - net->if_tx(net, pkt, sizeof(ip_hdr_t) + size + sizeof(eth_hdr_t), NETIF_BLOCK); + net->if_tx(net, pkt, sizeof(ip_hdr_t) + size + sizeof(eth_hdr_t), + NETIF_BLOCK); return 0; } @@ -120,45 +132,58 @@ ip_hdr_t *ip; int i; uint8 *data; + uint16 *ethproto; /* Get pointers */ - eth = (eth_hdr_t*)(pkt); - ip = (ip_hdr_t*)(pkt + sizeof(eth_hdr_t)); - data = (uint8 *)(pkt + sizeof(eth_hdr_t) + 4*(ip->version_ihl & 0x0f)); + eth = (eth_hdr_t*) (pkt); + ip = (ip_hdr_t*) (pkt + sizeof(eth_hdr_t)); + data = (uint8 *) (pkt + sizeof(eth_hdr_t) + 4 * + (ip->version_ihl & 0x0f)); + ethproto = (uint16 *) (pkt + 12); - /* Non-IP */ - if (eth->type[0] != 0x08) + /* Make sure this packet is actually marked as an IP packet */ + if(ntohs(*ethproto) != 0x0800) { + dbglog(DBG_KDEBUG, "net_ipv4: Discarding non IP packet\n"); return 0; + } - if (eth->type[1] != 0x00) - return 0; - /* Check ip header checksum */ i = ip->checksum; ip->checksum = 0; - ip->checksum = net_ipv4_checksum((uint16*)ip, 2 * (ip->version_ihl & 0x0f)); - if (i != ip->checksum) { - dbglog(DBG_KDEBUG, "net_ipv4: ip with invalid checksum\n"); + ip->checksum = net_ipv4_checksum((uint16*)ip, 2 * + (ip->version_ihl & 0x0f)); + + if(i != ip->checksum) { + dbglog(DBG_KDEBUG, "net_ipv4: Discarding recieved IP packet " + "with invalid checksum\n"); return 0; } - switch(ip->protocol) { + switch(ip->protocol) { case 1: - net_icmp_input(src, eth, ip, data, net_ntohs(ip->length) - + net_icmp_input(src, eth, ip, data, ntohs(ip->length) - (ip->version_ihl & 0x0f) * 4); break; case 17: - net_udp_input(src, eth, ip, data, net_ntohs(ip->length) - + net_udp_input(src, eth, ip, data, ntohs(ip->length) - (ip->version_ihl & 0x0f) * 4); break; default: - dbglog(DBG_KDEBUG, "net_ipv4: unknown ip protocol: %d\n", ip->protocol); + dbglog(DBG_KDEBUG, "net_ipv4: Discarding recieved IP " + "packet with unkown protocol: %d\n", + ip->protocol); } return 0; } uint32 net_ipv4_address(const uint8 addr[4]) { - uint32 ad = (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) | (addr[3]); - return ad; + return (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) | (addr[3]); } + +void net_ipv4_parse_address(uint32 addr, uint8 out[4]) { + out[0] = (uint8) ((addr >> 24) & 0xFF); + out[1] = (uint8) ((addr >> 16) & 0xFF); + out[2] = (uint8) ((addr >> 8) & 0xFF); + out[3] = (uint8) (addr & 0xFF); +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |