2017-05-15 12:49:25 +03:00
|
|
|
#include <stdio.h>
|
2017-05-15 12:55:26 +03:00
|
|
|
#include <sys/ioctl.h>
|
2017-05-15 12:49:25 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#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"
|
|
|
|
#include "ff_epoll.h"
|
|
|
|
|
|
|
|
|
|
|
|
#define MAX_EVENTS 512
|
|
|
|
|
|
|
|
struct epoll_event ev;
|
|
|
|
struct epoll_event events[MAX_EVENTS];
|
|
|
|
|
|
|
|
int epfd;
|
|
|
|
int sockfd;
|
|
|
|
|
|
|
|
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"
|
|
|
|
"Content-Length: 439\r\n"
|
|
|
|
"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 */
|
|
|
|
|
|
|
|
int nevents = ff_epoll_wait(epfd, events, MAX_EVENTS, 0);
|
|
|
|
int i;
|
|
|
|
|
2017-11-21 06:42:09 +03:00
|
|
|
for (i = 0; i < nevents; ++i) {
|
2017-05-15 12:49:25 +03:00
|
|
|
/* Handle new connect */
|
|
|
|
if (events[i].data.fd == sockfd) {
|
2017-12-21 17:45:10 +03:00
|
|
|
while (1) {
|
|
|
|
int nclientfd = ff_accept(sockfd, NULL, NULL);
|
|
|
|
if (nclientfd < 0) {
|
|
|
|
break;
|
|
|
|
}
|
2017-11-21 06:42:09 +03:00
|
|
|
|
2017-12-21 17:45:10 +03:00
|
|
|
/* Add to event list */
|
|
|
|
ev.data.fd = nclientfd;
|
|
|
|
ev.events = EPOLLIN;
|
|
|
|
if (ff_epoll_ctl(epfd, EPOLL_CTL_ADD, nclientfd, &ev) != 0) {
|
|
|
|
printf("ff_epoll_ctl failed:%d, %s\n", errno,
|
|
|
|
strerror(errno));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-05-15 12:49:25 +03:00
|
|
|
} else {
|
|
|
|
if (events[i].events & EPOLLERR ) {
|
|
|
|
/* Simply close socket */
|
2017-11-21 06:42:09 +03:00
|
|
|
ff_epoll_ctl(epfd, EPOLL_CTL_DEL, events[i].data.fd, NULL);
|
2017-05-15 12:49:25 +03:00
|
|
|
ff_close(events[i].data.fd);
|
|
|
|
} else if (events[i].events & EPOLLIN) {
|
|
|
|
char buf[256];
|
|
|
|
size_t readlen = ff_read( events[i].data.fd, buf, sizeof(buf));
|
2017-11-21 06:42:09 +03:00
|
|
|
if(readlen > 0) {
|
2017-05-15 12:55:26 +03:00
|
|
|
ff_write( events[i].data.fd, html, sizeof(html));
|
2017-11-21 06:42:09 +03:00
|
|
|
} else {
|
|
|
|
ff_epoll_ctl(epfd, EPOLL_CTL_DEL, events[i].data.fd, NULL);
|
|
|
|
ff_close( events[i].data.fd);
|
|
|
|
}
|
2017-05-15 12:49:25 +03:00
|
|
|
} else {
|
2017-11-21 06:42:09 +03:00
|
|
|
printf("unknown event: %8.8X\n", events[i].events);
|
2017-05-15 12:49:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char * argv[])
|
|
|
|
{
|
2017-08-08 17:36:49 +03:00
|
|
|
ff_init(argc, argv);
|
2017-05-15 12:49:25 +03:00
|
|
|
|
|
|
|
sockfd = ff_socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
printf("sockfd:%d\n", sockfd);
|
2017-09-14 13:57:37 +03:00
|
|
|
if (sockfd < 0) {
|
2017-05-15 12:49:25 +03:00
|
|
|
printf("ff_socket failed\n");
|
2017-09-14 13:57:37 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
2017-05-15 12:49:25 +03:00
|
|
|
|
|
|
|
int on = 1;
|
|
|
|
ff_ioctl(sockfd, FIONBIO, &on);
|
|
|
|
|
|
|
|
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) {
|
|
|
|
printf("ff_bind failed\n");
|
2017-09-14 13:57:37 +03:00
|
|
|
exit(1);
|
2017-05-15 12:49:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = ff_listen(sockfd, MAX_EVENTS);
|
|
|
|
if (ret < 0) {
|
|
|
|
printf("ff_listen failed\n");
|
2017-09-14 13:57:37 +03:00
|
|
|
exit(1);
|
2017-05-15 12:49:25 +03:00
|
|
|
}
|
2017-11-21 06:42:09 +03:00
|
|
|
|
2017-05-15 12:49:25 +03:00
|
|
|
assert((epfd = ff_epoll_create(0)) > 0);
|
2017-05-15 12:55:26 +03:00
|
|
|
ev.data.fd = sockfd;
|
|
|
|
ev.events = EPOLLIN;
|
|
|
|
ff_epoll_ctl(epfd, EPOLL_CTL_ADD, sockfd, &ev);
|
2017-05-15 12:49:25 +03:00
|
|
|
ff_run(loop, NULL);
|
|
|
|
return 0;
|
|
|
|
}
|