mirror of https://github.com/F-Stack/f-stack.git
IP_MAC_Parsing
This commit is contained in:
parent
49fe7d7e70
commit
5e524c75ab
|
@ -0,0 +1,116 @@
|
|||
#include <rte_eal.h>
|
||||
#include <rte_ethdev.h>
|
||||
#include <rte_mbuf.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define NUM_MBUFS (4096 - 1)
|
||||
#define BURST_SIZE 32
|
||||
|
||||
int gDpdkPortId = 0; // 网络适配器
|
||||
|
||||
// DPDK中用于保存以太网端口配置参数的结构体类型
|
||||
static const struct rte_eth_conf port_conf_default = {
|
||||
.rxmode = {.max_rx_pkt_len = RTE_ETHER_MAX_LEN}
|
||||
};
|
||||
|
||||
// 初始化以太网端口
|
||||
static void z_init_port(struct rte_mempool *mbuf_pool) {
|
||||
uint16_t nb_sys_ports = rte_eth_dev_count_avail();
|
||||
if (nb_sys_ports == 0) {
|
||||
rte_exit(EXIT_FAILURE, "No support eth found\n");
|
||||
}
|
||||
|
||||
struct rte_eth_dev_info dev_info;
|
||||
rte_eth_dev_info_get(gDpdkPortId, &dev_info);
|
||||
|
||||
const int num_rx_queues = 1; // 接收队列
|
||||
struct rte_eth_conf port_conf = port_conf_default;
|
||||
|
||||
if (rte_eth_dev_configure(gDpdkPortId, num_rx_queues, 0, &port_conf) < 0) {
|
||||
rte_exit(EXIT_FAILURE, "Could not configure port\n");
|
||||
}
|
||||
|
||||
if (rte_eth_rx_queue_setup(gDpdkPortId, 0, 1024, rte_eth_dev_socket_id(gDpdkPortId), NULL, mbuf_pool) < 0) {
|
||||
rte_exit(EXIT_FAILURE, "Could not setup RX queue\n");
|
||||
}
|
||||
|
||||
if (rte_eth_dev_start(gDpdkPortId) < 0) {
|
||||
rte_exit(EXIT_FAILURE, "Could not start\n");
|
||||
}
|
||||
|
||||
rte_eth_promiscuous_enable(gDpdkPortId);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
// 初始化 DPDK
|
||||
if (rte_eal_init(argc, argv) < 0) {
|
||||
rte_exit(EXIT_FAILURE, "Error with EAL init\n");
|
||||
}
|
||||
|
||||
// 创建内存池
|
||||
struct rte_mempool *mbuf_pool = rte_pktmbuf_pool_create("mbuf pool", NUM_MBUFS, 0, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
|
||||
if (mbuf_pool == NULL) {
|
||||
rte_exit(EXIT_FAILURE, "Could not create mbuf pool\n");
|
||||
}
|
||||
|
||||
// 初始化以太网端口
|
||||
z_init_port(mbuf_pool);
|
||||
|
||||
// 打开文件用于存储解析结果
|
||||
int fd = open("parsed_packets.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
|
||||
if (fd < 0) {
|
||||
rte_exit(EXIT_FAILURE, "Could not open output file\n");
|
||||
}
|
||||
|
||||
while (1) {
|
||||
struct rte_mbuf *mbufs[BURST_SIZE];
|
||||
unsigned num_recvd = rte_eth_rx_burst(gDpdkPortId, 0, mbufs, BURST_SIZE);
|
||||
if (num_recvd > BURST_SIZE) {
|
||||
rte_exit(EXIT_FAILURE, "Error receiving from eth\n");
|
||||
}
|
||||
|
||||
for (unsigned i = 0; i < num_recvd; i++) {
|
||||
struct rte_ether_hdr *eth_hdr = rte_pktmbuf_mtod(mbufs[i], struct rte_ether_hdr *);
|
||||
|
||||
// 解析以太网头
|
||||
char eth_src[18], eth_dst[18];
|
||||
snprintf(eth_src, sizeof(eth_src), "%02X:%02X:%02X:%02X:%02X:%02X",
|
||||
eth_hdr->s_addr.addr_bytes[0], eth_hdr->s_addr.addr_bytes[1],
|
||||
eth_hdr->s_addr.addr_bytes[2], eth_hdr->s_addr.addr_bytes[3],
|
||||
eth_hdr->s_addr.addr_bytes[4], eth_hdr->s_addr.addr_bytes[5]);
|
||||
|
||||
snprintf(eth_dst, sizeof(eth_dst), "%02X:%02X:%02X:%02X:%02X:%02X",
|
||||
eth_hdr->d_addr.addr_bytes[0], eth_hdr->d_addr.addr_bytes[1],
|
||||
eth_hdr->d_addr.addr_bytes[2], eth_hdr->d_addr.addr_bytes[3],
|
||||
eth_hdr->d_addr.addr_bytes[4], eth_hdr->d_addr.addr_bytes[5]);
|
||||
|
||||
// 判断是否为 IPv4
|
||||
if (eth_hdr->ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4)) {
|
||||
struct rte_ipv4_hdr *ip_hdr = rte_pktmbuf_mtod_offset(mbufs[i], struct rte_ipv4_hdr *, sizeof(struct rte_ether_hdr));
|
||||
|
||||
// 解析 IPv4 头
|
||||
struct in_addr src_addr, dst_addr;
|
||||
src_addr.s_addr = ip_hdr->src_addr;
|
||||
dst_addr.s_addr = ip_hdr->dst_addr;
|
||||
|
||||
char buffer[256];
|
||||
int len = snprintf(buffer, sizeof(buffer),
|
||||
"Ethernet: src=%s, dst=%s\nIP: src=%s, dst=%s\n\n",
|
||||
eth_src, eth_dst, inet_ntoa(src_addr), inet_ntoa(dst_addr));
|
||||
|
||||
if (write(fd, buffer, len) < 0) {
|
||||
rte_exit(EXIT_FAILURE, "Error writing to file\n");
|
||||
}
|
||||
}
|
||||
|
||||
rte_pktmbuf_free(mbufs[i]);
|
||||
}
|
||||
}
|
||||
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
|
@ -1,26 +0,0 @@
|
|||
TOPDIR=..
|
||||
|
||||
ifeq ($(FF_PATH),)
|
||||
FF_PATH=${TOPDIR}
|
||||
endif
|
||||
|
||||
ifneq ($(shell pkg-config --exists libdpdk && echo 0),0)
|
||||
$(error "No installation of DPDK found, maybe you should export environment variable `PKG_CONFIG_PATH`")
|
||||
endif
|
||||
|
||||
PKGCONF ?= pkg-config
|
||||
|
||||
CFLAGS += -O0 -g -gdwarf-2 $(shell $(PKGCONF) --cflags libdpdk)
|
||||
|
||||
LIBS+= $(shell $(PKGCONF) --static --libs libdpdk)
|
||||
LIBS+= -L${FF_PATH}/lib -Wl,--whole-archive,-lfstack,--no-whole-archive
|
||||
LIBS+= -Wl,--no-whole-archive -lrt -lm -ldl -lcrypto -pthread -lnuma
|
||||
|
||||
TARGET="helloworld"
|
||||
all:
|
||||
cc ${CFLAGS} -DINET6 -o ${TARGET} main.c ${LIBS}
|
||||
cc ${CFLAGS} -o ${TARGET}_epoll main_epoll.c ${LIBS}
|
||||
|
||||
.PHONY: clean
|
||||
clean:
|
||||
rm -f *.o ${TARGET} ${TARGET}_epoll
|
200
example/main.c
200
example/main.c
|
@ -1,200 +0,0 @@
|
|||
#include <stdio.h>
|
||||
#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 <sys/ioctl.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;
|
||||
#ifdef INET6
|
||||
int sockfd6;
|
||||
#endif
|
||||
|
||||
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: 438\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_kevent(kq, NULL, 0, events, MAX_EVENTS, NULL);
|
||||
int i;
|
||||
|
||||
if (nevents < 0) {
|
||||
printf("ff_kevent failed:%d, %s\n", errno,
|
||||
strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
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);
|
||||
#ifdef INET6
|
||||
} else if (clientfd == sockfd || clientfd == sockfd6) {
|
||||
#else
|
||||
} else if (clientfd == sockfd) {
|
||||
#endif
|
||||
int available = (int)event.data;
|
||||
do {
|
||||
int nclientfd = ff_accept(clientfd, NULL, NULL);
|
||||
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);
|
||||
} else if (event.filter == EVFILT_READ) {
|
||||
char buf[256];
|
||||
ssize_t readlen = ff_read(clientfd, buf, sizeof(buf));
|
||||
ssize_t writelen = ff_write(clientfd, html, sizeof(html) - 1);
|
||||
if (writelen < 0){
|
||||
printf("ff_write failed:%d, %s\n", errno,
|
||||
strerror(errno));
|
||||
ff_close(clientfd);
|
||||
}
|
||||
} else {
|
||||
printf("unknown event: %8.8X\n", event.flags);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char * argv[])
|
||||
{
|
||||
ff_init(argc, argv);
|
||||
|
||||
kq = ff_kqueue();
|
||||
if (kq < 0) {
|
||||
printf("ff_kqueue failed, errno:%d, %s\n", errno, strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
sockfd = ff_socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (sockfd < 0) {
|
||||
printf("ff_socket failed, sockfd:%d, errno:%d, %s\n", sockfd, errno, strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* Set non blocking */
|
||||
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, sockfd:%d, errno:%d, %s\n", sockfd, errno, strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
ret = ff_listen(sockfd, MAX_EVENTS);
|
||||
if (ret < 0) {
|
||||
printf("ff_listen failed, sockfd:%d, errno:%d, %s\n", sockfd, errno, strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
EV_SET(&kevSet, sockfd, EVFILT_READ, EV_ADD, 0, MAX_EVENTS, NULL);
|
||||
/* Update kqueue */
|
||||
ff_kevent(kq, &kevSet, 1, NULL, 0, NULL);
|
||||
|
||||
#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);
|
||||
}
|
||||
|
||||
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);
|
||||
ret = ff_kevent(kq, &kevSet, 1, NULL, 0, NULL);
|
||||
if (ret < 0) {
|
||||
printf("ff_kevent failed:%d, %s\n", errno, strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
#endif
|
||||
|
||||
ff_run(loop, NULL);
|
||||
return 0;
|
||||
}
|
|
@ -1,142 +0,0 @@
|
|||
#include <stdio.h>
|
||||
#include <sys/ioctl.h>
|
||||
#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: 438\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;
|
||||
|
||||
for (i = 0; i < nevents; ++i) {
|
||||
/* Handle new connect */
|
||||
if (events[i].data.fd == sockfd) {
|
||||
while (1) {
|
||||
int nclientfd = ff_accept(sockfd, NULL, NULL);
|
||||
if (nclientfd < 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
/* 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;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (events[i].events & EPOLLERR ) {
|
||||
/* Simply close socket */
|
||||
ff_epoll_ctl(epfd, EPOLL_CTL_DEL, events[i].data.fd, NULL);
|
||||
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));
|
||||
if(readlen > 0) {
|
||||
ff_write( events[i].data.fd, html, sizeof(html) - 1);
|
||||
} else {
|
||||
ff_epoll_ctl(epfd, EPOLL_CTL_DEL, events[i].data.fd, NULL);
|
||||
ff_close( events[i].data.fd);
|
||||
}
|
||||
} else {
|
||||
printf("unknown event: %8.8X\n", events[i].events);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char * argv[])
|
||||
{
|
||||
ff_init(argc, argv);
|
||||
|
||||
sockfd = ff_socket(AF_INET, SOCK_STREAM, 0);
|
||||
printf("sockfd:%d\n", sockfd);
|
||||
if (sockfd < 0) {
|
||||
printf("ff_socket failed\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
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");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
ret = ff_listen(sockfd, MAX_EVENTS);
|
||||
if (ret < 0) {
|
||||
printf("ff_listen failed\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
assert((epfd = ff_epoll_create(0)) > 0);
|
||||
ev.data.fd = sockfd;
|
||||
ev.events = EPOLLIN;
|
||||
ff_epoll_ctl(epfd, EPOLL_CTL_ADD, sockfd, &ev);
|
||||
ff_run(loop, NULL);
|
||||
return 0;
|
||||
}
|
|
@ -1,298 +0,0 @@
|
|||
#include <stdio.h>
|
||||
#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"
|
||||
|
||||
#define MAX_EVENTS 512
|
||||
|
||||
/* kevent set */
|
||||
struct kevent kevSet;
|
||||
/* events */
|
||||
struct kevent events[MAX_EVENTS];
|
||||
/* kq */
|
||||
int kq;
|
||||
int sockfd;
|
||||
#ifdef INET6
|
||||
int sockfd6;
|
||||
#endif
|
||||
|
||||
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: 438\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>";
|
||||
|
||||
|
||||
char html1[] =
|
||||
"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: 9438\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 to012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234"
|
||||
|
||||
"5678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234"
|
||||
|
||||
"56789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789\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>";
|
||||
|
||||
char html2[] =
|
||||
"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: 1228\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 to0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789\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>";
|
||||
|
||||
extern int ff_zc_mbuf_get(struct ff_zc_mbuf *m, int len);
|
||||
extern int ff_zc_mbuf_write(struct ff_zc_mbuf *m, const char *data, int len);
|
||||
|
||||
char *buf_tmp;
|
||||
char html_buf[10240];
|
||||
size_t buf_len = 0;
|
||||
struct ff_zc_mbuf zc_buf;
|
||||
|
||||
int loop(void *arg)
|
||||
{
|
||||
/* Wait for events to happen */
|
||||
unsigned nevents = ff_kevent(kq, NULL, 0, events, MAX_EVENTS, NULL);
|
||||
unsigned i;
|
||||
|
||||
buf_len = sizeof(html) - 1;
|
||||
buf_tmp = html;
|
||||
|
||||
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);
|
||||
#ifdef INET6
|
||||
} else if (clientfd == sockfd || clientfd == sockfd6) {
|
||||
#else
|
||||
} else if (clientfd == sockfd) {
|
||||
#endif
|
||||
int available = (int)event.data;
|
||||
do {
|
||||
int nclientfd = ff_accept(clientfd, NULL, NULL);
|
||||
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);
|
||||
} else if (event.filter == EVFILT_READ) {
|
||||
char buf[256];
|
||||
size_t readlen = ff_read(clientfd, buf, sizeof(buf));
|
||||
#ifdef FSTACK_ZC_SEND
|
||||
int ret = ff_zc_mbuf_get(&zc_buf, buf_len);
|
||||
if (ret < 0) {
|
||||
printf("ff_zc_mbuf_get failed, len:%d, errno:%d, %s\n", buf_len, errno, strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* APP can call ff_zc_mbuf_write multi times */
|
||||
int len_part = 1440, off, to_write_len;
|
||||
for (off = 0; off < buf_len;){
|
||||
to_write_len = (buf_len - off) > len_part ? len_part : (buf_len - off);
|
||||
ret = ff_zc_mbuf_write(&zc_buf, (const char *)buf_tmp + off, to_write_len);
|
||||
if (ret != to_write_len) {
|
||||
printf("ff_zc_mbuf_write failed, len:%d, errno:%d, %s\n", to_write_len, errno, strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
off += to_write_len;
|
||||
}
|
||||
|
||||
/* Or call ff_zc_mbuf_write one time */
|
||||
/*
|
||||
if (ret != buf_len) {
|
||||
printf("ff_zc_mbuf_write failed, len:%d, errno:%d, %s\n", buf_len, errno, strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
*/
|
||||
|
||||
/* Simulate the application load */
|
||||
int i, j = 0;
|
||||
for (i = 0; i < 10000; i++){
|
||||
j++;
|
||||
}
|
||||
ff_write(clientfd, zc_buf.bsd_mbuf, buf_len);
|
||||
#else
|
||||
memcpy(html_buf, buf_tmp, buf_len);
|
||||
|
||||
/* Simulate the application load */
|
||||
int i, j = 0;
|
||||
for (i = 0; i < 10000; i++){
|
||||
j++;
|
||||
}
|
||||
|
||||
ff_write(clientfd, html_buf, buf_len);
|
||||
#endif
|
||||
} else {
|
||||
printf("unknown event: %8.8X\n", event.flags);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char * argv[])
|
||||
{
|
||||
ff_init(argc, argv);
|
||||
|
||||
assert((kq = ff_kqueue()) > 0);
|
||||
|
||||
sockfd = ff_socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (sockfd < 0) {
|
||||
printf("ff_socket failed, sockfd:%d, errno:%d, %s\n", sockfd, errno, strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
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, sockfd:%d, errno:%d, %s\n", sockfd, errno, strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
ret = ff_listen(sockfd, MAX_EVENTS);
|
||||
if (ret < 0) {
|
||||
printf("ff_listen failed, sockfd:%d, errno:%d, %s\n", sockfd, errno, strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
EV_SET(&kevSet, sockfd, EVFILT_READ, EV_ADD, 0, MAX_EVENTS, NULL);
|
||||
/* Update kqueue */
|
||||
ff_kevent(kq, &kevSet, 1, NULL, 0, NULL);
|
||||
|
||||
#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);
|
||||
}
|
||||
|
||||
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);
|
||||
ff_kevent(kq, &kevSet, 1, NULL, 0, NULL);
|
||||
#endif
|
||||
|
||||
ff_run(loop, NULL);
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue