ICMP packet obsolete or malformed - sockets

I'm trying to use RAW sockets to create a ping program. The program I wrote however works only for localhost(127.0.0.1) and does not work for other IP addresses. While analyzing the generated packet in Wireshark, I get a message saying "Unknown ICMP (obsolete or malformed?)". It also tells me that the ICMP checksum is incorrect. I'm posting my code here
char source[20], destination[20];
struct sockaddr_in src, dst;
int addrlen= sizeof(src), recvSize, packetSize;
char *packet;
char *buffer;
struct iphdr* ip;
struct iphdr* ip_reply;
struct icmphdr* icmp;
unsigned short csum(unsigned short *, int);
char* getip();
int main(int argc, char* argv[]){
int pingSocket, optval;
struct protoent *protocol;
if(*(argv + 1) && (!(*(argv + 2)))){
//only one argument provided,assume it is the destination server
strncpy(destination, *(argv + 1), 15);
strncpy(source, getip(), 15);
}
inet_pton(AF_INET, destination, &(dst.sin_addr));
inet_pton(AF_INET, source, &(src.sin_addr));
protocol= getprotobyname("ICMP");
printf("Protocol number for ICMP is %d\n",protocol->p_proto);
pingSocket= socket(AF_INET, SOCK_RAW, protocol->p_proto); // Create socket
if(pingSocket< 0){
perror("Error creating socket: ");
exit(3);
}
printf("Socket created with identifier %d\n", pingSocket);
packetSize= sizeof(struct iphdr) + sizeof(struct icmphdr);
packet = (char *) malloc(packetSize);
buffer = (char *) malloc(packetSize);
ip= (struct iphdr*) packet;
icmp= (struct icmphdr*) (packet+ sizeof(struct iphdr));
memset(packet, 0, packetSize);
//Fill up the IP header
ip->ihl= 5;
ip->version= 4;
ip->tos= 0;
ip->tot_len = htons(packetSize);
ip->id = htons(0);
ip->frag_off= 0;
ip->ttl = 255;
ip->protocol= protocol->p_proto;
ip->saddr= src.sin_addr.s_addr;
ip->daddr= dst.sin_addr.s_addr;
setsockopt(pingSocket, protocol->p_proto, IP_HDRINCL, &optval, sizeof(int)); //HDRINCL to tell the kernel that the header is already included
icmp->type= ICMP_ECHO;
icmp->code= 0;
icmp->un.echo.id= rand();
icmp->un.echo.sequence= rand();
icmp->checksum= 0;
icmp->checksum = csum((unsigned short *)icmp, sizeof(struct icmphdr));
dst.sin_family= AF_INET;
sendto(pingSocket, packet, packetSize, 0, (struct sockaddr *)&dst, sizeof(dst));
printf("Sent ping request with size %d to %s\n", ip->tot_len, destination);
printf("Request's IP ID: %d and IP TTL: %d\n", ip->id, ip->ttl);
printf("Packet: \n%s\n", packet);
// Wait for response
if( (recvSize= recvfrom(pingSocket, buffer, sizeof(struct iphdr)+sizeof(struct icmphdr), 0, (struct sockaddr *)&dst, &addrlen)) < 0){
perror("Receive error: ");
}
else{
printf("Received a reply from %s of size %d\n", destination, recvSize);
ip_reply= (struct iphdr*) buffer;
printf("Reply's IP ID: %d and IP TTL: %d\n", ip_reply->id, ip_reply->ttl);
}
free(packet);
free(buffer);
close(pingSocket);
return 0;
}
unsigned short csum(unsigned short *ptr, int nbytes)
{
register long sum;
u_short oddbyte;
register u_short answer;
sum = 0;
while (nbytes > 1) {
sum += *ptr++;
nbytes -= 2;
}
if (nbytes == 1) {
oddbyte = 0;
*((u_char *) & oddbyte) = *(u_char *) ptr;
sum += oddbyte;
}
sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16);
answer = ~sum;
return (answer);
}
char* getip()
{
char buffer[256];
struct hostent* h;
gethostname(buffer, 256);
h = gethostbyname(buffer);
return inet_ntoa(*(struct in_addr *)h->h_addr);
}
I read somewhere that someone fixed this issue by setting the ICMP checksum to Zero first and then calculating it. This however doesn't work for me. Please help me out :)

Related

Raw socket not receiving UDP packet

I wrote an application using raw sockets that creates a UDP packet and sends to a destination. The application is working fine and I even saw the packet sent using Wireshark. Now, I want that packet to be captured by another application on the destination system. I want to be able to access the UDP header on the destination system. So, I created a receiver using raw sockets on dest system. But, I'm not able to receive the packet to my application. I'm able to capture the packet using SOCK_DGRAM socket, but not with raw socket.
I remember reading that raw sockets doesn't have the concept of ports. Can anyone explain me exactly what's going on the dest system, how the demultiplexing at transport layer works and how the protocol field of ip header effects the transport layer functionality?
Sender code:
int main(void){
char message[] = "This is something very useful";
int sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_UDP);
if(sockfd < 0){
perror("Error creating socket");
exit(1);
}
struct sockaddr_in this, other;
this.sin_family = AF_INET;
other.sin_family = AF_INET;
this.sin_port = htons(9000);
other.sin_port = htons(8000);
this.sin_addr.s_addr = INADDR_ANY;
other.sin_addr.s_addr = inet_addr("127.0.0.1");
if(bind(sockfd, (struct sockaddr *)&this, sizeof(this)) < 0){
printf("Bind failed\n");
exit(1);
}
char packet[64];
memset(packet, 0, 64);
struct udphdr *udph = (struct udphdr *) packet;
strcpy(packet + sizeof(struct udphdr), message);
udph->uh_sport = htons(8080);
udph->uh_dport = htons(8000);
udph->uh_ulen = htons(sizeof(struct udphdr) + sizeof(message));
udph->uh_sum = 0;
if(sendto(sockfd, packet, sizeof(struct udphdr) + sizeof(message), 0, (struct sockaddr *) &other, sizeof(other)) < 0)
perror("Error");
else
printf("Packet sent successfully\n");
close(sockfd);
return 0;
}
Receiver code:
int main(void){
int sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_UDP);
char message[64];
if(sockfd < 0){
perror("Error creating socket");
exit(1);
}
struct sockaddr_in this;
this.sin_family = AF_INET;
this.sin_port = htons(8000);
this.sin_addr.s_addr = INADDR_ANY;
if(bind(sockfd, (struct sockaddr *)&this, sizeof(this)) < 0){
printf("Bind failed\n");
exit(1);
}
if(recv(sockfd, message, 64, 0) < 0){
perror("Error");
exit(1);
}
printf("\n\n%s\n\n", message);
close(sockfd);
return 0;
}

C: Server/client socket program - Client error connecting

