diff options
| author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-07-01 03:32:09 +0000 |
|---|---|---|
| committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-07-01 03:32:09 +0000 |
| commit | 7115c12fbc9aae1cd46fdf049697a27fb996181a (patch) | |
| tree | 2e918f54f729766701aabdef488a6461bc623da1 /etc | |
| parent | 7aaba59909e8527190694285f56ca68772c97f6a (diff) | |
| download | luasocket-7115c12fbc9aae1cd46fdf049697a27fb996181a.tar.gz luasocket-7115c12fbc9aae1cd46fdf049697a27fb996181a.tar.bz2 luasocket-7115c12fbc9aae1cd46fdf049697a27fb996181a.zip | |
Moving on to beta2.
Diffstat (limited to 'etc')
| -rw-r--r-- | etc/tftp.lua | 25 |
1 files changed, 13 insertions, 12 deletions
diff --git a/etc/tftp.lua b/etc/tftp.lua index 2fc914e..70db050 100644 --- a/etc/tftp.lua +++ b/etc/tftp.lua | |||
| @@ -72,50 +72,51 @@ local function tget(gett) | |||
| 72 | local retries, dgram, sent, datahost, dataport, code | 72 | local retries, dgram, sent, datahost, dataport, code |
| 73 | local last = 0 | 73 | local last = 0 |
| 74 | local con = socket.try(socket.udp()) | 74 | local con = socket.try(socket.udp()) |
| 75 | local try = socket.newtry(function() con:close() end) | ||
| 75 | -- convert from name to ip if needed | 76 | -- convert from name to ip if needed |
| 76 | gett.host = socket.try(socket.dns.toip(gett.host)) | 77 | gett.host = try(socket.dns.toip(gett.host)) |
| 77 | con:settimeout(1) | 78 | con:settimeout(1) |
| 78 | -- first packet gives data host/port to be used for data transfers | 79 | -- first packet gives data host/port to be used for data transfers |
| 79 | retries = 0 | 80 | retries = 0 |
| 80 | repeat | 81 | repeat |
| 81 | sent = socket.try(con:sendto(RRQ(gett.path, "octet"), | 82 | sent = try(con:sendto(RRQ(gett.path, "octet"), |
| 82 | gett.host, gett.port)) | 83 | gett.host, gett.port)) |
| 83 | dgram, datahost, dataport = con:receivefrom() | 84 | dgram, datahost, dataport = con:receivefrom() |
| 84 | retries = retries + 1 | 85 | retries = retries + 1 |
| 85 | until dgram or datahost ~= "timeout" or retries > 5 | 86 | until dgram or datahost ~= "timeout" or retries > 5 |
| 86 | socket.try(dgram, datahost) | 87 | try(dgram, datahost) |
| 87 | -- associate socket with data host/port | 88 | -- associate socket with data host/port |
| 88 | socket.try(con:setpeername(datahost, dataport)) | 89 | try(con:setpeername(datahost, dataport)) |
| 89 | -- default sink | 90 | -- default sink |
| 90 | local sink = gett.sink or ltn12.sink.null() | 91 | local sink = gett.sink or ltn12.sink.null() |
| 91 | -- process all data packets | 92 | -- process all data packets |
| 92 | while 1 do | 93 | while 1 do |
| 93 | -- decode packet | 94 | -- decode packet |
| 94 | code = get_OP(dgram) | 95 | code = get_OP(dgram) |
| 95 | socket.try(code ~= OP_ERROR, get_ERROR(dgram)) | 96 | try(code ~= OP_ERROR, get_ERROR(dgram)) |
| 96 | socket.try(code == OP_DATA, "unhandled opcode " .. code) | 97 | try(code == OP_DATA, "unhandled opcode " .. code) |
| 97 | -- get data packet parts | 98 | -- get data packet parts |
| 98 | local block, data = split_DATA(dgram) | 99 | local block, data = split_DATA(dgram) |
| 99 | -- if not repeated, write | 100 | -- if not repeated, write |
| 100 | if block == last+1 then | 101 | if block == last+1 then |
| 101 | socket.try(sink(data)) | 102 | try(sink(data)) |
| 102 | last = block | 103 | last = block |
| 103 | end | 104 | end |
| 104 | -- last packet brings less than 512 bytes of data | 105 | -- last packet brings less than 512 bytes of data |
| 105 | if string.len(data) < 512 then | 106 | if string.len(data) < 512 then |
| 106 | socket.try(con:send(ACK(block))) | 107 | try(con:send(ACK(block))) |
| 107 | socket.try(con:close()) | 108 | try(con:close()) |
| 108 | socket.try(sink(nil)) | 109 | try(sink(nil)) |
| 109 | return 1 | 110 | return 1 |
| 110 | end | 111 | end |
| 111 | -- get the next packet | 112 | -- get the next packet |
| 112 | retries = 0 | 113 | retries = 0 |
| 113 | repeat | 114 | repeat |
| 114 | sent = socket.try(con:send(ACK(last))) | 115 | sent = try(con:send(ACK(last))) |
| 115 | dgram, err = con:receive() | 116 | dgram, err = con:receive() |
| 116 | retries = retries + 1 | 117 | retries = retries + 1 |
| 117 | until dgram or err ~= "timeout" or retries > 5 | 118 | until dgram or err ~= "timeout" or retries > 5 |
| 118 | socket.try(dgram, err) | 119 | try(dgram, err) |
| 119 | end | 120 | end |
| 120 | end | 121 | end |
| 121 | 122 | ||
