summaryrefslogtreecommitdiff
path: root/doc/examples/lua_examples.rst
blob: 43d88a4765e90ec3b521b3315e0684a755858a2b (plain)
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
.. _lua_examples:

Lua examples
============

Important that accel-ppp was built with lua support ``cmake -DLUA=TRUE`` or if system has more modern lua version, need this sets, for example ``cmake -DLUA=5.3``

Example accel-ppp configuration:

.. code-block:: sh

  [ipoe]
    lua-file=/etc/accel-ppp.lua
    username=lua:username_func

Create /etc/accel-ppp.lua and edit. Example for D-link switches with Option 82:

.. code-block:: sh

  #!lua
    function username_func(pkt)
      v,b1,b2,b3,b4=string.unpack(pkt:agent_remote_id():sub(-4),'bbbb')
      ip=b1..'.'..b2..'.'..b3..'.'..b4
      v,port=string.unpack(string.sub(pkt:agent_circuit_id(),'-1'),'b')
      local username=ip..'-'..port
  --  print(username)
      return username
  end

Object **pkt** has next functions:

**hdr(name)**
  Will return value which contained in DHCP packet header. ``name`` may receive next params: ``xid``, ``ciaddr``, ``giaddr``, ``chaddr``.
 
**ifname()**
  Will return interface name which received packet.

**ipaddr()**
  Will return client ip address exist in packet header.

**hwaddr()**
  Will return client MAC address.

**vlan()**
  Will return client VLAN.

.. code-block:: sh

  local vlan = pkt:vlan()
  local svid = bit.rshift(vlan,16)
  local cvid = bit.band(vlan,0xffff)

**options()**
  Will return table which contains number of DHCP option in received packet.

**option(num)**
  Will return value with option number ``num``.

**agent_circuit_id()**
  Will return ``agent_circuit_id`` option 82.

**agent_remote_id()**
  Will return ``agent_remote_id`` option 82.

.. admonition:: Note:

    All function return type ``string``, except for ``options()``

Also to accel-ppp includes packet **lpack** for disassemble binary data.
It add to object ``string`` additional function ``unpack(binary, fmt)``, where ``binary`` is string which contain binary data, and ``fmt`` is data format. To ``fmt`` may be sets next data types:

**z** - zero terminated string

**p** - string precended by length byte

**P** - string precended by length word

**f** - float

**d** - double

**c** - int8_t

**b** - uint8_t

**h** - int16_t

**H** - uint16_t

**i** - int32_t

**I** - uint32_t

**l** - int64_t

**L** - uint64_t

**<** - little endian

**>** - big endian

**=** - native endian