return 0;
}
-static int create_socket(struct fireperf_config* conf) {
+static int create_socket(struct fireperf_config* conf, int i) {
int r;
// Open a new socket
struct sockaddr_in6 addr = {
.sin6_family = AF_INET6,
- .sin6_port = htons(conf->port),
+ .sin6_port = htons(conf->port + i),
};
// Bind it to the selected port
return 0;
}
+static int is_listening_socket(struct fireperf_config* conf, int* sockets, int fd) {
+ for (unsigned int i = 0; i < conf->sockets; i++) {
+ if (sockets[i] == fd)
+ return 1;
+ }
+
+ return 0;
+}
+
int fireperf_server(struct fireperf_config* conf) {
struct fireperf_server_stats stats = { 0 };
DEBUG(conf, "Launching " PACKAGE_NAME " in server mode\n");
- int sockfd = -1;
+ int sockets[conf->sockets];
int epollfd = -1;
struct epoll_event events[EPOLL_MAX_EVENTS];
int r = 1;
- // Create listening socket
- sockfd = create_socket(conf);
- if (sockfd < 0)
- return 1;
-
// Initialize epoll()
epollfd = epoll_create1(0);
if (epollfd < 0) {
return 1;
}
- // Add listening socket
- struct epoll_event ev = {
- .events = EPOLLIN,
- .data.fd = sockfd,
- };
+ // Create listening sockets
+ for (unsigned int i = 0; i < conf->sockets; i++) {
+ int sockfd = create_socket(conf, i);
+ if (sockfd < 0)
+ return 1;
- if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sockfd, &ev)) {
- ERROR(conf, "Could not add socket file descriptor to epoll(): %s\n",
- strerror(errno));
- goto ERROR;
+ sockets[i] = sockfd;
+
+ // Add listening socket to epoll
+ struct epoll_event ev = {
+ .events = EPOLLIN,
+ .data.fd = sockfd,
+ };
+
+ if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sockfd, &ev)) {
+ ERROR(conf, "Could not add socket file descriptor to epoll(): %s\n",
+ strerror(errno));
+ goto ERROR;
+ }
}
// Create timerfd() to print statistics
goto ERROR;
}
- ev.events = EPOLLIN;
- ev.data.fd = timerfd;
+ struct epoll_event ev = {
+ .events = EPOLLIN,
+ .data.fd = timerfd,
+ };
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, timerfd, &ev)) {
ERROR(conf, "Could not add timerfd to epoll(): %s\n",
int fd = events[i].data.fd;
// The listening socket
- if (fd == sockfd) {
- int connfd = accept_connection(conf, sockfd);
+ if (is_listening_socket(conf, sockets, fd)) {
+ int connfd = accept_connection(conf, fd);
if (connfd < 0)
goto ERROR;
}
ERROR:
- if (sockfd > 0)
- close(sockfd);
+ for (unsigned int i = 0; i < conf->sockets; i++) {
+ if (sockets[i] > 0)
+ close(sockets[i]);
+ }
if (epollfd > 0)
close(epollfd);