Пример #1
0
void emberZllNetworkFoundHandler(const EmberZllNetwork *networkInfo,
                                 const EmberZllDeviceInfoRecord *deviceInfo)
{
#ifdef EMBER_AF_PLUGIN_ZLL_COMMISSIONING_LINK_INITIATOR
  if (touchLinkInProgress()) {
    int8_t lastHopRssi;

    if (networkInfo->nodeType < EMBER_ROUTER
        || EMBER_SLEEPY_END_DEVICE < networkInfo->nodeType) {
      // In any situation, the target must be a router or end device.  ZLL does
      // not use coordinators and any other types must have wandered into our
      // network in error.
      return;
    } else if (scanForTouchLink()) {
      // When scanning for the purposes of touch linking (as opposed to, for
      // example, resetting to factory new), additional restrictions apply, as
      // described below.
      if (!emberIsZllNetwork()
          && (isFactoryNew(networkInfo->state)
              || !isSameNetwork(networkInfo))) {
        // If the initiator device is on a non-ZLL network, the target cannot
        // be factory new or on a different network.  Those devices can only
        // join the network using classical ZigBee commissioning.
        return;
      } else if (networkInfo->nodeType == EMBER_END_DEVICE
                 || networkInfo->nodeType == EMBER_SLEEPY_END_DEVICE) {
        // When the initiator is factory new, the target cannot be an end
        // device.  If the target is not factory new and is also trying to
        // touch link, the stack will cancel our touch link and wait for a join
        // request.  When the initiator is not factory new, the target must
        // either be factory new or must be on the same network.
        if (amFactoryNew()
            || (!isFactoryNew(networkInfo->state)
                && !isSameNetwork(networkInfo))) {
          return;
        }
      }
    }

    emberGetLastHopRssi(&lastHopRssi);
    if (!targetNetworkFound()
        || 0 < targetCompare(networkInfo, lastHopRssi, &network, rssi)) {
      MEMMOVE(&network, networkInfo, sizeof(EmberZllNetwork));
      subDeviceCount = 0;
      if (deviceInfo != NULL) {
        MEMMOVE(&subDevices[0], deviceInfo, sizeof(EmberZllDeviceInfoRecord));
        subDeviceCount++;
      }
      rssi = lastHopRssi;
      flags |= TARGET_NETWORK_FOUND;
    }
  }
#endif //EMBER_AF_PLUGIN_ZLL_COMMISSIONING_LINK_INITIATOR

#ifdef EMBER_AF_PLUGIN_TEST_HARNESS_Z3
  emAfPluginTestHarnessZ3ZllNetworkFoundCallback(networkInfo);
#endif
}
Пример #2
0
int main(int argc, char **argv)
{
    tw_init_global();
    Printing_Enable = 0; //disable the real-time printing of Tx/Rx,
    tw_map_port_to_engine("tw0", "engine0");
    if(argc == 2)
    {
        ip = argv[1];
        if(isValidIpAddress(ip))
        {
            if(isSameNetwork(ip))
                arp_flag = 1;
            else
            {
                printf("Cannot resolve address outside of network.\n");
                exit(1);
            }
        }
        else
        {
            printf("Host address not valid.\n");
            exit(1);
        }
    }
    else
    {
        printf("Please specify twarp <x.x.x.x> \n");
        exit(1);
    }
    user_app_main(NULL);
}
Пример #3
0
std::string
ipmsg::convertIpAddressToMacAddress( std::string ipAddress, const std::vector<NetworkInterface> &nics )
{
	IPMSG_FUNC_ENTER( "std::string ipmsg::convertIpAddressToMacAddress( std::string ipAddress )");
	sockaddr_storage ip;
	if ( createSockAddrIn( &ip, ipAddress, 0 ) == NULL ) {
		IPMSG_FUNC_RETURN( "" );
	}
	char retbuf[20]={0};
	if ( ip.ss_family == AF_INET ) {
#ifdef SIOCGARP
		int sockfd = socket( AF_INET, SOCK_DGRAM, 0 );
		struct sockaddr_in *sockaddrp = ( struct sockaddr_in * )&ip;
		struct arpreq arpreq;
		struct sockaddr_in *sin;

		memset( &arpreq, 0, sizeof( arpreq ) );
		sin = ( struct sockaddr_in * )&arpreq.arp_pa;
		sin->sin_family = AF_INET;
		sin->sin_addr = sockaddrp->sin_addr;
#ifdef __linux__
		strcpy( arpreq.arp_dev, nics[0].DeviceName().c_str() );
		for( unsigned int i = 0; i < nics.size(); i++ ) {
			if ( isSameNetwork( &ip, nics[i].NetworkAddress(), nics[i].NetMask() ) ) {
				strcpy( arpreq.arp_dev, nics[i].DeviceName().c_str() );
				break;
			}
		}
#endif
		errno = 0;
		int rc = ioctl( sockfd, SIOCGARP, &arpreq );
		if ( rc == -1 ) {
			int err = errno;
			for( unsigned int i = 0; i < nics.size(); i++ ){
				if ( nics[i].IpAddress() == ipAddress ) {
					close( sockfd );
					IPMSG_FUNC_RETURN( nics[i].HardwareAddress() );
				}
			}
			fprintf( stderr, "ioctl in convertIpAddressToMacAddress:%s:%s\n", ipAddress.c_str(),strerror( err ) );
		} else {
			convertMacAddressToBuffer( ( unsigned char * )&arpreq.arp_ha.sa_data[0], retbuf, sizeof( retbuf ) );
		}
		close( sockfd );
		IPMSG_FUNC_RETURN( retbuf );
#else	// FreeBSD, etc...
		int s, mib[6];
		size_t len;
		char *buf;
 
		mib[0] = CTL_NET;
		mib[1] = PF_ROUTE;
		mib[2] = 0;
		mib[3] = AF_INET;
		mib[4] = NET_RT_FLAGS;
		mib[5] = RTF_LLINFO;
 
		if ( sysctl(mib, 6, NULL, &len, NULL, 0 ) < 0 ) {
//perror( "sysctl 1" );
			IPMSG_FUNC_RETURN( "" );
		}
		if( ( buf = (char*)malloc(len)) == NULL ) {
//perror( "malloc" );
			IPMSG_FUNC_RETURN( "" );
		}
		if ( sysctl( mib, 6, buf, &len, NULL, 0 ) < 0 ){
//perror( "sysctl 2" );
			free( buf );
			IPMSG_FUNC_RETURN( "" );
		}
		struct sockaddr_in *sin = ( struct sockaddr_in * )&ip;
		struct rt_msghdr *rtm;
		for( s = 0; s < ( int )len; s += rtm->rtm_msglen ){
			rtm = ( struct rt_msghdr * )( buf + s );
			struct sockaddr_inarp *inarp = ( struct sockaddr_inarp * )( rtm + 1 );
			struct sockaddr_dl *sdl = ( struct sockaddr_dl * )( inarp + 1 ); 

			if( sin->sin_addr.s_addr == inarp->sin_addr.s_addr ){
				convertMacAddressToBuffer( ( const unsigned char * )LLADDR( sdl ), retbuf, sizeof( retbuf ) );
				free( buf );
				IPMSG_FUNC_RETURN( retbuf );
			}
		}
		free( buf );
		IPMSG_FUNC_RETURN( "" );
#endif
	}
	if ( ip.ss_family == AF_INET6 ) {
		struct sockaddr_in6 *sockaddrp = ( struct sockaddr_in6 * )&ip;
		unsigned char mac[6];
		mac[0] = sockaddrp->sin6_addr.s6_addr[8] ^ 0x02;
		mac[1] = sockaddrp->sin6_addr.s6_addr[9];
		mac[2] = sockaddrp->sin6_addr.s6_addr[10];
		mac[3] = sockaddrp->sin6_addr.s6_addr[13];
		mac[4] = sockaddrp->sin6_addr.s6_addr[14];
		mac[5] = sockaddrp->sin6_addr.s6_addr[15];
		convertMacAddressToBuffer( mac, retbuf, sizeof( retbuf ) );
		IPMSG_FUNC_RETURN( retbuf );
	}
	IPMSG_FUNC_RETURN( "" );
}