[cvslog] (2004-12-09 06:43:42 UTC) Module eggdrop1.9: Change committed!

cvslog cvs at tsss.org
Thu Dec 9 00:43:42 CST 2004


CVSROOT    : /usr/local/cvsroot
Module     : eggdrop1.9
Commit time: 2004-12-09 06:43:42 UTC
Commited by: Bryan Drewery <lordares at shatow.net>

Modified files:
     lib/eggdrop/dns.c lib/eggdrop/timer.c lib/eggdrop/timer.h

Log message:

* Added timer_create_secs(int secs, char *name, Function callback)
  -Is just an easy way to create a timer based on seconds instead of declaring howlong
   and calling timer_create_complex.
* dns now caches answers in hosts[] and expires them according go ttl
  -No max cache settings yet or non-cache option, will add more functionality later...

  - Please respond if caching dns answers is a problem -

---------------------- diff included ----------------------
Index: eggdrop1.9/lib/eggdrop/dns.c
diff -u eggdrop1.9/lib/eggdrop/dns.c:1.7 eggdrop1.9/lib/eggdrop/dns.c:1.8
--- eggdrop1.9/lib/eggdrop/dns.c:1.7	Wed Dec  8 22:53:30 2004
+++ eggdrop1.9/lib/eggdrop/dns.c	Thu Dec  9 00:43:32 2004
@@ -18,7 +18,7 @@
  */
 
 #ifndef lint
-static const char rcsid[] = "$Id: dns.c,v 1.7 2004/12/09 04:53:30 lordares Exp $";
+static const char rcsid[] = "$Id: dns.c,v 1.8 2004/12/09 06:43:32 lordares Exp $";
 #endif
 
 #include <eggdrop/eggdrop.h>
@@ -67,7 +67,9 @@
 
 /* Entries from hosts */
 typedef struct {
-	char *host, *ip;
+	char *host;
+	char *ip;
+	time_t expiretime;
 } dns_host_t;
 
 static int query_id = 1;
@@ -84,6 +86,7 @@
 static int dns_idx = -1;
 static const char *dns_ip = NULL;
 
+static void del_host(int id);
 static int make_header(char *buf, int id);
 static int cut_host(const char *host, char *query);
 static int reverse_ip(const char *host, char *reverse);
@@ -296,6 +299,21 @@
 }
 
 
+void expire_queries()
+{
+	int i = 0, now = 0;
+
+	now = timer_get_now_sec(NULL);
+
+	for (i = 0; i < nhosts; i++) {
+		if (hosts[i].expiretime && (now >= hosts[i].expiretime)) {
+			del_host(i);
+			i--;
+		}
+	}
+}
+
+
 /* Read in .hosts and /etc/hosts and .resolv.conf and /etc/resolv.conf */
 int egg_dns_init()
 {
@@ -304,6 +322,7 @@
 	read_resolv(".resolv.conf");
 	read_hosts("/etc/hosts");
 	read_hosts(".hosts");
+	timer_create_secs(1, "dns_check_expires", (Function) expire_queries);
 	return(0);
 }
 
@@ -346,14 +365,32 @@
 	nservers++;
 }
 
-static void add_host(char *host, char *ip)
+static void add_host(char *host, char *ip, int ttl)
 {
 	hosts = realloc(hosts, (nhosts+1)*sizeof(*hosts));
 	hosts[nhosts].host = strdup(host);
 	hosts[nhosts].ip = strdup(ip);
+	hosts[nhosts].expiretime = timer_get_now_sec(NULL) + ttl;
 	nhosts++;
 }
 
