#include <stdio.h>
#include "lib.h"
#define DMID 26
#include "debug.h"

#ifdef WIN32
#include <windows.h>
#include <nspapi.h>
#include <svcguid.h>
#include "debug.h"
/*Win2000 - (contains value) HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\{FD89BFAA-6AA4-4FFE-A1FC-A78687268B24}\NameServer
			(is empty)       HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\NameServer

  WinNT   - (contains value) HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\NameServer

  Win95   - (contains value) HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\VxD\MSTCP

  1. Try to get Win2000 one...
  2. Try to get NT one...
  3. Try to get Win98/Win95 one...
*/

char *find_nameservers(char *buffer, int bfsz) 
{
	static char internal[BFSZ]="";
	static int found=0;

	if ( !buffer ) return NULL;
	if ( !found ) {
		DWORD type,knsize=BFSZ-1,ktsize=BFSZ-1;
		char keyname[BFSZ], keytype[BFSZ],*ptr;
		HKEY hKey,subkey;
		int index,r;

		/* Open Win2000 dns server key */
		if ( RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces",0,KEY_READ,&hKey) == ERROR_SUCCESS ) {
			for ( index = 0, knsize = ktsize = BFSZ-1; (r=RegEnumKeyEx(hKey,index,keyname,&knsize,NULL,keytype,&knsize,NULL)) == ERROR_SUCCESS; index++, knsize = ktsize = BFSZ-1 ) {
				sprintf(keytype,"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\%s",keyname);
				if ( RegOpenKeyEx(HKEY_LOCAL_MACHINE,keytype,0,KEY_QUERY_VALUE,&subkey) != ERROR_SUCCESS )
					continue;

				knsize=BFSZ-1;
				if ( RegQueryValueEx(subkey,"NameServer",0,&type,keyname,&knsize) == ERROR_SUCCESS ) {
					if ( (ptr=strstr(internal,keyname)) && (ptr == internal || *(ptr-1) == ',') && (*(ptr+lib_strlen(keyname)) == '\0' || *(ptr+lib_strlen(keyname)) == ',') ) {
						RegCloseKey(subkey);
						continue;
					}
					if ( internal[0] ) ncat(internal,",",BFSZ-1);
					ncat(internal,keyname,BFSZ-1);
				}
				RegCloseKey(subkey);
			}
			RegCloseKey(hKey);

			/* Did we fail to open one of the subkeys found in the key? */
			if ( r != ERROR_NO_MORE_ITEMS ) return NULL;

			/* Success */
			ncpy(buffer,internal,bfsz-1);
			found=1;
			return buffer;
		}

		/* Open NT dns server key */
		if ( RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters",0,KEY_QUERY_VALUE,&hKey) != ERROR_SUCCESS ) {
			/* Try Win95/Win98 key */
			if ( RegOpenKeyEx(HKEY_LOCAL_MACHINE,"System\\CurrentControlSet\\Services\\VxD\\MSTCP",0,KEY_QUERY_VALUE,&hKey) != ERROR_SUCCESS )
				return NULL;
		}

		/* Get dns server value */
		if ( RegQueryValueEx(hKey,"NameServer",0,&type,keyname,&knsize) != ERROR_SUCCESS ) {
			RegCloseKey(hKey);
			return NULL;
		}
		RegCloseKey(hKey);
		ncpy(internal,keyname,BFSZ-1);
		found=1;
	}
	ncpy(buffer,internal,bfsz-1);
	return buffer;
}
#else /*if not WIN32*/
#include <string.h>
#include "debug.h"
char *find_nameservers(char *buffer, int bfsz) {
	static char internal[BFSZ]="";
	static int found=0;

	if ( !buffer ) return NULL;
	if ( !found ) {
		char bf[BFSZ],*ptr,*str;
		FILE *fp;

		fp = fopen("/etc/resolv.conf","r");
		if ( !fp ) return NULL;

		while ( !feof(fp) ) {
			if ( !fgets(bf,BFSZ,fp) ) break;

			if ( strncmp(bf, "nameserver", 10) != 0 ) continue;
			if (bf[10] == 0) continue;
			strip_eol(bf);
			for ( str = bf+11; *str == ' ' || *str == '\t'; str++);
			if ( (ptr=strstr(internal,str)) && (ptr == internal || *(ptr-1) == ',') && (*(ptr+lib_strlen(str)) == '\0' || *(ptr+lib_strlen(str)) == ',') )
				continue;
			if ( internal[0] ) ncat(internal,",",BFSZ-1);
			ncat(internal,str,BFSZ-1);
		}
		fclose(fp);
	}
	ncpy(buffer,internal,bfsz-1);
	return buffer;
}
#endif /*WIN32*/
