Thread View: borland.public.cppbuilder.nativeapi
9 messages
9 total messages
Started by Boguslaw Fries
Fri, 07 Mar 2003 12:54
binded address
Author: Boguslaw Fries
Date: Fri, 07 Mar 2003 12:54
Date: Fri, 07 Mar 2003 12:54
22 lines
1343 bytes
1343 bytes
Hello,
If I have a pointer to a opened Socket, could I find the information
about Remote address and Remote Port?
Local Address and Local Port is simple:
SOCKET s;
SOCKADDR_IN name;
szLocalAddress char[ 256 ];
int sockType = 0;
int optlen = 4;
if( (getsockopt( s, SOL_SOCKET, SO_TYPE, ( char * ) &sockType, &optlen ) !=
SOCKET_ERROR )
{
sprintf( szLocalAddress, "%s:%d", inet_ntoa( name.sin_addr ), ntohs(
name.sin_port );
}
Best Regards
Boguslaw Fries
Re: binded address
Author: Remy Lebeau (TeamB)
Date: Fri, 07 Mar 2003 18:27
Date: Fri, 07 Mar 2003 18:27
18 lines
1391 bytes
1391 bytes
"Boguslaw Fries" <B.Fries@luca-online.de> wrote in message news:3e68956a@newsgroups.borland.com... > If I have a pointer to a opened Socket, could I > find the information about Remote address and > Remote Port? Use getpeername() for that. The sockaddr structure is filled in with information about the remote end of the connection. > Local Address and Local Port is simple: <snip> Use getsockname() instead of getsockopt(). Gambit
Re: binded address
Author: Boguslaw Fries
Date: Sat, 08 Mar 2003 11:15
Date: Sat, 08 Mar 2003 11:15
13 lines
1287 bytes
1287 bytes
I did it SOCKADDR_IN name; SOCKADDR_IN speer; getsockname( s, ( SOCKADDR *) &name, &namelen ) gives proper resulsts ( it doeas mena local address and port ) but getpeername( s ( SOCKADDR * ) & speer, &namelen ) gives SOCKET_ERROR and WSAGetLastError gives 10057 Boguslaw Fries
Re: binded address
Author: Michel Leunen
Date: Sat, 08 Mar 2003 11:41
Date: Sat, 08 Mar 2003 11:41
20 lines
1704 bytes
1704 bytes
Boguslaw Fries wrote: > getpeername( s ( SOCKADDR * ) & speer, &namelen ) gives SOCKET_ERROR > and WSAGetLastError gives 10057 Error 10057 means socket not connected. Are you properly connected to your peer address? Do you use connect() before using getpeername(): SOCKET s=socket(AF_INET,SOCK_STREAM,0); ... connect(s,(sockaddr*)&speer,sizeof(SOCKADDR_IN)); getpeername(s,(sockaddr*)&speer,&namelen); Michel -- ---------------------------------------- Michel Leunen mailto:michel@leunen.com http://www.leunen.com/cbuilder/ ----------------------------------------
Re: binded address
Author: Boguslaw Fries
Date: Sat, 08 Mar 2003 11:57
Date: Sat, 08 Mar 2003 11:57
13 lines
1470 bytes
1470 bytes
Problem is more cmplicated I wrote a program which read all socket from the system and I displaz the results something like netstat -a but with process names so shortly I got socket pointer from NtQuerySystemInformation using DuplicateHandle I enumerate the sockets and finally make getsocktename and socketpeer names socket names are correct BUT CONNECTED socket are not!!! Best regards Boguslaw Fries
Re: binded address
Author: Remy Lebeau (TeamB)
Date: Sat, 08 Mar 2003 21:57
Date: Sat, 08 Mar 2003 21:57
19 lines
1620 bytes
1620 bytes
"Boguslaw Fries" <B.Fries@luca-online.de> wrote in message news:3e69dbad@newsgroups.borland.com... > so shortly I got socket pointer from NtQuerySystemInformation How exactly? Looking at the documentaton and declarations for NtQuerySystemInformation() and its structures, I see nothing that returns socket-related information at all. > using DuplicateHandle Sockets are not kernel process objects. You can't duplicate them using kernel functions. I'm surprised DuplicateHandle() would do anything at all. What does your EXACT code look like? Gambit
Re: binded address
Author: Boguslaw Fries
Date: Sat, 08 Mar 2003 23:27
Date: Sat, 08 Mar 2003 23:27
41 lines
2280 bytes
2280 bytes
ok, I write
hCurrentProcess = GetCurrentProcess( );
OpenProcessToken( hCurrentProcess, TOKEN_QUERY | TOKEN:ADJUST_PRIVILEGES,
&hToken );
SetPrivilege( hToken, SE_DEBUG_NAME, true );
NtQuerySystemInformation( SystemHandleInformation, lpSystemInformation,
dwlength, &dwreturnlength );
....
lpSystemHandle = ( LPSYSTEM_HANDLE ) lpSystemInformation->systemHandles;
for( dwCount = 0; dwCount < lpSystemInformation->dwNumEntries; dwCount ++ )
{
hDup = NULL;
hproc = openP1ocess( PROCESS_DUP_HANDLE, 1,
lpSystemHandle->dwProcessId );
if( hproc )
{
DuplicateHandle( hproc, ( HANDLE ) lpSystemHandle ->uhandle,
hCurrentProcess, &hDup, STANDARD_RIGHTS_REQUIRED, false, 0);
if ( hdup )
{
s = ( SOCKET ) hDup;
nRet = getsockname( s, ( SOCKADDR * ) & name &namelen );
...
}
}
}
As above is a part of my code
name has alway proper information about socket name and address --
socket s
but getsockopt returns false protocol type of this socket ( always UDP )
and getpeername always fails wit 10057
thanks for your patience
Boguslaw Fries
Re: binded address
Author: Remy Lebeau (TeamB)
Date: Sun, 09 Mar 2003 01:21
Date: Sun, 09 Mar 2003 01:21
23 lines
2146 bytes
2146 bytes
"Boguslaw Fries" <B.Fries@luca-online.de> wrote in message news:3e6a7d78$2@newsgroups.borland.com... > ok, I write <snip> Your code sample is incomplete. What is lpSystemInformation declared as? What is a SYSTEM_HANDLE? SYSTEM_HANDLE is not documented by Microsoft. And SystemHandleInformation is not listed in NtQuerySystemInformation's supposed classes. In any case, your code is only dealing in process handles, as far as I can tell. A process handle is not a socket handle. They are completey different things from completely different libraries. Even in the off-chance that NtQuerySystemInformation() does return socket handles as well, based on the documentation I was able to find elsewhere, you should be using the SYSTEM_HANDLE's ObjectType member to see what the actual type of handle is being reported. Right now, you're assuming every handle is a socket handle, which is not the case. The OS has many different kinds of handles available. Gambit
Re: binded address
Author: Boguslaw Fries
Date: Sun, 09 Mar 2003 02:07
Date: Sun, 09 Mar 2003 02:07
17 lines
1506 bytes
1506 bytes
typedef struct _SYSTEM_HANDLE
{
dword dwprocessid,
byte bObjType,
Byte gFlags,
ushort uHandle,
dword pObject,
ulong Grantedaccess;
}
SYSTEM_HANDLE;
typedef struct _SSTEM
{
dword dwNumEntries;
SYSTEM_HANDLE systemHandles[ ANY_SIZE] ;
}SYSTEM_HANDLE_INFORMATION;
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads