// DSWifi Project - sgIP Internet Protocol Stack Implementation // Copyright (C) 2005-2006 Stephen Stair - sgstair@akkit.org - http://www.akkit.org /****************************************************************************** DSWifi Lib and test materials are licenced under the MIT open source licence: Copyright (c) 2005-2006 Stephen Stair Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ******************************************************************************/ #include "sgIP_Hub.h" #include "sgIP_ARP.h" ////////////////////////////////////////////////////////////////////////// // Global vars int NumHWInterfaces; int NumProtocolInterfaces; sgIP_Hub_Protocol ProtocolInterfaces[SGIP_HUB_MAXPROTOCOLINTERFACES]; sgIP_Hub_HWInterface HWInterfaces[SGIP_HUB_MAXHWINTERFACES]; ////////////////////////////////////////////////////////////////////////// // Private functions ////////////////////////////////////////////////////////////////////////// // Public functions void sgIP_Hub_Init() { NumHWInterfaces=0; NumProtocolInterfaces=0; } sgIP_Hub_Protocol * sgIP_Hub_AddProtocolInterface(int protocolID, int (*ReceivePacket)(sgIP_memblock *), int (*InterfaceInit)(sgIP_Hub_Protocol *)) { int n; if(NumProtocolInterfaces>=SGIP_HUB_MAXPROTOCOLINTERFACES) return 0; for(n=0;n=SGIP_HUB_MAXHWINTERFACES) return 0; for(n=0;nflags=0; NumProtocolInterfaces--; } extern void sgIP_Hub_RemoveHardwareInterface(sgIP_Hub_HWInterface * hw) { int n; for(n=0;nflags=0; NumHWInterfaces--; } int sgIP_Hub_ReceiveHardwarePacket(sgIP_Hub_HWInterface * hw, sgIP_memblock * packet) { if(!hw || !packet) return 0; if(hw->flags & SGIP_FLAG_HWINTERFACE_ENABLED) { int n; int protocol; protocol = ((unsigned short *)packet->datastart)[6]; // SGIP_DEBUG_MESSAGE(("hub: rx packet %04X %X",protocol,packet->totallength)); if(protocol==PROTOCOL_ETHER_ARP) { // arp sgIP_ARP_ProcessARPFrame(hw,packet); return 0; } if(protocol==PROTOCOL_ETHER_IP) { // IP, forward to the ARP system. } // hide ethernet header for higher-level protocols sgIP_memblock_exposeheader(packet,-14); for(n=0;nsnmask) == (dest_address & hw->snmask) // on same network || dest_address == 0xFFFFFFFF ) // or broadcast address, send directly. { return sgIP_ARP_SendProtocolFrame(hw,packet,protocol,dest_address); } else { // eek, on different network. Send to gateway return sgIP_ARP_SendProtocolFrame(hw,packet,protocol,hw->gateway); } } // send packet on a hardware interface. int sgIP_Hub_SendRawPacket(sgIP_Hub_HWInterface * hw, sgIP_memblock * packet) { if(!hw || !packet) return 0; if(hw->flags&SGIP_FLAG_HWINTERFACE_ENABLED) { return hw->TransmitFunction(hw,packet); } sgIP_memblock_free(packet); return 0; } int sgIP_Hub_IPMaxMessageSize(unsigned long ipaddr) { return SGIP_MTU_OVERRIDE; // hack - make this more accurate soon! } unsigned long sgIP_Hub_GetCompatibleIP(unsigned long destIP) { int n; for(n=0;n>8); } unsigned long htonl(unsigned long num) { return (num<<24) | ((num&0xFF00)<<8) | ((num&0xFF0000)>>8) | (num>>24); } #else unsigned short htons(unsigned short num) { return num; } unsigned long htonl(unsigned long num) { return num; } #endif