Merge pull request #563 from cshore/pull-request-app-uhttpd
[project/luci.git] / libs / luci-lib-nixio / src / protoent.c
1 /*
2  * nixio - Linux I/O library for lua
3  *
4  *   Copyright (C) 2011 Jo-Philipp Wich <jow@openwrt.org>
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  */
18
19 #include "nixio.h"
20
21 #ifndef __WINNT__
22 #include <netdb.h>
23 #endif
24
25 /**
26  * protoent conversion helper
27  */
28 static int nixio__pushprotoent(lua_State *L, struct protoent *e) {
29         int i;
30         if (e) {
31                 lua_newtable(L);
32
33                 lua_pushstring(L, e->p_name);
34                 lua_setfield(L, -2, "name");
35
36                 lua_pushnumber(L, e->p_proto);
37                 lua_setfield(L, -2, "proto");
38
39                 lua_newtable(L);
40                 for (i = 0; e->p_aliases[i]; i++) {
41                         lua_pushstring(L, e->p_aliases[i]);
42                         lua_rawseti(L, -2, i+1);
43                 }
44                 lua_setfield(L, -2, "aliases");
45                 return 1;
46         } else {
47                 return 0;
48         }
49 }
50
51 /**
52  * getprotobyname(name)
53  */
54 static int nixio_getprotobyname(lua_State *L) {
55         const char *name = luaL_checkstring(L, 1);
56         struct protoent *res = getprotobyname(name);
57         return nixio__pushprotoent(L, res);
58 }
59
60 /**
61  * getprotobynumber(proto)
62  */
63 static int nixio_getprotobynumber(lua_State *L) {
64         int proto = luaL_checkinteger(L, 1);
65         struct protoent *res = getprotobynumber(proto);
66         return nixio__pushprotoent(L, res);
67 }
68
69 /**
70  * getproto(name_or_proto)
71  */
72 static int nixio_getproto(lua_State *L) {
73         int i = 1;
74         struct protoent *res;
75         if (lua_isnumber(L, 1)) {
76                 return nixio_getprotobynumber(L);
77         } else if (lua_isstring(L, 1)) {
78                 return nixio_getprotobyname(L);
79         } else if (lua_isnoneornil(L, 1)) {
80                 setprotoent(1);
81                 lua_newtable(L);
82                 while ((res = getprotoent()) != NULL) {
83                         nixio__pushprotoent(L, res);
84                         lua_rawseti(L, -2, i++);
85                 }
86                 endprotoent();
87                 return 1;
88         } else {
89                 return luaL_argerror(L, 1, "supported values: <protoname>, <protonumber>");
90         }
91 }
92
93 /* module table */
94 static const luaL_reg R[] = {
95     {"getprotobyname",          nixio_getprotobyname},
96     {"getprotobynumber",        nixio_getprotobynumber},
97     {"getproto",                        nixio_getproto},
98     {NULL,                                      NULL}
99 };
100
101 void nixio_open_protoent(lua_State *L) {
102     luaL_register(L, NULL, R);
103 }