1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
modem.networkdatatimer -= modem.networkpolltick; //Timing this byte by byte!
if (net.packet && (!((modem.connected&2) && (packetserver_stage==PACKETSTAGE_SLIP)))) //Received a packet while not listening?
{
freez((void **)&net.packet,net.pktlen,"MODEM_PACKET"); //Release the packet to receive new packets again!
}
if (modem.connected || modem.ringing) //Are we connected?
{
if (modem.connected==2) //Running the packet server?
{
if (packetserver_stage!=PACKETSTAGE_SLIP) goto skipSLIP; //Don't handle SLIP!
//Handle packet server packet data transfers into the inputdatabuffer/outputbuffer to the network!
if (packetserver_receivebuffer) //Properly allocated?
{
if (net.packet) //Packet has been received? Try to start transmit it!
{
if (fifobuffer_freesize(packetserver_receivebuffer)>=2) //Valid to produce more data?
{
if (packetserver_packetpos==0) //New packet?
{
if (net.pktlen>sizeof(ethernetheader.data)) //Length OK(at least one byte of content)?
{
memcpy(ðernetheader.data,net.packet,sizeof(ethernetheader.data)); //Copy for inspection!
if (ethernetheader.type!=SDL_SwapBE16(0x0800)) //Invalid type?
{
//dolog("ethernetcard","Discarding type: %04X",SDL_SwapBE16(ethernetheader.type)); //Showing why we discard!
goto invalidpacket; //Invalid packet!
}
if ((memcmp(ðernetheader.dst,packetserver_sourceMAC,sizeof(ethernetheader.dst))!=0) && (memcmp(ðernetheader.dst,&packetserver_allMAC,sizeof(ethernetheader.dst))!=0)) //Invalid destination(and not broadcasting)?
{
//dolog("ethernetcard","Discarding destination."); //Showing why we discard!
goto invalidpacket; //Invalid packet!
}
//Valid packet! Receive it!
packetserver_packetpos = sizeof(ethernetheader.data); //Skip the ethernet header and give the raw IP data!
//dolog("ethernetcard","Skipping %u bytes of header data...",packetserver_packetpos); //Log it!
}
else //Invalid length?
{
//dolog("ethernetcard","Discarding invalid packet size: %u...",net.pktlen); //Log it!
invalidpacket:
//Discard the invalid packet!
freez((void **)&net.packet,net.pktlen,"MODEM_PACKET"); //Release the packet to receive new packets again!
//dolog("ethernetcard","Discarding invalid packet size or different cause: %u...",net.pktlen); //Log it!
net.packet = NULL; //No packet!
}
}
if (net.packet) //Still a valid packet to send?
{
//Convert the buffer into transmittable bytes using the proper encoding!
if (packetserver_packetpos<net.pktlen) //Not finished yet?
{
//Start transmitting data into the buffer, according to the protocol!
datatotransmit = net.packet[packetserver_packetpos++]; //Read the data to construct!
if (datatotransmit==SLIP_END) //End byte?
{
//dolog("ethernetcard","transmitting escaped SLIP END to client");
writefifobuffer(packetserver_receivebuffer,SLIP_ESC); //Escaped ...
writefifobuffer(packetserver_receivebuffer,SLIP_ESC_END); //END raw data!
}
else if (datatotransmit==SLIP_ESC) //ESC byte?
{
//dolog("ethernetcard","transmitting escaped SLIP ESC to client");
writefifobuffer(packetserver_receivebuffer,SLIP_ESC); //Escaped ...
writefifobuffer(packetserver_receivebuffer,SLIP_ESC_ESC); //ESC raw data!
}
else //Normal data?
{
//dolog("ethernetcard","transmitting raw to client: %02X",datatotransmit);
writefifobuffer(packetserver_receivebuffer,datatotransmit); //Unescaped!
}
}
else //Finished transferring a frame?
{
//dolog("ethernetcard","transmitting SLIP END to client and finishing packet buffer(size: %u)",net.pktlen);
writefifobuffer(packetserver_receivebuffer,SLIP_END); //END of frame!
logpacket(0,net.packet,net.pktlen); //Log it!
freez((void **)&net.packet,net.pktlen,"MODEM_PACKET"); //Release the packet to receive new packets again!
net.packet = NULL; //Discard the packet anyway, no matter what!
}
}
}
}
//Transmit the encoded packet buffer to the client!
if (fifobuffer_freesize(modem.outputbuffer) && peekfifobuffer(packetserver_receivebuffer,&datatotransmit)) //Able to transmit something?
{
for (;fifobuffer_freesize(modem.outputbuffer) && peekfifobuffer(packetserver_receivebuffer,&datatotransmit);) //Can we still transmit something more?
{
if (writefifobuffer(modem.outputbuffer,datatotransmit)) //Transmitted?
{
//dolog("ethernetcard","transmitted SLIP data to client: %02X",datatotransmit);
datatotransmit = readfifobuffer(packetserver_receivebuffer,&datatotransmit); //Discard the data that's being transmitted!
}
}
}
}
|