diff --git a/endpoint/core/server_subr.c b/endpoint/core/server_subr.c new file mode 100644 index 0000000..341a848 --- /dev/null +++ b/endpoint/core/server_subr.c @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2026, Chloe Moffett + * Provided under the BSD-3 clause + */ + +#include +#include +#include +#include +#include +#include +#include +#include "endpoint/server.h" +#include "endpoint/common.h" + +int +endpoint_server_init(struct endpoint_server *svp) +{ + struct sockaddr_in saddr; + int error; + + if (svp == NULL) { + errno = EINVAL; + return -1; + } + + svp->sockfd = socket(AF_INET, SOCK_STREAM, 0); + if (svp->sockfd < 0) { + perror("socket"); + return -1; + } + + /* Assign the address */ + bzero(&saddr, sizeof(saddr)); + saddr.sin_family = AF_INET; + saddr.sin_port = htons(SERVER_PORT); + saddr.sin_addr.s_addr = INADDR_ANY; + + /* Try to bind it */ + error = bind(svp->sockfd, (struct sockaddr *)&saddr, sizeof(saddr)); + if (error < 0) { + perror("bind"); + close(svp->sockfd); + svp->sockfd = -1; + return -1; + } + + /* Now mark it is active and listening */ + error = listen(svp->sockfd, SERVER_BACKLOG); + if (error < 0) { + perror("listen"); + close(svp->sockfd); + svp->sockfd = -1; + return -1; + } + + return 0; +} + +int +endpoint_server_close(struct endpoint_server *svp) +{ + if (svp == NULL) { + errno = EINVAL; + return -1; + } + + close(svp->sockfd); + return 0; +} diff --git a/endpoint/inc/endpoint/common.h b/endpoint/inc/endpoint/common.h index e705d2f..f9751ca 100644 --- a/endpoint/inc/endpoint/common.h +++ b/endpoint/inc/endpoint/common.h @@ -9,4 +9,10 @@ /* Server software version */ #define SERVER_VERSION "0.0.1" +/* Server listen port */ +#define SERVER_PORT 717 + +/* Server listen backlog */ +#define SERVER_BACKLOG 16 + #endif /* !ENDPOINT_COMMON_H */ diff --git a/endpoint/inc/endpoint/server.h b/endpoint/inc/endpoint/server.h new file mode 100644 index 0000000..1743681 --- /dev/null +++ b/endpoint/inc/endpoint/server.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2026, Chloe Moffett + * Provided under the BSD-3 clause + */ + +#ifndef ENDPOINT_SERVER_H +#define ENDPOINT_SERVER_H 1 + +#include +#include + +/* + * Represents the endpoint server state + * + * @sockfd: Socket file descriptor + */ +struct endpoint_server { + int sockfd; +}; + +/* + * Initialize a new server + * + * @svp: Server pointer + * + * Returns zero on success + */ +int endpoint_server_init(struct endpoint_server *svp); + +/* + * Close the endpoint server + * + * @svp: Server to close + * + * Returns zero on success + */ +int endpoint_server_close(struct endpoint_server *svp); + +#endif /* !ENDPOINT_SERVER_H */