Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

server: Support opening on an existing socket fd #111

Merged
merged 1 commit into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ enum nvnc__socket_type {
NVNC__SOCKET_TCP,
NVNC__SOCKET_UNIX,
NVNC__SOCKET_WEBSOCKET,
NVNC__SOCKET_FROM_FD,
};

struct nvnc {
Expand Down
1 change: 1 addition & 0 deletions include/neatvnc.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ extern const char nvnc_version[];
struct nvnc* nvnc_open(const char* addr, uint16_t port);
struct nvnc* nvnc_open_unix(const char *addr);
struct nvnc* nvnc_open_websocket(const char* addr, uint16_t port);
struct nvnc* nvnc_open_from_fd(int fd);
void nvnc_close(struct nvnc* self);

void nvnc_add_display(struct nvnc*, struct nvnc_display*);
Expand Down
21 changes: 15 additions & 6 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -1975,22 +1975,25 @@ static int bind_address_unix(const char* name)
}

static int bind_address(const char* name, uint16_t port,
enum nvnc__socket_type type)
int fd, enum nvnc__socket_type type)
{
switch (type) {
case NVNC__SOCKET_TCP:
case NVNC__SOCKET_WEBSOCKET:
return bind_address_tcp(name, port);
case NVNC__SOCKET_UNIX:
return bind_address_unix(name);
case NVNC__SOCKET_FROM_FD:
// nothing to bind
return fd;
}

nvnc_log(NVNC_LOG_PANIC, "Unknown socket address type");
return -1;
}

static struct nvnc* open_common(const char* address, uint16_t port,
enum nvnc__socket_type type)
int fd, enum nvnc__socket_type type)
{
nvnc__log_init();

Expand All @@ -2006,7 +2009,7 @@ static struct nvnc* open_common(const char* address, uint16_t port,

LIST_INIT(&self->clients);

self->fd = bind_address(address, port, type);
self->fd = bind_address(address, port, fd, type);
if (self->fd < 0)
goto bind_failure;

Expand Down Expand Up @@ -2039,14 +2042,14 @@ static struct nvnc* open_common(const char* address, uint16_t port,
EXPORT
struct nvnc* nvnc_open(const char* address, uint16_t port)
{
return open_common(address, port, NVNC__SOCKET_TCP);
return open_common(address, port, -1, NVNC__SOCKET_TCP);
}

EXPORT
struct nvnc* nvnc_open_websocket(const char *address, uint16_t port)
{
#ifdef ENABLE_WEBSOCKET
return open_common(address, port, NVNC__SOCKET_WEBSOCKET);
return open_common(address, port, -1, NVNC__SOCKET_WEBSOCKET);
#else
return NULL;
#endif
Expand All @@ -2055,7 +2058,13 @@ struct nvnc* nvnc_open_websocket(const char *address, uint16_t port)
EXPORT
struct nvnc* nvnc_open_unix(const char* address)
{
return open_common(address, 0, NVNC__SOCKET_UNIX);
return open_common(address, 0, -1, NVNC__SOCKET_UNIX);
}

EXPORT
struct nvnc* nvnc_open_from_fd(int fd)
{
return open_common(NULL, 0, fd, NVNC__SOCKET_FROM_FD);
}

static void unlink_fd_path(int fd)
Expand Down