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

support UNIX sockets #49

Merged
merged 1 commit into from
Apr 4, 2021
Merged

support UNIX sockets #49

merged 1 commit into from
Apr 4, 2021

Conversation

r-c-f
Copy link
Contributor

@r-c-f r-c-f commented Mar 17, 2021

Use an extended getaddrinfo_unix which will treat any absolute path
(i.e. address starting with '/') as a place to bind a UNIX domain
socket. Store the socket path in the main nvnc structure to allow for
cleanup on closure. Closes #1.

src/server.c Outdated
return -1;
if (!(addr = malloc(sizeof(*addr))))
return -1;
if (!(*res = calloc(1, sizeof(**res)))) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes assumptions about the inner workings of the C standard library.

src/server.c Outdated
@@ -1009,19 +1010,49 @@ static void on_connection(void* obj)
free(client);
}

static int bind_address(const char* name, int port)
static int getaddrinfo_unix(const char *name, const char *service, struct addrinfo *hints, struct addrinfo **res)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this project, the * is placed to the left.

src/server.c Outdated
return 0;
}

static int bind_address(const char* name, int port, bool unix_)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shoehorning unix sockets into this function is a bit messy. There should be two functions instead: bind_address_tcp and bind_address_unix.

src/server.c Outdated
@@ -1081,8 +1112,7 @@ static void on_main_dispatch(void* aml_obj)
process_fb_update_requests(client);
}

EXPORT
struct nvnc* nvnc_open(const char* address, uint16_t port)
static struct nvnc* open_common(const char *address, uint16_t port, bool unix_)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's call that boolean is_unix. This is OK, but I usually try to avoid boolean arguments like this because they make code harder to read. When you come across a line in code like this:

open_connection(address, 0, true)

you have to look at the definition of the function to see what it does. However, when you read something like

open_connection(address, 0, SOCKTYPE_UNIX);

you don't really have to look it up.

src/server.c Outdated
@@ -1094,7 +1124,8 @@ struct nvnc* nvnc_open(const char* address, uint16_t port)

LIST_INIT(&self->clients);

self->fd = bind_address(address, port);
self->fd = bind_address(address, port, unix_);

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not add an empty line here.

src/server.c Outdated
@@ -1125,9 +1156,25 @@ struct nvnc* nvnc_open(const char* address, uint16_t port)
aml_unref(self->poll_handle);
failure:
close(self->fd);
if (unix_) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if someone tried to create a socket that already existed? It seems that this will cause the already existing socket to be unlinked.

src/server.c Outdated
@@ -1140,6 +1187,16 @@ void nvnc_close(struct nvnc* self)
LIST_FOREACH_SAFE (client, &self->clients, link, tmp)
client_unref(client);

struct sockaddr_storage addr;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this code block can be extracted to a function to keep nvnc_close short and easy to read.

src/server.c Outdated
struct sockaddr_storage addr;
struct sockaddr_un *addr_unix;
socklen_t addr_len = sizeof(addr);
if (!getsockname(self->fd, (struct sockaddr*)&addr, &addr_len)) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This project compares against zero to check for errors. I.e. we use == 0, not !.

src/server.c Outdated
@@ -1081,8 +1106,12 @@ static void on_main_dispatch(void* aml_obj)
process_fb_update_requests(client);
}

EXPORT
struct nvnc* nvnc_open(const char* address, uint16_t port)
enum addrtype {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

definitions such as this one belong up top, between includes and function prototypes.

src/server.c Outdated
self->fd = bind_address_unix(address);
break;
default:
self->fd = -1;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This situation should never arise if the code is correctly written, so abort() will do nicely.

src/server.c Outdated

static void unlink_fd_path(int fd)
{
struct sockaddr_storage addr;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is sockaddr_storage really needed here?

Copy link
Owner

@any1 any1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is pretty close be being ready now.

src/server.c Outdated
@@ -1124,10 +1163,41 @@ struct nvnc* nvnc_open(const char* address, uint16_t port)
poll_start_failure:
aml_unref(self->poll_handle);
failure:
close(self->fd);
if (self->fd >= 0) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move failure: below close(self->fd) and call it bind_failure: instead of having this if here. listen_failure and handle_failure will also need to be added in reverse order above close().

src/server.c Outdated
return -1;
strcpy(addr.sun_path, name);

int fd;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: There's no need to pre-declare fd here.

};

if (strlen(name) >= sizeof(addr.sun_path))
return -1;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we set errno to ENAMETOOLONG here?

src/server.c Outdated
@@ -1094,7 +1123,17 @@ struct nvnc* nvnc_open(const char* address, uint16_t port)

LIST_INIT(&self->clients);

self->fd = bind_address(address, port);
switch (type) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be neat to turn extract this to a function called bind_address.

Copy link
Owner

@any1 any1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good after that one last comment has been addressed.

Can you squash into a single commit and force push?

src/server.c Outdated

switch (type) {
case ADDRTYPE_TCP:
fd = bind_address_tcp(name, port);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can return straight from the switch and move the default case outside the switch. Then, -Wswitch will give a warning when not all enum names are used.

Adds support for UNIX domain sockets with `nvnc_open_unix()` function.
Closes any1#1.
r-c-f added a commit to r-c-f/wayvnc that referenced this pull request Apr 4, 2021
Makes use of the functionality added in
any1/neatvnc#49 to support UNIX domain sockets
with a command line flag.
@any1 any1 merged commit b320723 into any1:master Apr 4, 2021
@any1
Copy link
Owner

any1 commented Apr 4, 2021

Thanks!

r-c-f added a commit to r-c-f/wayvnc that referenced this pull request Apr 5, 2021
Makes use of the functionality added in
any1/neatvnc#49 to support UNIX domain sockets
with a command line flag.
any1 pushed a commit to any1/wayvnc that referenced this pull request Apr 6, 2021
Makes use of the functionality added in
any1/neatvnc#49 to support UNIX domain sockets
with a command line flag.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unix Socket
2 participants