I'm new to networking and trying to create a simple client, server socket program in C, where arguments determine whether the program should run as a client or server. I did this by using simple if statements (if a flag is given, run as server, else run as client), but I'm not sure how to test this. I run my code with the argument to be a server in one terminal (on localhost and port number 3000 for example), and open another terminal and run the code with the argument to be a client (also on localhost and the same port).
The expected result is to see the client prompt the user for a message (if connected successfully), and send that message to the server, which prints out the message, however, I don't get the prompt on the client terminal to enter a message.
(I got the code for server and client behavior from one of many websites online, but they separate the client.c and server.c, whereas I want to combine both into one .c program)
Here's my code below, the error is triggered by
if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
in the client section of the code.
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
void error(char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
int sockfd, newsockfd, portno, clilen;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
int n, i, server = 0; // 1 = server, 0 = client
// check if server or client
for (i = 0; i<argc; i++) {
if (strcmp(argv[i], "-l") == 0)
server = 1;
}
// client
if (server == 0) {
struct hostent *server;
if (argc < 3) {
fprintf(stderr,"usage %s hostname port\n", argv[0]);
exit(0);
}
portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
server = gethostbyname(argv[1]);
if (server == NULL) {
fprintf(stderr,"FOUR*** ERROR, no such host\n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
printf("Please enter the message: ");
bzero(buffer,256);
fgets(buffer,255,stdin);
n = write(sockfd,buffer,strlen(buffer));
if (n < 0)
error("ERROR writing to socket");
bzero(buffer,256);
n = read(sockfd,buffer,255);
if (n < 0)
error("ERROR reading from socket");
printf("%s\n",buffer);
return 0;
}
// server
if (server == 1) {
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0)
error("ERROR on binding");
listen(sockfd,5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0)
error("ERROR on accept");
bzero(buffer,256);
n = read(newsockfd,buffer,255);
if (n < 0) error("ERROR reading from socket");
printf("Here is the message: %s\n",buffer);
n = write(newsockfd,"I got your message",18);
if (n < 0) error("ERROR writing to socket");
return 0;
}
}
Here's the exact error output:
In one terminal window, I run the program as a server first:
$ ./socketz -l localhost 2003
Then in another terminal window, I run the program as a client:
$ ./socketz localhost 2003
ERROR connecting: Connection refused
The reason you can't connect is because your server process is not listening on port 2003. In particular, on this line:
serv_addr.sin_port = htons(portno);
The value of portno is zero, which causes the value of serv_addr.sin_port to also be zero, which accept() interprets as meaning that it should just pick an available TCP port to bind to.
The root of the problem is here:
portno = atoi(argv[1]);
... that line assigns a value to portno based on the first argument you entered when running the program, but you entered this:
./a.out -l localhost 2003
So the first argument is "-l", which is a non-number so it will cause atoi() to return 0. I think what you intended was portno = atoi(argv[3]); instead.

send_to returns EINVAL in udp

I am creating a simple file transfer application, but send_to is returning EINVAL when i am trying to send from server side to client, while both send_to and recv_from are working fine on the client side.
port_number, portno, hostname are passed as arguments.
Code to set up server:
portno=port_number;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
optval = 1;
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
(const void *)&optval , sizeof(int));
bzero((char *) &serveraddr, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
serveraddr.sin_port = htons((unsigned short)portno);
if (bind(sockfd, (struct sockaddr *) &serveraddr,
sizeof(serveraddr)) < 0)
error("ERROR on binding");
//getsockname(sockfd, (struct sockaddr *)&clientaddr, &clientlen);
cout<<"server port no"<<serveraddr.sin_port<<endl;
clientlen = sizeof(clientaddr);
Code to set up client:
hostname = name;
portno = port;
/* socket: create the socket */
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
/* gethostbyname: get the server's DNS entry */
server = gethostbyname(hostname);
if (server == NULL)
{
fprintf(stderr,"ERROR, no such host as %s\n", hostname);
exit(-1);
}
/* build the server's Internet address */
memset((char *) &serveraddr,0, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serveraddr.sin_addr.s_addr, server->h_length);
serveraddr.sin_port = htons(portno);
serverlen = sizeof(serveraddr);
struct timeval tv;
tv.tv_sec = 1; // TIMEOUT IN SECONDS
tv.tv_usec = 0; // DEFAULT
if(setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0)
printf("Cannot Set SO_RCVTIMEO for socket\n");
Command that is failing:
if(sendto (sockfd, ack, strlen(ack), 0,(struct sockaddr*)
&clientaddr,sizeof(clientaddr) < 0)
error("ERROR in sending hello_ACK");
where clientadddr is declared as struct sockaddr_in
EINVAL error is generated because of an invalid argument. So you should either initialize the clientaddr as you have done for serveraddr or if you are receiving a HELLO message earlier/ or any other message from the client you can pass clientaddr as an argument to that recvFrom call.
recvfrom(socketFileDescriptor, buffer, bufferSize,0,(struct sockaddr *) &clientaddr, &clientlen))
here the clientlen is sizeof(clientaddr) if you are writing in C.

Not able to make connection to a server process located on different network using in C

I am trying to send messages between two system located on different network using C socket programming.
But when connect() system call initiated it is returning -1 so I am not able to connect to the server.
How can I get connect to a remote server located on different network or different machine. Same program is working when I am using client and server on local machine.
**Client code ----->**
int main(int argc, char *argv[]){
int sockfd,portno,n;
char buffer[256];
struct sockaddr_in serv_addr;
struct hostent *server;
if (argc<3)
error("error port number not provided");
portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd<0)
error("error while creating socket ");
server =(struct hostent *)gethostbyname(argv[1]);
if(server == NULL){
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
//bcopy((char *)server->h_addr,(char *)&serv_addr.sin_addr.s_addr, server->h_length);
serv_addr.sin_port=htons(portno);
if(connect(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr))<0)
error("error while connecting..");
while(strncpy(buffer,"bye",3)!=0){
bzero(buffer,256);
printf("\nYou:");
fgets(buffer,255,stdin);
//n= write(sockfd,buffer,strlen(buffer));
n=send(sockfd,(char*)&buffer,strlen(buffer),0);
if(n<0)
printf("message not delivered\n");
bzero(buffer,256);
//n= read(sockfd,buffer,255);
n= recv(sockfd,buffer,255,0);
printf("\nfrd:%s",buffer);
}
close(sockfd);
}
**Server code -->**
int main(int argc, char *argv[]){
int sockfd,listenfd,portno,clilen,n;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
if (argc<2)
error("error port number not provided");
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd<0)
error("error while creating socket ");
bzero((char*) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if(bind(sockfd,(struct sockaddr*)&serv_addr,sizeof serv_addr)<0)
error("error while binding socket");
listen(sockfd`enter code here`,5);
clilen = sizeof(cli_addr);
if ((listenfd = accept(sockfd, (struct sockaddr*)&cli_addr,&clilen))<0)
error("error while initializing listening");
printf("listening for connections..");
while(strncmp(buffer,"bye",3)!=0){
bzero(buffer,256);
//n= read(listenfd,buffer,255);
n= recv(listenfd,buffer,255,0);
if(n<0)
error("no message");
printf("\nfrd:%s",buffer);
printf("\nyou:");
fgets(buffer,255,stdin);
//n= write(listenfd,buffer,sizeof buffer);
n=send(listenfd,(char*)&buffer,strlen(buffer),0);
if(n<0)
printf("message not sent");
}
close(sockfd);
}
All of client code is the same as that in the server.
server =(struct hostent *)gethostbyname(argv[1]);
if(server == NULL){
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
argv1 contains the name of a host on the Internet, e.g. hsembedded.blogspot.in ;)
The function:
struct hostent *gethostbyname(char *name)
Takes such a name as an argument and returns a pointer to a hostent containing information about that host. The field char *h_addr contains the IP address. If this structure is NULL, the system could not locate a host with this name.
The mechanism by which this function works is complex, often involves querying large databases all around the country.
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
This code sets the fields in serv_addr. Much of it is the same as in the server. However, because the field server->h_addr is a character string, we use the function:
void bcopy(char *s1, char *s2, int length)
which copies length bytes from s1 to s2.
the error is actually due to absence of #include<netdb.h> in client code.

