diff options
Diffstat (limited to 'etc')
-rw-r--r-- | etc/dict.lua | 16 | ||||
-rw-r--r-- | etc/lp.lua | 23 | ||||
-rw-r--r-- | etc/tftp.lua | 17 |
3 files changed, 37 insertions, 19 deletions
diff --git a/etc/dict.lua b/etc/dict.lua index 8c197f5..716b8db 100644 --- a/etc/dict.lua +++ b/etc/dict.lua | |||
@@ -8,11 +8,14 @@ | |||
8 | ----------------------------------------------------------------------------- | 8 | ----------------------------------------------------------------------------- |
9 | -- Load required modules | 9 | -- Load required modules |
10 | ----------------------------------------------------------------------------- | 10 | ----------------------------------------------------------------------------- |
11 | local base = require("base") | ||
12 | local string = require("string") | ||
13 | local table = require("table") | ||
11 | local socket = require("socket") | 14 | local socket = require("socket") |
12 | local url = require("socket.url") | 15 | local url = require("socket.url") |
13 | local tp = require("socket.tp") | 16 | local tp = require("socket.tp") |
14 | 17 | ||
15 | module("socket.dict") | 18 | local dict = module("socket.dict") |
16 | 19 | ||
17 | ----------------------------------------------------------------------------- | 20 | ----------------------------------------------------------------------------- |
18 | -- Globals | 21 | -- Globals |
@@ -28,7 +31,7 @@ local metat = { __index = {} } | |||
28 | 31 | ||
29 | function open(host, port) | 32 | function open(host, port) |
30 | local tp = socket.try(tp.connect(host or HOST, port or PORT, TIMEOUT)) | 33 | local tp = socket.try(tp.connect(host or HOST, port or PORT, TIMEOUT)) |
31 | return setmetatable({tp = tp}, metat) | 34 | return base.setmetatable({tp = tp}, metat) |
32 | end | 35 | end |
33 | 36 | ||
34 | function metat.__index:greet() | 37 | function metat.__index:greet() |
@@ -37,7 +40,8 @@ end | |||
37 | 40 | ||
38 | function metat.__index:check(ok) | 41 | function metat.__index:check(ok) |
39 | local code, status = socket.try(self.tp:check(ok)) | 42 | local code, status = socket.try(self.tp:check(ok)) |
40 | return code, tonumber(socket.skip(2, string.find(status, "^%d%d%d (%d*)"))) | 43 | return code, |
44 | base.tonumber(socket.skip(2, string.find(status, "^%d%d%d (%d*)"))) | ||
41 | end | 45 | end |
42 | 46 | ||
43 | function metat.__index:getdef() | 47 | function metat.__index:getdef() |
@@ -116,7 +120,7 @@ local function parse(u) | |||
116 | if cmd == "m" then | 120 | if cmd == "m" then |
117 | arg = string.gsub(arg, "^:([^:]*)", function(f) t.strat = there(f) end) | 121 | arg = string.gsub(arg, "^:([^:]*)", function(f) t.strat = there(f) end) |
118 | end | 122 | end |
119 | string.gsub(arg, ":([^:]*)$", function(f) t.n = tonumber(f) end) | 123 | string.gsub(arg, ":([^:]*)$", function(f) t.n = base.tonumber(f) end) |
120 | return t | 124 | return t |
121 | end | 125 | end |
122 | 126 | ||
@@ -143,6 +147,8 @@ local function sget(u) | |||
143 | end | 147 | end |
144 | 148 | ||
145 | get = socket.protect(function(gett) | 149 | get = socket.protect(function(gett) |
146 | if type(gett) == "string" then return sget(gett) | 150 | if base.type(gett) == "string" then return sget(gett) |
147 | else return tget(gett) end | 151 | else return tget(gett) end |
148 | end) | 152 | end) |
153 | |||
154 | base.setmetatable(dict, nil) | ||
@@ -9,9 +9,12 @@ | |||
9 | if you have any questions: RFC 1179 | 9 | if you have any questions: RFC 1179 |
10 | ]] | 10 | ]] |
11 | -- make sure LuaSocket is loaded | 11 | -- make sure LuaSocket is loaded |
12 | local io = require("io") | ||
13 | local base = require("base") | ||
14 | local string = require("string") | ||
12 | local socket = require("socket") | 15 | local socket = require("socket") |
13 | local ltn12 = require("ltn12") | 16 | local ltn12 = require("ltn12") |
14 | local test = socket.try | 17 | local lp = module("socket.lp") |
15 | 18 | ||
16 | -- default port | 19 | -- default port |
17 | PORT = 515 | 20 | PORT = 515 |
@@ -28,7 +31,7 @@ local function connect(localhost, option) | |||
28 | local localport = 721 | 31 | local localport = 721 |
29 | local done, err | 32 | local done, err |
30 | repeat | 33 | repeat |
31 | skt = test(socket.tcp()) | 34 | skt = socket.try(socket.tcp()) |
32 | try(skt:settimeout(30)) | 35 | try(skt:settimeout(30)) |
33 | done, err = skt:bind(localhost, localport) | 36 | done, err = skt:bind(localhost, localport) |
34 | if not done then | 37 | if not done then |
@@ -37,8 +40,8 @@ local function connect(localhost, option) | |||
37 | skt = nil | 40 | skt = nil |
38 | else break end | 41 | else break end |
39 | until localport > 731 | 42 | until localport > 731 |
40 | test(skt, err) | 43 | socket.try(skt, err) |
41 | else skt = test(socket.tcp()) end | 44 | else skt = socket.try(socket.tcp()) end |
42 | try(skt:connect(host, port)) | 45 | try(skt:connect(host, port)) |
43 | return { skt = skt, try = try } | 46 | return { skt = skt, try = try } |
44 | end | 47 | end |
@@ -241,9 +244,9 @@ local format_codes = { | |||
241 | -- lp.send | 244 | -- lp.send |
242 | 245 | ||
243 | send = socket.protect(function(file, option) | 246 | send = socket.protect(function(file, option) |
244 | test(file, "invalid file name") | 247 | socket.try(file, "invalid file name") |
245 | test(option and type(option) == "table", "invalid options") | 248 | socket.try(option and base.type(option) == "table", "invalid options") |
246 | local fh = test(io.open(file,"rb")) | 249 | local fh = socket.try(io.open(file,"rb")) |
247 | local datafile_size = fh:seek("end") -- get total size | 250 | local datafile_size = fh:seek("end") -- get total size |
248 | fh:seek("set") -- go back to start of file | 251 | fh:seek("set") -- go back to start of file |
249 | local localhost = socket.dns.gethostname() or os.getenv("COMPUTERNAME") | 252 | local localhost = socket.dns.gethostname() or os.getenv("COMPUTERNAME") |
@@ -270,11 +273,11 @@ send = socket.protect(function(file, option) | |||
270 | lpfile, | 273 | lpfile, |
271 | ctlfn); -- mandatory part of ctl file | 274 | ctlfn); -- mandatory part of ctl file |
272 | if (option.banner) then cfile = cfile .. 'L'..user..'\10' end | 275 | if (option.banner) then cfile = cfile .. 'L'..user..'\10' end |
273 | if (option.indent) then cfile = cfile .. 'I'..tonumber(option.indent)..'\10' end | 276 | if (option.indent) then cfile = cfile .. 'I'..base.tonumber(option.indent)..'\10' end |
274 | if (option.mail) then cfile = cfile .. 'M'..string.sub((option.mail),1,128)..'\10' end | 277 | if (option.mail) then cfile = cfile .. 'M'..string.sub((option.mail),1,128)..'\10' end |
275 | if (fmt == 'p' and option.title) then cfile = cfile .. 'T'..string.sub((option.title),1,79)..'\10' end | 278 | if (fmt == 'p' and option.title) then cfile = cfile .. 'T'..string.sub((option.title),1,79)..'\10' end |
276 | if ((fmt == 'p' or fmt == 'l' or fmt == 'f') and option.width) then | 279 | if ((fmt == 'p' or fmt == 'l' or fmt == 'f') and option.width) then |
277 | cfile = cfile .. 'W'..tonumber(option,width)..'\10' | 280 | cfile = cfile .. 'W'..base.tonumber(option,width)..'\10' |
278 | end | 281 | end |
279 | 282 | ||
280 | con.skt:settimeout(option.timeout or 65) | 283 | con.skt:settimeout(option.timeout or 65) |
@@ -314,3 +317,5 @@ query = socket.protect(function(p) | |||
314 | con.skt:close() | 317 | con.skt:close() |
315 | return data | 318 | return data |
316 | end) | 319 | end) |
320 | |||
321 | base.setmetatable(lp, nil) | ||
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) | ||