Removed all code that uses OpenGL from Image.
[io/quag.git] / addons / Socket / source / LocalNameServers.c
blobd09fab28b60a9eebd16855b81f512f02bc98218a
1 /*
2 docCopyright("Steve Dekorte", 2002)
3 docLicense("BSD revised")
4 docDescription("Windows code by Mike Austin.")
5 docCategory("Networking")
6 */
8 #include "LocalNameServers.h"
9 #include <ctype.h>
11 void LocalNameServers_findIps(LocalNameServers *self);
13 LocalNameServers *LocalNameServers_new(void)
15 LocalNameServers *self = (LocalNameServers *)calloc(1, sizeof(LocalNameServers));
16 return self;
19 void LocalNameServers_free(LocalNameServers *self)
21 if (self->ips)
23 List_do_(self->ips, (ListDoCallback *)free);
26 List_free(self->ips);
27 free(self);
30 List *LocalNameServers_ips(LocalNameServers *self)
32 if (!self->ips)
34 self->ips = List_new();
35 LocalNameServers_findIps(self);
38 return self->ips;
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 -----------------------------
49 #include <string.h>
50 #include <stdlib.h>
51 #include <stdio.h>
52 #include "UArray.h"
54 static char *stringDeleteHashComment(char *s)
56 char *commentBeginning = strchr(s, '#');
58 if(!commentBeginning)
60 commentBeginning = s + strlen(s);
63 while(!isdigit(*commentBeginning))
65 *commentBeginning-- = '\0';
68 return s;
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");
88 if (fp)
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)
99 char *ip;
100 char *s = local_strdup(line);
102 stringDeleteHashComment(s);
103 ip = lastWhiteSpaceInString(s) + 1;
105 if (*ip)
107 //printf("LocalNameServers_findIps() found ip '%s'\n", ip);
108 LocalNameServers_addIPAddress_(self, ip);
111 free(s);
113 UArray_setSize_(ba, 0);
115 UArray_free(ba);
120 #if defined(WIN32) || defined(__CYGWIN__)
122 // Windows -----------------------------------------
124 #include <winsock2.h>
125 #include <iphlpapi.h>
127 void LocalNameServers_findIps(LocalNameServers *self)
129 FIXED_INFO info[16];
130 ULONG len = sizeof(FIXED_INFO) * 16;
132 if (GetNetworkParams(info, &len) != ERROR_SUCCESS)
134 printf("LocalNameServers error: GetNetworkParams() failed");
135 /*exit(1);*/
136 return;
141 IP_ADDR_STRING *addr = &info->DnsServerList;
143 while (addr)
145 LocalNameServers_addIPAddress_(self, addr->IpAddress.String);
146 /*printf("%s\n", addr->IpIPAddress.String);*/
147 addr = addr->Next;
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;
162 FILE *fp;
164 sprintf(command, "dig | grep SERVER: > %s", path);
166 system(command);
168 fp = fopen(path, "r");
169 fread(answer, 1, 1024, fp);
170 fclose(fp);
171 remove(path);
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);
177 goto done;
180 answer = answer + strlen(";; SERVER: ");
183 char *s = strstr(answer, "#");
184 if(s) s[0] = 0;
187 LocalNameServers_addIPAddress_(self, answer);
189 done:
190 free(command);
191 free(answerBuffer);
194 #else
196 // Unix ----------------------------------------
199 void LocalNameServers_findIps(LocalNameServers *self)
201 LocalNameServers_findIpsViaResolveConf(self);
204 #endif