Re: How can I extract the destination IP address from incoming request

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Open a separate UDP socket for each interface. It's a
good idea from performance standpoint as well - each
socket has its own receive buffer so you can handle
more incoming clients.

--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: agnickolov@xxxxxxxx
MVP VC FAQ: http://vcfaq.mvps.org
=====================================

"Bassam" <Bassam@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:B5EB246F-A4DA-406E-AF7D-EE80133A8F26@xxxxxxxxxxxxxxxx
Dear All,
I am writing a Radius server using winsock2 with win32 C++ on visual C++
2005 express edition on windows xp pro.

My application is listening for all the UDP packets on all the interfaces
on
a multihomed machin. However, I am trying to add a feature to my server
where
I can reject requests incoming from or sent to a specific interface(s).

My question is that how to extract the destination IP address of all the
request targeted to my server so that I can decide whether to further
process
the request or just ignore it.

A snippet pf my code:
------------------------------------------------
ADDRINFO addressHints;
LPADDRINFO radiusResult = NULL, pointerResult = NULL;

//some code here

SecureZeroMemory(&addressHints, sizeof(addressHints));

addressHints.ai_flags = AI_PASSIVE;
addressHints.ai_family = AF_UNSPEC;
addressHints.ai_socktype = SOCK_DGRAM;
addressHints.ai_protocol = IPPROTO_UDP;

//Listen to all the interfaces
if(getaddrinfo(NULL, "1812", &addressHints, &radiusResult) != 0)
{
cerr << "\n\n\nError no." << WSAGetLastError() << ": check getaddrinfo()"
<< endl;
exit(EXIT_FAILURE);
}


//create IPv4 and IPv6 sockets for radius (authentication and
authorization)
//and then bind it to the socket address structures
count = 0;
pointerResult = radiusResult;

while(pointerResult)
{

//create the authentication and authorization sockets for both IPv4 and
IPv6
if((radiusSocket[count] = WSASocket(pointerResult->ai_family,
pointerResult->ai_socktype, pointerResult->ai_protocol, NULL, 0, NULL)) ==
INVALID_SOCKET)
{
cerr << "\n\n\nError no." << WSAGetLastError() << ": check WSASocket()" <<
endl;
exit(EXIT_FAILURE);
}

if(bind(radiusSocket[count], pointerResult->ai_addr,
pointerResult->ai_addrlen)== SOCKET_ERROR)
{
cerr << "\n\n\nError no." << WSAGetLastError() << ": check bind()" <<
endl;
exit(EXIT_FAILURE);
}

count++;
//get the next entry of the winsock cataloge
pointerResult = pointerResult->ai_next;
}

while(true)
{
FD_ZERO(&fdReadSockets);

//Add all the socket handlers to all fd_set
for (count = 0; count < MAX_SOCKETS_ARRAY_SIZE; count++)
{
FD_SET(radiusSocket[count], &fdReadSockets);
FD_SET(accountSocket[count], &fdReadSockets);
}

if ((functionReturnValue = select(0, &fdReadSockets, NULL, NULL,
&selectTimeOut)) == SOCKET_ERROR)
{
cerr << "\n\n\nError no." << WSAGetLastError() << ": check select()" <<
endl;
exit(EXIT_FAILURE);
}

if (functionReturnValue > 0)
{
for (count = 0; count < MAX_SOCKETS_ARRAY_SIZE; count++)
{
//check the data received for authentication and authorization
if (FD_ISSET(radiusSocket[count], &fdReadSockets))
{
//some code here
flags = 0;
clientAddressSize = sizeof(clientAddress);
if ((WSARecvFrom(radiusSocket[count], radiusPacketData,
RADIUS_PACKET_FIELDS, &bytesReceived, &flags,
reinterpret_cast<PSOCKADDR>(&clientAddress), &clientAddressSize, NULL,
NULL)
== SOCKET_ERROR) && (WSAGetLastError() != WSAECONNRESET))
{
cerr << "\n\n\nError no." << WSAGetLastError() << ": check WSARecvFrom()"
<< endl;
exit(EXIT_FAILURE);
}



if (WSASendTo(radiusSocket[count], radiusPacketData, RADIUS_PACKET_FIELDS,
&bytesSent, flags, reinterpret_cast<PSOCKADDR>(&clientAddress),
clientAddressSize, NULL, NULL) == SOCKET_ERROR)
{
cerr << "\n\n\nError no." << WSAGetLastError() << ": check WSASendTo()"
<< endl;
exit(EXIT_FAILURE);
}
}

}

//house keeping stuff

------------------------------------------


Your help is much appreciated

Regards
Bassam


.



Relevant Pages