2010-02-03 5 views
6

में राउटर मैक (एआरपी के लिए सिस्टम कॉल के बिना) ठीक है - मैं उद्देश्य सी में एक डिमन लिख रहा हूं जो हर 5 सेकंड में जुड़े राउटर मैक पते की जांच करता है।उद्देश्य-सी

मैं उद्देश्य सी के लिए बिल्कुल नया हूं, और मैं जो कुछ भी कर रहा हूं उसे करने का एक बेहतर तरीका ढूंढ रहा हूं।

मैं वर्तमान में फोन कर रहा हूँ "एआरपी-एक" और "कार्य" के माध्यम से परिणाम को पार्स:

NSTask *task; 
task = [[NSTask alloc] init]; 
[task setLaunchPath: @"/usr/sbin/arp"]; 

NSArray *arguments; 
arguments = [NSArray arrayWithObjects: @"-a", nil]; 
[task setArguments: arguments]; 

NSPipe *pipe; 
pipe = [NSPipe pipe]; 
[task setStandardOutput: pipe]; 

NSFileHandle *file; 
file = [pipe fileHandleForReading]; 

[task launch]; 

NSData *data; 
data = [file readDataToEndOfFile]; 

मुझे डर है कि यह बहुत ही कुशल नहीं है हूँ।

कोई सुझाव? मैं हर 5 सेकंड में एक बार यह कोडब्लॉक चला रहा हूं।

+1

सबसे पहले, मैं बस यह साफ़ करना चाहता हूं कि "arp -a" सिस्टम कॉल नहीं है। एक सिस्टम कॉल एक विशेष कार्य है जो वास्तव में ऑपरेटिंग सिस्टम में एक हुक है। ऐसा लगता है कि आप सिर्फ एक बाहरी प्रोग्राम का उपयोग नहीं करना चाहते हैं, सिस्टम कॉल नहीं। सिस्टम कॉल के उदाहरण पढ़ने(), लिखना(), सॉकेट(), आदि – BobbyShaftoe

+0

इसके बारे में खेद है। मैं भाषा के लिए नया हूं, खासकर शब्दावली। कोई जानकारी यह कैसे करनी है? –

+0

क्या आप कह सकते हैं कि आप ऐसा करने का प्रयास क्यों कर रहे हैं? ओएस एक्स पहले से ही विभिन्न नेटवर्क निगरानी और कॉन्फ़िगरेशन सेवाएं प्रदान करता है जो आपके स्वयं के रोलिंग से अधिक उपयोगी हो सकते हैं। –

उत्तर

6

ऐप्पल का arp का कार्यान्वयन खुला स्रोत है। इसके कार्यान्वयन के विचार के लिए the file पर एक नज़र डालें ... यह बहुत ही जटिल नहीं है। हालांकि यह शुद्ध एएनएसआई सी है।

आपको कार्यक्षमता के बहुमत को कॉपी-पेस्ट करने में सक्षम होना चाहिए ... और परिणामों को प्रिंट करने के बजाय, केवल कच्चे पते को स्टोर करें।

संपादित करें: यहां स्रोत का एक अलग संस्करण है जो केवल arp -a के बराबर चलता है। यह किसी भी विशेष निर्देश के बिना संकलित करना चाहिए।

/* 
* Copyright (c) 1999 Apple Computer, Inc. All rights reserved. 
* 
* @[email protected] 
* 
* This file contains Original Code and/or Modifications of Original Code 
* as defined in and that are subject to the Apple Public Source License 
* Version 2.0 (the 'License'). You may not use this file except in 
* compliance with the License. Please obtain a copy of the License at 
* http://www.opensource.apple.com/apsl/ and read it before using this 
* file. 
* 
* The Original Code and all software distributed under the License are 
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 
* Please see the License for the specific language governing rights and 
* limitations under the License. 
* 
* @[email protected] 
*/ 


#include <sys/param.h> 
#include <sys/file.h> 
#include <sys/socket.h> 
#include <sys/sysctl.h> 

#include <net/if.h> 
#include <net/if_dl.h> 
#include <net/if_types.h> 
#include <net/route.h> 

#include <netinet/in.h> 
#include <netinet/if_ether.h> 

#include <arpa/inet.h> 

#include <err.h> 
#include <errno.h> 
#include <netdb.h> 
#include <nlist.h> 
#include <paths.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <unistd.h> 

static int nflag; 

void 
ether_print(cp) 
    u_char *cp; 
{ 
    printf("%x:%x:%x:%x:%x:%x", cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]); 
} 


/* 
* Dump the entire arp table 
*/ 
int 
dump(addr) 
    u_long addr; 
{ 
    int mib[6]; 
    size_t needed; 
    char *host, *lim, *buf, *next; 
    struct rt_msghdr *rtm; 
    struct sockaddr_inarp *sin; 
    struct sockaddr_dl *sdl; 
    extern int h_errno; 
    struct hostent *hp; 
    int found_entry = 0; 

    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, &needed, NULL, 0) < 0) 
     err(1, "route-sysctl-estimate"); 
    if ((buf = malloc(needed)) == NULL) 
     err(1, "malloc"); 
    if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) 
     err(1, "actual retrieval of routing table"); 
    lim = buf + needed; 
    for (next = buf; next < lim; next += rtm->rtm_msglen) { 
     rtm = (struct rt_msghdr *)next; 
     sin = (struct sockaddr_inarp *)(rtm + 1); 
     sdl = (struct sockaddr_dl *)(sin + 1); 
     if (addr) { 
      if (addr != sin->sin_addr.s_addr) 
       continue; 
      found_entry = 1; 
     } 
     if (nflag == 0) 
      hp = gethostbyaddr((caddr_t)&(sin->sin_addr), 
       sizeof sin->sin_addr, AF_INET); 
     else 
      hp = 0; 
     if (hp) 
      host = hp->h_name; 
     else { 
      host = "?"; 
      if (h_errno == TRY_AGAIN) 
       nflag = 1; 
     } 
     printf("%s (%s) at ", host, inet_ntoa(sin->sin_addr)); 
     if (sdl->sdl_alen) 
      ether_print((u_char *)LLADDR(sdl)); 
     else 
      printf("(incomplete)"); 
     if (rtm->rtm_rmx.rmx_expire == 0) 
      printf(" permanent"); 
     if (sin->sin_other & SIN_PROXY) 
      printf(" published (proxy only)"); 
     if (rtm->rtm_addrs & RTA_NETMASK) { 
      sin = (struct sockaddr_inarp *) 
       (sdl->sdl_len + (char *)sdl); 
      if (sin->sin_addr.s_addr == 0xffffffff) 
       printf(" published"); 
      if (sin->sin_len != 8) 
       printf("(weird)"); 
     } 
     printf("\n"); 
    } 
    return (found_entry); 
} 

int main (int argc, char const *argv[]) 
{ 
    dump(0); 
    return 0; 
} 
+0

कुछ कारणों से, arp.c और arp.h दोनों डाउनलोड करने के बाद, मैं इसे संकलित नहीं कर सकता ... विचार? –

+0

हाँ, वे '# परिभाषित' के साथ कुछ मजाकिया सामान करते हैं। मैंने अपने उत्तर में केवल 'arp -a' के एक व्यावहारिक, पूरी तरह से स्टैंडअलोन कार्यान्वयन जोड़ा है। –

+0

वाह - ठंडा। मैं इसे देख लूंगा और रिपोर्ट करूंगा। बहुत उपयोगी। धन्यवाद। –

 संबंधित मुद्दे

  • कोई संबंधित समस्या नहीं^_^