Esempio n. 1
0
extern mStatus mDNSPlatformPosixRefreshInterfaceList(mDNS *const m)
	{
	int err;
	ClearInterfaceList(m);
	err = SetupInterfaceList(m);
	return PosixErrorToStatus(err);
	}
Esempio n. 2
0
void InterfaceManager::DiscoverInterfaces( InterfaceList_t & cInterfaces )
{
	ClearInterfaceList( cInterfaces );
	int nSock = socket( AF_INET, SOCK_STREAM, 0 );

	int nIFaceCount = ioctl( nSock, SIOCGIFCOUNT, NULL );

	if( nIFaceCount == 0 )
		return;

	struct ifreq *psIFReqs = ( struct ifreq * )calloc( nIFaceCount, sizeof( struct ifreq ) );
	struct ifconf sIFConf;
	sIFConf.ifc_len = nIFaceCount * sizeof( struct ifreq );
	sIFConf.ifc_req = psIFReqs;

	ioctl( nSock, SIOCGIFCONF, &sIFConf );
	char zMACBuf[18];

	for( int i = 0; i < nIFaceCount; i++ )
	{
		struct ifreq sReq;

		memcpy( &sReq, &( sIFConf.ifc_req[i] ), sizeof( sReq ) );
		ioctl( nSock, SIOCGIFFLAGS, &sReq );
		short nFlags = sReq.ifr_flags;

		if( !( nFlags & IFF_LOOPBACK ) )
		{
			ioctl( nSock, SIOCGIFHWADDR, &sReq );
			uint8 *pnMac = ( uint8 * )( sReq.ifr_hwaddr.sa_data );

			sprintf( zMACBuf, "%.2X:%.2X:%.2X:%.2X:%.2X:%.2X", ( uint )pnMac[0], ( uint )pnMac[1], ( uint )pnMac[2], ( uint )pnMac[3], ( uint )pnMac[4], ( uint )pnMac[5] );

			ioctl( nSock, SIOCGIFFLAGS, &sReq );

			Interface *pcIFace = new Interface( Static, sIFConf.ifc_req[i].ifr_name, zMACBuf );

			pcIFace->SetEnabled( nFlags & IFF_UP );

			ioctl( nSock, SIOCGIFADDR, &sReq );
			IPAddress_t *pnAddr = ( IPAddress_t * ) & ( ( struct sockaddr_in * )&( sReq.ifr_addr ) )->sin_addr;

			pcIFace->SetAddress( *pnAddr );

			ioctl( nSock, SIOCGIFNETMASK, &sReq );
			pcIFace->SetNetmask( *pnAddr );

			cInterfaces.push_back( pcIFace );
		}
	}
}
Esempio n. 3
0
// mDNS core calls this routine to clean up the platform-specific data.
// In our case all we need to do is to tear down every network interface.
mDNSexport void mDNSPlatformClose(mDNS *const m)
	{
	assert(m != NULL);
	ClearInterfaceList(m);
	}
Esempio n. 4
0
void InterfaceManager::LoadConfig( InterfaceList_t & cInterfaces )
{
	ClearInterfaceList( cInterfaces );
	File cFile;

	if( cFile.SetTo( CONFIG_FILE ) == 0 )
	{
		StringReader cSr( &cFile );
		std::vector < String >cLines;

		cLines.push_back( cSr.ReadLine() );
		while( cSr.HasLines() )
		{
			cLines.push_back( cSr.ReadLine() );
		}

		String cName;
		String cMAC;
		IPAddress_t nAddress;
		IPAddress_t nNetmask;
		IPAddress_t nGateway;
		bool bEnabled = false;
		bool bDefault = false;
		IFaceType_t nType = Static;
		bool bInIFace = false;
		Interface *pcIFace = NULL;

		for( std::vector < String >::iterator i = cLines.begin(); i != cLines.end(  ); i++ )
		{

			if( *i == "interface" )
			{
				bInIFace = true;
				continue;
			}

			if( *i == "" )
			{
				if( bInIFace )
				{
					pcIFace = new Interface( nType, cName, cMAC );
					pcIFace->SetEnabled( bEnabled );
					pcIFace->SetDefault( bDefault );
					if( nType == Static )
					{
						pcIFace->SetAddress( nAddress );
						pcIFace->SetNetmask( nNetmask );
						pcIFace->SetGateway( nGateway );
					}
					cInterfaces.push_back( pcIFace );
				}
				bInIFace = false;
				continue;
			}

			if( bInIFace )
			{
				if( ( *i ).find( "type" ) == 0 )
				{
					nType = ( *i ).substr( 5 ) == "Static" ? Static : DHCP;
				}
				else if( ( *i ).find( "name" ) == 0 )
				{
					cName = ( *i ).substr( 5 );
				}
				else if( ( *i ).find( "mac" ) == 0 )
				{
					cMAC = ( *i ).substr( 4 );
				}
				else if( ( *i ).find( "address" ) == 0 )
				{
					ParseIPAddress( ( *i ).substr( 8 ), &nAddress );
				}
				else if( ( *i ).find( "netmask" ) == 0 )
				{
					ParseIPAddress( ( *i ).substr( 8 ), &nNetmask );
				}
				else if( ( *i ).find( "gateway" ) == 0 )
				{
					ParseIPAddress( ( *i ).substr( 8 ), &nGateway );
				}
				else if( ( *i ).find( "enabled" ) == 0 )
				{
					bEnabled = ( *i ).substr( 8 ) == "true";
				}
				else if( ( *i ).find( "default" ) == 0 )
				{
					bDefault = ( *i ).substr( 8 ) == "true";
				}
			}
		}
	}
}