include stdint.h
[rigol] / rigold.c
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>
8 #include <unistd.h>
9 #include <signal.h>
10
11 #include "scope.h"
12 #include "commands.h"
13
14 #define TIMEOUT 4
15
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
38 static void serve_index(int s, struct scope *sc)
39 {
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=\"/lcd.png\" height=\"234\" width=\"320\">\n");
46 send_text(s, "</body></html>\n");
47 }
48
49 static void serve_lcd(int s, struct scope *sc)
50 {
51 char buf[256];
52 int imglen;
53 unsigned char *png;
54
55 claimscope(sc);
56 png = get_lcd(sc, &imglen, 0);
57 releasescope(sc);
58
59 if (png == NULL)
60 return;
61
62
63 send_text(s, "HTTP/1.0 200 OK\n");
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
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
91 static void parse_request(int s, struct scope *sc)
92 {
93 int ret;
94 int eor;
95 char buf[4096];
96 char file[4096];
97 const char delim[] = " \t\x0d\x0a";
98 char *saveptr;
99 char *token;
100
101 alarm(TIMEOUT);
102 ret=read(s, buf, sizeof(buf)-1);
103 if (ret == -1) {
104 perror("read");
105 return;
106 }
107 buf[ret] = 0;
108
109 eor = is_eor(buf);
110
111 token = strtok_r(buf, delim, &saveptr);
112 if (token == NULL)
113 return;
114 /* TODO: Only GET... */
115 token = strtok_r(NULL, delim, &saveptr);
116 if (token == NULL)
117 return;
118 bzero(&file, sizeof(file));
119 strncpy(file, token, sizeof(file)-1);
120
121 while (!eor) {
122 alarm(TIMEOUT);
123 ret=read(s, buf, sizeof(buf)-1);
124 if (ret == -1) {
125 perror("read");
126 return;
127 }
128 buf[ret] = 0;
129 eor = is_eor(buf);
130 }
131 alarm(0);
132
133 if (strcmp("/", file) == 0) {
134 serve_index(s, sc);
135 } else if (strcmp("/lcd.png", file) == 0) {
136 serve_lcd(s, sc);
137 }
138 }
139
140 void sighandler(int sig)
141 {
142 switch(sig) {
143 case SIGPIPE:
144 break;
145 case SIGALRM:
146 default:
147 printf("Signal %d received\n", sig);
148 break;
149 }
150 }
151
152 int main(int argc, char **argv)
153 {
154 struct sigaction act;
155 int sock, csock;
156 int opt;
157 socklen_t slen;
158 struct scope *sc;
159 struct sockaddr_in sin, clientsin;
160 unsigned short port = 8088;
161
162 sc = initscope();
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 }
175
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
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
220 closescope(sc);
221 return 0;
222 }
Impressum, Datenschutz