Merge pull request #3413 from pymumu/master
[project/luci.git] / libs / luci-lib-nixio / src / nixio.h
1 #ifndef NIXIO_H_
2 #define NIXIO_H_
3
4 #define NIXIO_OOM "out of memory"
5
6 #define NIXIO_META "nixio.socket"
7 #define NIXIO_FILE_META "nixio.file"
8 #define NIXIO_GLOB_META "nixio.glob"
9 #define NIXIO_DIR_META "nixio.dir"
10 #define _FILE_OFFSET_BITS 64
11
12 #define NIXIO_PUSH_CONSTANT(x) \
13 lua_pushinteger(L, x); \
14 lua_setfield(L, -2, #x);
15
16 /* uClibc: broken as always */
17 #define _LARGEFILE_SOURCE
18
19 #include <lua.h>
20 #include <lualib.h>
21 #include <lauxlib.h>
22 #include <luaconf.h>
23
24 #if LUA_VERSION_NUM < 501
25 #define luaL_Reg luaL_reg
26 #endif
27
28 #define NIXIO_BUFFERSIZE 8192
29
30 typedef struct nixio_socket {
31 int fd;
32 int domain;
33 int type;
34 int protocol;
35 } nixio_sock;
36
37 typedef struct nixio_address {
38 int family;
39 char host[128];
40 int port;
41 int prefix;
42 } nixio_addr;
43
44 int nixio__perror(lua_State *L);
45 int nixio__pstatus(lua_State *L, int condition);
46
47 #if defined(LUA_NUMBER_DOUBLE) || defined(LNUM_DOUBLE) || defined(LNUM_LDOUBLE)
48 #define NIXIO_DOUBLE 1
49 #define nixio__checknumber luaL_checknumber
50 #define nixio__pushnumber lua_pushnumber
51 #define nixio__optnumber luaL_optnumber
52 #else
53 #define nixio__checknumber luaL_checkinteger
54 #define nixio__pushnumber lua_pushinteger
55 #define nixio__optnumber luaL_optinteger
56 #endif
57
58
59 #ifndef __WINNT__
60
61 #define NIXIO_API extern
62
63 #include <sys/types.h>
64 #include <sys/socket.h>
65 #include <arpa/inet.h>
66 #include <netinet/in.h>
67 #include <netinet/tcp.h>
68 #include <sys/un.h>
69 #include <netdb.h>
70 #include <poll.h>
71 #include <sys/stat.h>
72 #include <errno.h>
73
74 #define NIXIO_SEP "/"
75 #define NIXIO_PATHSEP ":"
76
77 #define nixio__perror_s nixio__perror
78 #define nixio__pstatus_s nixio__pstatus
79
80 int nixio__check_group(lua_State *L, int idx);
81 int nixio__check_user(lua_State *L, int idx);
82
83 typedef struct stat nixio_stat_t;
84
85 #else /* __WINNT__ */
86
87 #define NIXIO_API extern __declspec(dllexport)
88 #define NIXIO_SEP "\\"
89 #define NIXIO_PATHSEP ";"
90 #include "mingw-compat.h"
91
92 typedef struct _stati64 nixio_stat_t;
93
94 #endif
95
96 nixio_sock* nixio__checksock(lua_State *L);
97 int nixio__checksockfd(lua_State *L);
98 int nixio__checkfd(lua_State *L, int ud);
99 int nixio__tofd(lua_State *L, int ud);
100 int nixio__nulliter(lua_State *L);
101
102 int nixio__addr_parse(nixio_addr *addr, struct sockaddr *saddr);
103 int nixio__addr_write(nixio_addr *addr, struct sockaddr *saddr);
104
105 int nixio__check_mode(lua_State *L, int idx, int def);
106 int nixio__mode_write(int mode, char *modestr);
107
108 int nixio__push_stat(lua_State *L, nixio_stat_t *buf);
109
110 const char nixio__bin2hex[16];
111
112 /* Module functions */
113 void nixio_open_file(lua_State *L);
114 void nixio_open_socket(lua_State *L);
115 void nixio_open_sockopt(lua_State *L);
116 void nixio_open_bind(lua_State *L);
117 void nixio_open_address(lua_State *L);
118 void nixio_open_protoent(lua_State *L);
119 void nixio_open_poll(lua_State *L);
120 void nixio_open_io(lua_State *L);
121 void nixio_open_splice(lua_State *L);
122 void nixio_open_process(lua_State *L);
123 void nixio_open_syslog(lua_State *L);
124 void nixio_open_bit(lua_State *L);
125 void nixio_open_bin(lua_State *L);
126 void nixio_open_fs(lua_State *L);
127 void nixio_open_user(lua_State *L);
128
129 #ifndef NO_TLS
130 void nixio_open_tls_crypto(lua_State *L);
131 void nixio_open_tls_context(lua_State *L);
132 void nixio_open_tls_socket(lua_State *L);
133 #endif
134
135 /* Method functions */
136
137 #endif /* NIXIO_H_ */