From 0aed72b1e5c960989c4ef076b138793a123783eb Mon Sep 17 00:00:00 2001
From: rswindell <>
Date: Fri, 7 Oct 2005 06:23:15 +0000
Subject: [PATCH] New xpdev module: netwrap, for network (but not socket)
 related wrappers: currently contains get/freeNameServerList().

---
 src/xpdev/netwrap.c | 119 ++++++++++++++++++++++++++++++++++++++++++++
 src/xpdev/netwrap.h |  55 ++++++++++++++++++++
 2 files changed, 174 insertions(+)
 create mode 100644 src/xpdev/netwrap.c
 create mode 100644 src/xpdev/netwrap.h

diff --git a/src/xpdev/netwrap.c b/src/xpdev/netwrap.c
new file mode 100644
index 0000000000..703bc3a26c
--- /dev/null
+++ b/src/xpdev/netwrap.c
@@ -0,0 +1,119 @@
+/* netwrap.c */
+
+/* Network related wrapper functions */
+
+/* $Id$ */
+
+/****************************************************************************
+ * @format.tab-size 4		(Plain Text/Source Code File Header)			*
+ * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
+ *																			*
+ * Copyright 2005 Rob Swindell - http://www.synchro.net/copyright.html		*
+ *																			*
+ * This library is free software; you can redistribute it and/or			*
+ * modify it under the terms of the GNU Lesser General Public License		*
+ * as published by the Free Software Foundation; either version 2			*
+ * of the License, or (at your option) any later version.					*
+ * See the GNU Lesser General Public License for more details: lgpl.txt or	*
+ * http://www.fsf.org/copyleft/lesser.html									*
+ *																			*
+ * Anonymous FTP access to the most recent released source is available at	*
+ * ftp://vert.synchro.net, ftp://cvs.synchro.net and ftp://ftp.synchro.net	*
+ *																			*
+ * Anonymous CVS access to the development source and modification history	*
+ * is available at cvs.synchro.net:/cvsroot/sbbs, example:					*
+ * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs login			*
+ *     (just hit return, no password is necessary)							*
+ * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs checkout src		*
+ *																			*
+ * For Synchronet coding style and modification guidelines, see				*
+ * http://www.synchro.net/source.html										*
+ *																			*
+ * You are encouraged to submit any modifications (preferably in Unix diff	*
+ * format) via e-mail to mods@synchro.net									*
+ *																			*
+ * Note: If this box doesn't appear square, then you need to fix your tabs.	*
+ ****************************************************************************/
+
+#include "genwrap.h"	/* truncsp */
+#include "netwrap.h"	/* verify prototypes */
+
+#include <stdlib.h>		/* malloc() */
+
+#if defined(_WIN32)
+	#include <iphlpapi.h>	/* GetNetworkParams */
+#endif
+
+str_list_t getNameServerList(void)
+{
+#ifdef __unix__	/* Look up DNS server address */
+	FILE*	fp;
+	char*	p;
+	char	str[128];
+	str_list_t	list;
+
+	if((list=strListInit())==NULL)
+		return(NULL);
+	if((fp=fopen("/etc/resolv.conf","r"))!=NULL) {
+		while(!feof(fp)) {
+			if(fgets(str,sizeof(str),fp)==NULL)
+				break;
+			truncsp(str);
+			p=str;
+			SKIP_WHITESPACE(p);
+			if(strnicmp(p,"nameserver",10)!=0) /* no match */
+				continue;
+			FIND_WHITESPACE(p);	/* skip "nameserver" */
+			SKIP_WHITESPACE(p);	/* skip more white-space */
+			strListPush(&list,p);
+		}
+		fclose(fp);
+	}
+	return(list);
+
+#elif defined(_WIN32)
+	FIXED_INFO* FixedInfo=NULL;
+	ULONG    	FixedInfoLen=0;
+	IP_ADDR_STRING* ip;
+	str_list_t	list;
+
+	if((list=strListInit())==NULL)
+		return(NULL);
+	if(GetNetworkParams(FixedInfo,&FixedInfoLen) == ERROR_BUFFER_OVERFLOW) {
+        FixedInfo=(FIXED_INFO*)malloc(FixedInfoLen);
+		if(GetNetworkParams(FixedInfo,&FixedInfoLen) == ERROR_SUCCESS) {
+			ip=&FixedInfo->DnsServerList;
+			for(; ip!=NULL; ip=ip->Next)
+				strListPush(&list,ip->IpAddress.String);
+		}
+        if(FixedInfo!=NULL)
+            free(FixedInfo);
+    }
+	return(list);
+#else
+	#error "Need a get_nameserver() implementation for this platform"
+#endif
+}
+
+/* In case we want to DLL-export getNameServerList in the future */
+void freeNameServerList(str_list_t list)
+{
+	strListFree(&list);
+}
+
+#if NETWRAP_TEST
+int main(void)
+{
+	size_t		i;
+	str_list_t	list;
+
+	if((list=getNameServerList())!=NULL) {
+		for(i=0;list[i]!=NULL;i++)
+			printf("%s\n",list[i]);
+		freeNameServerList(list);
+	}
+
+	return 0;
+}
+#endif
+
diff --git a/src/xpdev/netwrap.h b/src/xpdev/netwrap.h
new file mode 100644
index 0000000000..c02717aff7
--- /dev/null
+++ b/src/xpdev/netwrap.h
@@ -0,0 +1,55 @@
+/* netwrap.h */
+
+/* Network related wrapper functions */
+
+/* $Id$ */
+
+/****************************************************************************
+ * @format.tab-size 4		(Plain Text/Source Code File Header)			*
+ * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
+ *																			*
+ * Copyright 2005 Rob Swindell - http://www.synchro.net/copyright.html		*
+ *																			*
+ * This library is free software; you can redistribute it and/or			*
+ * modify it under the terms of the GNU Lesser General Public License		*
+ * as published by the Free Software Foundation; either version 2			*
+ * of the License, or (at your option) any later version.					*
+ * See the GNU Lesser General Public License for more details: lgpl.txt or	*
+ * http://www.fsf.org/copyleft/lesser.html									*
+ *																			*
+ * Anonymous FTP access to the most recent released source is available at	*
+ * ftp://vert.synchro.net, ftp://cvs.synchro.net and ftp://ftp.synchro.net	*
+ *																			*
+ * Anonymous CVS access to the development source and modification history	*
+ * is available at cvs.synchro.net:/cvsroot/sbbs, example:					*
+ * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs login			*
+ *     (just hit return, no password is necessary)							*
+ * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs checkout src		*
+ *																			*
+ * For Synchronet coding style and modification guidelines, see				*
+ * http://www.synchro.net/source.html										*
+ *																			*
+ * You are encouraged to submit any modifications (preferably in Unix diff	*
+ * format) via e-mail to mods@synchro.net									*
+ *																			*
+ * Note: If this box doesn't appear square, then you need to fix your tabs.	*
+ ****************************************************************************/
+
+#ifndef _NETWRAP_H
+#define _NETWRAP_H
+
+#include <stddef.h>		/* size_t */
+#include "str_list.h"	/* string list functions and types */
+
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
+str_list_t	getNameServerList(void);
+void		freeNameServerList(str_list_t);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif	/* Don't add anything after this line */
-- 
GitLab