liminix

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

tufted.fnl (1158B)


      1 (local tftp (require :tftp))
      2 (local { : realpath} (require :posix.stdlib))
      3 (local { : view } (require :fennel))
      4 
      5 (local options
      6        (match arg
      7          ["-a" ip-address dir]
      8          { :allow ip-address :base-directory (realpath dir)}
      9 
     10          [dir]
     11          { :allow nil :base-directory (realpath dir)}
     12 
     13          []
     14          (error "missing command line parameters")
     15          ))
     16 
     17 (print (.. "TFTP serving from " options.base-directory))
     18 
     19 (fn merge-pathname [directory filename]
     20   (if (directory:match "/$")
     21       (.. directory  filename)
     22       (.. directory "/" filename)))
     23 
     24 (->
     25  (tftp:listen
     26   (fn [file host port]
     27     (if (or (not options.allow) (= host options.allow))
     28         (let [pathname (merge-pathname options.base-directory file)
     29               f (io.open pathname "rb")
     30               size (f:seek "end")]
     31           (f:seek "set" 0)
     32           (var eof? false)
     33           (values
     34            (fn handler [reqlen]
     35              (let [bytes (f:read reqlen)]
     36                (if bytes
     37                    (values true bytes)
     38                    (values false nil))))
     39            size))
     40         (error "host not allowed")))
     41   nil
     42   ["*"]
     43   69)
     44 
     45 (os.exit))