1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,34 @@ |
1 |
+/* |
|
2 |
+ * iobuf.h |
|
3 |
+ * |
|
4 |
+ * A O/O buffer library based on sbuf. |
|
5 |
+ * |
|
6 |
+ * Header file. |
|
7 |
+ * |
|
8 |
+ * History: |
|
9 |
+ * 21/01/2014 Creation. |
|
10 |
+ * |
|
11 |
+ * Author: Dario Rodriguez dario@softhome.net |
|
12 |
+ * This library is licensed on the terms of the GNU LGPL v2+ |
|
13 |
+ */ |
|
14 |
+ |
|
15 |
+#ifndef IOBUF_H |
|
16 |
+#define IOBUF_H |
|
17 |
+ |
|
18 |
+#include "sbuf.h" |
|
19 |
+ |
|
20 |
+typedef struct { |
|
21 |
+ int iobufnum; |
|
22 |
+ sbuf *in; |
|
23 |
+ sbuf *out; |
|
24 |
+} siobuf; |
|
25 |
+ |
|
26 |
+/* iobuf */ |
|
27 |
+siobuf *iobuf_init(int insize,int outsize); |
|
28 |
+siobuf *iobuf_staticinit(siobuf *uninitiobuf,int insize, int outsize, char *inbuf, char *outbuf); |
|
29 |
+ |
|
30 |
+siobuf *iobuf_free(siobuf *); |
|
31 |
+siobuf *iobuf_staticfree(siobuf *); |
|
32 |
+ |
|
33 |
+#endif |
|
34 |
+ |
0 | 35 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,25 @@ |
1 |
+/* |
|
2 |
+ * loglib.h |
|
3 |
+ * |
|
4 |
+ * A non-intrusive minimal log framework. |
|
5 |
+ * |
|
6 |
+ * History: |
|
7 |
+ * 27/01/2014 Creation |
|
8 |
+ * |
|
9 |
+ * Author: Dario Rodriguez dario@softhome.net |
|
10 |
+ * This file is licensed under the terms of the GNU LGPL v2+ |
|
11 |
+ */ |
|
12 |
+ |
|
13 |
+#ifndef LOGLIB_H |
|
14 |
+#define LOGLIB_H |
|
15 |
+ |
|
16 |
+#ifndef __GNUC__ |
|
17 |
+void log_write(char *type, char *format, ...); |
|
18 |
+#else |
|
19 |
+void log_write(char *type, char *format, ...) __attribute__ ((format (printf, 2, 3))); |
|
20 |
+#endif |
|
21 |
+ |
|
22 |
+char *log_hexdumpstr(char *data, long datasize); |
|
23 |
+void log_setwritefunc(void (*newwritefunc)(int fd, char *buf, long bufsize, void *usrptr),void *usrptr); |
|
24 |
+ |
|
25 |
+#endif |
0 | 26 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,22 @@ |
1 |
+/* |
|
2 |
+ * parselib.h |
|
3 |
+ * |
|
4 |
+ * Convenienct functions to parse config file lines. |
|
5 |
+ * |
|
6 |
+ * History: |
|
7 |
+ * 27/01/2014 Creation |
|
8 |
+ * |
|
9 |
+ * Author: Dario Rodriguez dario@softhome.net |
|
10 |
+ * This file is licensed under the terms of the GNU LGPL v2+ |
|
11 |
+ */ |
|
12 |
+ |
|
13 |
+#ifndef PARSELIB_H |
|
14 |
+#define PARSELIB_H |
|
15 |
+ |
|
16 |
+char *field_int(char *ptr, int *value); |
|
17 |
+char *field_long(char *ptr, long *value); |
|
18 |
+char *field_quotedstr(char *ptr, char *str, long strsize); |
|
19 |
+char *field_unquotedstr(char *ptr, char *str, long strsize); |
|
20 |
+char *field_hexint(char *ptr, int *value); |
|
21 |
+ |
|
22 |
+#endif |
0 | 23 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,54 @@ |
1 |
+/* |
|
2 |
+ * sbuf.h |
|
3 |
+ * |
|
4 |
+ * A string buffer library. |
|
5 |
+ * |
|
6 |
+ * Header file. |
|
7 |
+ * |
|
8 |
+ * History: |
|
9 |
+ * 21/01/2014 Creation. |
|
10 |
+ * 27/01/2014 Finish initial API. |
|
11 |
+ * |
|
12 |
+ * Author: Dario Rodriguez dario@softhome.net |
|
13 |
+ * This library is licensed on the terms of the GNU LGPL v2+ |
|
14 |
+ */ |
|
15 |
+ |
|
16 |
+#ifndef SBUF_H |
|
17 |
+#define SBUF_H |
|
18 |
+ |
|
19 |
+typedef struct { |
|
20 |
+ long used; |
|
21 |
+ long got; |
|
22 |
+ long size; |
|
23 |
+ char *buf |
|
24 |
+} sbuf; |
|
25 |
+ |
|
26 |
+/* sbuf, high level api */ |
|
27 |
+sbuf *sbuf_init(long bufsize); |
|
28 |
+sbuf *sbuf_staticinit(sbuf *uninitsbuf, long bufsize, char *buf); |
|
29 |
+void sbuf_free(sbuf *buf); |
|
30 |
+void sbuf_staticfree(sbuf *buf); |
|
31 |
+ |
|
32 |
+int sbuf_fill(sbuf *buf, int fd, long numbytes); |
|
33 |
+char *sbuf_getline(sbuf *buf); |
|
34 |
+void sbuf_discard(sbuf *buf); |
|
35 |
+void sbuf_wipe(sbuf *buf); |
|
36 |
+ |
|
37 |
+/* sbuf, addutional functionality */ |
|
38 |
+char *sbuf_getbytes(sbuf *buf, long numbytes); |
|
39 |
+long sbuf_count(sbuf *buf); |
|
40 |
+char *sbuf_ptr(sbuf *buf); |
|
41 |
+long sbuf_add(sbuf *buf, char *data, long datasize); |
|
42 |
+long sbuf_addstr(sbuf *buf, char *str); |
|
43 |
+long sbuf_unused(sbuf *buf); |
|
44 |
+char *sbuf_ptrunused(sbuf *buf); |
|
45 |
+long sbuf_addfromunused(sbuf *buf, long numbytes); |
|
46 |
+long sbuf_send(sbuf *buf, int fd, long numbytes); |
|
47 |
+ |
|
48 |
+/* low level api */ |
|
49 |
+long strbuf_fill(char *strbuf, long strbufsize, long *used, int fd. long numbytes); |
|
50 |
+char *strbuf_getline(char *strbuf, long strbufsize, long used, long *got); |
|
51 |
+void strbuf_discard(char *strbuf, long strbufsize, long *used, long *got); |
|
52 |
+ |
|
53 |
+#endif |
|
54 |
+ |
0 | 55 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,69 @@ |
1 |
+/* |
|
2 |
+ * socklib.c |
|
3 |
+ * |
|
4 |
+ * Handy functions for IPv4 socket connections. |
|
5 |
+ * |
|
6 |
+ * History: |
|
7 |
+ * 28/01/2014 Creation |
|
8 |
+ * |
|
9 |
+ * Author: Dario Rodriguez dario@softhome.net |
|
10 |
+ * This file is licensed under the terms of the GNU LGPL v2+ |
|
11 |
+ */ |
|
12 |
+ |
|
13 |
+#include <stdlib.h> |
|
14 |
+#include <string.h> |
|
15 |
+#include <netdb.h> |
|
16 |
+#include <netinet/in.h> |
|
17 |
+#include <sys/types.h> |
|
18 |
+#include <sys/socket.h> |
|
19 |
+ |
|
20 |
+#include "socklib.h" |
|
21 |
+ |
|
22 |
+struct sockaddr * |
|
23 |
+sock_genip(char *hostname, long *resulthostsize) |
|
24 |
+{ |
|
25 |
+ struct addrinfo hints,*ai; |
|
26 |
+ struct sockaddr_in *in; |
|
27 |
+ char *host; |
|
28 |
+ memset(&hints,0,sizeof(hints)); |
|
29 |
+ hints.ai_family=AF_UNSPEC; |
|
30 |
+ if(getaddrinfo(hostname,NULL,&hints,&ai)!=0) |
|
31 |
+ return(NULL); |
|
32 |
+ in=(struct sockaddr_in *)ai->addr; |
|
33 |
+ if((host=malloc(ai->ai_addrlen))==NULL) { |
|
34 |
+ freeaddrinfo(ai),ai=NULL; |
|
35 |
+ return(NULL); |
|
36 |
+ } |
|
37 |
+ |
|
38 |
+ memcpy(host,ai->ai_addr); |
|
39 |
+ *resulthostsize=ai->ai_addrlen; |
|
40 |
+ freeaddrinfo(ai),ai=NULL; |
|
41 |
+ return(host); |
|
42 |
+} |
|
43 |
+ |
|
44 |
+int |
|
45 |
+sock_genport(char *portname, int fallback) |
|
46 |
+{ |
|
47 |
+ struct addrinfo *ai; |
|
48 |
+ int port; |
|
49 |
+ if(getaddrinfo(NULL,portname,NULL,&ai)!=0) { |
|
50 |
+ return(fallback); |
|
51 |
+ } |
|
52 |
+ port=ai-> |
|
53 |
+ *resulthostsize=ai->ai_addrlen; |
|
54 |
+ freeaddrinfo(ai),ai=NULL; |
|
55 |
+ return(host); |
|
56 |
+} |
|
57 |
+ |
|
58 |
+int |
|
59 |
+sock_connect(char *host, long hostsize, int port) |
|
60 |
+{ |
|
61 |
+} |
|
62 |
+ |
|
63 |
+int sock_server(int port); |
|
64 |
+int sock_serverbinded(char *host, long hostsize, int port); |
|
65 |
+int sock_getinfo(int socket, char *ip, int *port); |
|
66 |
+int sock_select(long timoutms, ...); /* int numfds, int *resfdreadgroup1, int *fdreadgroup1,...,-1,int numfds, int *resfdwritegroup1, int *fdwritegroup1,...,-1); */ |
|
67 |
+int sock_queued(int socket); |
|
68 |
+int sock_setblocking(int socket, int block); |
|
69 |
+#endif |
0 | 70 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,30 @@ |
1 |
+/* |
|
2 |
+ * socklib.h |
|
3 |
+ * |
|
4 |
+ * Handy functions for IPv4 socket sonnections. |
|
5 |
+ * |
|
6 |
+ * Limitations: |
|
7 |
+ * These are for the simplest case, as the recommended approach |
|
8 |
+ * is to integrate the sock_connect() timeout inside the main |
|
9 |
+ * loop as to don't be blocking. |
|
10 |
+ * |
|
11 |
+ * History: |
|
12 |
+ * 27/01/2014 Creation |
|
13 |
+ * |
|
14 |
+ * Author: Dario Rodriguez dario@softhome.net |
|
15 |
+ * This file is licensed under the terms of the GNU LGPL v2+ |
|
16 |
+ */ |
|
17 |
+ |
|
18 |
+#ifndef SOCKLIB_H |
|
19 |
+#define SOCKLIB_H |
|
20 |
+ |
|
21 |
+char *sock_genip(char *hostname, long *resulthostsize); |
|
22 |
+int sock_genport(char *portname, int fallback); |
|
23 |
+int sock_connect(char *host, long hostsize, int port); |
|
24 |
+int sock_server(int port); |
|
25 |
+int sock_serverbinded(char *host, long hostsize, int port); |
|
26 |
+int sock_getinfo(int socket, char *ip, int *port); |
|
27 |
+int sock_select(long timoutms, ...); /* int numfds, int *resfdreadgroup1, int *fdreadgroup1,...,-1,int numfds, int *resfdwritegroup1, int *fdwritegroup1,...,-1); */ |
|
28 |
+int sock_queued(int socket); |
|
29 |
+int sock_setblocking(int socket, int block); |
|
30 |
+#endif |
0 | 31 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,36 @@ |
1 |
+/* |
|
2 |
+ * webkernel.v |
|
3 |
+ * |
|
4 |
+ * A small embeddable web server. |
|
5 |
+ * |
|
6 |
+ * 21/01/2014 Creation. |
|
7 |
+ * |
|
8 |
+ * Author: Dario Rodriguez dario@softhome.net |
|
9 |
+ * This library is licensed on the terms of the GNU LGPL v2+ |
|
10 |
+ */ |
|
11 |
+ |
|
12 |
+ |
|
13 |
+typedef struct { |
|
14 |
+ int dummy; |
|
15 |
+} swk; |
|
16 |
+ |
|
17 |
+wk * |
|
18 |
+wk_init(int port) |
|
19 |
+{ |
|
20 |
+} |
|
21 |
+ |
|
22 |
+void |
|
23 |
+wk_free(wk *web) |
|
24 |
+{ |
|
25 |
+ |
|
26 |
+} |
|
27 |
+ |
|
28 |
+int wk_select(wk *web, int timemoutms,...); |
|
29 |
+int wk_httpaccept(wk *web, int *httpnum); |
|
30 |
+int wk_httpisrequest(char *line); |
|
31 |
+int wk_httpfromdisk(wk *web, int httpnum,char *firstline); |
|
32 |
+int wk_httpreset(wk *web, int httpnum); |
|
33 |
+int wk_httpclose(wk *web, int httpnum); |
|
34 |
+int wk_iobufget(wk *web); |
|
35 |
+int wk_iobuffree(wk *web, int iobufnum); |
|
36 |
+ |
0 | 37 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,34 @@ |
1 |
+/* |
|
2 |
+ * webkernel.h |
|
3 |
+ * |
|
4 |
+ * A small embeddable web server. |
|
5 |
+ * |
|
6 |
+ * Header file. |
|
7 |
+ * |
|
8 |
+ * History: |
|
9 |
+ * 21/01/2014 Creation. |
|
10 |
+ * |
|
11 |
+ * Author: Dario Rodriguez dario@softhome.net |
|
12 |
+ * This library is licensed on the terms of the GNU LGPL v2+ |
|
13 |
+ */ |
|
14 |
+ |
|
15 |
+#ifndef WEBKERNEL_H |
|
16 |
+#define WEBKERNEL_H |
|
17 |
+#include "iobuf.h" |
|
18 |
+ |
|
19 |
+typedef void wk; |
|
20 |
+ |
|
21 |
+wk *wk_init(int port); |
|
22 |
+void wk_free(wk *web); |
|
23 |
+ |
|
24 |
+int wk_select(wk *web, int timemoutms,...); |
|
25 |
+int wk_httpaccept(wk *web, int *httpnum); |
|
26 |
+int wk_httpisrequest(char *line); |
|
27 |
+int wk_httpfromdisk(wk *web, int httpnum,char *firstline); |
|
28 |
+int wk_httpreset(wk *web, int httpnum); |
|
29 |
+int wk_httpclose(wk *web, int httpnum); |
|
30 |
+ |
|
31 |
+siobuf *wk_iobufget(wk *web); |
|
32 |
+wk_iobuffree(wk *web, int iobufnum); |
|
33 |
+ |
|
34 |
+#endif |