2017-04-21 10:43:26 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
2017-11-21 03:42:09 +00:00
|
|
|
#include <string.h>
|
2017-04-21 10:43:26 +00:00
|
|
|
#include <strings.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "ff_config.h"
|
|
|
|
#include "ff_api.h"
|
|
|
|
|
|
|
|
#define MAX_EVENTS 512
|
|
|
|
|
|
|
|
/* kevent set */
|
|
|
|
struct kevent kevSet;
|
|
|
|
/* events */
|
|
|
|
struct kevent events[MAX_EVENTS];
|
|
|
|
/* kq */
|
|
|
|
int kq;
|
|
|
|
int sockfd;
|
2019-08-08 14:03:57 +00:00
|
|
|
#ifdef INET6
|
|
|
|
int sockfd6;
|
|
|
|
#endif
|
2017-04-21 10:43:26 +00:00
|
|
|
|
|
|
|
char html[] =
|
|
|
|
"HTTP/1.1 200 OK\r\n"
|
|
|
|
"Server: F-Stack\r\n"
|
|
|
|
"Date: Sat, 25 Feb 2017 09:26:33 GMT\r\n"
|
|
|
|
"Content-Type: text/html\r\n"
|
2019-03-14 09:17:58 +00:00
|
|
|
"Content-Length: 438\r\n"
|
2017-04-21 10:43:26 +00:00
|
|
|
"Last-Modified: Tue, 21 Feb 2017 09:44:03 GMT\r\n"
|
|
|
|
"Connection: keep-alive\r\n"
|
|
|
|
"Accept-Ranges: bytes\r\n"
|
|
|
|
"\r\n"
|
|
|
|
"<!DOCTYPE html>\r\n"
|
|
|
|
"<html>\r\n"
|
|
|
|
"<head>\r\n"
|
|
|
|
"<title>Welcome to F-Stack!</title>\r\n"
|
|
|
|
"<style>\r\n"
|
|
|
|
" body { \r\n"
|
|
|
|
" width: 35em;\r\n"
|
|
|
|
" margin: 0 auto; \r\n"
|
|
|
|
" font-family: Tahoma, Verdana, Arial, sans-serif;\r\n"
|
|
|
|
" }\r\n"
|
|
|
|
"</style>\r\n"
|
|
|
|
"</head>\r\n"
|
|
|
|
"<body>\r\n"
|
|
|
|
"<h1>Welcome to F-Stack!</h1>\r\n"
|
|
|
|
"\r\n"
|
|
|
|
"<p>For online documentation and support please refer to\r\n"
|
|
|
|
"<a href=\"http://F-Stack.org/\">F-Stack.org</a>.<br/>\r\n"
|
|
|
|
"\r\n"
|
|
|
|
"<p><em>Thank you for using F-Stack.</em></p>\r\n"
|
|
|
|
"</body>\r\n"
|
|
|
|
"</html>";
|
|
|
|
|
|
|
|
int loop(void *arg)
|
|
|
|
{
|
|
|
|
/* Wait for events to happen */
|
|
|
|
unsigned nevents = ff_kevent(kq, NULL, 0, events, MAX_EVENTS, NULL);
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
for (i = 0; i < nevents; ++i) {
|
|
|
|
struct kevent event = events[i];
|
|
|
|
int clientfd = (int)event.ident;
|
|
|
|
|
|
|
|
/* Handle disconnect */
|
|
|
|
if (event.flags & EV_EOF) {
|
|
|
|
/* Simply close socket */
|
|
|
|
ff_close(clientfd);
|
2019-08-08 14:03:57 +00:00
|
|
|
#ifdef INET6
|
|
|
|
} else if (clientfd == sockfd || clientfd == sockfd6) {
|
|
|
|
#else
|
2017-04-21 10:43:26 +00:00
|
|
|
} else if (clientfd == sockfd) {
|
2019-08-08 14:03:57 +00:00
|
|
|
#endif
|
2017-12-21 14:45:10 +00:00
|
|
|
int available = (int)event.data;
|
|
|
|
do {
|
2019-08-08 14:03:57 +00:00
|
|
|
int nclientfd = ff_accept(clientfd, NULL, NULL);
|
2017-12-21 14:45:10 +00:00
|
|
|
if (nclientfd < 0) {
|
|
|
|
printf("ff_accept failed:%d, %s\n", errno,
|
|
|
|
strerror(errno));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add to event list */
|
|
|
|
EV_SET(&kevSet, nclientfd, EVFILT_READ, EV_ADD, 0, 0, NULL);
|
|
|
|
|
|
|
|
if(ff_kevent(kq, &kevSet, 1, NULL, 0, NULL) < 0) {
|
|
|
|
printf("ff_kevent error:%d, %s\n", errno,
|
|
|
|
strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
available--;
|
|
|
|
} while (available);
|
2017-04-21 10:43:26 +00:00
|
|
|
} else if (event.filter == EVFILT_READ) {
|
|
|
|
char buf[256];
|
|
|
|
size_t readlen = ff_read(clientfd, buf, sizeof(buf));
|
|
|
|
|
2019-03-14 09:17:58 +00:00
|
|
|
ff_write(clientfd, html, sizeof(html) - 1);
|
2017-04-21 10:43:26 +00:00
|
|
|
} else {
|
2017-05-10 09:20:41 +00:00
|
|
|
printf("unknown event: %8.8X\n", event.flags);
|
2017-04-21 10:43:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char * argv[])
|
|
|
|
{
|
2017-08-08 14:36:49 +00:00
|
|
|
ff_init(argc, argv);
|
2017-04-21 10:43:26 +00:00
|
|
|
|
2019-08-08 14:03:57 +00:00
|
|
|
assert((kq = ff_kqueue()) > 0);
|
|
|
|
|
2017-08-08 15:47:38 +00:00
|
|
|
sockfd = ff_socket(AF_INET, SOCK_STREAM, 0);
|
2017-09-14 10:57:37 +00:00
|
|
|
if (sockfd < 0) {
|
2019-08-08 14:03:57 +00:00
|
|
|
printf("ff_socket failed, sockfd:%d, errno:%d, %s\n", sockfd, errno, strerror(errno));
|
2017-09-14 10:57:37 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2017-04-21 10:43:26 +00:00
|
|
|
|
|
|
|
struct sockaddr_in my_addr;
|
|
|
|
bzero(&my_addr, sizeof(my_addr));
|
|
|
|
my_addr.sin_family = AF_INET;
|
|
|
|
my_addr.sin_port = htons(80);
|
|
|
|
my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
|
|
|
|
|
|
int ret = ff_bind(sockfd, (struct linux_sockaddr *)&my_addr, sizeof(my_addr));
|
|
|
|
if (ret < 0) {
|
2019-08-08 14:03:57 +00:00
|
|
|
printf("ff_bind failed, sockfd:%d, errno:%d, %s\n", sockfd, errno, strerror(errno));
|
2017-09-14 10:57:37 +00:00
|
|
|
exit(1);
|
2017-04-21 10:43:26 +00:00
|
|
|
}
|
|
|
|
|
2019-08-08 14:03:57 +00:00
|
|
|
ret = ff_listen(sockfd, MAX_EVENTS);
|
2017-04-21 10:43:26 +00:00
|
|
|
if (ret < 0) {
|
2019-08-08 14:03:57 +00:00
|
|
|
printf("ff_listen failed, sockfd:%d, errno:%d, %s\n", sockfd, errno, strerror(errno));
|
2017-09-14 10:57:37 +00:00
|
|
|
exit(1);
|
2017-04-21 10:43:26 +00:00
|
|
|
}
|
|
|
|
|
2017-12-21 14:45:10 +00:00
|
|
|
EV_SET(&kevSet, sockfd, EVFILT_READ, EV_ADD, 0, MAX_EVENTS, NULL);
|
2019-08-08 14:03:57 +00:00
|
|
|
/* Update kqueue */
|
|
|
|
ff_kevent(kq, &kevSet, 1, NULL, 0, NULL);
|
2017-04-21 10:43:26 +00:00
|
|
|
|
2019-08-08 14:03:57 +00:00
|
|
|
#ifdef INET6
|
|
|
|
sockfd6 = ff_socket(AF_INET6, SOCK_STREAM, 0);
|
|
|
|
if (sockfd6 < 0) {
|
|
|
|
printf("ff_socket failed, sockfd6:%d, errno:%d, %s\n", sockfd6, errno, strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
2017-04-21 10:43:26 +00:00
|
|
|
|
2019-08-08 14:03:57 +00:00
|
|
|
struct sockaddr_in6 my_addr6;
|
|
|
|
bzero(&my_addr6, sizeof(my_addr6));
|
|
|
|
my_addr6.sin6_family = AF_INET6;
|
|
|
|
my_addr6.sin6_port = htons(80);
|
|
|
|
my_addr6.sin6_addr = in6addr_any;
|
|
|
|
|
|
|
|
ret = ff_bind(sockfd6, (struct linux_sockaddr *)&my_addr6, sizeof(my_addr6));
|
|
|
|
if (ret < 0) {
|
|
|
|
printf("ff_bind failed, sockfd6:%d, errno:%d, %s\n", sockfd6, errno, strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = ff_listen(sockfd6, MAX_EVENTS);
|
|
|
|
if (ret < 0) {
|
|
|
|
printf("ff_listen failed, sockfd6:%d, errno:%d, %s\n", sockfd6, errno, strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
EV_SET(&kevSet, sockfd6, EVFILT_READ, EV_ADD, 0, MAX_EVENTS, NULL);
|
2017-04-21 10:43:26 +00:00
|
|
|
ff_kevent(kq, &kevSet, 1, NULL, 0, NULL);
|
2019-08-08 14:03:57 +00:00
|
|
|
#endif
|
2017-04-21 10:43:26 +00:00
|
|
|
|
|
|
|
ff_run(loop, NULL);
|
|
|
|
return 0;
|
|
|
|
}
|