public inbox for gwml@vger.gnuweeb.org
 help / color / mirror / Atom feed
From: Ahmad Gani <reyuki@gnuweeb.org>
To: Ammar Faizi <ammarfaizi2@gnuweeb.org>
Cc: Ahmad Gani <reyuki@gnuweeb.org>,
	Alviro Iskandar Setiawan <alviro.iskandar@gnuweeb.org>,
	GNU/Weeb Mailing List <gwml@vger.gnuweeb.org>
Subject: [PATCH gwproxy v3 9/9] dnslookup: code style changes
Date: Tue,  5 Aug 2025 13:49:29 +0700	[thread overview]
Message-ID: <20250805064933.109080-10-reyuki@gnuweeb.org> (raw)
In-Reply-To: <20250805064933.109080-1-reyuki@gnuweeb.org>

As requested by Sir Alviro, I change the place of variable declaration
and other minor refactoring.

Signed-off-by: Ahmad Gani <reyuki@gnuweeb.org>
---
 src/gwproxy/dnslookup.c | 82 ++++++++++++++++++++++++++---------------
 src/gwproxy/dnslookup.h |  1 +
 src/gwproxy/dnsparser.c | 15 +++++---
 3 files changed, 64 insertions(+), 34 deletions(-)

diff --git a/src/gwproxy/dnslookup.c b/src/gwproxy/dnslookup.c
index 740955ff6bef..91d38b4e64d2 100644
--- a/src/gwproxy/dnslookup.c
+++ b/src/gwproxy/dnslookup.c
@@ -15,10 +15,11 @@ int gw_ares_process_event(gw_ares_channel_t *channel)
 	struct gw_ares_request *req;
 	gwdns_answ_data raw_answ;
 	socklen_t addrlen;
-	int ret, idx;
+	int ret;
 
 	addrlen = sizeof(*channel->addr);
