]> git.zerfleddert.de Git - rigol/blame - rigold.c
don't try to build an image with broken data
[rigol] / rigold.c
CommitLineData
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>
659f6954 9
713be7a4 10#include "scope.h"
ad9fbc05
MG
11#include "commands.h"
12
13static int send_binary(int s, char *buf, int len)
14{
15 int ret;
16
17 while(len > 0) {
18 ret = write(s, buf, len);
19 if (ret == -1) {
20 perror("write");
21 return ret;
22 }
23 buf += ret;
24 len -= ret;
25 }
26
27 return 0;
28}
29
30static int send_text(int s, char *buf)
31{
32 return send_binary(s, buf, strlen(buf));
33}
34
713be7a4 35static void serve_index(int s, struct scope *sc)
ad9fbc05
MG
36{
37 send_text(s, "HTTP/1.0 200 OK");
38 send_text(s, "Content-type: text/html\n\n");
713be7a4
MG
39 send_text(s, "<html><head><title>");
40 send_text(s, scope_idn(sc));
41 send_text(s, "</title></head><body bgcolor=\"#ffffff\" text=\"#000000\">\n");
ad9fbc05
MG
42 send_text(s, "<img src=\"/lcd.png\" height=\"234\" width=\"320\">\n");
43 send_text(s, "</body></html>\n");
44}
45
713be7a4 46static void serve_lcd(int s, struct scope *sc)
ad9fbc05
MG
47{
48 char buf[256];
49 int imglen;
50 unsigned char *png;
51
713be7a4 52 claimscope(sc);
ad9fbc05 53 png = get_lcd(sc, &imglen, 0);
713be7a4 54 releasescope(sc);
ad9fbc05
MG
55
56 if (png == NULL)
57 return;
58
59
60 send_text(s, "HTTP/1.0 200 OK");
61 send_text(s, "Content-type: image/png\n");
62 snprintf(buf, sizeof(buf), "Content-length: %u\n\n", imglen);
63 send_text(s, buf);
64 send_binary(s, (char*)png, imglen);
65 free(png);
66}
67
713be7a4 68static void parse_request(int s, struct scope *sc)
ad9fbc05
MG
69{
70 int ret;
71 char buf[1024];
72 char file[1024];
73 const char delim[] = " \t\x0d\x0a";
74 const char crlf[] = "\x0d\x0a";
75 char *saveptr;
76 char *token;
77
78 ret=read(s, buf, sizeof(buf)-1);
79 if (ret == -1) {
80 perror("read");
81 return;
82 }
83 buf[ret] = 0;
84
85 token = strtok_r(buf, delim, &saveptr);
77385d56
MG
86 if (token == NULL)
87 return;
ad9fbc05
MG
88 /* TODO: Only GET... */
89 token = strtok_r(NULL, delim, &saveptr);
77385d56
MG
90 if (token == NULL)
91 return;
ad9fbc05
MG
92 bzero(&file, sizeof(file));
93 strncpy(file, token, sizeof(file)-1);
94
95 do {
96 token = strtok_r(NULL, crlf, &saveptr);
97 /* TODO: FIXME */
98 #if 0
99 if (token == NULL) {
100 ret=read(s, buf, sizeof(buf)-1);
101 if (ret == -1) {
102 perror("read");
103 return;
104 }
105 buf[ret] = 0;
106 token = strtok_r(buf, crlf, &saveptr);
107 }
108 #endif
109 } while(token != NULL);
110
111 if (strcmp("/", file) == 0) {
713be7a4 112 serve_index(s, sc);
ad9fbc05
MG
113 } else if (strcmp("/lcd.png", file) == 0) {
114 serve_lcd(s, sc);
115 }
116}
659f6954
MG
117
118int main(int argc, char **argv)
119{
ad9fbc05
MG
120 int sock, csock;
121 int opt;
122 socklen_t slen;
713be7a4 123 struct scope *sc;
ad9fbc05 124 struct sockaddr_in sin, clientsin;
77385d56 125 unsigned short port = 8088;
659f6954 126
713be7a4 127 sc = initscope();
ad9fbc05
MG
128
129 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
130 perror("socket");
131 exit(EXIT_FAILURE);
132 }
133
134 if (argc == 2) {
135 port=atoi(argv[1]);
136 }
137
138 bzero(&sin, sizeof(sin));
139 sin.sin_addr.s_addr=htonl(INADDR_ANY);
140 sin.sin_family=AF_INET;
141 sin.sin_port=htons(port);
142
143 opt = 1;
144 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt,sizeof(opt));
145
146 if (bind(sock, (struct sockaddr*)&sin, sizeof(sin)) == -1) {
147 perror("bind");
148 exit(EXIT_FAILURE);
149 }
150
151 listen(sock, 32);
152 printf("Listening on Port %u\n", port);
153
154 while(1) {
155 bzero(&clientsin, sizeof(clientsin));
156 slen = sizeof(clientsin);
157 if ((csock = accept(sock, (struct sockaddr*)&clientsin, &slen)) == -1) {
158 perror("accept");
159 }
160
161 parse_request(csock, sc);
162
163 close(csock);
164 }
165
713be7a4 166 closescope(sc);
659f6954
MG
167 return 0;
168}
Impressum, Datenschutz