Browse Source

use struct interface internally instead of struct uloop_fd

Signed-off-by: Felix Fietkau <nbd@openwrt.org>
Felix Fietkau 10 years ago
parent
commit
70ec71015a
9 changed files with 55 additions and 57 deletions
  1. 2 4
      announce.c
  2. 1 2
      announce.h
  3. 6 6
      cache.c
  4. 1 1
      cache.h
  5. 2 2
      dns.c
  6. 1 1
      dns.h
  7. 14 13
      main.c
  8. 24 24
      service.c
  9. 4 4
      service.h

+ 2 - 4
announce.c

@@ -36,7 +36,6 @@ enum {
 };
 
 static struct uloop_timeout announce;
-struct uloop_fd *announce_fd;
 static int announce_state;
 int announce_ttl = 75 * 60;
 
@@ -69,17 +68,16 @@ announce_timer(struct uloop_timeout *timeout)
 			announce_state++;
 
 		case STATE_ANNOUNCE:
-			service_announce(announce_fd);
+			service_announce(cur_iface);
 			uloop_timeout_set(timeout, announce_ttl * 800);
 			break;
 	}
 }
 
 void
-announce_init(struct uloop_fd *u)
+announce_init(void)
 {
 	announce_state = STATE_PROBE1;
 	announce.cb = announce_timer;
-	announce_fd = u;
 	uloop_timeout_set(&announce, 100);
 }

+ 1 - 2
announce.h

@@ -15,7 +15,6 @@
 #define _ANNOUNCE_H__
 
 extern int announce_ttl;
-extern struct uloop_fd *announce_fd;
-extern void announce_init(struct uloop_fd *u);
+extern void announce_init(void);
 
 #endif

+ 6 - 6
cache.c

@@ -123,7 +123,7 @@ cache_scan(void)
 }
 
 static struct cache_entry*
-cache_entry(struct uloop_fd *u, char *entry, int hlen, int ttl)
+cache_entry(struct interface *iface, char *entry, int hlen, int ttl)
 {
 	struct cache_entry *s;
 	char *entry_buf;
@@ -153,7 +153,7 @@ cache_entry(struct uloop_fd *u, char *entry, int hlen, int ttl)
 	avl_insert(&entries, &s->avl);
 
 	if (!hlen)
-		dns_send_question(cur_iface, entry, TYPE_PTR);
+		dns_send_question(iface, entry, TYPE_PTR);
 
 	return s;
 }
@@ -215,7 +215,7 @@ cache_host_is_known(char *record)
 }
 
 void
