Commit 9dee56a4 authored by Florian Fischer's avatar Florian Fischer
Browse files

support ipv6

parent 5a03476d
Pipeline #57968 failed with stages
in 25 seconds
......@@ -2,7 +2,7 @@
// Copyright © 2020 Florian Fischer
/* #include <assert.h> */
#include <errno.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
......@@ -78,25 +78,38 @@ void client_func(void* arg) {
#define BACKLOG 5
static void welcome_func() {
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
die("socket failed");
int sockfd;
struct addrinfo* servaddr;
char* port_s;
if (asprintf(&port_s, "%d", port) < 0) {
die("asprintf failed");
}
int err = getaddrinfo("::", port_s, NULL, &servaddr);
if (err) {
if (err == EAI_SYSTEM) {
die("getaddrinfo failed");
}
fprintf(stderr, "error in getaddrinfo: %s", gai_strerror(err));
exit(EXIT_FAILURE);
}
struct sockaddr_in servaddr;
memset(&servaddr, 0, sizeof(servaddr)); // NOLINT
free(port_s);
// assign IP, PORT
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons(port);
// socket creation and verification
sockfd = socket(servaddr->ai_family, servaddr->ai_socktype, servaddr->ai_protocol);
if (sockfd == -1) {
die("listen socket creation failed");
}
int reuseaddr = 1;
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(reuseaddr)) == -1) {
die("setsockopt failed");
}
if (bind(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) == -1) {
if (bind(sockfd, servaddr->ai_addr, servaddr->ai_addrlen) == -1) {
die("bind failed");
}
......@@ -105,7 +118,7 @@ static void welcome_func() {
}
for (;;) {
int client_fd = async_accept(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr), UNSERIALIZED);
int client_fd = async_accept(sockfd, NULL, 0, UNSERIALIZED);
if (unlikely(client_fd < 0)) {
perror("accept failed");
}
......@@ -127,9 +140,6 @@ int main(int argc, char* argv[]) {
exit(EXIT_FAILURE);
}
const int decimal = 10;
port = (int)strtol(argv[1], NULL, decimal);
init_runtime();
fiber* welcome_fiber = fiber_from0(welcome_func);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment