Comparison

util/ip.lua @ 8434:ec8f37baffaa

util.ip: Simplify creation of IPv6-mapped IPv4 addresses
author Kim Alvefur <zash@zash.se>
date Fri, 01 Dec 2017 04:33:49 +0100
parent 8433:ca7f8e60410a
child 8435:47195f035d2f
comparison
equal deleted inserted replaced
8433:ca7f8e60410a 8434:ec8f37baffaa
153 else 153 else
154 return 40; 154 return 40;
155 end 155 end
156 end 156 end
157 157
158 local function toV4mapped(ip)
159 local fields = {};
160 local ret = "::ffff:";
161 ip:gsub("([^.]*).?", function (c) fields[#fields + 1] = tonumber(c) end);
162 ret = ret .. ("%02x"):format(fields[1]);
163 ret = ret .. ("%02x"):format(fields[2]);
164 ret = ret .. ":"
165 ret = ret .. ("%02x"):format(fields[3]);
166 ret = ret .. ("%02x"):format(fields[4]);
167 return new_ip(ret, "IPv6");
168 end
169
170 function ip_methods:toV4mapped() 158 function ip_methods:toV4mapped()
171 if self.proto ~= "IPv4" then return nil, "No IPv4 address" end 159 if self.proto ~= "IPv4" then return nil, "No IPv4 address" end
172 local value = toV4mapped(self.addr); 160 local value = new_ip("::ffff:" .. self.normal);
173 self.toV4mapped = value;
174 return value; 161 return value;
175 end 162 end
176 163
177 function ip_methods:label() 164 function ip_methods:label()
178 local value; 165 local value;