diff options
author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-11-27 07:58:04 +0000 |
---|---|---|
committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-11-27 07:58:04 +0000 |
commit | 7c97e8e40aaa665226fb54449773dc3134e755b2 (patch) | |
tree | 47888d4c924fc24bf3b355bf58120ea3cdc74bc4 /etc/tftp.lua | |
parent | eb0fc857ddea6f084d338589e2a33d3e7d4eade6 (diff) | |
download | luasocket-7c97e8e40aaa665226fb54449773dc3134e755b2.tar.gz luasocket-7c97e8e40aaa665226fb54449773dc3134e755b2.tar.bz2 luasocket-7c97e8e40aaa665226fb54449773dc3134e755b2.zip |
Almost ready for beta3
Diffstat (limited to 'etc/tftp.lua')
-rw-r--r-- | etc/tftp.lua | 17 |
1 files changed, 12 insertions, 5 deletions
diff --git a/etc/tftp.lua b/etc/tftp.lua index f4af8bc..83a08b9 100644 --- a/etc/tftp.lua +++ b/etc/tftp.lua | |||
@@ -8,11 +8,14 @@ | |||
8 | ----------------------------------------------------------------------------- | 8 | ----------------------------------------------------------------------------- |
9 | -- Load required files | 9 | -- Load required files |
10 | ----------------------------------------------------------------------------- | 10 | ----------------------------------------------------------------------------- |
11 | local base = require("base") | ||
12 | local table = require("table") | ||
13 | local math = require("math") | ||
14 | local string = require("string") | ||
11 | local socket = require("socket") | 15 | local socket = require("socket") |
12 | local ltn12 = require("ltn12") | 16 | local ltn12 = require("ltn12") |
13 | local url = require("socket.url") | 17 | local url = require("socket.url") |
14 | 18 | local tftp = module("socket.tftp") | |
15 | module("socket.tftp") | ||
16 | 19 | ||
17 | ----------------------------------------------------------------------------- | 20 | ----------------------------------------------------------------------------- |
18 | -- Program constants | 21 | -- Program constants |
@@ -73,16 +76,18 @@ end | |||
73 | local function tget(gett) | 76 | local function tget(gett) |
74 | local retries, dgram, sent, datahost, dataport, code | 77 | local retries, dgram, sent, datahost, dataport, code |
75 | local last = 0 | 78 | local last = 0 |
79 | socket.try(gett.host, "missing host") | ||
76 | local con = socket.try(socket.udp()) | 80 | local con = socket.try(socket.udp()) |
77 | local try = socket.newtry(function() con:close() end) | 81 | local try = socket.newtry(function() con:close() end) |
78 | -- convert from name to ip if needed | 82 | -- convert from name to ip if needed |
79 | gett.host = try(socket.dns.toip(gett.host)) | 83 | gett.host = try(socket.dns.toip(gett.host)) |
80 | con:settimeout(1) | 84 | con:settimeout(1) |
81 | -- first packet gives data host/port to be used for data transfers | 85 | -- first packet gives data host/port to be used for data transfers |
86 | local path = string.gsub(gett.path or "", "^/", "") | ||
87 | path = url.unescape(path) | ||
82 | retries = 0 | 88 | retries = 0 |
83 | repeat | 89 | repeat |
84 | sent = try(con:sendto(RRQ(gett.path, "octet"), | 90 | sent = try(con:sendto(RRQ(path, "octet"), gett.host, gett.port)) |
85 | gett.host, gett.port)) | ||
86 | dgram, datahost, dataport = con:receivefrom() | 91 | dgram, datahost, dataport = con:receivefrom() |
87 | retries = retries + 1 | 92 | retries = retries + 1 |
88 | until dgram or datahost ~= "timeout" or retries > 5 | 93 | until dgram or datahost ~= "timeout" or retries > 5 |
@@ -144,6 +149,8 @@ local function sget(u) | |||
144 | end | 149 | end |
145 | 150 | ||
146 | get = socket.protect(function(gett) | 151 | get = socket.protect(function(gett) |
147 | if type(gett) == "string" then return sget(gett) | 152 | if base.type(gett) == "string" then return sget(gett) |
148 | else return tget(gett) end | 153 | else return tget(gett) end |
149 | end) | 154 | end) |
155 | |||
156 | base.setmetatable(tftp, nil) | ||