Is it possible to write a simple TFTP server as an iRule to return static content? I see some examples for load balancing TFTP requests, but not for actually acting as a TFTP server. I have a very small and static TFTP file that I need to distribute, and rather than running a dedicated TFTP server it would be far better if it could be deployed as an iRule.
Is this possible? Any resources I should be looking at?
It’s probably possible to do this, though I’m wondering how the encoding of the file in chunks can be done most efficiently. There’s an example of a simple UDP server implemented in an iRule here:
I don’t think the protocol handling would be that difficult, the problem is how to handle serving up the file, that command is disabled in iRules. Here’s a start, maybe someone else has some ideas?
when CLIENT_ACCEPTED {
log local0. "Client connected from [IP::client_addr]"
binary scan [UDP::payload] xc opcode
log local0. "Opcode is $opcode"
if { $opcode == 1 } {
binary scan [UDP::payload] xxa* string
log local0. "String is $string"
set stringlist [split $string \000]
set file [lindex $stringlist 0]
set mode [lindex $stringlist 1]
log local0. "File is $file, Mode is $mode"
if { $mode != "octet" && $mode != "netascii" } {
reject
} else {
log local0. "Send the file!"
write goes here
}
} else { reject }
}
BTW, the code above is hacked from the example jquinby mentioned on the TCL wiki. Obviously don’t need a few of those variables, just there for development.
I think dropping the file into some sort of class (along the lines of the http-server-in-an-irule) is a possibility, but the TFTP protocol specifies 512 byte chunks sent to the client after each previous one is ACK’ed.
1. client sends RRQ (opcode 1) with filename
2. Server sends block 1, client rx’s and ACKs block 1.
3. Server sends block 2, client acks block 2, and so on.
4. As soon as the client gets a block thats <512 bytes, it knows that this is the final block.
We can also do away with the necessity of handling write-requests (WRQ, opcode 2), since this is read-only. Respond to them with opcode 5 (“error”, along with an error number and some useful ascii text).
All that remains (and I suppose it’s easy enough for a non-developer to say) is a method of storing the file, then iterating over it in 512 byte chunks until one of length < 512 can be sent.
For better compliance with the TFTP spec, the server is expected to deal well with a “resend block X” command sent by the client.
when CLIENT_ACCEPTED {
set blockdata(1) [binary format H* 000300014620494e495452442e494d473b31\
202020202020202020202020202020202020202020202020202020202020202020202020\
202020202020202020202020202020202020202020202020202020202020202020202020\
202020202020202020202020202020202020202020202020202020202020202020202020\
202020202020202020202020202020202020202020202020202020202020202020202020\
202020202020202020202020202020202020202020202020202020202020202020202020\
2020202020202020202020202020202009696e697472642e696d670d0a4620524541444d\
452e3b312020202020202020202020202020202020202020202020202020202020202020\
202020202020202020202020202020202020202020202020202020202020202020202020\
202020202020202020202020202020202020202020202020202020202020202020202020\
202020202020202020202020202020202020202020202020202020202020202020202020\
202020202020202020202020202020202020202020202020202020202020202020202020\
202020202020202020202020202020202020202020202009524541444d450d0a4620564d\
4c494e555a2e3b3120202020202020202020202020202020202020202020202020202020\
202020202020202020202020202020202020202020202020202020202020]
set blockdata(2) [binary format H* 000300022020202020202020202020202020\
202020202020202020202020202020202020202020202020202020202020202020202020\
202020202020202020202020202020202020202020202020202020202020202020202020\
202020202020202020202020202020202020202020202020202020202020202020202020\
20202020202020202020202020202020202009766d6c696e757a0d0a]
node [IP::client_addr] [TCP::client_port]
snat [IP::local_addr]
}
when CLIENT_DATA {
UDP::payload replace 0 [UDP::payload length] $blockdata(1)
}
when SERVER_DATA {
binary scan [UDP::payload] I tmp
set opcode [expr $tmp >> 16]
set blockid [expr $tmp & 0xffff]
if { $blockid < [array size blockdata] } {
UDP::respond $blockdata([expr $blockid + 1])
}
UDP::drop
}
it works when I connect from linux tftp client… right, this irule is not complete. I see that citizen_elah and jquinby already started a good one. so this can be merge with irule in previous post. For example, no filename checking and no type checking, so this irule will always return same data no matter what filename/mode client ask for.
A little closer. This works with the pre-embedded blocks that natty76 is using in his. I’ve created an external file class called “tftp_file”. Just need to do that bit. Two things left undone:
1. client mode switching (netascii v. binary)
2. client retries (resend block X)
when CLIENT_ACCEPTED {
log local0. "Client connected from [IP::client_addr]"
binary scan [UDP::payload] xc opcode
log local0. "Opcode is $opcode"
if { $opcode == 1 } {
binary scan [UDP::payload] xxa* string
log local0. "String is $string"
set stringlist [split $string \000]
set file [lindex $stringlist 0]
set mode [lindex $stringlist 1]
log local0. "File is $file, Mode is $mode"
if { $mode != "octet" && $mode != "netascii" } {
reject
} else {
log local0. "Send the file!"
set blockdata(1) [binary format H* 000300014620494e495452442e494d473b3120202020202020202020202020202020202020\
20202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020\
202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202\
020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020\
202009696e697472642e696d670d0a4620524541444d452e3b312020202020202020202020202020202020202020202020202020202020202020\
20202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020\
20202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020\
20202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202009524541444d45\
0d0a4620564d4c494e555a2e3b3120202020202020202020202020202020202020202020202020202020202020202020202020202020202020202\
020202020202020202020202020]
set blockdata(2) [binary format H* 0003000220202020202020202020202020202020202020202020202020202020202020202020\
2020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020\
202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202009766d6c696e757a0d0a]
UDP::respond $blockdata(1)
UDP::respond $blockdata(2)
}
} else { reject }
}
Haven’t quite figured out the correct ascii to hex conversion. I am cutting into blocks first, then prepending the blocks with 0003xxxx, where xxxx is the blockID.
Ok, got the enconding figured out (I ripped it straight from the Tcl wiki). Here’s the latest version.
when CLIENT_ACCEPTED {
log local0. "Client connected from [IP::client_addr]"
binary scan [UDP::payload] xc opcode
log local0. "Opcode is $opcode"
if { $opcode == 1 } {
binary scan [UDP::payload] xxa* string
log local0. "String is $string"
set stringlist [split $string \000]
set file [lindex $stringlist 0]
set mode [lindex $stringlist 1]
log local0. "File is $file, Mode is $mode"
if { $mode != "octet" && $mode != "netascii" } {
reject
} else {
log local0. "Send the file!"
set data [binary format xcSa* 3 1 [string range $::tftp_file 0 511]]
log local0. "data = $data"
UDP::respond $data
}
} else { reject }
}
So - incrementing the block ID (the “1” in the format command above) along with moving to the next 512 bytes is all that’s left, modulo the changes citizen_elah has added that improve the protocol handling. This is pretty close. This will hopefully let us avoid having to embed the file into the actual rule.
OK, stuck again. I can’t test it because I can’t get it to parse right in the IRE. I’m missing something dumb here, but am going cross-eyed trying to find it. I’ll post what I have and come back to it later after I clear my head.
(updated with some comments and a little visual cleanup)
when CLIENT_ACCEPTED {
log local0. "Client connected from [IP::client_addr]"
binary scan [UDP::payload] xc opcode
log local0. "Opcode is $opcode"
if { $opcode == 1 } {
binary scan [UDP::payload] xxa* string
log local0. "String is $string"
set stringlist [split $string \000]
set file [lindex $stringlist 0]
set mode [lindex $stringlist 1]
log local0. "File is $file, Mode is $mode"
if { $mode != "octet" && $mode != "netascii" } {
reject
} else {
log local0. "Send the file!"
set block_id 1
set block_start 0
set block_len 512
set last 0
while { $last < 1 } {
log local0. "Block number $block_id"
set data [binary format xcSa* 3 $block_id [string range $::tftp_file $block_start $block_end]]
test for the last block - is it less than 512 bytes? If so, set a flag
if { [string length [string range $::tftp_file $block_start $block_end]] < 512 } then {
set last 1
}
log local0. "data = $data"
send the data
UDP::respond $data
increment block_id and shift the pointers to the
next block of data to send
incr block_id
block_start = block_len
set block_len = block_len + 512
}
We should be breaking out of the while loop now
The last block should have been sent.
}
}
} else { reject }
}
Pay particular attention to the treatment of text within the class file. No newlines are allowed, the block must be quoted, and there must be a trailing comma.
The warning about memory is also useful - the file will be read into (and served from) memory, so be wary of using this method to transfer giant files. Not that anyone would do that with TFTP, but you never know.
If you’re using this iRule to serve binary data, there may need to be additional tweaks. You might have uuencode it first, then add the b64decode statement (see the above page and the lines for sending the PNG file).
just a comment that you may forward request onto server side which you will get 2 things in return
- server will send file from ephemeral port (not port 69)
- retry in case packet loss. you can have routine in SERVER_DATA that replies content based on ack it receives. if let say block 10 is loss, client will resend ack-for block 9. irule can detect block id from ack message and response with block “ack + 1”
however, I didnt read RFC, feel free to correct me if I am wrong.
The behavior you describe is correct. The way I read it, the server should wait for ACK(block) before sending block(ACK+1). In any case, the final rule looks great. I was a little surprised to not find one out there already.