| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | ! Copyright (C) 2007 Doug Coleman. | 
					
						
							|  |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							|  |  |  | USING: alien alien.c-types alien.syntax byte-arrays io | 
					
						
							| 
									
										
										
										
											2008-05-13 19:24:46 -04:00
										 |  |  | io.sockets kernel structs math math.parser | 
					
						
							| 
									
										
										
										
											2008-01-30 13:49:20 -05:00
										 |  |  | prettyprint sequences ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | IN: io.sockets.headers | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | C-STRUCT: etherneth | 
					
						
							|  |  |  |     { { "char" 6 } "dmac" } | 
					
						
							|  |  |  |     { { "char" 6 } "smac" } | 
					
						
							|  |  |  |     { "ushort" "type" } ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-26 22:38:30 -05:00
										 |  |  | : >mac-address ( byte-array -- string )
 | 
					
						
							|  |  |  |     6 memory>byte-array | 
					
						
							|  |  |  |     [ >hex 2 48 pad-left ] { } map-as ":" join ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : etherneth. ( etherneth -- )
 | 
					
						
							|  |  |  |     [ etherneth-dmac "Dest   MAC: " write >mac-address . ] keep
 | 
					
						
							|  |  |  |     [ etherneth-smac "Source MAC: " write >mac-address . ] keep
 | 
					
						
							|  |  |  |     [ etherneth-type "Type      : " write .h ] keep
 | 
					
						
							|  |  |  |     drop ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | C-STRUCT: iph | 
					
						
							|  |  |  |     { "uchar" "hl|v" } ! hl is 4 bits, v is 4 bits | 
					
						
							|  |  |  |     { "uchar" "tos" } | 
					
						
							|  |  |  |     { "short" "len" } | 
					
						
							|  |  |  |     { "short" "id" } | 
					
						
							|  |  |  |     { "short" "off" } | 
					
						
							|  |  |  |     { "uchar" "ttl" } | 
					
						
							|  |  |  |     { "uchar" "p" } | 
					
						
							|  |  |  |     { "ushort" "ip_sum" } | 
					
						
							|  |  |  |     { "uint" "ip_src" } | 
					
						
							|  |  |  |     { "uint" "ip_dst" } ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : iph-hl ( iph -- n )
 | 
					
						
							|  |  |  |     iph-hl|v -4 shift ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : iph-v ( iph -- n )
 | 
					
						
							| 
									
										
										
										
											2011-11-23 21:49:33 -05:00
										 |  |  |     iph-hl|v 0x0f bitand ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : set-iph-hl ( n iph -- )
 | 
					
						
							| 
									
										
										
										
											2011-11-23 21:49:33 -05:00
										 |  |  |     [ iph-hl|v 0x0f bitand >r 4 shift r> bitor ] keep
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     set-iph-hl|v ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : set-iph-v ( n iph -- )
 | 
					
						
							| 
									
										
										
										
											2011-11-23 21:49:33 -05:00
										 |  |  |     [ iph-hl|v 0xf0 bitand bitor ] keep
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     set-iph-hl|v ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | C-STRUCT: icmph | 
					
						
							|  |  |  |     { "uchar" "type" } | 
					
						
							|  |  |  |     { "uchar" "code" } | 
					
						
							|  |  |  |     { "short" "chksum" } | 
					
						
							|  |  |  |     { "ushort" "id" } | 
					
						
							|  |  |  |     { "ushort" "seq" } ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | C-STRUCT: udph | 
					
						
							|  |  |  |     { "ushort" "sport" } | 
					
						
							|  |  |  |     { "ushort" "dport" } | 
					
						
							|  |  |  |     { "ushort" "len" } | 
					
						
							|  |  |  |     { "ushort" "check" } ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | C-STRUCT: tcph | 
					
						
							|  |  |  |     { "ushort" "sport" } | 
					
						
							|  |  |  |     { "ushort" "dport" } | 
					
						
							|  |  |  |     { "uint" "seq" } | 
					
						
							|  |  |  |     { "uint" "ack" } | 
					
						
							|  |  |  |     { "uchar" "x2|off" } | 
					
						
							|  |  |  |     { "uchar" "flags" } | 
					
						
							|  |  |  |     { "ushort" "win" } | 
					
						
							|  |  |  |     { "ushort" "sum" } | 
					
						
							|  |  |  |     { "ushort" "urp" } ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : tcph-x2 ( iph -- n )
 | 
					
						
							|  |  |  |     tcph-x2|off -4 shift ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : tcph-off ( iph -- n )
 | 
					
						
							| 
									
										
										
										
											2011-11-23 21:49:33 -05:00
										 |  |  |     tcph-x2|off 0x0f bitand ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : set-tcph-x2 ( n iph -- )
 | 
					
						
							| 
									
										
										
										
											2011-11-23 21:49:33 -05:00
										 |  |  |     [ tcph-x2|off 0x0f bitand >r 4 shift r> bitor ] keep
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     set-tcph-x2|off ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : set-tcph-off ( n iph -- )
 | 
					
						
							| 
									
										
										
										
											2011-11-23 21:49:33 -05:00
										 |  |  |     [ tcph-x2|off 0x0f bitand bitor ] keep
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     set-tcph-x2|off ;
 | 
					
						
							|  |  |  | 
 |