-cache_answer(struct uloop_fd *u, uint8_t *base, int blen, char *name, struct dns_answer *a, uint8_t *rdata)
+cache_answer(struct interface *iface, uint8_t *base, int blen, char *name, struct dns_answer *a, uint8_t *rdata)
 {
 	struct dns_srv_data *dsd = (struct dns_srv_data *) rdata;
 	struct cache_record *r;
@@ -248,7 +248,7 @@ cache_answer(struct uloop_fd *u, uint8_t *base, int blen, char *name, struct dns
 		    nlen + 1 < rdlength && !strcmp(rdata_buffer + rdlength - nlen, name))
 			host_len = rdlength - nlen - 1;
 
-		cache_entry(u, rdata_buffer, host_len, a->ttl);
+		cache_entry(iface, rdata_buffer, host_len, a->ttl);
 		return;
 
 	case TYPE_SRV:
@@ -278,14 +278,14 @@ cache_answer(struct uloop_fd *u, uint8_t *base, int blen, char *name, struct dns
 		break;
 
 	case TYPE_A:
-		cache_entry(u, name, strlen(name), a->ttl);
+		cache_entry(iface, name, strlen(name), a->ttl);
 		if (a->rdlength != 4)
 			return;
 		dlen = 4;
 		break;
 
 	case TYPE_AAAA:
-		cache_entry(u, name, strlen(name), a->ttl);
+		cache_entry(iface, name, strlen(name), a->ttl);
 		if (a->rdlength != 16)
 			return;
 		dlen = 16;

+ 1 - 1
cache.h

@@ -46,7 +46,7 @@ extern struct avl_tree records, entries;
 extern int cache_init(void);
 extern void cache_scan(void);
 extern void cache_cleanup(void);
-extern void cache_answer(struct uloop_fd *u, uint8_t *base, int blen,
+extern void cache_answer(struct interface *iface, uint8_t *base, int blen,
 		char *name, struct dns_answer *a, uint8_t *rdata);
 extern int cache_host_is_known(char *record);
 extern char* cache_lookup_name(const char *key);

+ 2 - 2
dns.c

@@ -136,7 +136,7 @@ dns_add_answer(int type, const uint8_t *rdata, uint16_t rdlength)
 }
 
 void
-dns_send_answer(struct uloop_fd *u, const char *answer)
+dns_send_answer(struct interface *iface, const char *answer)
 {
 	uint8_t buffer[256];
 	struct dns_header h = { 0 };
@@ -179,7 +179,7 @@ dns_send_answer(struct uloop_fd *u, const char *answer)
 		DBG(1, "A <- %s %s\n", dns_type_string(dns_reply[i].type), answer);
 	}
 
-	if (interface_send_packet(cur_iface, iov, (dns_answer_cnt * 3) + 1) < 0)
+	if (interface_send_packet(iface, iov, (dns_answer_cnt * 3) + 1) < 0)
 		fprintf(stderr, "failed to send question\n");
 
 	for (i = 0; i < dns_answer_cnt; i++) {

+ 1 - 1
dns.h

@@ -71,7 +71,7 @@ extern char rdata_buffer[MAX_DATA_LEN + 1];
 extern void dns_send_question(struct interface *iface, const char *question, int type);
 extern void dns_init_answer(void);
 extern void dns_add_answer(int type, const uint8_t *rdata, uint16_t rdlength);
-extern void dns_send_answer(struct uloop_fd *u, const char *answer);
+extern void dns_send_answer(struct interface *iface, const char *answer);
 extern char* dns_consume_name(const uint8_t *base, int blen, uint8_t **data, int *len);
 extern struct dns_answer* dns_consume_answer(uint8_t **data, int *len);
 extern struct dns_question* dns_consume_question(uint8_t **data, int *len);

+ 14 - 13
main.c

@@ -44,7 +44,7 @@ static struct uloop_timeout reconnect;
 char *iface_name = "eth0";
 
 static int
-parse_answer(struct uloop_fd *u, uint8_t *buffer, int len, uint8_t **b, int *rlen, int cache)
+parse_answer(struct interface *iface, uint8_t *buffer, int len, uint8_t **b, int *rlen, int cache)
 {
 	char *name = dns_consume_name(buffer, len, b, rlen);
 	struct dns_answer *a;
@@ -71,13 +71,13 @@ parse_answer(struct uloop_fd *u, uint8_t *buffer, int len, uint8_t **b, int *rle
 	*b += a->rdlength;
 
 	if (cache)
-		cache_answer(u, buffer, len, name, a, rdata);
+		cache_answer(iface, buffer, len, name, a, rdata);
 
 	return 0;
 }
 
 static void
-parse_question(struct uloop_fd *u, char *name, struct dns_question *q)
+parse_question(struct interface *iface, char *name, struct dns_question *q)
 {
 	char *host;
 
@@ -87,12 +87,12 @@ parse_question(struct uloop_fd *u, char *name, struct dns_question *q)
 	case TYPE_ANY:
 		host = service_name("local");
 		if (!strcmp(name, host))
-			service_reply(u, NULL);
+			service_reply(iface, NULL);
 		break;
 
 	case TYPE_PTR:
-		service_announce_services(u, name);
-		service_reply(u, name);
+		service_announce_services(iface, name);
+		service_reply(iface, name);
 		break;
 
 	case TYPE_AAAA:
@@ -101,7 +101,7 @@ parse_question(struct uloop_fd *u, char *name, struct dns_question *q)
 		if (host)
 			*host = '\0';
 		if (!strcmp(hostname, name))
-			service_reply_a(u, q->type);
+			service_reply_a(iface, q->type);
 		break;
 	};
 }
@@ -109,7 +109,8 @@ parse_question(struct uloop_fd *u, char *name, struct dns_question *q)
 static void
 read_socket(struct uloop_fd *u, unsigned int events)
 {
-	uint8_t buffer[8 * 1024];
+	struct interface *iface = container_of(u, struct interface, fd);
+	static uint8_t buffer[8 * 1024];
 	uint8_t *b = buffer;
 	struct dns_header *h;
 	int len, rlen;
@@ -150,20 +151,20 @@ read_socket(struct uloop_fd *u, unsigned int events)
 		}
 
 		if (!(h->flags & FLAG_RESPONSE))
-			parse_question(announce_fd, name, q);
+			parse_question(iface, name, q);
 	}
 
 	if (!(h->flags & FLAG_RESPONSE))
 		return;
 
 	while (h->answers-- > 0)
-		parse_answer(u, buffer, len, &b, &rlen, 1);
+		parse_answer(iface, buffer, len, &b, &rlen, 1);
 
 	while (h->authority-- > 0)
-		parse_answer(u, buffer, len, &b, &rlen, 0);
+		parse_answer(iface, buffer, len, &b, &rlen, 0);
 
 	while (h->additional-- > 0)
-		parse_answer(u, buffer, len, &b, &rlen, 1);
+		parse_answer(iface, buffer, len, &b, &rlen, 1);
 }
 
 static void
@@ -183,7 +184,7 @@ reconnect_socket(struct uloop_timeout *timeout)
 		uloop_fd_add(&cur_iface->fd, ULOOP_READ);
 		sleep(5);
 		dns_send_question(cur_iface, "_services._dns-sd._udp.local", TYPE_PTR);
-		announce_init(&cur_iface->fd);
+		announce_init();
 	}
 }
 

+ 24 - 24
service.c

