974a814f348f8d0fdeb9fb2da71eb52c9ba9da18
[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
10 #include "scope.h"
11 #include "commands.h"
12
13 static 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
30 static int send_text(int s, char *buf)
31 {
32 return send_binary(s, buf, strlen(buf));
33 }
34
35 static void serve_index(int s, struct scope *sc)
36 {
37 send_text(s, "HTTP/1.0 200 OK");
38 send_text(s, "Content-type: text/html\n\n");
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");
42 send_text(s, "<img src=\"/lcd.png\" height=\"234\" width=\"320\">\n");
43 send_text(s, "</body></html>\n");
44 }
45
46 static void serve_lcd(int s, struct scope *sc)
47 {
48 char buf[256];
49 int imglen;
50 unsigned char *png;
51
52 claimscope(sc);
53 png = get_lcd(sc, &imglen, 0);
54 releasescope(sc);
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
68 static void parse_request(int s, struct scope *sc)
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);
86 if (token == NULL)
87 return;
88 /* TODO: Only GET... */
89 token = strtok_r(NULL, delim, &saveptr);
90 if (token == NULL)
91 return;
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) {
112 serve_index(s, sc);
113 } else if (strcmp("/lcd.png", file) == 0) {
114 serve_lcd(s, sc);
115 }
116 }
117
118 int main(int argc, char **argv)
119 {
120 int sock, csock;
121 int opt;
122 socklen_t slen;
123 struct scope *sc;
124 struct sockaddr_in sin, clientsin;
125 unsigned short port = 8088;
126
127 sc = initscope();
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
166 closescope(sc);
167 return 0;
168 }
Impressum, Datenschutz