summaryrefslogtreecommitdiff
path: root/com32/lua/src/dhcp.c
blob: af948131f7c1459809425b9b1d7aa89427a19791 (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
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
/* ----------------------------------------------------------------------- *
 *
 *   Copyright 2007 H. Peter Anvin - All Rights Reserved
 *   Copyright 2011 Timothy J Gleason <timmgleason_at_gmail.com> - All Rights Reserved
 *
 *   Permission is hereby granted, free of charge, to any person
 *   obtaining a copy of this software and associated documentation
 *   files (the "Software"), to deal in the Software without
 *   restriction, including without limitation the rights to use,
 *   copy, modify, merge, publish, distribute, sublicense, and/or
 *   sell copies of the Software, and to permit persons to whom
 *   the Software is furnished to do so, subject to the following
 *   conditions:
 *
 *   The above copyright notice and this permission notice shall
 *   be included in all copies or substantial portions of the Software.
 *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 *   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 *   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 *   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 *   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 *   OTHER DEALINGS IN THE SOFTWARE.
 *
 * ----------------------------------------------------------------------- */

/*
 * dhcp.c
 *
 * Adds DHCPINFO functionality to the lua.c32 binary
 *
 * gettable() returns a table of the BOOTP message fields returned by
 * the DHCP server for use in a Lua pxeboot script
 * See http://tools.ietf.org/html/rfc1542
 *
 *	lua key value		RFC key
 * ----------------------------------------------------------------------- 
 *	opcode			op	 message opcode 
 *	hardware.type		htype	 Hardware address type 
 *	hardware.length		hlen	 Hardware address length 
 *	hops			hops	 Used by relay agents 
 *	transaction.id		xid	 transaction id 
 *	elapsed.seconds		secs	 Secs elapsed since client boot 
 *	flags			flags	 DHCP Flags field 
 *	client.ip.addr		ciaddr	 client IP addr 
 *	your.ip.addr		yiaddr	 'Your' IP addr. (from server) 
 *	server.ip.addr		siaddr	 Boot server IP addr 
 *	gateway.ip.addr		giaddr	 Relay agent IP addr 
 *	client.mac		chaddr	 Client hardware addr 
 *	server.hostname		sname	 Optl. boot server hostname 
 * 	boot.file		file	 boot file name (ascii path) 
 *	magic.cookie		cookie	 Magic cookie 
 *
 * getoptions() returns a table of the DHCP Options field of the BOOTP
 * message returned by the DHCP server for use in a Lua pxeboot script.
 * Many of the options are reurned formatted in as strings in a standard,
 * recognizable format, such as IP addresses.
 *
 * 1, 2, and 4 byte numerical options are returned as integers. 
 *
 * Other Options with non-standard formats are returned as strings of the 
 * raw binary number that was returned by the DHCP server and must be decoded
 * in a Lua script 
 *
 * The Options table returns the Option code as the key except where there
 * are multiple values returned. In those cases, an extra key increment number
 * is added to allow individual access to each Option value.
 * 
 *	lua key value		value Name
 * ----------------------------------------------------------------------- 
 *	1			Subnet Mask
 *	6.1			DNS Server [element 1]
 *	6.2			DNS Server [element 2]
 *	6.3			DNS Server [element 3]
 *	209			PXE Configuration File
 *	21.1			Policy Filter [element 1]
 *	21.2			Policy Filter [element 2]
 *	
 * Options that can have a list of values, but contain only one (like Option 6)
 * will not return with .sub key values.	
 *
 * Usage:
            t = dhcp.gettable()

            for k,v in pairs(t) do
              print(k.." : "..v)
            end
 */

#include <stdio.h>
#include "dhcp.h"
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
#include <syslinux/pxe.h>

#define STR_BUF_SIZE	129 /* Sized to accomdate File field in BOOTP message */

void
ip_address_list(lua_State *L, uint8_t* value, uint8_t len, uint8_t option )
{
  static char	op_name[64];
  static char	op_value[255];
  int		loop;

  loop = len/4;

  if ( loop == 1) {
    sprintf(op_name, "%u", option);
    lua_pushstring(L, op_name);
    sprintf(op_value, "%u.%u.%u.%u", value[0], value[1], value[2], value[3]);
    lua_pushstring(L, op_value);
    lua_settable(L,-3);
  } else {
      for (int done = 0; done < loop; done++){
	sprintf(op_name, "%u.%d", option, done+1);
	lua_pushstring(L, op_name);
	sprintf(op_value, "%u.%u.%u.%u", value[0+(done*4)], value[1+(done*4)], value[2+(done*4)], value[3+(done*4)]);
	lua_pushstring(L, op_value);
	lua_settable(L,-3);
      }
  }

}

static int dhcp_getoptions(lua_State *L)
{
  void*		dhcpdata = 0;
  dhcp_t*	dhcp = 0;
  size_t	dhcplen = 0;

  /* Append the DHCP info */
  if (pxe_get_cached_info(PXENV_PACKET_TYPE_DHCP_ACK,
    &dhcpdata, &dhcplen))
  {
    return 0;
  }

  dhcp = (dhcp_t*)dhcpdata;

  lua_newtable(L);

  int		done = 0;
  uint8_t*	ptr = (uint8_t*)&dhcp->options;
  uint8_t	len;
  uint8_t	option;
  uint8_t*	value;
  static char	op_name[64];

  do {
      option = *ptr++;
      len = *ptr++;
      value = ptr;
      ptr += len;
      switch (option) {
// IP Address formatted lists, including singles
	case 1:
	case 3:
	case 4:
	case 5:
	case 6:
	case 7:
	case 8:
	case 9:
	case 10:
	case 11:
	case 16:
	case 21: /* Policy Filters - address/mask */
	case 28:
	case 32:
	case 33: /* Static routes - destination/router */
	case 41:
	case 42:
	case 44:
	case 45:
	case 47:
	case 48:
	case 49:
	case 50:
	case 51:
	case 54:
	case 65:
	case 68:
	case 69:
	case 70:
	case 71:
	case 72:
	case 73:
	case 74:
	case 75:
	case 76:
	  ip_address_list(L, value, len, option);
          break;
// 4byte options - numerical
        case 2:
        case 24:
        case 35:
        case 38:
        case 58:
        case 59:
        case 211:
	  sprintf(op_name, "%u", option);
	  lua_pushstring(L, op_name);
	  lua_pushinteger(L, ntohl(*(long*)value));
	  lua_settable(L,-3);
          break;
// 2byte options -numerical
        case 13:
        case 22:
        case 26:
        case 57:
	  sprintf(op_name, "%u", option);
	  lua_pushstring(L, op_name);
	  lua_pushinteger(L, ntohs(*(short*)value));
	  lua_settable(L,-3);
          break;
// 1byte options - numerical
        case 19:
        case 20:
        case 23:
        case 27:
        case 29:
        case 30:
        case 31:
        case 34:
        case 36:
        case 37:
        case 39:
        case 46:
        case 52:
        case 53:
	  sprintf(op_name, "%u", option);
	  lua_pushstring(L, op_name);
	  lua_pushinteger(L, *value);
	  lua_settable(L,-3);
          break;
        case 255: 
          done = 1;
	  break;
        default:
	  sprintf(op_name, "%u", option);
	  lua_pushstring(L, op_name);
	  lua_pushlstring(L, (const char*)value, len);
	  lua_settable(L,-3);
          break;
      }

    } while (!done);

  return 1;
}

static int	dhcp_gettable(lua_State *L)
{
  void*		dhcpdata = 0;
  dhcp_t*	dhcp = 0;
  size_t	dhcplen = 0;
  static char	dhcp_arg[STR_BUF_SIZE];

  /* Append the DHCP info */
  if (pxe_get_cached_info(PXENV_PACKET_TYPE_DHCP_ACK,
    &dhcpdata, &dhcplen))
  {
    return 0;
  }

  dhcp = (dhcp_t*)dhcpdata;


  lua_newtable(L);

  lua_pushstring(L, "opcode");
  lua_pushinteger(L, dhcp->op);
  lua_settable(L,-3);

  lua_pushstring(L, "hardware.type");
  lua_pushinteger(L, dhcp->htype);
  lua_settable(L,-3);

  lua_pushstring(L, "hardware.length");
  lua_pushinteger(L, dhcp->hlen);
  lua_settable(L,-3);

  lua_pushstring(L, "hops");
  lua_pushinteger(L, dhcp->hops);
  lua_settable(L,-3);

  lua_pushstring(L, "transaction.id");
  lua_pushinteger(L, ntohl(dhcp->xid));
  lua_settable(L,-3);

  lua_pushstring(L, "elapsed.seconds");
  lua_pushinteger(L, ntohs(dhcp->secs));
  lua_settable(L,-3);

  lua_pushstring(L, "flags");
  lua_pushinteger(L, ntohs(dhcp->flags));
  lua_settable(L,-3);

  sprintf(dhcp_arg, "%u.%u.%u.%u", dhcp->ciaddr[0], dhcp->ciaddr[1], dhcp->ciaddr[2], dhcp->ciaddr[3]);
  lua_pushstring(L, "client.ip.addr");
  lua_pushstring(L, dhcp_arg);
  lua_settable(L,-3);

  sprintf(dhcp_arg, "%u.%u.%u.%u", dhcp->yiaddr[0], dhcp->yiaddr[1], dhcp->yiaddr[2], dhcp->yiaddr[3]);
  lua_pushstring(L, "your.ip.addr");
  lua_pushstring(L, dhcp_arg);
  lua_settable(L,-3);

  sprintf(dhcp_arg, "%u.%u.%u.%u", dhcp->siaddr[0], dhcp->siaddr[1], dhcp->siaddr[2], dhcp->siaddr[3]);
  lua_pushstring(L, "server.ip.addr");
  lua_pushstring(L, dhcp_arg);
  lua_settable(L,-3);

  sprintf(dhcp_arg, "%u.%u.%u.%u", dhcp->giaddr[0], dhcp->giaddr[1], dhcp->giaddr[2], dhcp->giaddr[3]);
  lua_pushstring(L, "gateway.ip.addr");
  lua_pushstring(L, dhcp_arg);
  lua_settable(L,-3);

  sprintf(dhcp_arg, "%02X:%02X:%02X:%02X:%02X:%02X",
                    dhcp->chaddr[0], dhcp->chaddr[1], dhcp->chaddr[2],
                    dhcp->chaddr[3], dhcp->chaddr[4], dhcp->chaddr[5]);
  lua_pushstring(L, "client.mac");
  lua_pushstring(L, dhcp_arg);
  lua_settable(L,-3);

  snprintf(dhcp_arg, STR_BUF_SIZE, "%s", dhcp->sname);
  dhcp_arg[STR_BUF_SIZE-1] = 0; 	/* Guarantee for lua_pushstring that dhcp_arg is 0 terminated /*/
  lua_pushstring(L, "server.hostname");
  lua_pushstring(L, dhcp_arg);
  lua_settable(L,-3);

  snprintf(dhcp_arg, STR_BUF_SIZE, "%s", dhcp->file);
  dhcp_arg[STR_BUF_SIZE-1] = 0; 	/* Guarantee for lua_pushstring that dhcp_arg is 0 terminated /*/
  lua_pushstring(L, "boot.file");
  lua_pushstring(L, dhcp_arg);
  lua_settable(L,-3);

  sprintf(dhcp_arg, "%u.%u.%u.%u", dhcp->cookie[0], dhcp->cookie[1], dhcp->cookie[2], dhcp->cookie[3]);
  lua_pushstring(L, "magic.cookie");
  lua_pushstring(L, dhcp_arg);
  lua_settable(L,-3);

  return 1;
}

static const luaL_reg dhcplib[] = {
  {"gettable", dhcp_gettable},
  {"getoptions", dhcp_getoptions},
  {NULL, NULL}
};

LUALIB_API int luaopen_dhcp (lua_State *L) {
  luaL_openlib(L, LUA_DHCPLIBNAME, dhcplib, 0);
  return 1;
}