How can I extract the destination IP address from incoming request
- From: Bassam <Bassam@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Mon, 8 Jan 2007 21:32:02 -0800
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
.
- Follow-Ups:
- Re: How can I extract the destination IP address from incoming request
- From: Alexander Nickolov
- Re: How can I extract the destination IP address from incoming request
- From: Arkady Frenkel
- Re: How can I extract the destination IP address from incoming request
- Prev by Date: How to convert two byte sequence to an unsigned short.
- Next by Date: RE: wlanapi.h :cannot compile
- Previous by thread: How to convert two byte sequence to an unsigned short.
- Next by thread: Re: How can I extract the destination IP address from incoming request
- Index(es):
Relevant Pages
|