View Full Version : Getting network connection for a specified PID
Florian
2006.12.09, 09:23 AM
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:
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:
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 :)
OneSadCookie
2006.12.09, 06:25 PM
UDP isn't a connection-based protocol.
Florian
2006.12.10, 04:36 AM
So there is no simple way of getting the Server-IP?
OneSadCookie
2006.12.10, 05:19 AM
Only by watching the traffic to see where it's going.
Florian
2006.12.23, 05:05 AM
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:
- (void)awakeFromNib
{
OSStatus myStatus;
AuthorizationFlags myFlags=kAuthorizationFlagDefaults;
AuthorizationRef myAuthorizationRef;
myStatus=AuthorizationCreate(NULL,kAuthorizationEm ptyEnvironment,myFlags,&myAuthorizationRef);
if(myStatus==errAuthorizationSuccess){
AuthorizationItem myItems={kAuthorizationRightExecute,0,NULL,0};
AuthorizationRights myRights={1,&myItems};
myFlags=kAuthorizationFlagDefaults| kAuthorizationFlagInteractionAllowed| kAuthorizationFlagPreAuthorize| kAuthorizationFlagExtendRights;
myStatus=AuthorizationCopyRights(myAuthorizationRe f,&myRights,NULL,myFlags,NULL);
if(myStatus==errAuthorizationSuccess){
FILE*myCommunicationsPipe=NULL;
char*myArguments[]={ "-t",NULL};
NSString *tool=@"/usr/sbin/tcpdump";
myFlags=kAuthorizationFlagDefaults;
myStatus=AuthorizationExecuteWithPrivileges(myAuth orizationRef,[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 :)
OneSadCookie
2006.12.23, 05:22 AM
Maybe it's printing to standard error?
Florian
2006.12.23, 05:46 AM
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?
vBulletin® v3.6.8, Copyright ©2000-2008, Jelsoft Enterprises Ltd.