|
|
1.1 root 1: /* netwrap.c */
2:
3: /* Network related wrapper functions */
4:
5: /* $Id: netwrap.c,v 1.1 2005/10/07 06:23:15 rswindell Exp $ */
6:
7: /****************************************************************************
8: * @format.tab-size 4 (Plain Text/Source Code File Header) *
9: * @format.use-tabs true (see http://www.synchro.net/ptsc_hdr.html) *
10: * *
11: * Copyright 2005 Rob Swindell - http://www.synchro.net/copyright.html *
12: * *
13: * This library is free software; you can redistribute it and/or *
14: * modify it under the terms of the GNU Lesser General Public License *
15: * as published by the Free Software Foundation; either version 2 *
16: * of the License, or (at your option) any later version. *
17: * See the GNU Lesser General Public License for more details: lgpl.txt or *
18: * http://www.fsf.org/copyleft/lesser.html *
19: * *
20: * Anonymous FTP access to the most recent released source is available at *
21: * ftp://vert.synchro.net, ftp://cvs.synchro.net and ftp://ftp.synchro.net *
22: * *
23: * Anonymous CVS access to the development source and modification history *
24: * is available at cvs.synchro.net:/cvsroot/sbbs, example: *
25: * cvs -d :pserver:[email protected]:/cvsroot/sbbs login *
26: * (just hit return, no password is necessary) *
27: * cvs -d :pserver:[email protected]:/cvsroot/sbbs checkout src *
28: * *
29: * For Synchronet coding style and modification guidelines, see *
30: * http://www.synchro.net/source.html *
31: * *
32: * You are encouraged to submit any modifications (preferably in Unix diff *
33: * format) via e-mail to [email protected] *
34: * *
35: * Note: If this box doesn't appear square, then you need to fix your tabs. *
36: ****************************************************************************/
37:
38: #include "genwrap.h" /* truncsp */
39: #include "netwrap.h" /* verify prototypes */
40:
41: #include <stdlib.h> /* malloc() */
42:
43: #if defined(_WIN32)
44: #include <iphlpapi.h> /* GetNetworkParams */
45: #endif
46:
47: str_list_t getNameServerList(void)
48: {
49: #ifdef __unix__ /* Look up DNS server address */
50: FILE* fp;
51: char* p;
52: char str[128];
53: str_list_t list;
54:
55: if((list=strListInit())==NULL)
56: return(NULL);
57: if((fp=fopen("/etc/resolv.conf","r"))!=NULL) {
58: while(!feof(fp)) {
59: if(fgets(str,sizeof(str),fp)==NULL)
60: break;
61: truncsp(str);
62: p=str;
63: SKIP_WHITESPACE(p);
64: if(strnicmp(p,"nameserver",10)!=0) /* no match */
65: continue;
66: FIND_WHITESPACE(p); /* skip "nameserver" */
67: SKIP_WHITESPACE(p); /* skip more white-space */
68: strListPush(&list,p);
69: }
70: fclose(fp);
71: }
72: return(list);
73:
74: #elif defined(_WIN32)
75: FIXED_INFO* FixedInfo=NULL;
76: ULONG FixedInfoLen=0;
77: IP_ADDR_STRING* ip;
78: str_list_t list;
79:
80: if((list=strListInit())==NULL)
81: return(NULL);
82: if(GetNetworkParams(FixedInfo,&FixedInfoLen) == ERROR_BUFFER_OVERFLOW) {
83: FixedInfo=(FIXED_INFO*)malloc(FixedInfoLen);
84: if(GetNetworkParams(FixedInfo,&FixedInfoLen) == ERROR_SUCCESS) {
85: ip=&FixedInfo->DnsServerList;
86: for(; ip!=NULL; ip=ip->Next)
87: strListPush(&list,ip->IpAddress.String);
88: }
89: if(FixedInfo!=NULL)
90: free(FixedInfo);
91: }
92: return(list);
93: #else
94: #error "Need a get_nameserver() implementation for this platform"
95: #endif
96: }
97:
98: /* In case we want to DLL-export getNameServerList in the future */
99: void freeNameServerList(str_list_t list)
100: {
101: strListFree(&list);
102: }
103:
104: #if NETWRAP_TEST
105: int main(void)
106: {
107: size_t i;
108: str_list_t list;
109:
110: if((list=getNameServerList())!=NULL) {
111: for(i=0;list[i]!=NULL;i++)
112: printf("%s\n",list[i]);
113: freeNameServerList(list);
114: }
115:
116: return 0;
117: }
118: #endif
119:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.