How to send UDP message to loopback address and then read from it?

I have a c program. it first tries to send UDP message to the loopback address, then read from the the loopback.
But first the sendto() function fails with the message "sendto fails: Invalid argument".
The code looks like:
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
int main(void)
{
struct sockaddr_in servaddr;
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
bzero(&servaddr, sizeof(servaddr));
struct in_addr addr;
char dottedaddr[20];
inet_aton("127.0.0.1", &addr);
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = addr.s_addr;
servaddr.sin_port = htons(0);
struct sockaddr_in cliaddr;
inet_aton("192.168.2.12", &addr);
cliaddr.sin_family = AF_INET;
cliaddr.sin_addr.s_addr = addr.s_addr;
cliaddr.sin_port = htons(5000);
if(bind(sockfd, (const struct sockaddr *)&cliaddr, sizeof(cliaddr)) == -1)
{
perror("bind failed");
exit(1);
}
char buf[] = {'h', 'i', ' ', 'i', ' ', 'l', 'o', 'v', 'e', ' ', 'y', 'o', 'u', 0};
if( sendto(sockfd, buf, sizeof(buf), 0, (const struct sockaddr *)&servaddr, sizeof(servaddr)) == -1)
{
perror("sendto fails");
exit(2);
}
fd_set readFd;
FD_ZERO(&readFd);
FD_SET(sockfd, &readFd);
struct timeval timeout;
timeout.tv_sec = 5;
timeout.tv_usec = 0;
int ret = select(sockfd + 1, &readFd, NULL, NULL, &timeout);
if(ret > 0)
{
if(FD_ISSET(sockfd, &readFd))
{
char buf2[21];
struct sockaddr_in from;
int len = sizeof(from);
if(recvfrom(sockfd, buf2, sizeof(buf2), 0, (struct sockaddr *)&from, &len) == -1)
{
perror("recvfrom fails");
}
}
}
else if (ret == 0)
{
printf("select time out \n");
}
else
{
printf("select fails");
}
}
if i change the server port from 0 to 5000, then sendto() can succeed. What is the reason ?
The second question is, after the server port is changed to 5000, the select() cannot detect the socket is readable or not. It simply timeout. I think sockfd should be readable since i just send a message to the loopback address. Is there anything wrong with the code?
thank you!
if i change the server port from 0 to 5000, then sendto() can succeed. What is the reason ?
UDP required packets to have specific source and destination port greater that zero. The only case when you can use zero port is the bind call; in that case socket will be bind on some free non-zero port and future packets from that sockets will use that number as src port.
You always should specify non-zero destination port as a param of sendto() for udp packets.
I think sockfd should be readable since i just send a message to the loopback address.
I am not sure, but it looks like you don't listen loopback. When you bind, you bind only on 192.168.2.12 network interface. You should use INADDR_ANY to bind on all interfaces, including loopback.