#if 0 cd [file dirname [info script]] source makefile.tcl return #endif #include "socketUdp.h" #include "udp.h" #include "b.h" #include "debug.h" static int tcpId=0; SocketUdp::SocketUdp() { } U32 SocketUdp::send(const U8* data,U32 len) { Ethernet *eth; eth=ec->send(); if(!eth) { ifdebug(1) { debug<< "send !ethernet\r\n"; } return 0; } *eth->destination=distant.ethernet; *eth->source=local.ethernet; eth->type=(U16)0x0800; Ip *ip=(Ip *)eth->data; ip->headerLength=5; ip->version=4; ip->services=0; ip->totalLength=20+sizeof(Udp)+len; ip->identification=tcpId++; ip->ff=0; ip->ttl=20; ip->protocol=0x11; ip->headerChecksum=0; *ip->source=local.ip; *ip->destination=distant.ip; ip->checkSumSet(); Udp *udp=(Udp*)ip->data(); udp->source=local.port; udp->destination=distant.port; udp->length=sizeof(Udp)+len; udp->crc=0; bCopy(data,udp->data,len); if(!ec->send(eth,sizeof(Ethernet)+20+sizeof(Udp)+len)) return ~0; return len; } U32 SocketUdp::recv(U8* data,U32 len) { U32 size=~0; U32 ethLen; Ethernet *eth; eth=ec->receive(ðLen); if(!eth) { ifdebug(1) { debug << "recv !ethernet\r\n"; } return size; } ifdebug(2) { for(unsigned j=0;j<2;j++) { Ethernet::Address *ethAddr; ethAddr= j==0 ? (Ethernet::Address*)eth->destination : (Ethernet::Address*)eth->source; for(unsigned i=0;ibytes);i++) { if(i!=0) debug << ':'; debug.sendHex(ethAddr->bytes[i]); } debug << (j==0 ? " ":"\r\n"); } } if( *eth->destination==local.ethernet && *eth->source==distant.ethernet && eth->type==(U16)0x0800 ) { Ip *ip=(Ip *)eth->data; ifdebug(2) { for(unsigned j=0;j<2;j++) { Ip::Address *ipAddr; ipAddr= j==0 ? (Ip::Address*)ip->destination : (Ip::Address*)ip->source; for(unsigned i=0;ibytes);i++) { if(i!=0) debug << '.'; debug.sendInt(ipAddr->bytes[i]); } debug << (j==0 ? " ":"\r\n"); } } if( *ip->destination==local.ip && *ip->source==distant.ip && ip->protocol==0x11 ) { Udp *udp=(Udp*)ip->data(); if( udp->destination==local.port // && udp->source==distant.port ) { distant.port=udp->source; size=udp->length-sizeof(Udp); if(size>len) size=len; bCopy(udp->data,data,size); } } } ec->receive(eth); return size; }