]>
git.zerfleddert.de Git - rigol/blob - rigold.c
2 #include <sys/socket.h>
14 static int send_binary(int s
, char *buf
, int len
)
19 ret
= write(s
, buf
, len
);
31 static int send_text(int s
, char *buf
)
33 return send_binary(s
, buf
, strlen(buf
));
36 static void serve_index(int s
, struct scope
*sc
)
38 send_text(s
, "HTTP/1.0 200 OK");
39 send_text(s
, "Content-type: text/html\n\n");
40 send_text(s
, "<html><head><title>");
41 send_text(s
, scope_idn(sc
));
42 send_text(s
, "</title></head><body bgcolor=\"#ffffff\" text=\"#000000\">\n");
43 send_text(s
, "<img src=\"/lcd.png\" height=\"234\" width=\"320\">\n");
44 send_text(s
, "</body></html>\n");
47 static void serve_lcd(int s
, struct scope
*sc
)
54 png
= get_lcd(sc
, &imglen
, 0);
61 send_text(s
, "HTTP/1.0 200 OK");
62 send_text(s
, "Content-type: image/png\n");
63 snprintf(buf
, sizeof(buf
), "Content-length: %u\n\n", imglen
);
65 send_binary(s
, (char*)png
, imglen
);
69 static void parse_request(int s
, struct scope
*sc
)
74 const char delim
[] = " \t\x0d\x0a";
75 const char crlf
[] = "\x0d\x0a";
79 ret
=read(s
, buf
, sizeof(buf
)-1);
86 token
= strtok_r(buf
, delim
, &saveptr
);
89 /* TODO: Only GET... */
90 token
= strtok_r(NULL
, delim
, &saveptr
);
93 bzero(&file
, sizeof(file
));
94 strncpy(file
, token
, sizeof(file
)-1);
97 token
= strtok_r(NULL
, crlf
, &saveptr
);
101 ret
=read(s
, buf
, sizeof(buf
)-1);
107 token
= strtok_r(buf
, crlf
, &saveptr
);
110 } while(token
!= NULL
);
112 if (strcmp("/", file
) == 0) {
114 } else if (strcmp("/lcd.png", file
) == 0) {
119 void sighandler(int sig
)
121 printf("Signal %d received\n", sig
);
124 int main(int argc
, char **argv
)
126 struct sigaction act
;
131 struct sockaddr_in sin
, clientsin
;
132 unsigned short port
= 8088;
136 printf("Scope not found!\n");
140 bzero(&act
, sizeof(act
));
141 act
.sa_handler
= sighandler
;
142 act
.sa_flags
= SA_RESTART
;
143 if (sigaction(SIGPIPE
, &act
, NULL
) == -1) {
148 if ((sock
= socket(AF_INET
, SOCK_STREAM
, 0)) == -1) {
157 bzero(&sin
, sizeof(sin
));
158 sin
.sin_addr
.s_addr
=htonl(INADDR_ANY
);
159 sin
.sin_family
=AF_INET
;
160 sin
.sin_port
=htons(port
);
163 setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, &opt
,sizeof(opt
));
165 if (bind(sock
, (struct sockaddr
*)&sin
, sizeof(sin
)) == -1) {
171 printf("Listening on Port %u\n", port
);
174 bzero(&clientsin
, sizeof(clientsin
));
175 slen
= sizeof(clientsin
);
176 if ((csock
= accept(sock
, (struct sockaddr
*)&clientsin
, &slen
)) == -1) {
180 parse_request(csock
, sc
);