public inbox for [email protected]
 help / color / mirror / Atom feed
* [PATCH gwhttpd 0/2] Two gwhttpd fixes
@ 2022-06-26  2:45 Ammar Faizi
  2022-06-26  2:45 ` [PATCH gwhttpd 1/2] gwhttpd: Explicitly set sa_family before bind() Ammar Faizi
  2022-06-26  2:45 ` [PATCH gwhttpd 2/2] gwhttpd: Fix `EBADF` errors at destruction Ammar Faizi
  0 siblings, 2 replies; 3+ messages in thread
From: Ammar Faizi @ 2022-06-26  2:45 UTC (permalink / raw)
  To: GNU/Weeb Mailing List; +Cc: Ammar Faizi, Yonle

Hi,

This small series contains two fixes for gwhttpd.

## Patch 1
Yonle reported that he is getting an error on bind():

  Address family not supported by protocol

This doesn't mean that Yonle's device doesn't support IPv4, but it's
because we don't explicitly set the sa_family to AF_INET. This will
happen to work just fine only on a platform where `AF_INET == 0` since
we have a prior memset() call that zeroes the whole struct sockaddr_in.

On platforms that have `AF_INET != 0`, this will break. Fix this by
simply setting the `saddr.sa_family` to AF_INET at initialization.

## Patch 2
When Yonle reported an issue about error on bind(), he attached an
strace output and it has many EBADF errors like this:

  close(0) = 0
  close(0) = -1 EBADF (Bad file descriptor)
  close(0) = -1 EBADF (Bad file descriptor)
  close(0) = -1 EBADF (Bad file descriptor)
  close(0) = -1 EBADF (Bad file descriptor)
  close(0) = -1 EBADF (Bad file descriptor)
  ...

This happens because all sess[i].fd is set to 0 at initialization while
the destructor is supposed to close the file descriptor when it's not
equal to -1. Fix this by setting all file descriptor fields to -1 at
initialization.

Reported-by: Yonle <[email protected]>
Signed-off-by: Ammar Faizi <[email protected]>
---

Ammar Faizi (2):
  gwhttpd: Explicitly set sa_family before bind()
  gwhttpd: Fix `EBADF` errors at destruction

 gwhttpd.cpp | 2 ++
 1 file changed, 2 insertions(+)

base-commit: 864446870ed865423aadb4cb2c46e334010e72b4
-- 
Ammar Faizi


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH gwhttpd 1/2] gwhttpd: Explicitly set sa_family before bind()
  2022-06-26  2:45 [PATCH gwhttpd 0/2] Two gwhttpd fixes Ammar Faizi
@ 2022-06-26  2:45 ` Ammar Faizi
  2022-06-26  2:45 ` [PATCH gwhttpd 2/2] gwhttpd: Fix `EBADF` errors at destruction Ammar Faizi
  1 sibling, 0 replies; 3+ messages in thread
From: Ammar Faizi @ 2022-06-26  2:45 UTC (permalink / raw)
  To: GNU/Weeb Mailing List; +Cc: Ammar Faizi, Yonle

Yonle reported that he is getting an error on bind():

  Address family not supported by protocol

This doesn't mean that Yonle's device doesn't support IPv4, but it's
because we don't explicitly set the sa_family to AF_INET. This will
happen to work just fine only on a platform where `AF_INET == 0` since
we have a prior memset() call that zeroes the whole struct sockaddr_in.

On platforms that have `AF_INET != 0`, this will break. Fix this by
simply setting the `saddr.sa_family` to AF_INET at initialization.

Fixes: https://github.com/ammarfaizi2/gwhttpd/issues/1
Link: https://t.me/GNUWeeb/627604
Reported-by: Yonle <[email protected]>
Signed-off-by: Ammar Faizi <[email protected]>
---
 gwhttpd.cpp | 1 +
 1 file changed, 1 insertion(+)

diff --git a/gwhttpd.cpp b/gwhttpd.cpp
index 4ec3fe3..d038939 100644
--- a/gwhttpd.cpp
+++ b/gwhttpd.cpp
@@ -142,6 +142,7 @@ static int init_socket(struct server_state *state, const char *addr,
 	}
 
 	memset(&saddr, 0, sizeof(saddr));
+	saddr.sin_family = AF_INET;
 	saddr.sin_port = htons(port);
 	saddr.sin_addr.s_addr = inet_addr(addr);
 
-- 
Ammar Faizi


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH gwhttpd 2/2] gwhttpd: Fix `EBADF` errors at destruction
  2022-06-26  2:45 [PATCH gwhttpd 0/2] Two gwhttpd fixes Ammar Faizi
  2022-06-26  2:45 ` [PATCH gwhttpd 1/2] gwhttpd: Explicitly set sa_family before bind() Ammar Faizi
@ 2022-06-26  2:45 ` Ammar Faizi
  1 sibling, 0 replies; 3+ messages in thread
From: Ammar Faizi @ 2022-06-26  2:45 UTC (permalink / raw)
  To: GNU/Weeb Mailing List; +Cc: Ammar Faizi, Yonle

When Yonle reported an issue about error on bind(), he attached an
strace output and it has many EBADF errors like this:

  close(0) = 0
  close(0) = -1 EBADF (Bad file descriptor)
  close(0) = -1 EBADF (Bad file descriptor)
  close(0) = -1 EBADF (Bad file descriptor)
  close(0) = -1 EBADF (Bad file descriptor)
  close(0) = -1 EBADF (Bad file descriptor)
  ...

This happens because all sess[i].fd is set to 0 at initialization while
the destructor is supposed to close the file descriptor when it's not
equal to -1. Fix this by setting all file descriptor fields to -1 at
initialization.

Link: https://github.com/ammarfaizi2/gwhttpd/issues/1
Reported-by: Yonle <[email protected]>
Signed-off-by: Ammar Faizi <[email protected]>
---
 gwhttpd.cpp | 1 +
 1 file changed, 1 insertion(+)

diff --git a/gwhttpd.cpp b/gwhttpd.cpp
index d038939..0f4261d 100644
--- a/gwhttpd.cpp
+++ b/gwhttpd.cpp
@@ -111,6 +111,7 @@ static int init_state(struct server_state *state)
 	}
 
 	for (i = NR_MAX_CLIENTS - 1; i--; ) {
+		state->sess[i].fd = -1;
 		state->sess[i].idx = i;
 		state->sess_free_idx->push(i);
 	}
-- 
Ammar Faizi


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-06-26  2:45 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-06-26  2:45 [PATCH gwhttpd 0/2] Two gwhttpd fixes Ammar Faizi
2022-06-26  2:45 ` [PATCH gwhttpd 1/2] gwhttpd: Explicitly set sa_family before bind() Ammar Faizi
2022-06-26  2:45 ` [PATCH gwhttpd 2/2] gwhttpd: Fix `EBADF` errors at destruction Ammar Faizi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox