Software /
code /
prosody
Comparison
util/ip.lua @ 8432:da807f4f706c
util.ip: Simplify bitstring method
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Fri, 01 Dec 2017 04:32:20 +0100 |
parent | 8431:a5a03d40a20c |
child | 8433:ca7f8e60410a |
comparison
equal
deleted
inserted
replaced
8431:a5a03d40a20c | 8432:da807f4f706c |
---|---|
4 -- This project is MIT/X11 licensed. Please see the | 4 -- This project is MIT/X11 licensed. Please see the |
5 -- COPYING file in the source package for more information. | 5 -- COPYING file in the source package for more information. |
6 -- | 6 -- |
7 | 7 |
8 local net = require "util.net"; | 8 local net = require "util.net"; |
9 local hex = require "util.hex"; | |
9 | 10 |
10 local ip_methods = {}; | 11 local ip_methods = {}; |
11 | 12 |
12 local ip_mt = { | 13 local ip_mt = { |
13 __index = function (ip, key) | 14 __index = function (ip, key) |
50 | 51 |
51 return setmetatable({ addr = ipStr, packed = packed, proto = proto, zone = zone }, ip_mt); | 52 return setmetatable({ addr = ipStr, packed = packed, proto = proto, zone = zone }, ip_mt); |
52 end | 53 end |
53 | 54 |
54 function ip_methods.bits(ip) | 55 function ip_methods.bits(ip) |
55 ip = (ip.addr):upper(); | 56 return hex.to(ip.packed):upper():gsub(".", hex2bits); |
56 ip:gsub("([^:]*):?", function (c) fields[#fields + 1] = c end); | |
57 if not ip:match(":$") then fields[#fields] = nil; end | |
58 for i, field in ipairs(fields) do | |
59 if field:len() == 0 and i ~= 1 and i ~= #fields then | |
60 for _ = 1, 16 * (9 - #fields) do | |
61 result = result .. "0"; | |
62 end | |
63 else | |
64 for _ = 1, 4 - field:len() do | |
65 result = result .. "0000"; | |
66 end | |
67 for j = 1, field:len() do | |
68 result = result .. hex2bits[field:sub(j, j)]; | |
69 end | |
70 end | |
71 end | |
72 return result; | |
73 end | 57 end |
74 | 58 |
75 function ip_methods.bits_full(ip) | 59 function ip_methods.bits_full(ip) |
76 if ip.proto == "IPv4" then | 60 if ip.proto == "IPv4" then |
77 ip = ip.toV4mapped; | 61 ip = ip.toV4mapped; |