File: types.lua

package info (click to toggle)
lua-ljsyscall 0.12-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,732 kB
  • sloc: ansic: 434; sh: 59; makefile: 3
file content (363 lines) | stat: -rw-r--r-- 10,031 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
-- this module defines the types with metatypes that are always common, so do not get errors redefining metatypes

local require, error, assert, tonumber, tostring,
setmetatable, pairs, ipairs, unpack, rawget, rawset,
pcall, type, table, string = 
require, error, assert, tonumber, tostring,
setmetatable, pairs, ipairs, unpack, rawget, rawset,
pcall, type, table, string

local ffi = require "ffi"

local bit = require "syscall.bit"

local t, ctypes, pt, s = {}, {}, {}, {}
local types = {t = t, pt = pt, s = s, ctypes = ctypes}

local h = require "syscall.helpers"

local addtype, addtype_var, addtype_fn, addraw2 = h.addtype, h.addtype_var, h.addtype_fn, h.addraw2
local addtype1, addtype2, addptrtype = h.addtype1, h.addtype2, h.addptrtype
local ptt, reviter, mktype, istype, lenfn, lenmt, getfd, newfn
  = h.ptt, h.reviter, h.mktype, h.istype, h.lenfn, h.lenmt, h.getfd, h.newfn
local ntohl, ntohl, ntohs, htons, htonl = h.ntohl, h.ntohl, h.ntohs, h.htons, h.htonl
local split, trim, strflag = h.split, h.trim, h.strflag
local align = h.align

local addtypes = {
  char = "char",
  uchar = "unsigned char",
  int = "int",
  uint = "unsigned int",
  int8 = "int8_t",
  uint8 = "uint8_t",
  int16 = "int16_t",
  uint16 = "uint16_t",
  int32 = "int32_t",
  uint32 = "uint32_t",
  int64 = "int64_t",
  uint64 = "uint64_t",
  long = "long",
  ulong = "unsigned long",
}

for k, v in pairs(addtypes) do addtype(types, k, v) end

local addtypes1 = {
  char1 = "char",
  uchar1 = "unsigned char",
  int1 = "int",
  uint1 = "unsigned int",
  int16_1 = "int16_t",
  uint16_1 = "uint16_t",
  int32_1 = "int32_t",
  uint32_1 = "uint32_t",
  int64_1 = "int64_t",
  uint64_1 = "uint64_t",
  long1 = "long",
  ulong1 = "unsigned long",
  intptr1 = "intptr_t",
  size1 = "size_t",
}

for k, v in pairs(addtypes1) do addtype1(types, k, v) end

local addtypes2 = {
  char2 = "char",
  int2 = "int",
  uint2 = "unsigned int",
}

for k, v in pairs(addtypes2) do addtype2(types, k, v) end

local ptrtypes = {
  uintptr = "uintptr_t",
  intptr = "intptr_t",
}

for k, v in pairs(ptrtypes) do addptrtype(types, k, v) end

t.ints = ffi.typeof("int[?]")
t.buffer = ffi.typeof("char[?]") -- TODO rename as chars?
t.string_array = ffi.typeof("const char *[?]")

local mt = {}

mt.iovec = {
  index = {
    base = function(self) return self.iov_base end,
    len = function(self) return self.iov_len end,
  },
}

addtype(types, "iovec", "struct iovec", mt.iovec)

