Changeset 360

Show
Ignore:
Timestamp:
03/03/06 00:23:26 (3 years ago)
Author:
oxff
Message:

core: added ability to specify `if:ethX' as bind_address

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • nepenthes/trunk/conf/nepenthes.conf.dist

    r347 r360  
    116116        use_rawsockets              "0"; // unstable feature 
    117117        bind_address                "0.0.0.0"; 
     118 
     119        // specify "if:ethX" to get the ip from an interface at startup, 
     120        // only works on linux! 
    118121    }; 
    119122     
  • nepenthes/trunk/nepenthes-core/src/SocketManager.cpp

    r341 r360  
    55 * 
    66 * 
    7  * Copyright (C) 2005  Paul Baecher & Markus Koetter 
     7 * Copyright (C) 2005  Paul Baecher & Markus Koetter & Georg Wicherski 
    88 *  
    99 * This program is free software; you can redistribute it and/or 
     
    6161#include "Config.hpp" 
    6262 
     63#ifdef __linux__ // bind to interface on linux 
     64#include <net/if.h> 
     65#include <sys/types.h> 
     66#include <sys/ioctl.h> 
     67#include <sys/socket.h> 
     68#include <netinet/in.h> 
     69#include <arpa/inet.h> 
     70#endif // end bind to if 
     71 
    6372using namespace nepenthes; 
    6473using namespace std; 
     
    117126 
    118127        try { 
    119                 m_BindAddress = inet_addr(m_Nepenthes->getConfig()->getValString("nepenthes.socketmanager.bind_address")); 
     128                string bindAddressString = m_Nepenthes->getConfig()->getValString("nepenthes.socketmanager.bind_address"); 
     129                 
     130                #ifdef __linux__ 
     131                if(bindAddressString.substr(0, 3) == string("if:")) 
     132                { 
     133                        const char * interfaceName = bindAddressString.substr(3).c_str(); 
     134                        int ifaceSocket = socket(AF_INET, SOCK_STREAM, 0); 
     135                        struct ifreq interfaceRequest; 
     136                        struct sockaddr_in addrInterface; 
     137                                                                
     138                        strncpy(interfaceRequest.ifr_name, interfaceName, IFNAMSIZ - 1); 
     139                         
     140                        if(ifaceSocket < 0 || ioctl(ifaceSocket, SIOCGIFADDR, &interfaceRequest) < 0) 
     141                        { 
     142                                logCrit("Failed to obtain address for interface %s: %s!\n", interfaceName, strerror(errno)); 
     143                        } else 
     144                        {               
     145                                        memcpy(&addrInterface, &(interfaceRequest.ifr_addr), sizeof(addrInterface)); 
     146                                        logInfo("Obtained address of interface %s: %s\n", interfaceName, inet_ntoa(addrInterface.sin_addr)); 
     147                                        m_BindAddress = addrInterface.sin_addr.s_addr; 
     148                                         
     149                                        close(ifaceSocket); 
     150                        } 
     151                         
     152                } else 
     153                #endif 
     154                { 
     155                        m_BindAddress = inet_addr(bindAddressString.c_str()); 
     156                } 
     157                 
    120158                if (m_BindAddress != INADDR_ANY) 
    121159                {