Getting network connection for a specified PID

Apprentice
Posts: 12
Joined: 2006.12
Post: #1
Hi!

I´m currently writing an application that needs to find out, to which IP a specified process is connected to.
For example, when I have opened Quake 3 and connected to a server, then my app should tell me the IP of this server, but i dont have a clue how to do that.

I allready tried to get the IP like that:
Code:
lsof -i

This works for apps like adium or something like that. It prints the server, process name and process id.
But for Quake 3 or other games I only get something like that:

Code:
Quake3    3037  Flo   28u  IPv4 0x02943040      0t0  UDP *:27960

Why doesn´t it tell me the IP, only the port?
And how do i get the IP?

Thanks in advance Smile
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
UDP isn't a connection-based protocol.
Quote this message in a reply
Apprentice
Posts: 12
Joined: 2006.12
Post: #3
So there is no simple way of getting the Server-IP?
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #4
Only by watching the traffic to see where it's going.
Quote this message in a reply
Apprentice
Posts: 12
Joined: 2006.12
Post: #5
Hi!

Watching the traffic with tcpdump works quite good, but I ant to start tcpdump from my app and get the output. Since tcpdump must be startet as superuser, i cant do it via NSTask. Here is my code:

Code:
- (void)awakeFromNib
{
    OSStatus myStatus;
    AuthorizationFlags myFlags=kAuthorizationFlagDefaults;
    AuthorizationRef myAuthorizationRef;

    myStatus=AuthorizationCreate(NULL,kAuthorizationEmptyEnvironment,myFlags,&myAuthorizationRef);

    if(myStatus==errAuthorizationSuccess){
        AuthorizationItem myItems={kAuthorizationRightExecute,0,NULL,0};
        AuthorizationRights myRights={1,&myItems};
        myFlags=kAuthorizationFlagDefaults| kAuthorizationFlagInteractionAllowed| kAuthorizationFlagPreAuthorize| kAuthorizationFlagExtendRights;
        myStatus=AuthorizationCopyRights(myAuthorizationRef,&myRights,NULL,myFlags,NULL);

        if(myStatus==errAuthorizationSuccess){
            FILE*myCommunicationsPipe=NULL;
            char*myArguments[]={ "-t",NULL};
                
            NSString *tool=@"/usr/sbin/tcpdump";

            myFlags=kAuthorizationFlagDefaults;
            myStatus=AuthorizationExecuteWithPrivileges(myAuthorizationRef,[tool cString],myFlags,myArguments,&myCommunicationsPipe);

            NSFileHandle *myfilehandle=[[NSFileHandle alloc] initWithFileDescriptor:fileno(myCommunicationsPipe)];
            [NSThread detachNewThreadSelector:@selector(copyData:) toTarget:self withObject:myfilehandle];

            fflush(myCommunicationsPipe);
        }
    }
    return;
}

- (void)copyData:(NSFileHandle*)handle {
    NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
    NSData *data;

    while([data=[handle availableData] length]) {
        NSString *string=[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
    NSLog(@"%@", string);
        [string release];
    }

    [pool release];
}

That prints the output of tcpdump in the Console, but not as NSLog. Very rarely I get something via NSLog, but normaly its just printed in the Console. But why?!
I cant use the Output, if I dont get it via NSLog...

Has anyone an idea?
Thanks in advance Smile
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #6
Maybe it's printing to standard error?
Quote this message in a reply
Apprentice
Posts: 12
Joined: 2006.12
Post: #7
OneSadCookie Wrote:Maybe it's printing to standard error?

Hm. But AuthorizationExecuteWithPrivileges doenst give a file handle to stderr back... How to get it then? And why prints NSLog something sometimes?
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Is it possible to implement http connection for iPhone Development? joyjit_b 2 5,553 Aug 8, 2008 09:13 AM
Last Post: Taxxodium
  java network api San_Andreas 1 3,076 May 1, 2008 08:05 AM
Last Post: AndyKorth
  Ways to Network. BinarySpike 28 12,181 May 10, 2005 07:44 PM
Last Post: kodex
  Network Code Design Bachus 5 4,429 Sep 26, 2004 06:54 PM
Last Post: arcnon
  Network Servers - whose connected to me? Zenith 3 4,568 Oct 2, 2003 04:29 PM
Last Post: Steven