Compare commits

..

2 Commits

Author SHA1 Message Date
chloe 3446f78436 endpoint: server: Add server creation logic
Signed-off-by: Ian Moffett <ian@mirocom.org>
2026-05-04 02:42:17 -04:00
chloe 9ffcf336d7 libremail+tools: Add mailbox_create() function
It is best to prefer mailbox_create() rather than try_mkdir() directly
as we'll be able to encapslate mailbox creation logic within it.

Signed-off-by: Chloe M. <chloe@mirocom.org>
2026-05-03 15:49:05 -04:00
6 changed files with 167 additions and 1 deletions
+70
View File
@@ -0,0 +1,70 @@
/*
* Copyright (c) 2026, Chloe Moffett
* Provided under the BSD-3 clause
*/
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdint.h>
#include <stdio.h>
#include <strings.h>
#include <errno.h>
#include <unistd.h>
#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;
}
+6
View File
@@ -9,4 +9,10 @@
/* Server software version */ /* Server software version */
#define SERVER_VERSION "0.0.1" #define SERVER_VERSION "0.0.1"
/* Server listen port */
#define SERVER_PORT 717
/* Server listen backlog */
#define SERVER_BACKLOG 16
#endif /* !ENDPOINT_COMMON_H */ #endif /* !ENDPOINT_COMMON_H */
+39
View File
@@ -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 <stdint.h>
#include <stddef.h>
/*
* 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 */
+29
View File
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2026, Chloe Moffett
* Provided under the BSD-3 clause
*/
#include <stdint.h>
#include <stdio.h>
#include <stddef.h>
#include <errno.h>
#include "libremail/mailbox.h"
int
mailbox_create(const char *path, mode_t mode)
{
int error;
if (path == NULL) {
errno = EINVAL;
return -1;
}
error = mkdir(path, mode);
if (error < 0) {
perror("mkdir");
return -1;
}
return 0;
}
+21
View File
@@ -0,0 +1,21 @@
/*
* Copyright (c) 2026, Chloe Moffett
* Provided under the BSD-3 clause
*/
#ifndef LIBREMAIL_MAILBOX_H
#define LIBREMAIL_MAILBOX_H 1
#include <sys/stat.h>
/*
* Create a mailbox directory
*
* @path: Path to mailbox to create
* @mode: Mode to assign to directory
*
* Returns zero on success
*/
int mailbox_create(const char *path, mode_t mode);
#endif /* !LIBREMAIL_MAILBOX_H */
+2 -1
View File
@@ -10,6 +10,7 @@
#include <dirent.h> #include <dirent.h>
#include "libremail/file.h" #include "libremail/file.h"
#include "libremail/common.h" #include "libremail/common.h"
#include "libremail/mailbox.h"
/* Maximum command components */ /* Maximum command components */
#define CMD_MAX_CNP 6 #define CMD_MAX_CNP 6
@@ -120,7 +121,7 @@ cmd_mailbox_create(const char *cmdlist[CMD_MAX_CNP], size_t count)
MAILBOX_PREFIX, cmdlist[2]); MAILBOX_PREFIX, cmdlist[2]);
/* Create the mailbox */ /* Create the mailbox */
if (try_mkdir(pathbuf, DEFAULT_MAILBOX_MODE) < 0) { if (mailbox_create(pathbuf, DEFAULT_MAILBOX_MODE) < 0) {
printf("fatal: failed to create mailbox\n"); printf("fatal: failed to create mailbox\n");
perror("try_mkdir"); perror("try_mkdir");
return -1; return -1;