From 70ec71015a42e6e4e04b0281b80ec80e5f5f959a Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Mon, 9 Jun 2014 21:09:33 +0200 Subject: [PATCH] use struct interface internally instead of struct uloop_fd Signed-off-by: Felix Fietkau --- announce.c | 6 ++---- announce.h | 3 +-- cache.c | 12 ++++++------ cache.h | 2 +- dns.c | 4 ++-- dns.h | 2 +- main.c | 27 ++++++++++++++------------- service.c | 48 ++++++++++++++++++++++++------------------------ service.h | 8 ++++---- 9 files changed, 55 insertions(+), 57 deletions(-) diff --git a/announce.c b/announce.c index 7b52794..4971c4e 100644 --- a/announce.c +++ b/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); } diff --git a/announce.h b/announce.h index 50ba631..efd06ff 100644 --- a/announce.h +++ b/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 diff --git a/cache.c b/cache.c index 4182016..123925a 100644 --- a/cache.c +++ b/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; diff --git a/cache.h b/cache.h index 4e8a421..0dfacd4 100644 --- a/cache.h +++ b/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); diff --git a/dns.c b/dns.c index a646024..b37f82e 100644 --- a/dns.c +++ b/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++) { diff --git a/dns.h b/dns.h index b39608e..6edf1de 100644 --- a/dns.h +++ b/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); diff --git a/main.c b/main.c index 6624d36..baadda1 100644 --- a/main.c +++ b/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(); } } diff --git a/service.c b/service.c index cf1d336..4a9ccc8 100644 --- a/service.c +++ b/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 diff --git a/service.h b/service.h index 7dd0ff1..6aeea0f 100644 --- a/service.h +++ b/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 -- 2.11.0