fix this too
[cascardo/rnetproxy.git] / resp.c
1 /*
2  *  Copyright (C) 2013  Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 3 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License along
15  *  with this program; if not, write to the Free Software Foundation, Inc.,
16  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 #include <unistd.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <stdint.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <netdb.h>
26 #include <fcntl.h>
27
28 static int add_field(char *buffer, size_t len, char *key, int klen, char *val, int vlen)
29 {
30         int n = 0;
31         if (len < (klen + vlen + 3))
32                 return -ENOMEM;
33         if (klen > 0x7f || klen < 0)
34                 return -EINVAL;
35         if (vlen > 0x7fff || vlen < 0)
36                 return -EINVAL;
37         buffer[0] = klen & 0x7f;
38         if (vlen > 0x7f)
39                 buffer[0] |= 0x80;
40         buffer++;
41         n++;
42         memcpy(buffer, key, klen);
43         buffer += klen;
44         n += klen;
45         if (vlen > 0x7f) {
46                 buffer[0] = (vlen >> 8) & 0x7f;
47                 buffer[1] = vlen & 0xff;
48                 buffer += 2;
49                 n += 2;
50         } else {
51                 buffer[0] = vlen & 0x7f;
52                 buffer++;
53                 n++;
54         }
55         memcpy(buffer, val, vlen);
56         n += vlen;
57         return n;
58 }
59
60 static int add_u32(char *buffer, int len, char *key, uint32_t val)
61 {
62         uint32_t nval = htonl(val);
63         return add_field(buffer, len, key, strlen(key), (char *) &nval, sizeof(val));
64 }
65
66 static int add_u8(char *buffer, int len, char *key, uint8_t val)
67 {
68         return add_field(buffer, len, key, strlen(key), (char *) &val, sizeof(val));
69 }
70
71 static int add_ascii(char *buffer, int len, char *key, char *val)
72 {
73         return add_field(buffer, len, key, strlen(key), val, strlen(val));
74 }
75
76 int resp(char **buffer)
77 {
78         int n;
79         int slen = 2048;
80         char *p;
81         char *end;
82         char file[512];
83         int r = 0;
84         uint64_t pos = 0;
85         //r = read(0, file, sizeof(file) - 1);
86         file[r] = 0;
87         if (*buffer) {
88                 return -EINVAL;
89         }
90         *buffer = malloc(slen);
91         if (*buffer == NULL) {
92                 return -ENOMEM;
93         }
94         memset(*buffer, 0, slen);
95         end = *buffer + slen;
96         p = *buffer;
97         p[0] = 0x01;
98         p++;
99         n = add_field(p, end - p, "POS_INICIO", strlen("POS_INICIO"), (char *)&pos, 8);
100         p += n;
101 /*
102         n = add_u32(p, end - p, "POS_INICIO", 0);
103         p += n;
104         n = add_u8(p, end - p, "SUBTIPO", 0);
105         p += n;
106         n = add_ascii(p, end - p, "TEXTO", "0001.ERRO! Buahahahaha!");
107         p += n;
108         n = add_ascii(p, end - p, "ARQUIVO", file);
109         p += n;
110 */
111         return p - *buffer;
112 }
113
114 #ifdef MAIN
115 int main(int argc, char **argv)
116 {
117         size_t n;
118         char *buffer = NULL;
119         n = resp(&buffer);
120         if (n > 0)
121                 write(1, buffer, n);
122         free(buffer);
123         return 0;
124 }
125 #endif