+static void del_host(int id)
+{
+  free(hosts[id].host);
+  free(hosts[id].ip);
+  hosts[id].expiretime = 0;
+
+  nhosts--;
+
+  if (id < nhosts)
+    memcpy(&hosts[id], &hosts[nhosts], sizeof(dns_host_t));
+  else
+    memset(&hosts[id], 0, sizeof(dns_host_t));
+
+  hosts = (dns_host_t *) realloc(hosts, (nhosts+1)*sizeof(*hosts));
+}
+
+
 static int read_thing(char *buf, char *ip)
 {
 	int skip, len;
@@ -396,7 +433,7 @@
 		if (!strlen(ip)) continue;
 		while ((n = read_thing(buf+skip, host))) {
 			skip += n;
-			if (strlen(host)) add_host(host, ip);
+			if (strlen(host)) add_host(host, ip, 0);
 		}
 	}
 	fclose(fp);
@@ -536,11 +573,13 @@
 			/*fprintf(fp, "ipv4 reply\n");*/
 			inet_ntop(AF_INET, ptr, result, 512);
 			answer_add(&q->answer, result);
+			add_host(q->query, result, reply.ttl);
 		}
 		else if (reply.type == 28) {
 			/*fprintf(fp, "ipv6 reply\n");*/
 			inet_ntop(AF_INET6, ptr, result, 512);
 			answer_add(&q->answer, result);
+			add_host(q->query, result, reply.ttl);
 			return;
 		}
 		else if (reply.type == 12) {
@@ -567,6 +606,7 @@
 			if (strlen(result)) {
 				result[strlen(result)-1] = 0;
 				answer_add(&q->answer, result);
+				add_host(result, q->query, reply.ttl);
 			}
 			ptr = placeholder;
 		}
Index: eggdrop1.9/lib/eggdrop/timer.c
diff -u eggdrop1.9/lib/eggdrop/timer.c:1.7 eggdrop1.9/lib/eggdrop/timer.c:1.8
--- eggdrop1.9/lib/eggdrop/timer.c:1.7	Sun Oct 17 00:14:06 2004
+++ eggdrop1.9/lib/eggdrop/timer.c	Thu Dec  9 00:43:32 2004
@@ -18,7 +18,7 @@
  */
 
 #ifndef lint
-static const char rcsid[] = "$Id: timer.c,v 1.7 2004/10/17 05:14:06 stdarg Exp $";
+static const char rcsid[] = "$Id: timer.c,v 1.8 2004/12/09 06:43:32 lordares Exp $";
 #endif
 
 #include <eggdrop/eggdrop.h>
@@ -135,6 +135,16 @@
 	return(0);
 }
 
+int timer_create_secs(int secs, const char *name, Function callback)
+{
+        egg_timeval_t howlong;
+
+        howlong.sec = secs;
+        howlong.usec = 0;
+
+        return timer_create_repeater(&howlong, name, callback);
+}
+
 int timer_create_complex(egg_timeval_t *howlong, const char *name, Function callback, void *client_data, int flags)
 {
 	egg_timer_t *timer;
Index: eggdrop1.9/lib/eggdrop/timer.h
diff -u eggdrop1.9/lib/eggdrop/timer.h:1.2 eggdrop1.9/lib/eggdrop/timer.h:1.3
--- eggdrop1.9/lib/eggdrop/timer.h:1.2	Wed Oct  6 09:59:09 2004
+++ eggdrop1.9/lib/eggdrop/timer.h	Thu Dec  9 00:43:32 2004
@@ -16,7 +16,7 @@
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  *
- * $Id: timer.h,v 1.2 2004/10/06 14:59:09 stdarg Exp $
+ * $Id: timer.h,v 1.3 2004/12/09 06:43:32 lordares Exp $
  */
 
 #ifndef _EGG_TIMER_H_
@@ -53,6 +53,7 @@
 extern int timer_get_now_sec(int *sec);
 extern int timer_update_now(egg_timeval_t *_now);
 extern int timer_diff(egg_timeval_t *from_time, egg_timeval_t *to_time, egg_timeval_t *diff);
+extern int timer_create_secs(int secs, const char *name, Function callback);
 extern int timer_create_complex(egg_timeval_t *howlong, const char *name, Function callback, void *client_data, int flags);
 extern int timer_destroy(int timer_id);
 extern int timer_destroy_all();
----------------------- End of diff -----------------------



More information about the Changes mailing list