-	ret = __sys_recvfrom(channel->sockfd, recv_buff, UDP_MSG_LIMIT, MSG_NOSIGNAL, &channel->addr->sa, &addrlen);
+	ret = __sys_recvfrom(channel->sockfd, recv_buff, UDP_MSG_LIMIT,
+				MSG_NOSIGNAL, &channel->addr->sa, &addrlen);
 	if (ret < 0) {
 		/* GW_ARES_WAITING is unused for now. */
 		if (ret == -EAGAIN)
@@ -33,6 +34,7 @@ int gw_ares_process_event(gw_ares_channel_t *channel)
 
 	printf("%d bytes received\n", ret);
 	req = NULL;
+	int idx;
 	for (idx = 0; idx < channel->req_nr; idx++) {
 		if (!memcmp(&channel->reqs[idx]->txid, recv_buff, 2)) {
 			req = channel->reqs[idx];
@@ -63,9 +65,16 @@ int gw_ares_process_event(gw_ares_channel_t *channel)
 	}
 
 	printf("ancount=%u\n", raw_answ.hdr.ancount);
-	for (size_t i = 0; i < raw_answ.hdr.ancount; i++) {
-		gwdns_serialized_answ *answ = raw_answ.rr_answ[i];
-		struct gw_addrinfo_node *new_node = malloc(sizeof(*new_node));
+
+	size_t i;
+	for (i = 0; i < raw_answ.hdr.ancount; i++) {
+		struct gw_addrinfo_node *new_node;
+		gwdns_serialized_answ *answ;
+		struct sockaddr_in6 *i6;
+		struct sockaddr_in *i4;
+
+		answ = raw_answ.rr_answ[i];
+		new_node = malloc(sizeof(*new_node));
 		if (!new_node) {
 			ret = GW_ARES_ENOMEM;
 			goto exit_free;
@@ -73,27 +82,29 @@ int gw_ares_process_event(gw_ares_channel_t *channel)
 		new_node->ai_next = NULL;
 
 		if (answ->rr_type == TYPE_AAAA) {
+			i6 = &new_node->ai_addr.i6;
 			new_node->ai_family = AF_INET6;
-			new_node->ai_addrlen = sizeof(new_node->ai_addr.i6);
-			new_node->ai_addr.i6.sin6_port = req->dst_port;
-			new_node->ai_addr.i6.sin6_family = AF_INET6;
+			new_node->ai_addrlen = sizeof(i6);
+			i6->sin6_port = req->dst_port;
+			i6->sin6_family = AF_INET6;
 			/*
 			 * no overflow.
 			 * it's guaranteed to be true by serialize_answ function
 			 */
-			assert(sizeof(new_node->ai_addr.i6.sin6_addr) == answ->rdlength);
-			memcpy(&new_node->ai_addr.i6.sin6_addr, answ->rdata, answ->rdlength);
+			assert(sizeof(i6->sin6_addr) == answ->rdlength);
+			memcpy(&i6->sin6_addr, answ->rdata, answ->rdlength);
 		} else {
+			i4 = &new_node->ai_addr.i4;
 			new_node->ai_family = AF_INET;
-			new_node->ai_addrlen = sizeof(new_node->ai_addr.i4);
-			new_node->ai_addr.i4.sin_port = req->dst_port;
-			new_node->ai_addr.i4.sin_family = AF_INET;
+			new_node->ai_addrlen = sizeof(i4);
+			i4->sin_port = req->dst_port;
+			i4->sin_family = AF_INET;
 			/*
 			 * no overflow.
 			 * it's guaranteed to be true by serialize_answ function
 			 */
-			assert(sizeof(new_node->ai_addr.i4.sin_addr) == answ->rdlength);
-			memcpy(&new_node->ai_addr.i4.sin_addr, answ->rdata, answ->rdlength);
+			assert(sizeof(i4->sin_addr) == answ->rdlength);
+			memcpy(&i4->sin_addr, answ->rdata, answ->rdlength);
 			new_node->ai_ttl = answ->ttl;
 		}
 
@@ -110,7 +121,9 @@ exit_free:
 	req->refcnt--;
 	assert(req->refcnt >= 0);
 	if (!req->refcnt) {
-		channel->getaddrinfo_cb(req->callback_args, GW_ARES_SUCCESS, req->results);
+		channel->getaddrinfo_cb(
+			req->callback_args, GW_ARES_SUCCESS, req->results
+		);
 		free(req);
 		channel->req_nr--;
 		req = channel->reqs[channel->req_nr];
@@ -120,7 +133,8 @@ exit_free:
 	return ret;
 }
 
-static int resolve_name(gw_ares_channel_t *channel, const char *name, uint16_t type, uint16_t txid)
+static int resolve_name(gw_ares_channel_t *channel, const char *name,
+			uint16_t type, uint16_t txid)
 {
 	uint8_t send_buff[UDP_MSG_LIMIT];
 	gwdns_question_part q;
@@ -143,7 +157,10 @@ static int resolve_name(gw_ares_channel_t *channel, const char *name, uint16_t t
 		return ret;
 	}
 
-	ret = __sys_sendto(channel->sockfd, send_buff, buff_len, MSG_NOSIGNAL, &channel->addr->sa, channel->addrlen);
+	ret = __sys_sendto(
+		channel->sockfd, send_buff, buff_len, MSG_NOSIGNAL,
+		&channel->addr->sa, channel->addrlen
+	);
 	if (ret < 0)
 		return ret;
 
@@ -163,6 +180,7 @@ static int resize_reqs(gw_ares_channel_t *channel)
 	memset(ptr[channel->req_nr], 0, channel->req_nr * sizeof(*channel->reqs));
 	channel->reqs = ptr;
 	channel->req_cap = new_cap;
+
 	return 0;
 }
 
@@ -296,8 +314,11 @@ int gw_ares_init(gw_ares_channel_t **channel, struct gw_ares_options *opts)
 	 * recursion desired (RD) bit and other things in the future.
 	 */
 	c->flags = opts->flags;
-	for (int i = 0; i < c->nr_server; i++) {
-		ret = convert_str_to_ssaddr(opts->servers[i], &c->servers[i], DEFAULT_DOMAIN_PORT);
+	int i;
+	for (i = 0; i < c->nr_server; i++) {
+		ret = convert_str_to_ssaddr(
+			opts->servers[i], &c->servers[i], DEFAULT_DOMAIN_PORT
+		);
 		if (ret)
 			goto error_free_all;
 	}
@@ -312,7 +333,8 @@ int gw_ares_init(gw_ares_channel_t **channel, struct gw_ares_options *opts)
 	c->getaddrinfo_cb = opts->getaddrinfo_cb;
 	c->sockfd = ret;
 	c->addr = &c->servers[0];
-	c->addrlen = c->addr->sa.sa_family == AF_INET ? sizeof(c->addr->i4) : sizeof(c->addr->i6);
+	c->addrlen = c->addr->sa.sa_family == AF_INET ?
+			sizeof(c->addr->i4) : sizeof(c->addr->i6);
 
 	return 0;
 error_free_all:
@@ -355,9 +377,13 @@ static void getaddrinfo_cb(void *arg, int status, struct gw_ares_addrinfo *resul
 	while (node) {
 		int r = convert_ssaddr_to_str(buf, &node->ai_addr);
 		assert(!r);
-		printf("%s: %s\n", node->ai_family == AF_INET6 ? "IPv6" : "IPv4", buf);
+		printf(
+			"%s: %s\n",
+			node->ai_family == AF_INET6 ? "IPv6" : "IPv4", buf
+		);
 		node = node->ai_next;
 	}
+
 	gw_ares_freeaddrinfo(result);
 }
 
@@ -390,18 +416,16 @@ int main(void)
 	gw_ares_getaddrinfo(channel, "facebook.com", "80", &hints, NULL);
 	gw_ares_getaddrinfo(channel, "github.com", "80", &hints, NULL);
 
-	while (1) {
+	while (true) {
 		if (!channel->req_nr)
 			break;
-		ret = __sys_epoll_wait(epfd, &ev, 1, -1);
+
+		ret = __sys_epoll_wait(epfd, &ev, 1024, -1);
 		if (ret < 0)
 			return -EXIT_FAILURE;
 
-		if (ev.events & EPOLLIN) {
-			ret = gw_ares_process_event(channel);
-			if (ret)
-				printf("gw_ares_process_event return %d\n", ret);
-		}
+		if (ev.events & EPOLLIN)
+			gw_ares_process_event(channel);
 	}
 
 	gw_ares_deinit(channel);
diff --git a/src/gwproxy/dnslookup.h b/src/gwproxy/dnslookup.h
index 6c7db15b7d6f..a98ed6cff23c 100644
--- a/src/gwproxy/dnslookup.h
+++ b/src/gwproxy/dnslookup.h
@@ -139,6 +139,7 @@ void gw_ares_freeaddrinfo(struct gw_ares_addrinfo *ai);
  * @return zero on success and negative integer on error
  */
 int gw_ares_init(gw_ares_channel_t **channel, struct gw_ares_options *opts);
+
 /*
  * Free the resources allocated by gw_ares_init.
  * 
diff --git a/src/gwproxy/dnsparser.c b/src/gwproxy/dnsparser.c
index 2d8af1da2893..88564504bbff 100644
--- a/src/gwproxy/dnsparser.c
+++ b/src/gwproxy/dnsparser.c
@@ -122,7 +122,8 @@ int serialize_answ(uint16_t txid, uint8_t *in, size_t in_len, gwdns_answ_data *o
 	if (!out->rr_answ)
 		return -ENOMEM;
 
-	for (size_t i = 0; i < hdr->ancount; i++) {
+	size_t i;
+	for (i = 0; i < hdr->ancount; i++) {
 		uint16_t is_compressed, rdlength;
 		gwdns_serialized_answ *item = malloc(sizeof(gwdns_serialized_answ));
 		if (!item) {
@@ -211,7 +212,8 @@ int serialize_answ(uint16_t txid, uint8_t *in, size_t in_len, gwdns_answ_data *o
 
 	return 0;
 exit_free:
-	for (size_t i = 0; i < out->hdr.ancount; i++) {
+	size_t i;
+	for (i = 0; i < out->hdr.ancount; i++) {
 		free(out->rr_answ[i]->rdata);
 		free(out->rr_answ[i]);
 	}
@@ -221,7 +223,8 @@ exit_free:
 
 void free_serialize_answ(gwdns_answ_data *answ)
 {
-	for (size_t i = 0; i < answ->hdr.ancount; i++) {
+	size_t i;
+	for (i = 0; i < answ->hdr.ancount; i++) {
 		free(answ->rr_answ[i]->rdata);
 		free(answ->rr_answ[i]);
 	}
@@ -300,7 +303,8 @@ void test_parse_ipv4(void)
 
 	memcpy(&txid, recv_pkt, 2);
 	assert(!serialize_answ(txid, recv_pkt, sizeof(recv_pkt), &d));
-	for (size_t i = 0; i < d.hdr.ancount; i++) {
+	size_t i;
+	for (i = 0; i < d.hdr.ancount; i++) {
 		struct gwp_sockaddr gs;
 		gwdns_serialized_answ *answ;
 		char buff[FULL_ADDRSTRLEN];
@@ -367,7 +371,8 @@ void test_parse_ipv6(void)
 
 	ret = serialize_answ(txid, recv_pkt, sizeof(recv_pkt), &d);
 	assert(!ret);
-	for (size_t i = 0; i < d.hdr.ancount; i++) {
+	size_t i;
+	for (i = 0; i < d.hdr.ancount; i++) {
 		struct gwp_sockaddr gs;
 		gwdns_serialized_answ *answ;
 		char buff[FULL_ADDRSTRLEN];
-- 
Ahmad Gani


  parent reply	other threads:[~2025-08-05  6:50 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-05  6:49 [PATCH gwproxy v3 0/9] Initial work for DNS lookup implementation Ahmad Gani
2025-08-05  6:49 ` [PATCH gwproxy v3 1/9] dnslookup: Split common functionality and struct into net.h and net.c Ahmad Gani
2025-08-05  9:26   ` Ammar Faizi
2025-08-05  6:49 ` [PATCH gwproxy v3 2/9] dnslookup: Add a new parameter, default_port Ahmad Gani
2025-08-05  9:26   ` Ammar Faizi
2025-08-05 10:15     ` reyuki
2025-08-05 10:26       ` Ammar Faizi
2025-08-05 10:43         ` Ahmad Gani
2025-08-05 10:46           ` Ammar Faizi
2025-08-05 12:45             ` Ahmad Gani
2025-08-05  6:49 ` [PATCH gwproxy v3 3/9] dnslookup: Allow only port string number Ahmad Gani
2025-08-05  6:49 ` [PATCH gwproxy v3 4/9] dnslookup: Initial work for implementation of C-ares-like getaddrinfo function Ahmad Gani
2025-08-05  6:49 ` [PATCH gwproxy v3 5/9] dnsparser: Update unit test of dns parser Ahmad Gani
2025-08-05  9:27   ` Ammar Faizi
2025-08-05  6:49 ` [PATCH gwproxy v3 6/9] dnsparser: Fix serialize_answ function Ahmad Gani
2025-08-05  9:26   ` Ammar Faizi
2025-08-05 12:47     ` Ahmad Gani
2025-08-05 13:04       ` Ammar Faizi
2025-08-05 13:12         ` Ahmad Gani
2025-08-05 13:51         ` Ahmad Gani
2025-08-05 14:02           ` Ammar Faizi
2025-08-05  6:49 ` [PATCH gwproxy v3 7/9] dnsparser: Transaction id creation is delegated to caller Ahmad Gani
2025-08-05  6:49 ` [PATCH gwproxy v3 8/9] dnslookup: Make gw_ares_getaddrinfo asynchronous Ahmad Gani
2025-08-05  6:49 ` Ahmad Gani [this message]
2025-08-05  9:26   ` [PATCH gwproxy v3 9/9] dnslookup: code style changes Ammar Faizi
2025-08-05 13:22 ` [PATCH gwproxy v3 0/9] Initial work for DNS lookup implementation Ammar Faizi
2025-08-05 13:28   ` Ahmad Gani

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250805064933.109080-10-reyuki@gnuweeb.org \
    --to=reyuki@gnuweeb.org \
    --cc=alviro.iskandar@gnuweeb.org \
    --cc=ammarfaizi2@gnuweeb.org \
    --cc=gwml@vger.gnuweeb.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox