]>
git.zerfleddert.de Git - rigol/blob - rigold.c
974a814f348f8d0fdeb9fb2da71eb52c9ba9da18
2 #include <sys/socket.h>
13 static int send_binary(int s
, char *buf
, int len
)
18 ret
= write(s
, buf
, len
);
30 static int send_text(int s
, char *buf
)
32 return send_binary(s
, buf
, strlen(buf
));
35 static void serve_index(int s
, struct scope
*sc
)
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");
46 static void serve_lcd(int s
, struct scope
*sc
)
53 png
= get_lcd(sc
, &imglen
, 0);
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
);
64 send_binary(s
, (char*)png
, imglen
);
68 static void parse_request(int s
, struct scope
*sc
)
73 const char delim
[] = " \t\x0d\x0a";
74 const char crlf
[] = "\x0d\x0a";
78 ret
=read(s
, buf
, sizeof(buf
)-1);
85 token
= strtok_r(buf
, delim
, &saveptr
);
88 /* TODO: Only GET... */
89 token
= strtok_r(NULL
, delim
, &saveptr
);
92 bzero(&file
, sizeof(file
));
93 strncpy(file
, token
, sizeof(file
)-1);
96 token
= strtok_r(NULL
, crlf
, &saveptr
);
100 ret
=read(s
, buf
, sizeof(buf
)-1);
106 token
= strtok_r(buf
, crlf
, &saveptr
);
109 } while(token
!= NULL
);
111 if (strcmp("/", file
) == 0) {
113 } else if (strcmp("/lcd.png", file
) == 0) {
118 int main(int argc
, char **argv
)
124 struct sockaddr_in sin
, clientsin
;
125 unsigned short port
= 8088;
129 if ((sock
= socket(AF_INET
, SOCK_STREAM
, 0)) == -1) {
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
);
144 setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, &opt
,sizeof(opt
));
146 if (bind(sock
, (struct sockaddr
*)&sin
, sizeof(sin
)) == -1) {
152 printf("Listening on Port %u\n", port
);
155 bzero(&clientsin
, sizeof(clientsin
));
156 slen
= sizeof(clientsin
);
157 if ((csock
= accept(sock
, (struct sockaddr
*)&clientsin
, &slen
)) == -1) {
161 parse_request(csock
, sc
);