mt.iovecs = {
  __len = function(io) return io.count end,
  __tostring = function(io)
    local s = {}
    for i = 0, io.count - 1 do
      local iovec = io.iov[i]
      s[i + 1] = ffi.string(iovec.iov_base, iovec.iov_len)
    end
    return table.concat(s)
  end;
  __new = function(tp, is)
    if type(is) == 'number' then return ffi.new(tp, is, is) end
    local count = #is
    local iov = ffi.new(tp, count, count)
    local j = 0
    for n, i in ipairs(is) do
      if type(i) == 'string' then
        local buf = t.buffer(#i)
        ffi.copy(buf, i, #i)
        iov.iov[j].iov_base = buf
        iov.iov[j].iov_len = #i
      elseif type(i) == 'number' then
        iov.iov[j].iov_base = t.buffer(i)
        iov.iov[j].iov_len = i
      elseif ffi.istype(t.iovec, i) then
        ffi.copy(iov[n], i, s.iovec)
      elseif type(i) == 'cdata' or type(i) == 'userdata' then -- eg buffer or other structure, userdata if luaffi
        iov.iov[j].iov_base = i
        iov.iov[j].iov_len = ffi.sizeof(i)
      else -- eg table
        iov.iov[j] = i
      end
      j = j + 1
    end
    return iov
  end,
}

addtype_var(types, "iovecs", "struct {int count; struct iovec iov[?];}", mt.iovecs)

-- convert strings to inet addresses and the reverse
local function inet4_ntop(src)
  local b = pt.uchar(src)
  return b[0] .. "." .. b[1] .. "." .. b[2] .. "." .. b[3]
end

local function inet6_ntop(src)
  local a = src.s6_addr
  local parts = {256*a[0] + a[1], 256*a[2] + a[3],   256*a[4] + a[5],   256*a[6] + a[7],
                 256*a[8] + a[9], 256*a[10] + a[11], 256*a[12] + a[13], 256*a[14] + a[15]}

  for i = 1, #parts do parts[i] = string.format("%x", parts[i]) end

  local start, max = 0, 0
  for i = 1, #parts do
    if parts[i] == "0" then
      local count = 0
      for j = i, #parts do
        if parts[j] == "0" then count = count + 1 else break end
      end
      if count > max then max, start = count, i end
    end
  end

  if max > 2 then
    parts[start] = ""
    if start == 1 or start + max == 9 then parts[start] = ":" end
    if start == 1 and start + max == 9 then parts[start] = "::" end 
    for i = 1, max - 1 do table.remove(parts, start + 1) end
  end

  return table.concat(parts, ":")
end

local function inet4_pton(src)
  local ip4 = split("%.", src)
  if #ip4 ~= 4 then error "malformed IP address" end
  return htonl(tonumber(ip4[1]) * 0x1000000 + tonumber(ip4[2]) * 0x10000 + tonumber(ip4[3]) * 0x100 + tonumber(ip4[4]))
end

local function hex(str) return tonumber("0x" .. str) end

local function inet6_pton(src, addr)
  -- TODO allow form with decimals at end for ipv4 addresses
  local ip8 = split(":", src)
  if #ip8 > 8 then return nil end
  local before, after = src:find("::")
  before, after = src:sub(1, before - 1), src:sub(after + 1)
  if before then
    if #ip8 == 8 then return nil end -- must be some missing
    if before == "" then before = "0" end
    if after == "" then after = "0" end
    src = before .. ":" .. string.rep("0:", 8 - #ip8 + 1) .. after
    ip8 = split(":", src)
  end
  for i = 1, 8 do
    addr.s6_addr[i * 2 - 1] = bit.band(hex(ip8[i]), 0xff)
    addr.s6_addr[i * 2 - 2] = bit.rshift(hex(ip8[i]), 8)
  end
  return addr
end

local inaddr = strflag {
  ANY = "0.0.0.0",
  LOOPBACK = "127.0.0.1",
  BROADCAST = "255.255.255.255",
}

local in6addr = strflag {
  ANY = "::",
  LOOPBACK = "::1",
}

 -- given this address and a mask, return a netmask and broadcast as in_addr
local function mask_bcast(address, netmask)
  local bcast = t.in_addr()
  local nmask = t.in_addr() -- TODO
  if netmask > 32 then error("bad netmask " .. netmask) end
  if netmask < 32 then nmask.s_addr = htonl(bit.rshift(-1, netmask)) end
  bcast.s_addr = bit.bor(tonumber(address.s_addr), nmask.s_addr)
  return {address = address, broadcast = bcast, netmask = nmask}
end

mt.in_addr = {
  __index = {
    get_mask_bcast = function(addr, mask) return mask_bcast(addr, mask) end,
  },
  newindex = {
    addr = function(addr, s)
      if ffi.istype(t.in_addr, s) then
        addr.s_addr = s.s_addr
      elseif type(s) == "string" then
        if inaddr[s] then s = inaddr[s] end
        addr.s_addr = inet4_pton(s)
      else -- number
        addr.s_addr = htonl(s)
      end
    end,
  },
  __tostring = inet4_ntop,
  __new = function(tp, s)
    local addr = ffi.new(tp)
    if s then addr.addr = s end
    return addr
  end,
  __len = lenfn,
}

addtype(types, "in_addr", "struct in_addr", mt.in_addr)

mt.in6_addr = {
  __tostring = inet6_ntop,
  __new = function(tp, s)
    local addr = ffi.new(tp)
    if s then
      if in6addr[s] then s = in6addr[s] end
      addr = inet6_pton(s, addr)
    end
    return addr
  end,
  __len = lenfn,
}

addtype(types, "in6_addr", "struct in6_addr", mt.in6_addr)

-- ip, udp types. Need endian conversions.
local ptchar = ffi.typeof("char *")
local uint16 = ffi.typeof("uint16_t[1]")

local function ip_checksum(buf, size, c, notfinal)
  c = c or 0
  local b8 = ffi.cast(ptchar, buf)
  local i16 = uint16()
  for i = 0, size - 1, 2 do
    ffi.copy(i16, b8 + i, 2)
    c = c + i16[0]
  end
  if size % 2 == 1 then
    i16[0] = 0
    ffi.copy(i16, b8[size - 1], 1)
    c = c + i16[0]
  end

  local v = bit.band(c, 0xffff)
  if v < 0 then v = v + 0x10000 end -- positive
  c = bit.rshift(c, 16) + v
  c = c + bit.rshift(c, 16)

  if not notfinal then c = bit.bnot(c) end
  if c < 0 then c = c + 0x10000 end -- positive
  return c
end

mt.iphdr = {
  index = {
    checksum = function(i) return function(i)
      i.check = 0
      i.check = ip_checksum(i, s.iphdr)
      return i.check
    end end,
  },
}

addtype(types, "iphdr", "struct iphdr", mt.iphdr)

local udphdr_size = ffi.sizeof("struct udphdr")

-- ugh, naming problems as cannot remove namespace as usual
-- checksum = function(u, ...) return 0 end, -- TODO checksum, needs IP packet info too. as method.
mt.udphdr = {
  index = {
    src = function(u) return ntohs(u.source) end,
    dst = function(u) return ntohs(u.dest) end,
    length = function(u) return ntohs(u.len) end,
    checksum = function(i) return function(i, ip, body)
      local bip = pt.char(ip)
      local bup = pt.char(i)
      local cs = 0
      -- checksum pseudo header
      cs = ip_checksum(bip + ffi.offsetof(ip, "saddr"), 4, cs, true)
      cs = ip_checksum(bip + ffi.offsetof(ip, "daddr"), 4, cs, true)
      local pr = t.char2(0, 17) -- c.IPPROTO.UDP
      cs = ip_checksum(pr, 2, cs, true)
      cs = ip_checksum(bup + ffi.offsetof(i, "len"), 2, cs, true)
      -- checksum udp header
      i.check = 0
      cs = ip_checksum(i, udphdr_size, cs, true)
      -- checksum body
      cs = ip_checksum(body, i.length - udphdr_size, cs)
      if cs == 0 then cs = 0xffff end
      i.check = cs
      return cs
    end end,
  },
  newindex = {
    src = function(u, v) u.source = htons(v) end,
    dst = function(u, v) u.dest = htons(v) end,
    length = function(u, v) u.len = htons(v) end,
  },
}

addtype(types, "udphdr", "struct udphdr", mt.udphdr)

mt.ethhdr = {
  -- TODO
}

addtype(types, "ethhdr", "struct ethhdr", mt.ethhdr)

mt.winsize = {
  index = {
    row = function(ws) return ws.ws_row end,
    col = function(ws) return ws.ws_col end,
    xpixel = function(ws) return ws.ws_xpixel end,
    ypixel = function(ws) return ws.ws_ypixel end,
  },
  newindex = {
    row = function(ws, v) ws.ws_row = v end,
    col = function(ws, v) ws.ws_col = v end,
    xpixel = function(ws, v) ws.ws_xpixel = v end,
    ypixel = function(ws, v) ws.ws_ypixel = v end,
  },
  __new = newfn,
}

addtype(types, "winsize", "struct winsize", mt.winsize)

return types