]>
cvs.zerfleddert.de Git - rigol/blob - rigold.c
2 #include <sys/socket.h>
16 static int send_binary(int s
, char *buf
, int len
)
21 ret
= write(s
, buf
, len
);
33 static int send_text(int s
, char *buf
)
35 return send_binary(s
, buf
, strlen(buf
));
38 static void serve_index(int s
, struct scope
*sc
, char *param
)
40 send_text(s
, "HTTP/1.0 200 OK\n");
41 send_text(s
, "Content-type: text/html\n\n");
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");
45 send_text(s
, "<img src=\"/cgi-bin/lcd\" height=\"234\" width=\"320\">\n");
46 send_text(s
, "<br>\n");
47 send_text(s
, "<form method=\"get\" action=\"\">\n");
48 send_text(s
, "<input type=\"text\" name=\"cmd\" value=\"");
50 if (strncmp(param
, "cmd=", 4) == 0)
55 send_text(s
, "\">\n");
56 send_text(s
, "<input type=\"submit\">\n");
57 send_text(s
, "</form>\n");
59 unsigned char buf
[1024*1024];
63 if (strchr (param
, '?')) {
64 res
= sendscpi(sc
, param
, buf
, sizeof(buf
));
65 send_text(s
, "<pre>< ");
68 send_binary(s
, (char*)buf
, res
);
69 send_text(s
, "</pre>\n");
71 sendscpi(sc
, param
, NULL
, 0);
75 send_text(s
, "</body></html>\n");
78 static void serve_lcd(int s
, struct scope
*sc
)
85 png
= get_lcd(sc
, &imglen
, 0);
92 send_text(s
, "HTTP/1.0 200 OK\n");
93 send_text(s
, "Content-type: image/png\n");
94 snprintf(buf
, sizeof(buf
), "Content-length: %u\n\n", imglen
);
96 send_binary(s
, (char*)png
, imglen
);
100 static void serve_404(int s
)
102 send_text(s
, "HTTP/1.0 404 Not found\n");
103 send_text(s
, "Content-type: text/html\n\n");
104 send_text(s
, "<html><head><title>404</title></head>");
105 send_text(s
, "<body bgcolor=\"#ffffff\" text=\"#000000\">\n");
106 send_text(s
, "<h1>404</h1>");
107 send_text(s
, "</body></html>\n");
110 int is_eor(char *buf
)
114 /* This does not completely work, when the request is split
115 in multiple packets... */
116 if (strstr(buf
, "\x0d\x0a\x0d\x0a")) {
118 } else if (strstr(buf
, "\x0a\x0a")) {
120 } else if (strcmp(buf
, "\x0d\x0a") == 0) {
122 } else if (strcmp(buf
, "\x0a") == 0) {
130 static void parse_request(int s
, struct scope
*sc
)
136 const char delim
[] = " \t\x0d\x0a";
142 ret
=read(s
, buf
, sizeof(buf
)-1);
151 token
= strtok_r(buf
, delim
, &saveptr
);
154 /* TODO: Only GET... */
155 token
= strtok_r(NULL
, delim
, &saveptr
);
158 bzero(&file
, sizeof(file
));
159 strncpy(file
, token
, sizeof(file
)-1);
163 ret
=read(s
, buf
, sizeof(buf
)-1);
173 param
= strchr(file
, '?');
178 while ((token
= strchr(param
, '%')) != NULL
) {
181 if (strlen(token
+1) < 2)
184 strncpy(buf
, token
+1, 3);
187 *token
= (char)strtoul(buf
, NULL
, 16);
188 memmove(token
+1, token
+3, strlen(token
)-2);
193 if (strcmp("/", file
) == 0) {
194 serve_index(s
, sc
, param
);
195 } else if (strcmp("/cgi-bin/lcd", file
) == 0) {
202 void sighandler(int sig
)
209 printf("Signal %d received\n", sig
);
214 int main(int argc
, char **argv
)
216 struct sigaction act
;
221 struct sockaddr_in sin
, clientsin
;
222 unsigned short port
= 8088;
226 printf("Scope not found!\n");
230 bzero(&act
, sizeof(act
));
231 act
.sa_handler
= sighandler
;
232 act
.sa_flags
= SA_RESTART
;
233 if (sigaction(SIGPIPE
, &act
, NULL
) == -1) {
238 bzero(&act
, sizeof(act
));
239 act
.sa_handler
= sighandler
;
240 if (sigaction(SIGALRM
, &act
, NULL
) == -1) {
245 if ((sock
= socket(AF_INET
, SOCK_STREAM
, 0)) == -1) {
254 bzero(&sin
, sizeof(sin
));
255 sin
.sin_addr
.s_addr
=htonl(INADDR_ANY
);
256 sin
.sin_family
=AF_INET
;
257 sin
.sin_port
=htons(port
);
260 setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, &opt
,sizeof(opt
));
262 if (bind(sock
, (struct sockaddr
*)&sin
, sizeof(sin
)) == -1) {
268 printf("Listening on Port %u\n", port
);
271 bzero(&clientsin
, sizeof(clientsin
));
272 slen
= sizeof(clientsin
);
273 if ((csock
= accept(sock
, (struct sockaddr
*)&clientsin
, &slen
)) == -1) {
277 parse_request(csock
, sc
);