]>
Commit | Line | Data |
---|---|---|
ad9fbc05 MG |
1 | #include <sys/types.h> |
2 | #include <sys/socket.h> | |
3 | #include <arpa/inet.h> | |
4 | #include <stdio.h> | |
5 | #include <stdlib.h> | |
6 | #include <string.h> | |
7 | #include <strings.h> | |
713be7a4 | 8 | #include <unistd.h> |
7a226bb8 | 9 | #include <signal.h> |
659f6954 | 10 | |
713be7a4 | 11 | #include "scope.h" |
ad9fbc05 MG |
12 | #include "commands.h" |
13 | ||
33b7545a MG |
14 | #define TIMEOUT 4 |
15 | ||
ad9fbc05 MG |
16 | static int send_binary(int s, char *buf, int len) |
17 | { | |
18 | int ret; | |
19 | ||
20 | while(len > 0) { | |
21 | ret = write(s, buf, len); | |
22 | if (ret == -1) { | |
23 | perror("write"); | |
24 | return ret; | |
25 | } | |
26 | buf += ret; | |
27 | len -= ret; | |
28 | } | |
29 | ||
30 | return 0; | |
31 | } | |
32 | ||
33 | static int send_text(int s, char *buf) | |
34 | { | |
35 | return send_binary(s, buf, strlen(buf)); | |
36 | } | |
37 | ||
713be7a4 | 38 | static void serve_index(int s, struct scope *sc) |
ad9fbc05 | 39 | { |
33b7545a | 40 | send_text(s, "HTTP/1.0 200 OK\n"); |
ad9fbc05 | 41 | send_text(s, "Content-type: text/html\n\n"); |
713be7a4 MG |
42 | send_text(s, "<html><head><title>"); |
43 | send_text(s, scope_idn(sc)); | |
44 | send_text(s, "</title></head><body bgcolor=\"#ffffff\" text=\"#000000\">\n"); | |
ad9fbc05 MG |
45 | send_text(s, "<img src=\"/lcd.png\" height=\"234\" width=\"320\">\n"); |
46 | send_text(s, "</body></html>\n"); | |
47 | } | |
48 | ||
713be7a4 | 49 | static void serve_lcd(int s, struct scope *sc) |
ad9fbc05 MG |
50 | { |
51 | char buf[256]; | |
52 | int imglen; | |
53 | unsigned char *png; | |
54 | ||
713be7a4 | 55 | claimscope(sc); |
ad9fbc05 | 56 | png = get_lcd(sc, &imglen, 0); |
713be7a4 | 57 | releasescope(sc); |
ad9fbc05 MG |
58 | |
59 | if (png == NULL) | |
60 | return; | |
61 | ||
62 | ||
33b7545a | 63 | send_text(s, "HTTP/1.0 200 OK\n"); |
ad9fbc05 MG |
64 | send_text(s, "Content-type: image/png\n"); |
65 | snprintf(buf, sizeof(buf), "Content-length: %u\n\n", imglen); | |
66 | send_text(s, buf); | |
67 | send_binary(s, (char*)png, imglen); | |
68 | free(png); | |
69 | } | |
70 | ||
33b7545a MG |
71 | int is_eor(char *buf) |
72 | { | |
73 | int eor = 0; | |
74 | ||
75 | /* This does not completely work, when the request is split | |
76 | in multiple packets... */ | |
77 | if (strstr(buf, "\x0d\x0a\x0d\x0a")) { | |
78 | eor = 1; | |
79 | } else if (strstr(buf, "\x0a\x0a")) { | |
80 | eor = 1; | |
81 | } else if (strcmp(buf, "\x0d\x0a") == 0) { | |
82 | eor = 1; | |
83 | } else if (strcmp(buf, "\x0a") == 0) { | |
84 | eor = 1; | |
85 | } | |
86 | ||
87 | return eor; | |
88 | } | |
89 | ||
90 | ||
713be7a4 | 91 | static void parse_request(int s, struct scope *sc) |
ad9fbc05 MG |
92 | { |
93 | int ret; | |
33b7545a MG |
94 | int eor; |
95 | char buf[4096]; | |
96 | char file[4096]; | |
ad9fbc05 | 97 | const char delim[] = " \t\x0d\x0a"; |
ad9fbc05 MG |
98 | char *saveptr; |
99 | char *token; | |
100 | ||
33b7545a | 101 | alarm(TIMEOUT); |
ad9fbc05 MG |
102 | ret=read(s, buf, sizeof(buf)-1); |
103 | if (ret == -1) { | |
104 | perror("read"); | |
105 | return; | |
106 | } | |
107 | buf[ret] = 0; | |
108 | ||
33b7545a MG |
109 | eor = is_eor(buf); |
110 | ||
ad9fbc05 | 111 | token = strtok_r(buf, delim, &saveptr); |
77385d56 MG |
112 | if (token == NULL) |
113 | return; | |
ad9fbc05 MG |
114 | /* TODO: Only GET... */ |
115 | token = strtok_r(NULL, delim, &saveptr); | |
77385d56 MG |
116 | if (token == NULL) |
117 | return; | |
ad9fbc05 MG |
118 | bzero(&file, sizeof(file)); |
119 | strncpy(file, token, sizeof(file)-1); | |
120 | ||
33b7545a MG |
121 | while (!eor) { |
122 | alarm(TIMEOUT); | |
123 | ret=read(s, buf, sizeof(buf)-1); | |
124 | if (ret == -1) { | |
125 | perror("read"); | |
126 | return; | |
ad9fbc05 | 127 | } |
33b7545a MG |
128 | buf[ret] = 0; |
129 | eor = is_eor(buf); | |
130 | } | |
131 | alarm(0); | |
ad9fbc05 MG |
132 | |
133 | if (strcmp("/", file) == 0) { | |
713be7a4 | 134 | serve_index(s, sc); |
ad9fbc05 MG |
135 | } else if (strcmp("/lcd.png", file) == 0) { |
136 | serve_lcd(s, sc); | |
137 | } | |
138 | } | |
659f6954 | 139 | |
7a226bb8 MG |
140 | void sighandler(int sig) |
141 | { | |
33b7545a MG |
142 | switch(sig) { |
143 | case SIGPIPE: | |
144 | break; | |
145 | case SIGALRM: | |
146 | default: | |
147 | printf("Signal %d received\n", sig); | |
148 | break; | |
149 | } | |
7a226bb8 MG |
150 | } |
151 | ||
659f6954 MG |
152 | int main(int argc, char **argv) |
153 | { | |
7a226bb8 | 154 | struct sigaction act; |
ad9fbc05 MG |
155 | int sock, csock; |
156 | int opt; | |
157 | socklen_t slen; | |
713be7a4 | 158 | struct scope *sc; |
ad9fbc05 | 159 | struct sockaddr_in sin, clientsin; |
77385d56 | 160 | unsigned short port = 8088; |
659f6954 | 161 | |
713be7a4 | 162 | sc = initscope(); |
7a226bb8 MG |
163 | if (sc == NULL) { |
164 | printf("Scope not found!\n"); | |
165 | exit(EXIT_FAILURE); | |
166 | } | |
167 | ||
168 | bzero(&act, sizeof(act)); | |
169 | act.sa_handler = sighandler; | |
170 | act.sa_flags = SA_RESTART; | |
171 | if (sigaction(SIGPIPE, &act, NULL) == -1) { | |
172 | perror("sigaction"); | |
173 | exit(EXIT_FAILURE); | |
174 | } | |
ad9fbc05 | 175 | |
33b7545a MG |
176 | bzero(&act, sizeof(act)); |
177 | act.sa_handler = sighandler; | |
178 | if (sigaction(SIGALRM, &act, NULL) == -1) { | |
179 | perror("sigaction"); | |
180 | exit(EXIT_FAILURE); | |
181 | } | |
182 | ||
ad9fbc05 MG |
183 | if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) { |
184 | perror("socket"); | |
185 | exit(EXIT_FAILURE); | |
186 | } | |
187 | ||
188 | if (argc == 2) { | |
189 | port=atoi(argv[1]); | |
190 | } | |
191 | ||
192 | bzero(&sin, sizeof(sin)); | |
193 | sin.sin_addr.s_addr=htonl(INADDR_ANY); | |
194 | sin.sin_family=AF_INET; | |
195 | sin.sin_port=htons(port); | |
196 | ||
197 | opt = 1; | |
198 | setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt,sizeof(opt)); | |
199 | ||
200 | if (bind(sock, (struct sockaddr*)&sin, sizeof(sin)) == -1) { | |
201 | perror("bind"); | |
202 | exit(EXIT_FAILURE); | |
203 | } | |
204 | ||
205 | listen(sock, 32); | |
206 | printf("Listening on Port %u\n", port); | |
207 | ||
208 | while(1) { | |
209 | bzero(&clientsin, sizeof(clientsin)); | |
210 | slen = sizeof(clientsin); | |
211 | if ((csock = accept(sock, (struct sockaddr*)&clientsin, &slen)) == -1) { | |
212 | perror("accept"); | |
213 | } | |
214 | ||
215 | parse_request(csock, sc); | |
216 | ||
217 | close(csock); | |
218 | } | |
219 | ||
713be7a4 | 220 | closescope(sc); |
659f6954 MG |
221 | return 0; |
222 | } |