2 docCopyright("Steve Dekorte", 2002)
3 docLicense("BSD revised")
4 docDescription("Windows code by Mike Austin.")
5 docCategory("Networking")
8 #include "LocalNameServers.h"
11 void LocalNameServers_findIps(LocalNameServers
*self
);
13 LocalNameServers
*LocalNameServers_new(void)
15 LocalNameServers
*self
= (LocalNameServers
*)calloc(1, sizeof(LocalNameServers
));
19 void LocalNameServers_free(LocalNameServers
*self
)
23 List_do_(self
->ips
, (ListDoCallback
*)free
);
30 List
*LocalNameServers_ips(LocalNameServers
*self
)
34 self
->ips
= List_new();
35 LocalNameServers_findIps(self
);
41 void LocalNameServers_addIPAddress_(LocalNameServers
*self
, const char *s
)
43 char *newIPAddress
= strcpy(malloc(strlen(s
) + 1), s
);
44 List_append_(self
->ips
, newIPAddress
);
47 // --- generic -----------------------------
54 static char *stringDeleteHashComment(char *s
)
56 char *commentBeginning
= strchr(s
, '#');
60 commentBeginning
= s
+ strlen(s
);
63 while(!isdigit(*commentBeginning
))
65 *commentBeginning
-- = '\0';
71 static char *lastWhiteSpaceInString(char *s
)
73 char *lastSpace
= strrchr(s
, ' ');
74 char *lastTab
= strrchr(s
, '\t');
75 char *lastWhiteSpace
= lastSpace
> lastTab
? lastSpace
: lastTab
;
76 return lastWhiteSpace
;
79 static char *local_strdup(char *s
) // because OSXs is buggy
81 return strcpy(malloc(strlen(s
)+1), s
);
84 void LocalNameServers_findIpsViaResolveConf(LocalNameServers
*self
)
86 FILE *fp
= fopen("/etc/resolv.conf", "r");
90 UArray
*ba
= UArray_new();
92 while (UArray_readLineFromCStream_(ba
, fp
))
94 char *line
= (char *)UArray_bytes(ba
);
96 /*printf("line = %s\n", line);*/
97 if (strstr(line
, "nameserver") == line
)
100 char *s
= local_strdup(line
);
102 stringDeleteHashComment(s
);
103 ip
= lastWhiteSpaceInString(s
) + 1;
107 //printf("LocalNameServers_findIps() found ip '%s'\n", ip);
108 LocalNameServers_addIPAddress_(self
, ip
);
113 UArray_setSize_(ba
, 0);
120 #if defined(WIN32) || defined(__CYGWIN__)
122 // Windows -----------------------------------------
124 #include <winsock2.h>
125 #include <iphlpapi.h>
127 void LocalNameServers_findIps(LocalNameServers
*self
)
130 ULONG len
= sizeof(FIXED_INFO
) * 16;
132 if (GetNetworkParams(info
, &len
) != ERROR_SUCCESS
)
134 printf("LocalNameServers error: GetNetworkParams() failed");
141 IP_ADDR_STRING
*addr
= &info
->DnsServerList
;
145 LocalNameServers_addIPAddress_(self
, addr
->IpAddress
.String
);
146 /*printf("%s\n", addr->IpIPAddress.String);*/
152 #elif defined(__APPLE__)
154 // OSX ----------------------------------------
156 void LocalNameServers_findIps(LocalNameServers
*self
)
158 char *path
= tmpnam(NULL
);
159 char *command
= malloc(1024);
160 char *answerBuffer
= calloc(1, 1024);
161 char *answer
= answerBuffer
;
164 sprintf(command
, "dig | grep SERVER: > %s", path
);
168 fp
= fopen(path
, "r");
169 fread(answer
, 1, 1024, fp
);
173 if (strlen(answer
) < strlen(";; SERVER: "))
175 //printf("LocalNameServers warning: unable to find nameservers using 'dig | grep SERVER:'\nParsing resolve.conf instead.");
176 LocalNameServers_findIpsViaResolveConf(self
);
180 answer
= answer
+ strlen(";; SERVER: ");
183 char *s
= strstr(answer
, "#");
187 LocalNameServers_addIPAddress_(self
, answer
);
196 // Unix ----------------------------------------
199 void LocalNameServers_findIps(LocalNameServers
*self
)
201 LocalNameServers_findIpsViaResolveConf(self
);