【问题标题】:optimize snmp library to search a device ip address in iphone优化 snmp 库以在 iphone 中搜索设备 ip 地址
【发布时间】:2015-12-10 00:45:37
【问题描述】:

我在 iPhone 应用中使用 Mobile snmp++ 库 (https://github.com/Zchander/mobile-snmp-plusplus/tree/master/Mobile%20SNMP%2B%2B) 使用 swift 语言扫描设备。

Mobile Snmp++ 库是用 Objective-C 编写的,并且带有桥接头,我可以在我的 swift 项目中使用这个库并且它运行良好。

假设我需要扫描特定 IP 地址范围内的设备 170.23.45.0 到 170.23.45.255。为此,我使用 XISMobile_SNMP_PP.mm 中的 getoid 函数 (https://github.com/Zchander/mobile-snmp-plusplus/blob/master/Mobile%20SNMP%2B%2B/XISMobile_SNMP_PP.mm)

要从单个 IP 地址获得响应,每个响应大约需要 1-2 秒。所以为了减少时间,我使用下面链接中建议的多线程(最多只有 20 个线程,因为我们将在 iphone 中使用该应用程序),从 0 到 255 的完整扫描需要 20 秒。我需要将此时间减少到 5 秒左右。 optimized way to search a device ip address within a range in iphone

问题:每次搜索开始时,getoid函数都会打开socket并发送数据,然后得到响应并关闭socket。 那么我们不能保持套接字打开,扫描所有IP地址并得到它的响应并最后关闭套接字吗?我需要将搜索时间从 20 秒减少到 5 秒,那么我们如何在 getoid 函数中做到这一点。

- (NSDictionary *)getOid:(NSString *)oid
                 address:(NSString *)hostAddress
             snmpVersion:(uint)version
              remotePort:(NSNumber *)aPort
           withCommunity:(NSString *)community
                   retry:(uint)retries
                 timeout:(uint)timeout
                   error:(NSError * __autoreleasing*)error
{
    int status;

    uint l_retries;
    uint l_timeout;
    NSNumber *localPort;

    snmp_version snmpVersion = version1;
    OctetStr snmpCommunity([community UTF8String]);

    if ( aPort == nil ) {
        localPort = [NSNumber numberWithInteger:161];
    } else localPort = aPort;

    if ( retries > 100 ) {
        l_retries = 100;
    } else l_retries = retries;

    if ( timeout < 100 ) {
        l_timeout = 100;
    } else if ( timeout > 500 ) {
        l_timeout = 500;
    } else l_timeout = timeout;

    switch ( version ) {
        case 1:
            snmpVersion = version1;
            break;
        case 2:
            snmpVersion = version2c;
            break;
        default:
            snmpVersion = version1;
            break;
    }

    // Generate a SNMP++ generic address
    UdpAddress udpAddress([hostAddress UTF8String]);

    // Check if it is a valid address, if we got an invalid address
    // we return a 'nil' dictionary and an error code
    if ( !udpAddress.valid() ) {
        *error = [self constructError:ERR_INVALID_DESTINATION];
#ifdef DEBUG
        NSLog(@"ERROR SNMPController (getOid:hostAddress:oid:snmpVersion:remotePort:withCommunity:retry:timeout:error:)");
        NSLog(@"ERROR SNMP++ Invalid host address or IP: %@", hostAddress);
        NSLog(@"ERROR ====================");
#endif
        return nil;
    }

    // Check if we got a valid Oid, otherwise use sysDescr
    Oid localOid([oid UTF8String]);
    if ( !localOid.valid() ) {
#ifdef DEBUG
        NSLog(@"ERROR SNMPController (getOid:hostAddress:oid:snmpVersion:remotePort:withCommunity:retry:timeout:error:)");
        NSLog(@"ERROR SNMP++ We got an invalid Oid (%@), we are using sysDescr for now (.1.3.6.1.2.1.1.1.0)", oid);
        NSLog(@"ERROR ====================");
#endif
        Oid localOid("1.3.6.1.2.1.1.1.0");
    }

    // Create the SNMP session
    Snmp snmp(status, 0, (udpAddress.get_ip_version() == Address::version_ipv6));

    if ( status != SNMP_CLASS_SUCCESS ) {
#ifdef DEBUG
        NSLog(@"ERROR SNMPController (getOid:hostAddress:oid:snmpVersion:remotePort:withCommunity:retry:timeout:error:)");
        NSLog(@"ERROR SNMP++ Could not create session: %s", snmp.error_msg(status));
        NSLog(@"ERROR ====================");
#endif
        *error = [self constructError:ERR_NO_SNMP_SESSION];
        return nil;
    }

    // We are ready to build the SNMP++ object we need
    Pdu pdu;                                    // construct a Pdu object
    Vb vb;                                      // construct a Vb object
    vb.set_oid(localOid);                       // set the Oid portion of the Vb
    pdu += vb;                                  // add the vb to the Pdu

    // Set the port
    udpAddress.set_port([localPort integerValue]);
    CTarget ctarget(udpAddress);                // Make a target using the address

    ctarget.set_version(snmpVersion);           // Set the SNMP version
    ctarget.set_retry(l_retries);               // Set the number of retries
    ctarget.set_timeout(l_timeout);             // Set the timeout for the request
    ctarget.set_readcommunity(snmpCommunity);   // Set the read community name

    // Issue the request, in blocked mode
#ifdef DEBUG
    NSLog(@"DEBUG SNMPController (getOid:hostAddress:oid:snmpVersion:remotePort:withCommunity:retry:timeout:error:)");
    NSLog(@"DEBUG SNMP++ GET to %@ (oid: %@) with version %d on Port: %d using community %@ with retries %d and timeout %d", hostAddress, oid, version, [aPort integerValue], community, retries, timeout);
    NSLog(@"DEBUG SNMP++ What is the community we are sending.... %s", snmpCommunity.get_printable());
    NSLog(@"DEBUG ====================");
#endif

    SnmpTarget *target;
    target = &ctarget;

    status = snmp.get(pdu, *target);

    NSMutableDictionary *resultsDict = [[NSMutableDictionary alloc] init];

    if ( status == SNMP_CLASS_SUCCESS ) {
        pdu.get_vb(vb, 0);

#ifdef DEBUG
        NSLog(@"DEBUG SNMPController (getOid:hostAddress:oid:snmpVersion:remotePort:withCommunity:retry:timeout:error:)");
        NSLog(@"DEBUG SNMP++ -- Oid: %s", vb.get_printable_oid());
        NSLog(@"DEBUG SNMP++ -- Value: %s", vb.get_printable_value());
#endif

        // Add the result(s) to the resultsDict
        [resultsDict setObject:[NSString stringWithUTF8String:vb.get_printable_value()] forKey:[NSString stringWithUTF8String:vb.get_printable_oid()]];

        if ( (vb.get_syntax() == sNMP_SYNTAX_ENDOFMIBVIEW) ||
             (vb.get_syntax() == sNMP_SYNTAX_NOSUCHINSTANCE) ||
             (vb.get_syntax() == sNMP_SYNTAX_NOSUCHOBJECT)) {

            NSLog(@"ERROR SNMP++ Exception found: %lu", vb.get_syntax());

        } else {

            NSLog(@"ERROR SNMP++ GET Error: %s (%d)", snmp.error_msg(status), status);

        }
#ifdef DEBUG
        NSLog(@"DEBUG ====================");
#endif
    }

    // Make sure error is nil!

    *error = nil;

    return ( resultsDict != nil ) ? [NSDictionary dictionaryWithDictionary:resultsDict] : nil;

}

【问题讨论】:

    标签: ios iphone swift sockets search


    【解决方案1】:

    您使用的底层 C++ 库支持在收到响应时使用回调。如果您更新 Objective-C 包装器以使用这种方法而不是线程阻塞 getOid 函数,您将看到更好的性能,并且您只需要使用 1 或 2 个线程。

    我去看看

    int Snmp::get(Pdu &pdu, const SnmpTarget &target, const snmp_callback callback, const void * callback_data)

    uxsnmp.cpp。您甚至可以将其包装以使用 Obj-C 块。这就是你要获得表现的地方。

    只要您使用异步库钩子,像这样触发大量 SNMP-over-UDP 查询是非常可行的,我使用 Ruby 中的 EventMachine 以同样的方式进行 DNS 查询获得了很好的结果 - 我得到了在 3-5 秒内完成约 1500 个查询!!

    【讨论】:

    • 能否请您提供示例...因为我还需要在 6 秒内触发 500 个查询。
    • 关于如何进行回调以及混合 C 和 Obj-C 代码有几个问题。 stackoverflow.com/questions/801976/… 就是一个例子。你应该在那里找到一些例子!
    【解决方案2】:

    在 iOS 中尝试Concurrency Programming 以增加并发工作的数量。

    【讨论】:

      猜你喜欢
      • 2015-11-28
      • 2014-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-24
      • 1970-01-01
      • 2013-10-14
      相关资源
      最近更新 更多