قناع الشبكة الفرعية iPhone WiFi وعنوان جهاز التوجيه

StackOverflow https://stackoverflow.com/questions/2023592

  •  19-09-2019
  •  | 
  •  

سؤال

لدي رمز يسمح لي بتحديد عنوان MAC وعنوان IP لاتصال WiFi على iPhone، لكن لا يمكنني معرفة كيفية الحصول على قناع الشبكة الفرعية وعنوان جهاز التوجيه للاتصال. يمكن لأي شخص لي نقطة في الاتجاه الصحيح هنا؟

هل كانت مفيدة؟

المحلول

يمكنك الحصول على هذه المعلومات عن طريق الاتصال getifaddrs.. وبعد (يمكنني استخدام هذه الوظيفة في تطبيق منجم لمعرفة عنوان IP iPhone.)

struct ifaddrs *ifa = NULL, *ifList;
getifaddrs(&ifList); // should check for errors
for (ifa = ifList; ifa != NULL; ifa = ifa->ifa_next) {
   ifa->ifa_addr // interface address
   ifa->ifa_netmask // subnet mask
   ifa->ifa_dstaddr // broadcast address, NOT router address
}
freeifaddrs(ifList); // clean up after yourself

هذا يجعلك قناع الشبكة الفرعية؛ ل عنوان جهاز التوجيه، راجع هذا السؤال.

هذه هي جميع أشكال الشبكات Unix المدرسة القديمة، سيتعين عليك اختيار أي من الواجهات هي اتصال WiFi (أشياء أخرى مثل واجهة الاسترجاع ستكون هناك أيضا). ثم قد تضطر إلى استخدام وظائف مثل inet_ntoa () بناء على تنسيق ما تريد قراءة عناوين IP. انها ليست سيئة، مملة وقبيحة. استمتع!

نصائح أخرى

NSString *address = @"error";
NSString *netmask = @"error";
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
int success = 0;

// retrieve the current interfaces - returns 0 on success
success = getifaddrs(&interfaces);
if (success == 0)
{
    // Loop through linked list of interfaces
    temp_addr = interfaces;
    while(temp_addr != NULL)
    {
        if(temp_addr->ifa_addr->sa_family == AF_INET)
        {
            // Check if interface is en0 which is the wifi connection on the iPhone

            if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"])
            {
                // Get NSString from C String
                address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
                netmask = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_netmask)->sin_addr)];
            }
        }

        temp_addr = temp_addr->ifa_next;
    }
}

// Free memory
freeifaddrs(interfaces);
NSLog(@"address %@", address);
NSLog(@"netmask %@", netmask);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top