Click here to Skip to main content
15,881,173 members
Home / Discussions / Hosting and Servers
   

Hosting and Servers

 
AnswerRe: Express checkout validate does not working. Pin
Richard MacCutchan6-Feb-12 23:00
mveRichard MacCutchan6-Feb-12 23:00 
QuestionBuilding DHCP packet ( DHCPDISCOVER ) with C#, packet structure Pin
oorlov6-Feb-12 18:02
oorlov6-Feb-12 18:02 
AnswerRe: Building DHCP packet ( DHCPDISCOVER ) with C#, packet structure Pin
Richard MacCutchan6-Feb-12 22:58
mveRichard MacCutchan6-Feb-12 22:58 
Questioncustom directory listing script in apache/php in shared hosting Pin
Jayapal Chandran2-Feb-12 20:17
Jayapal Chandran2-Feb-12 20:17 
QuestionUnable to copy inside Programme Files Folder Pin
siddisagar2-Feb-12 5:48
siddisagar2-Feb-12 5:48 
AnswerRe: Unable to copy inside Programme Files Folder Pin
Chandrakantt29-Feb-12 5:56
Chandrakantt29-Feb-12 5:56 
QuestionHow to deploy web application on IIS Express 7.5 for local network or internet Pin
Technoses30-Jan-12 23:32
Technoses30-Jan-12 23:32 
QuestionConnectionless socket (Client&Server) programming - ICMP protocol Pin
Lakkan17-Jan-12 2:10
Lakkan17-Jan-12 2:10 
Hi,
I have written connection less socket server app and client app, using ICMP protocol, though client and server are able to communicate with each other..(ServerApp and Client App are running on two different machines on a network....)
Environment that I am working on is : Window7 operating system and Visual studio 2005..langauge:C++/VC++
Socket functions used : Rawsockets, ICMP protocol, bind, sendto, recvfrom.
Objective : The connectionless socket server should able to respond to the client, At the server side I should able see all the values that are sent by the client...vice versa..Also Ethereal Network Analyzer should show correct values..I mean for EchoRequest , it should be EchoRequest, and for EchoReply it should show EchoReply..
Issue1 : Socket Server App is unable to respond with out a IPHeader, I mean, Do I need to send IPHeader along with ICMP Header in a EchoRequest structure as shown below:
Below is the EchoRequest structure that will be send from the client
typedef struct tagECHOREQUEST
{
IPHDR ipHdr; -> //is it required
ICMPHDR icmpHdr;
DWORD dwTime;
char cData[REQ_DATASIZE];
}ECHOREQUEST
Issue2: Though ClientApp and Serever App are able to communicate each other, Ethereal window captures EchoReply for both EchoRequest and EchoReply as shown in the screen shot below..Please let me know why this is happening.. How can I get right message...