@@ -86,7 +86,7 @@ service_name(const char *domain)
 }
 
 static void
-service_send_ptr(struct uloop_fd *u, struct service *s)
+service_add_ptr(struct service *s)
 {
 	unsigned char buffer[MAX_NAME_LEN];
 	const char *host = service_name(s->service);
@@ -99,7 +99,7 @@ service_send_ptr(struct uloop_fd *u, struct service *s)
 }
 
 static void
-service_send_ptr_c(struct uloop_fd *u, const char *host)
+service_add_ptr_c(const char *host)
 {
 	unsigned char buffer[MAX_NAME_LEN];
 	int len = dn_comp(host, buffer, MAX_NAME_LEN, NULL, NULL);
@@ -111,15 +111,15 @@ service_send_ptr_c(struct uloop_fd *u, const char *host)
 }
 
 static void
-service_send_a(struct uloop_fd *u)
+service_send_a(struct interface *iface)
 {
 	unsigned char buffer[MAX_NAME_LEN];
 	char *host = service_name("local");
 	int len = dn_comp(host, buffer, MAX_NAME_LEN, NULL, NULL);
 	struct in_addr in;
 
-	if (!inet_aton(cur_iface->ip, &in)) {
-		fprintf(stderr, "%s is not valid\n", cur_iface->ip);
+	if (!inet_aton(iface->ip, &in)) {
+		fprintf(stderr, "%s is not valid\n", iface->ip);
 		return;
 	}
 
@@ -130,7 +130,7 @@ service_send_a(struct uloop_fd *u)
 }
 
 static void
-service_send_srv(struct uloop_fd *u, struct service *s)
+service_add_srv(struct service *s)
 {
 	unsigned char buffer[MAX_NAME_LEN];
 	struct dns_srv_data *sd;
@@ -167,18 +167,18 @@ service_timeout(struct service *s)
 }
 
 void
-service_reply_a(struct uloop_fd *u, int type)
+service_reply_a(struct interface *iface, int type)
 {
 	if (type != TYPE_A)
 		return;
 
 	dns_init_answer();
-	service_send_a(u);
-	dns_send_answer(u, service_name("local"));
+	service_send_a(iface);
+	dns_send_answer(iface, service_name("local"));
 }
 
 void
-service_reply(struct uloop_fd *u, const char *match)
+service_reply(struct interface *iface, const char *match)
 {
 	struct service *s;
 
@@ -195,26 +195,26 @@ service_reply(struct uloop_fd *u, const char *match)
 			continue;
 
 		dns_init_answer();
-		service_send_ptr(u, s);
-		dns_send_answer(u, service);
+		service_add_ptr(s);
+		dns_send_answer(iface, service);
 
 		dns_init_answer();
-		service_send_srv(u, s);
+		service_add_srv(s);
 		if (s->txt && s->txt_len)
 			dns_add_answer(TYPE_TXT, (uint8_t *) s->txt, s->txt_len);
-		dns_send_answer(u, host);
+		dns_send_answer(iface, host);
 	}
 
 	if (match)
 		return;
 
 	dns_init_answer();
-	service_send_a(u);
-	dns_send_answer(u, service_name("local"));
+	service_send_a(iface);
+	dns_send_answer(iface, service_name("local"));
 }
 
 void
-service_announce_services(struct uloop_fd *u, const char *service)
+service_announce_services(struct interface *iface, const char *service)
 {
 	struct service *s;
 	int tcp = 1;
@@ -231,20 +231,20 @@ service_announce_services(struct uloop_fd *u, const char *service)
 			continue;
 		s->t = 0;
 		dns_init_answer();
-		service_send_ptr_c(u, s->service);
+		service_add_ptr_c(s->service);
 		if (tcp)
-			dns_send_answer(u, sdtcp);
+			dns_send_answer(iface, sdtcp);
 		else
-			dns_send_answer(u, sdudp);
-		service_reply(u, s->service);
+			dns_send_answer(iface, sdudp);
+		service_reply(iface, s->service);
 	}
 }
 
 void
-service_announce(struct uloop_fd *u)
+service_announce(struct interface *iface)
 {
-	service_announce_services(u, sdudp);
-	service_announce_services(u, sdtcp);
+	service_announce_services(iface, sdudp);
+	service_announce_services(iface, sdtcp);
 }
 
 static void

+ 4 - 4
service.h

@@ -18,9 +18,9 @@ extern char *hostname;
 extern char *service_name(const char *domain);
 extern void service_init(void);
 extern void service_cleanup(void);
-extern void service_announce(struct uloop_fd *u);
-extern void service_announce_services(struct uloop_fd *u, const char *service);
-extern void service_reply(struct uloop_fd *u, const char *match);
-extern void service_reply_a(struct uloop_fd *u, int type);
+extern void service_announce(struct interface *iface);
+extern void service_announce_services(struct interface *iface, const char *service);
+extern void service_reply(struct interface *iface, const char *match);
+extern void service_reply_a(struct interface *iface, int type);
 
 #endif