Annotation of sbbs/src/xpdev/netwrap.c, revision 1.1.1.2

1.1       root        1: /* netwrap.c */
                      2: 
                      3: /* Network related wrapper functions */
                      4: 
1.1.1.2 ! root        5: /* $Id: netwrap.c,v 1.5 2009/08/19 00:16:14 rswindell Exp $ */
1.1       root        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:  *                                                                                                                                                     *
1.1.1.2 ! root       11:  * Copyright 2009 Rob Swindell - http://www.synchro.net/copyright.html         *
1.1       root       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 */
1.1.1.2 ! root       40: #include "sockwrap.h"
1.1       root       41: 
                     42: #include <stdlib.h>            /* malloc() */
1.1.1.2 ! root       43: #include <ctype.h>             /* isspace() */
1.1       root       44: 
                     45: #if defined(_WIN32)
                     46:        #include <iphlpapi.h>   /* GetNetworkParams */
                     47: #endif
                     48: 
                     49: str_list_t getNameServerList(void)
                     50: {
                     51: #ifdef __unix__        /* Look up DNS server address */
                     52:        FILE*   fp;
                     53:        char*   p;
                     54:        char    str[128];
                     55:        str_list_t      list;
                     56: 
                     57:        if((list=strListInit())==NULL)
                     58:                return(NULL);
                     59:        if((fp=fopen("/etc/resolv.conf","r"))!=NULL) {
                     60:                while(!feof(fp)) {
                     61:                        if(fgets(str,sizeof(str),fp)==NULL)
                     62:                                break;
                     63:                        truncsp(str);
                     64:                        p=str;
                     65:                        SKIP_WHITESPACE(p);
                     66:                        if(strnicmp(p,"nameserver",10)!=0) /* no match */
                     67:                                continue;
                     68:                        FIND_WHITESPACE(p);     /* skip "nameserver" */
                     69:                        SKIP_WHITESPACE(p);     /* skip more white-space */
                     70:                        strListPush(&list,p);
                     71:                }
                     72:                fclose(fp);
                     73:        }
                     74:        return(list);
                     75: 
                     76: #elif defined(_WIN32)
                     77:        FIXED_INFO* FixedInfo=NULL;
                     78:        ULONG           FixedInfoLen=0;
                     79:        IP_ADDR_STRING* ip;
                     80:        str_list_t      list;
                     81: 
                     82:        if((list=strListInit())==NULL)
                     83:                return(NULL);
                     84:        if(GetNetworkParams(FixedInfo,&FixedInfoLen) == ERROR_BUFFER_OVERFLOW) {
                     85:         FixedInfo=(FIXED_INFO*)malloc(FixedInfoLen);
                     86:                if(GetNetworkParams(FixedInfo,&FixedInfoLen) == ERROR_SUCCESS) {
                     87:                        ip=&FixedInfo->DnsServerList;
                     88:                        for(; ip!=NULL; ip=ip->Next)
                     89:                                strListPush(&list,ip->IpAddress.String);
                     90:                }
                     91:         if(FixedInfo!=NULL)
                     92:             free(FixedInfo);
                     93:     }
                     94:        return(list);
                     95: #else
                     96:        #error "Need a get_nameserver() implementation for this platform"
                     97: #endif
                     98: }
                     99: 
1.1.1.2 ! root      100: const char* getHostNameByAddr(const char* str)
        !           101: {
        !           102:        HOSTENT*        h;
        !           103:        uint32_t        ip;
        !           104: 
        !           105: #ifdef _WIN32
        !           106:        WSADATA wsaData;
        !           107:        WSAStartup(MAKEWORD(2, 2), &wsaData);
        !           108: #endif
        !           109:        if(str==NULL)
        !           110:                return NULL;
        !           111:        if((ip=inet_addr(str)) == INADDR_NONE)
        !           112:                return str;
        !           113:        if((h=gethostbyaddr((char *)&ip,sizeof(ip),AF_INET))==NULL)
        !           114:                return NULL;
        !           115: 
        !           116: #ifdef _WIN32
        !           117:        WSACleanup();
        !           118: #endif
        !           119: 
        !           120:        return h->h_name;
        !           121: }
        !           122: 
1.1       root      123: /* In case we want to DLL-export getNameServerList in the future */
                    124: void freeNameServerList(str_list_t list)
                    125: {
                    126:        strListFree(&list);
                    127: }
                    128: 
                    129: #if NETWRAP_TEST
1.1.1.2 ! root      130: int main(int argc, char** argv)
1.1       root      131: {
                    132:        size_t          i;
                    133:        str_list_t      list;
                    134: 
                    135:        if((list=getNameServerList())!=NULL) {
                    136:                for(i=0;list[i]!=NULL;i++)
                    137:                        printf("%s\n",list[i]);
                    138:                freeNameServerList(list);
                    139:        }
                    140: 
1.1.1.2 ! root      141:        if(argc>1)
        !           142:                printf("%s\n", getHostNameByAddr(argv[1]));
1.1       root      143:        return 0;
                    144: }
                    145: #endif
                    146: 

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.