Issue3:...(Why this happens, please let me know..) if fill the values for IPHeader and send the EchoRequest to the Server App, though client is able to coomunicate with server, it also reads the correct values into ipReplyHdr(i.e into second IPHDR as shown below in the EchoReply strcture) , but it returns the ICMP source quench message.. Ethereal window also shows the Source Quech (flow control)..,some of the values that are read into IPHDR(i.e First member in EchoReply Structure) are incorrect, not all. it has read all correct values into ICMPHeader...(First IPHeader values are partially correct, second IPHeader values are completly correct, it also read correct values into ICMP Header)..
Issue4:...(Is it correct?) if I send only IPHeader without filling values, Server App receives the messsage, but receiving EchoReply structure should contain two IPHeader members as shown below, where recvfrom function receives values only into ipHdr(some of the value in the IPHdr are incorrect) , then If I readValues into ICMPHDR member,immediately after that, all values are zero.. looks like Ethereal is taking these values into consideration and showing echoreply for EchoRequest..
Filled IPHeader : I mean assigning values to IPHeader members as shown below:
echoReq.ipHdr.Version = 0x4; //IPversion..
echoReq.ipHdr.TOS = 0; // Type Of Service
echoReq.ipHdr.TotLen = htons(sizeof(echoReq)); // Total Length
echoReq.ipHdr.ID = htons(0); // Identification
echoReq.ipHdr.FlagOff = htons(0); // Flags and Fragment Offset
echoReq.ipHdr.TTL = 255; // Time To Live
echoReq.ipHdr.Protocol = IPPROTO_ICMP; // Protocol
echoReq.ipHdr.Checksum = 0; // Checksum
echoReq.ipHdr.iaSrc.S_un.S_addr = inet_addr(strSourceIpAddr); //SourceAddr
echoReq.ipHdr.iaDst.S_un.S_addr = inet_addr(strIpAddr); // DestinationAddr
echoReq.ipHdr.Checksum = in_cksum((u_short*)&echoReq.ipHdr,sizeof (echoReq.ipHdr));
Only IPHeader, I mean with out assigning any of those above values, I just would be sending only member of IPHEADER..
// ICMP Echo Reply..
typedef struct tagECHOREPLY
{
IPHDR ipHdr;
IPHDR ipReplyHdr;
ICMPHDR icmpHdr;
DWORD dwTime;
char cData[REQ_DATASIZE];
char cFiller[256];
}ECHOREPLY,*PECHOREPLY;
Issue5: (How does this work...)When Server App sends the EchoReply back to the Client App, Client App receives the message succesully, in this case, I am not sending any IPHEADER, as I did for ECHOREQUEST..
I just think, problem will be solved, if recvfrom function on the server side is able to respond to sendTo function, without having IPHeader member into theEchoRequest structure, as of now I am including IPHeader member into the EchoRequest structure, but recvfrom function on the server side does not respond to sendTo function without having IPHeader. (EchoRequest structure is shown above..)..
Also I have attached code for both server and client at the bottom..
Below I have placed entire code both from Server App and Client App, First one is server side code(both header file and cpp file, later one is server code(both header and cpp file)..
ClientApp (Header file)..
<pre lang="c++">
#include "stdafx.h"
#include <string>
#include <winsock2.h>
#include <Ws2tcpip.h>
using namespace std;
#define REQ_DATASIZE 27
#define ICMP_ECHOREPLY 0
#define ICMP_ECHOREQ 8
#pragma pack(1)
typedef struct tagIPHDR
{
unsigned char Version;
unsigned char TOS;
unsigned short TotLen;
unsigned short ID;
unsigned short FlagOff;
unsigned char TTL;
unsigned char Protocol;
unsigned short Checksum;
struct in_addr iaSrc; // Internet Address - Source
struct in_addr iaDst; // Internet Address - Destination
} IPHDR, *PIPHDR;
typedef struct tagICMPHDR
{
unsigned char Type;
unsigned char Code;
unsigned short Checksum;
unsigned short ID;
unsigned short Seq;
char cData;
}ICMPHDR, *PICMPHDR;
//ICMP Echo Request..
typedef struct tagECHOREQUEST
{
IPHDR ipHdr;
ICMPHDR icmpHdr;
DWORD dwTime;
char cData[REQ_DATASIZE];
}ECHOREQUEST, *PECHOREQUEST;
//ICMP Echo Reply..
typedef struct tagECHOREPLY
{
IPHDR ipHdr;
ICMPHDR icmpHdr;
DWORD dwTime;
char cData[REQ_DATASIZE];
char cFiller[256];
}ECHOREPLY,*PECHOREPLY;
//Here it creates the SOCKET object..
SOCKET m_rawSocket;
sockaddr_in objSenderAddr;
//function declarations
bool SendEchoRequest(string& strIPAddress,SOCKET& objRawSocket);
bool EchoReceiveMessage(SOCKET& objRawSocket);
#pragma pack()
ClientApp (Source File)..
// ICMPTestClient.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include "ICMPTestClient.h"
#pragma comment(lib, "ws2_32.lib")
using namespace std;
#define IPPROTO_ICMP 1
#define IPPROTO_IP 0
u_short in_cksum(u_short *addr, int len);
int main(int argc, void* argv[])
{
//Here it gets the server address from the user
string strServerAddress;
cout << "Please enter server address:\n";
cin >> strServerAddress;

//Here intilizes the socket environment...
SOCKET objRawSocket;

WSADATA wsaData;
WORD wVersionRequested = MAKEWORD(2,0);
//init Winsock session will be called only once..
int startup = WSAStartup(wVersionRequested,&wsaData);
if(startup)
{
cout << "Errot initializing winsock\n";
return 0;
}
//if requested version does not match the
if(wsaData.wVersion != wVersionRequested)
{
cout << "Error winsock version not supported\n";
return 0;
}
objRawSocket = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
if(SOCKET_ERROR == objRawSocket)
{
cout << "Failed while creating a socket\n";
objRawSocket = NULL;
return 0;
}
//Here it sends echo request..
SendEchoRequest(strServerAddress,objRawSocket);
//Here it receives echo response..
EchoReceiveMessage(objRawSocket);
//Here it closes the socket...
closesocket(objRawSocket);
WSACleanup();
getchar();
getchar();
return 0;
}
//Here it sends the echorequest to the server
bool SendEchoRequest(string& strIPAddress,SOCKET& objRawSocket)
{
bool bIsEchoSent = true;

try{
static ECHOREQUEST echoReq;
int nId = 1;
int nSeq = 1;
int nRet,nIndex;

//Fill in echo request..
echoReq.icmpHdr.Type = ICMP_ECHOREQ;
echoReq.icmpHdr.Code = 0;
echoReq.icmpHdr.Checksum = 0;
echoReq.icmpHdr.ID = nId++;
echoReq.icmpHdr.Seq = nSeq++;
//Fill in some data to send..
for(nIndex = 0; nIndex < REQ_DATASIZE-1; nIndex++)
{
echoReq.cData[nIndex] = 'a'+nIndex;
}
//save tick count when sent..
echoReq.dwTime = GetTickCount();
//Put data in packet and compute checksum
echoReq.icmpHdr.Checksum = in_cksum((u_short*)&echoReq,sizeof(ECHOREQUEST));
//Send the echo request..
sockaddr_in objSockDest;
objSockDest.sin_addr.s_addr = ::inet_addr(strIPAddress.c_str());
objSockDest.sin_family = AF_INET;
objSockDest.sin_port = 0;

nRet = sendto(objRawSocket, //socket
(const char*)&echoReq, //buffer
sizeof(ECHOREQUEST),
0, //flags
(SOCKADDR*)&objSockDest, //destination
sizeof(objSockDest)); //address length

}
catch(...)
{
bIsEchoSent = false;
}
return bIsEchoSent;
}
//Here it receives the echo message from the server...
bool EchoReceiveMessage(SOCKET& objRawSocket)
{
ECHOREPLY echoReply;
int nAddrLen = sizeof(struct sockaddr_in);
int nRet;
sockaddr_in objServerAddress;

if((nRet = recvfrom(objRawSocket,(LPSTR)&echoReply,sizeof(ECHOREPLY),0,(struct sockaddr *)&objServerAddress,
&nAddrLen)) > 0)
{
char* strInetAddr = inet_ntoa(objServerAddress.sin_addr);
cout << "Echo is received from..:"<<strInetAddr <<"\n";

}
else if(nRet == SOCKET_ERROR)
{
cout << "Failure while receiving data:\n" << WSAGetLastError();
return false;
}
return true;
}
//Check sum is done for ICMP Request..
u_short in_cksum(u_short *addr, int len)
{
register int nleft = len;
register u_short *w = addr;
register u_short answer;
register int sum = 0;
/*
* Our algorithm is simple, using a 32 bit accumulator (sum),
* we add sequential 16 bit words to it, and at the end, fold
* back all the carry bits from the top 16 bits into the lower
* 16 bits.
*/
while( nleft > 1 ) {
sum += *w++;
nleft -= 2;
}
/* mop up an odd byte, if necessary */
if( nleft == 1 ) {
u_short u = 0;
*(u_char *)(&u) = *(u_char *)w ;
sum += u;
}
/*
* add back carry outs from top 16 bits to low 16 bits
*/
sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
sum += (sum >> 16); /* add carry */
answer = ~sum; /* truncate to 16 bits */
return (answer);
} Server Header File

// ICMPEchoServer.h : header file
#include "stdafx.h"
#define REQ_DATASIZE 27
#define ICMP_ECHOREPLY 0
#define ICMP_ECHOREQ 8
#define SM_EVENT WM_USER+192
#include <winsock2.h>
#include <Ws2tcpip.h>
#pragma pack(1)
typedef struct tagIPHDR
{
u_char version; // version number
u_char TOS; // Type Of Service
u_short TotLen; // Total Length
u_short ID; // Identification
u_short FlagOff; // Flags and Fragment Offset
u_char TTL; // Time To Live
u_char Protocol; // Protocol
u_short Checksum; // Checksum
struct in_addr iaSrc; // Internet Address - Source
struct in_addr iaDst; // Internet Address - Destination
}IPHDR, *PIPHDR;
// ICMP Header - RFC 792
typedef struct tagICMPHDR
{
u_char Type; // Type
u_char Code; // Code
u_short Checksum; // Checksum
u_short ID; // Identification
u_short Seq; // Sequence
char Data; // Data
}ICMPHDR, *PICMPHDR;
#define REQ_DATASIZE 27 // Echo Request Data size
// ICMP Echo Reply..
typedef struct tagECHOREPLY
{
IPHDR ipHdr;
IPHDR ipReplyHdr;
ICMPHDR icmpHdr;
DWORD dwTime;
char cData[REQ_DATASIZE];
char cFiller[256];
}ECHOREPLY,*PECHOREPLY;
//ICMP Echo Request...
typedef struct tagECHOREQUEST
{
ICMPHDR icmpHdr;
DWORD dwTime;
char cData[REQ_DATASIZE];
}ECHOREQUEST, *PECHOREQUEST;
//Here it is object client address...
sockaddr_in objClientAddress;
sockaddr_in objServAddress;
//Here it initializes the socket..
bool OnInitializeSocketENV();
//Here it creates the socket..
bool OnCreateSocket(SOCKET& rawSocket);
//Here it binds the socket..
bool BindSocket(SOCKET& rawSocket);
//Here it receives the echo message
bool RecvEchoMessage(SOCKET& rawSocket);

#pragma pack()

Server CPP file


// ICMPEchoServer.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "ICMPEchoServer.h"
#pragma comment(lib, "ws2_32.lib")
#define IPPROTO_ICMP 1
//Here it calculates the checksum algorithm
u_short in_cksum(u_short *addr, int len);

//int _tmain(int argc, _TCHAR* argv[])
int main(int argc, void* argv[])
{
//Here it is a instance os a socket..
SOCKET rawSocket;
//Here it initializes the socket environment..
if(!OnInitializeSocketENV()) return 0;
//Here it creates the socket..
if(!OnCreateSocket(rawSocket)) return 0 ;
//Here it binds the socket..
if(!BindSocket(rawSocket)) return 0;

printf("Socket is listening..\n");
//Here it receives the echo response..
if(!RecvEchoMessage(rawSocket)) return 0;

closesocket(rawSocket);
WSACleanup();
getchar();

return 0;
}
//Here it initializes the socket environment...
bool OnInitializeSocketENV()
{
WSADATA wsaData;
WORD wVersionRequested = MAKEWORD(2,0);
//init Winsock session will be called only once..
static int startup = WSAStartup(wVersionRequested,&wsaData);
if(startup)
{
printf("Errot initializing winsock\n");
return false;
}
//Here it is checking for version requested...
if(wsaData.wVersion != wVersionRequested)
{
printf("Error winsock version not supported\n");
return false;
}
return true;
}
//Here it creates the socket object...
bool OnCreateSocket(SOCKET& rawSocket)
{
rawSocket = socket(AF_INET, SOCK_RAW,IPPROTO_ICMP);
if(rawSocket == NULL)
{
printf("Socket creation failre..\n");
return false;
}
return true;
}
//Here it binds the socket..
bool BindSocket(SOCKET& rawSocket)
{
//Here preparation is done to bind the socket.
objServAddress.sin_addr.s_addr = INADDR_ANY;
objServAddress.sin_family = AF_INET;
objServAddress.sin_port = 0;
//Here it binds the socket...
int nRetValue = bind(rawSocket,(struct sockaddr*)&objServAddress,sizeof(objServAddress));
if(nRetValue == SOCKET_ERROR)
{
printf("Socket binding is failed\n");
closesocket(rawSocket);
rawSocket = NULL;
return false;
}
return true;
}
//Here it receives the echo message
bool RecvEchoMessage(SOCKET& rawSocket)
{
ECHOREPLY echoReply;

int nAddrLen = sizeof(struct sockaddr_in);
int nRet;

//char on=1;
//setsockopt(rawSocket,IPPROTO_IP,IP_HDRINCL,&on,sizeof(on));
if((nRet = recvfrom(rawSocket,(LPSTR)&echoReply,sizeof(ECHOREPLY),0,(struct sockaddr *)&objClientAddress,
&nAddrLen)) > 0)
{
printf("\nEcho is received from..: %s\n",inet_ntoa(objClientAddress.sin_addr));
printf("\n..............IPHEADER INFO.........................\n");
printf("size of IPHeder=%d\n",sizeof(echoReply.ipHdr));
printf("IP Header Version :%d\n",echoReply.ipHdr.version);
printf("Type of service :%d\n",echoReply.ipHdr.TOS);
printf("TTL :%d\n",echoReply.ipHdr.TTL);
//Here it sends the echo request..
if(echoReply.ipHdr.Protocol == IPPROTO_ICMP)
printf("It is a ICMP protocol %d\n",echoReply.ipHdr.Protocol);
else
printf("It is not a ICMP protocol:%d\n",echoReply.ipHdr.Protocol);

printf("Here is source address :%s\n",inet_ntoa(echoReply.ipHdr.iaSrc));
printf("Here is destination address:%s\n",inet_ntoa(echoReply.ipHdr.iaDst));

printf("\n\n..............ICMP HEADER INFO.........................\n");
printf("Message Type...: %d\n",echoReply.icmpHdr.Type);
printf("Message Code...: %d\n",echoReply.icmpHdr.Code);
printf("ID.............: %d\n",echoReply.icmpHdr.ID);
printf("SEQ............: %d\n",echoReply.icmpHdr.Seq);
printf("CHECKSUM.......: %d\n",echoReply.icmpHdr.Checksum);
printf("DATA sent......: %s\n",echoReply.cData);

//Here it constructs the echo reply and sends the same back to the client
ECHOREQUEST echoReq;
int nId = 1;
int nSeq = 1;
int nRet,nIndex;

//Fill in echo request..
echoReq.icmpHdr.Type = ICMP_ECHOREPLY;
echoReq.icmpHdr.Code = 0;
echoReq.icmpHdr.Checksum = 0;
echoReq.icmpHdr.ID = nId++;
echoReq.icmpHdr.Seq = nSeq++;
//Fill in some data to send..
for(nIndex = 0; nIndex < REQ_DATASIZE-1; nIndex++)
{
echoReq.cData[nIndex] = 'a'+nIndex;
}
//save tick count when sent..
echoReq.dwTime = GetTickCount();
//Put data in packet and compute checksum
echoReq.icmpHdr.Checksum = in_cksum((u_short*)&echoReq,sizeof(ECHOREQUEST));

if((nRet = sendto(rawSocket,(LPSTR)&echoReq,sizeof(ECHOREQUEST),0,(struct sockaddr *)&objClientAddress,
nAddrLen)) > 0)
{
printf("\nReply is sent to...:%s\n",inet_ntoa(objClientAddress.sin_addr));
}
}
if(nRet == SOCKET_ERROR)
{
int nError = WSAGetLastError();
printf("Failure while sending and receiving data, Error:%d\n",nError);
return false;
}
printf("Received value is...:%d",nRet);
return true;
}
//Check sum is done for ICMP Request..
u_short in_cksum(u_short *addr, int len)
{
register int nleft = len;
register u_short *w = addr;
register u_short answer;
register int sum = 0;
/*
* Our algorithm is simple, using a 32 bit accumulator (sum),
* we add sequential 16 bit words to it, and at the end, fold
* back all the carry bits from the top 16 bits into the lower
* 16 bits.
*/
while( nleft > 1 ) {
sum += *w++;
nleft -= 2;
}
/* mop up an odd byte, if necessary */
if( nleft == 1 ) {
u_short u = 0;
*(u_char *)(&u) = *(u_char *)w ;
sum += u;
}
/*
* add back carry outs from top 16 bits to low 16 bits
*/
sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
sum += (sum >> 16); /* add carry */
answer = ~sum; /* truncate to 16 bits */
return (answer);
}


//Here it waits for echo reply.
int WaitForEchoReply(SOCKET& objRawsocket);
//Here it waits for echo reply...
int WaitForEchoReply(SOCKET& objRawsocket)
{
struct timeval Timeout;
fd_set readfds;

readfds.fd_count = 1;
readfds.fd_array[0] = objRawsocket;

Timeout.tv_sec = 5;
Timeout.tv_usec = 0;

int nSelect = select(0, &readfds, NULL, NULL, NULL);
int nErrorCode = WSAGetLastError();
printf("Select Error Code:\n",nErrorCode);

return(select(1, &readfds, NULL, NULL, NULL));
}

</pre>

Please let me know, if you require some more information, I would be glad to provide ASAP..
Best Regards..
Lakkan.

-- modified 25-Jan-12 2:01am.
QuestionIIS Web sharing is blocked in the network Pin
yesu prakash7-Jan-12 18:14
yesu prakash7-Jan-12 18:14 
QuestionAutomatically Start App on Citrix WI Login Pin
MWRivera28-Dec-11 4:49
MWRivera28-Dec-11 4:49 
AnswerRe: Automatically Start App on Citrix WI Login Pin
DaveAuld1-Jan-12 20:19
professionalDaveAuld1-Jan-12 20:19 
QuestionHow do I configure hMailServer for relaying mail sent to local SMTP onward to GMail? Pin
Brady Kelly27-Dec-11 3:02
Brady Kelly27-Dec-11 3:02 
QuestionSuggestion / Clarrification Needed. Pin
Hema Bairavan19-Dec-11 19:58
Hema Bairavan19-Dec-11 19:58 
SuggestionRe: Suggestion / Clarrification Needed. Pin
AprNgp29-Dec-11 18:40
AprNgp29-Dec-11 18:40 
QuestionMillettech Information and Technology Co., Ltd. Pin
millettech16-Dec-11 20:26
millettech16-Dec-11 20:26 
AnswerRe: Millettech Information and Technology Co., Ltd. Pin
thatraja16-Dec-11 20:43
professionalthatraja16-Dec-11 20:43 
GeneralRe: Millettech Information and Technology Co., Ltd. Pin
Eddy Vluggen17-Dec-11 1:54
professionalEddy Vluggen17-Dec-11 1:54 
QuestionCitrix Client - The location of Menu / Contest ment strip Pin
prashantkharade13-Dec-11 2:29
prashantkharade13-Dec-11 2:29 
QuestionSuggestions Require Pin
uspatel6-Dec-11 20:50
professionaluspatel6-Dec-11 20:50 
AnswerRe: Suggestions Require Pin
DaveAuld7-Dec-11 3:49
professionalDaveAuld7-Dec-11 3:49 
GeneralRe: Suggestions Require Pin
uspatel20-Dec-11 23:29
professionaluspatel20-Dec-11 23:29 
GeneralRe: Suggestions Require Pin
AprNgp29-Dec-11 18:41
AprNgp29-Dec-11 18:41 
GeneralRe: Suggestions Require Pin
uspatel1-Jan-12 19:16
professionaluspatel1-Jan-12 19:16 
GeneralRe: Suggestions Require Pin
AprNgp2-Jan-12 5:50
AprNgp2-Jan-12 5:50 
Questionsocket prograaming doubt! Pin
robin70019-Nov-11 10:05
robin70019-Nov-11 10:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.