-- (c) William Welch 2004 -- -- This software is provided 'as-is', without any express or implied -- warranty. In no event will the authors be held liable for any damages -- arising from the use of this software. -- -- Permission is granted to anyone to use this software for any purpose, -- including commercial applications, and to alter it and redistribute it -- freely, subject to the following restrictions: -- -- 1. The origin of this software must not be misrepresented; -- you must not claim that you wrote the original software. -- If you use this software in a product, an acknowledgment in -- the product documentation would be appreciated but is not required. -- -- 2. Altered source versions must be plainly marked as such, and must -- not be misrepresented as being the original software. -- -- 3. This notice may not be removed or altered from any source distribution. -- -- This license is commonly known as the zlib/libpng License. -- -- udp.jal bvwelch 20 may 2004 -- revised 18 march 2005 -- See RFC 768 for details -- TODO: document UDP header and how the dest PORT may be used as a command, opcode, etc. -- TODO: provide an example of unsolicited UDP packets being transmitted without any -- polling from the host. -- NOTE: this is just a simple "echo" sort of example of what we could do with UDP. -- feel free to experiment var byte ticktock ticktock = 0 function udp_is_for_me return bit is var byte sum_hi, sum_lo, n, app_size_hi, app_size_lo, tmp, cnt_hi, cnt_lo -- Make sure this is a UDP packet. It might be TCP or something else. if MemRdEx(ip_off , 9) != 17 then return false end if -- ******************************************** -- TODO: the app may do whatever else with the received packet. -- n = MemRd(ip_off , 3) + ip_off -- NOTE: we could just return here, and never send any reply back to the host. -- return true ticktock = ticktock + 1 LoadPointer_0 ( UDP_APP_OFF ) MemNext = "J" MemNext = "A" MemNext = "L" MemNext = " " MemNext = "W" MemNext = "I" MemNext = "F" MemNext = "I" MemNext = " " MemNext = hex_to_ascii (ticktock >> 4) MemNext = hex_to_ascii (ticktock & 0x0f) MemNext = 0 app_size_hi = 1000 >> 8 app_size_lo = 1000 & 0xff -- ***** end of application-specific code -- ****************************************** -- ***** Prepare to transmit the application-specific data. -- TODO: how to define UDP source and dest ports in the best way ? -- swap the UDP source and dest port MemSwpEx ( udp_off , 0 , udp_off , 2 , 2 ) -- update the length fields in UDP header and IP header -- note byte swap I16_LDL ( L16 , app_size_hi , app_size_lo ) I16_ADD8 ( L16, udp_hdrsize ) I16_SVL ( L16 , cnt_hi , cnt_lo) I16_LDL ( UDP_LEN , cnt_lo , cnt_hi ) I16_ADD8 ( L16, ip_hdrsize ) I16_SVL ( L16 , cnt_hi , cnt_lo) I16_LDL ( IP_LEN , cnt_lo , cnt_hi ) -- swap IP src and dest ip_swap_addr -- do the UDP checksum chksum_udp -- now do the IP chksum n = ( MemRd(ip_off) & 0x0F ) << 2 chksum_write(ip_off, 0, 10, 0 , n) -- swap ethernet source and dest net_swap_addr -- now send pkt -- note byte swap I16_SVL ( IP_LEN , cnt_lo , cnt_hi) I16_LDL ( L16 , cnt_hi , cnt_lo ) I16_ADD8 ( L16, 14 ) I16_SVL ( L16 , cnt_hi , cnt_lo) net_txpkt(cnt_hi , cnt_lo) return true end function