]>
git.zerfleddert.de Git - rigol/blob - rigold.c
6e383b71fb2322555fcf0ff384c95909cdd53cb5
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
)
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");
49 static void serve_lcd(int s
, struct scope
*sc
)
56 png
= get_lcd(sc
, &imglen
, 0);
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
);
67 send_binary(s
, (char*)png
, imglen
);
75 /* This does not completely work, when the request is split
76 in multiple packets... */
77 if (strstr(buf
, "\x0d\x0a\x0d\x0a")) {
79 } else if (strstr(buf
, "\x0a\x0a")) {
81 } else if (strcmp(buf
, "\x0d\x0a") == 0) {
83 } else if (strcmp(buf
, "\x0a") == 0) {
91 static void parse_request(int s
, struct scope
*sc
)
97 const char delim
[] = " \t\x0d\x0a";
102 ret
=read(s
, buf
, sizeof(buf
)-1);
111 token
= strtok_r(buf
, delim
, &saveptr
);
114 /* TODO: Only GET... */
115 token
= strtok_r(NULL
, delim
, &saveptr
);
118 bzero(&file
, sizeof(file
));
119 strncpy(file
, token
, sizeof(file
)-1);
123 ret
=read(s
, buf
, sizeof(buf
)-1);
133 if (strcmp("/", file
) == 0) {
135 } else if (strcmp("/lcd.png", file
) == 0) {
140 void sighandler(int sig
)
147 printf("Signal %d received\n", sig
);
152 int main(int argc
, char **argv
)
154 struct sigaction act
;
159 struct sockaddr_in sin
, clientsin
;
160 unsigned short port
= 8088;
164 printf("Scope not found!\n");
168 bzero(&act
, sizeof(act
));
169 act
.sa_handler
= sighandler
;
170 act
.sa_flags
= SA_RESTART
;
171 if (sigaction(SIGPIPE
, &act
, NULL
) == -1) {
176 bzero(&act
, sizeof(act
));
177 act
.sa_handler
= sighandler
;
178 if (sigaction(SIGALRM
, &act
, NULL
) == -1) {
183 if ((sock
= socket(AF_INET
, SOCK_STREAM
, 0)) == -1) {
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
);
198 setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, &opt
,sizeof(opt
));
200 if (bind(sock
, (struct sockaddr
*)&sin
, sizeof(sin
)) == -1) {
206 printf("Listening on Port %u\n", port
);
209 bzero(&clientsin
, sizeof(clientsin
));
210 slen
= sizeof(clientsin
);
211 if ((csock
= accept(sock
, (struct sockaddr
*)&clientsin
, &slen
)) == -1) {
215 parse_request(csock
, sc
);