Take the following code example
https://gist.github.com/3825444
/*
Testing arbitrary raw ip packets
works only if datagram is filled with 0
filling with anything else will not send any packets, or atleast wireshark does not detect anything
this is strange
*/
#include<stdio.h>
#include<string.h> //memset
#include<sys/socket.h>
#include<stdlib.h> //for exit(0);
#include<errno.h> //For errno - the error number
#include<netinet/tcp.h> //Provides declarations for tcp header
#include<netinet/ip.h> //Provides declarations for ip header
int main (void)
{
//Create a raw socket
int s = socket (PF_INET, SOCK_RAW, IPPROTO_TCP);
if(s < 0)
{
perror("socket");
}
//Datagram to represent the packet
char datagram[4096] , source_ip[32];
struct sockaddr_in sin;
strcpy(source_ip , "192.168.1.2");
sin.sin_family = AF_INET;
sin.sin_port = htons(80);
sin.sin_addr.s_addr = inet_addr ("1.2.3.4");
memset (datagram, 2 , 4096); /* zero out the buffer */
//IP_HDRINCL to tell the kernel that headers are included in the packet
int one = 1;
const int *val = &one;
if (setsockopt (s, IPPROTO_IP, IP_HDRINCL, val, sizeof (one)) < 0)
{
printf ("Error setting IP_HDRINCL. Error number : %d . Error message : %s \n" , errno , strerror(errno));
exit(0);
}
//Uncommend the loop if you want to flood :)
while (1)
{
//Send the packet
if (sendto (s, /* our socket */
datagram, /* the buffer containing headers and data */
512, /* total length of our datagram */
0, /* routing flags, normally always 0 */
(struct sockaddr *) &sin, /* socket addr, just like in */
sizeof (sin)) < 0) /* a normal send() */
{
perror("sendto");
}
//Data send successfully
else
{
printf ("Packet Send \n");
}
}
return 0;
}
The above program does not generate any packets, or atleast wireshark will not detect any.
However if the datagram is filled with 0 by doing
memset (datagram, 0 , 4096); /* zero out the buffer */
then plenty of packets are generate and are detected by wireshark.
Why such a difference ?
You're putting garbage into the header. It's more remarkable that setting zeros succeeds than that setting 2's fails.
Related
I am using sendto() function in C. I have set the destination address and dest port. While sending UDP frames I can see the frames in Wireshark and the number of packet Wireshark shows are exactly as I have defined in my program.
The problem is even though the destination address is not reachable the frames are being sent and I can see it in Wireshark.
Should not the sendto() function generates a error if the destination IP is not existing?
if (sendto(sockfd, &buffer[i], UDP_FRAME, 0,
(const struct sockaddr*)&server, sizeof(server)) < 0)
{
fprintf(stderr, "Error in sendto()\n");
//return EXIT_FAILURE;
}
Dest. IP: 234.168.0.1
Dest port: 80 or 9 (discard protocol)
#define PORT (80)
#define FRAMES (20000)
#define UDP_FRAME (1442)
#define SERVERADDRESS "234.168.0.1"
#define BUFFER_SIZE (FRAMES * UDP_FRAME)
char buffer[BUFFER_SIZE];
int main(int argc, char **argv)
{
struct timespec start, end, loop_start, loop_end;
int sockfd, count_frame = 0, frames_total, i = UDP_FRAME, n=1;
struct sockaddr_in server;
printf("Build Data...\n");
build(buffer, sizeof(buffer));
printf("Configure socket...\n");
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0)
{
fprintf(stderr, "Error opening socket");
return EXIT_FAILURE;
}
/*----------------------------------------------------*/
/*--- Initialize address protocol ---*/
/*----------------------------------------------------*/
bzero((char*)&server, sizeof(server));
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr(SERVERADDRESS);
server.sin_port = htons(PORT);
/*---------------------------------------------------*/
/*--- S E N D I N G D A T A --*/
/*---------------------------------------------------*/
printf("\nSend UDP data...\n\n");
clock_gettime(CLOCK_MONOTONIC_RAW, &start);
clock_gettime(CLOCK_MONOTONIC_RAW, &loop_start);
frames_total = 0;
for (int i = 0; i < BUFFER_SIZE; i += UDP_FRAME) {
//while(1) {
if (sendto(sockfd, &buffer[i], UDP_FRAME, 0,
(const struct sockaddr*)&server, sizeof(server)) < 0)
{
fprintf(stderr, "Error in sendto()\n");
//return EXIT_FAILURE;
}
count_frame += 1;
clock_gettime(CLOCK_MONOTONIC_RAW, &loop_end);
if ((loop_end.tv_nsec - loop_start.tv_nsec) > 5000000) {
printf("\nCount [%d] ... ", n);
printf("Fames sent: %d\n", count_frame);
frames_total += count_frame;
n+=1;
count_frame = 0;
clock_gettime(CLOCK_MONOTONIC_RAW, &loop_start);
}
}
printf("Total successful counted frames: %d \n", frames_total);
return EXIT_SUCCESS;
}
UDP is an unreliable protocol. A call to sendto is successful once the packet leaves the interface. After that, whether it gets to its destination or not is up to the network.
Even if the network supports ICMP messages stating that the host or port is not reachable, it won't matter in your particular case because you're sending to a multicast address. If you have at least one multicast-capable interface, the system will pick one to send the packet over. It could be received by multiple (or no) hosts. So it doesn't make sense to say that the destination is not reachable.
sendto() will give you an error if the host doesn't know a route to the host (which is almost never the case, since your host will have a default gateway). Otherwise, you might (or might not) receive an ICMP destination unreachable message if your packet did not reach the targeted application, but this is unreliable and won't be communicated by the call to sendto().
What you can do is to query the socket with
struct sock_extended_err err;
socklen_t errlen = sizeof(err);
getsockopt(fd, SOL_IP, IP_RECVERR, &err, &errlen);
for received errors, which will give you detailed information about received errors on the socket (i.e. ICMP port unreachable, ICMP host unreachable, etc. pp). This can help, but as I said, it is not realiable, since ICMP messages are often strictly rate limited, filtered on the way or not sent at all, if your packet is blocked by a packet filter (firewall).
I want my application to create TCP SYN packet and send on network. I did not set IP_HDRINCL using setsockopt because I want the kernel to fill the IP Header.
I tried
using a byte buffer to include TCP Header + Payload
using a byte buffer to include IP Header + TCP Header + Payload.
Both above methods returned sendto error -1 and errno is set to 88
Following is the code I used
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <errno.h>
int main()
{
int sockfd;
if(sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_TCP) < 0)
{
printf("sokcet functin failed : \n");
exit (-1);
}
char packet[512];
struct sockaddr_in remote; // remote address
struct iphdr *ip = (struct iphdr *) packet;
struct tcphdr *tcp = (struct tcphdr *) packet + sizeof(struct iphdr);
/*
struct tcphdr *tcp = (struct tcphdr *) packet; // tcp header
*/
remote.sin_family = AF_INET; // family
remote.sin_addr.s_addr = inet_addr("192.168.0.57"); // destination ip
remote.sin_port = htons(atoi("3868")); // destination port
memset(packet, 0, 512); // set packet to 0
tcp->source = htons(atoi("6668")); // source port
tcp->dest = htons(atoi("3868")); // destination port
tcp->seq = htons(random()); // inital sequence number
tcp->ack_seq = htons(0); // acknowledgement number
tcp->ack = 0; // acknowledgement flag
tcp->syn = 1; // synchronize flag
tcp->rst = 0; // reset flag
tcp->psh = 0; // push flag
tcp->fin = 0; // finish flag
tcp->urg = 0; // urgent flag
tcp->check = 0; // tcp checksum
tcp->doff = 5; // data offset
int err;
if((err = sendto(sockfd, packet, sizeof(struct iphdr), 0, (struct sockaddr *)&remote, sizeof(struct sockaddr))) < 0)
{ // send packet
printf("Error: Can't send packet : %d %d !\n\n", err, errno);
return -1;
}
Why i am getting the sendto error -1 and errno 88?
Also, I want to know how to determine the length of data that is to be used in second argument in sendto function. If I hard code it to size of packet byte buffer i.e. 512 bytes, is it wrong ?
Here I am not giving the solution but
you can check the meaning of your error and can correct your error by and also I can not comment because I haven't much reputation.. I just want to help...
int err = sendto(sockfd, packet, sizeof(struct iphdr), 0, (struct sockaddr *)&remote, sizeof(struct sockaddr)));
int saved_error = errno; // Save errno flag that is set by sendto.
if(err < 0) {
fprintf(stderr, "Send Error: %s\n", strerror(saved_error));
}
I think it may help...But this is not the answer...This will give the meaning of error so that you can correct your error..
For the SOCK_RAW you can use AF_ATMPVC: Access to raw ATM PVCs
In your code:
if((sockfd = socket(AF_ATMPVC, SOCK_RAW, IPPROTO_TCP)) < 0)
I hope it might be helpful
I am trying to write a client/server application using RAW sockets.
There are multiple problems:
When the client sends a message to the server using sendto() method, an error invalid argument is returned by sendto() method. Why this error message?. The corresponding code is marked under the section ERROR 1. The code of sendto() is commented in this post.
Since I have commented the send message part, the client should wait for a message; recvfrom() being a blocking system call. Instead, recvfrom() returns with a message E always. From where did this message arrive?. The corresponding code is marked as ERROR 2.
If I change protocol (3rd) argument in socket() to 0 or IPPROTO_RAW I get Protocol not supported error. Why these errors?
The operating system is Ubuntu.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h> // For the socket () etc. functions.
#include <netinet/in.h> // For IPv4 data struct..
#include <string.h> // For memset.
#include <arpa/inet.h> // For inet_pton ().
#define BUF_SIZE 30
void main ()
{
int rst; // Return status of functions.
/**************** Create a socket. *******************************/
int sfd; // Socket file descriptor.
sfd = socket (AF_INET, SOCK_RAW, IPPROTO_UDP); /*
* AF_INET --> IPv4, SOCK_RAW for Raw socket,
* 0 --> for any protocol. */
if (sfd == -1)
{
perror ("Client: socket error");
exit (1);
}
/*********** Server's address ***********************************/
struct sockaddr_in srv_addr;
socklen_t addrlen = sizeof (struct sockaddr_in);
// Initializing the server's address to zero.
memset (&srv_addr, 0, addrlen);
srv_addr.sin_family = AF_INET; // Address is in IPv4 format.
// srv_addr.sin_port = htons (0); // Port number of the server.
rst = inet_pton (AF_INET, "127.0.0.1", &srv_addr.sin_addr); /* Note
* that third field should point to an in_addr (in6_addr). */
if (rst <= 0)
{
perror ("Client Presentation to network address conversion.\n");
exit (1);
}
/****************** ERROR 1 ************************************
******************* Sending message to the server. *************/
const int flags = 0;
const char *msg = "Hello";
/* rst = sendto (sfd, msg, strlen(msg)+1, flags,
(struct sockaddr *) &srv_addr,
sizeof (struct sockaddr_in));
if (rst < 0)
{
perror ("Client: Sendto function call failed");
exit (1);
}
else
printf ("Client: Sent data size = %d\n", rst);
*/
/******************* ERROR 2 ***********************************
******************* Receiving message from server. ************/
// Initializing the server's address to zero.
memset (&srv_addr, 0, addrlen);
char buf[BUF_SIZE] = {'\0'};
rst = recvfrom (sfd, buf, BUF_SIZE, flags,
(struct sockaddr *) &srv_addr,
&addrlen);
if (rst < 0)
{
perror ("Client: couldn't receive");
exit (1);
}
printf ("Message from server = |%s|\n", buf);
/* Address of the server. */
const char *buf2 = inet_ntop (AF_INET,
(struct sockaddr *) &srv_addr, buf, BUF_SIZE);
if (buf2 == NULL)
{
perror ("Client: Conversion of sender's address to presentation failed");
exit (1);
}
printf ("Servers address, = %s\n", buf2);
close (sfd);
}
SOCK_RAW is not for use with UDP. SOCK_DGRAM is correct. For a tutorial, see:
a tutorial from Rutgers
edit: overlooked the init of the srv_addr... sorry.
using AF_INET + SOCK_RAW socket you can send anything - the payload is just added on top of the IP-layer. the IPPROTO_UDP just tells the kernel what the next layer will be (the layer your payload is added to) and which value the protocol field of the IP header must be set to. so to stay save (if you go to send raw data) set the protocol to something not commonly used).
you need the permission to create a raw socket. this commonly means: start as root, create the socket and then drop the privileges.
q2: this is the message you send to yourself (and a strong indication that your code somehow worked). The 'E' is just the first byte (0x45) in the IP-header - version 4 and header length 5. just dump the whole buffer..., eg.
printf ("Message from server = |");
for (i = 0; i < rst; i++)
printf("%c", isprint(buf[i]) ? buf[i] : '?') ;
printf ("|\n");
q3:
0 means: guess what is usually used (eg. INET + DGRAM -> TCP). As you specified raw the kernel is not able to choose a common protocol for the next layer.
IPPROTO_RAW should work (see comment of #nos)
I am sending TCP SYN packets (with no payload) to a webserver in the same network. I am using sniffex.c
for capturing the packets .
The problem is that after I send a SYN packet, I do not receive the SYN/ACK packet from server.
In sniffex.c:
I have used my LAN IP as the source ip.
I have set the filter as "tcp" .
I am sending to port 80
When i print the fields of the sent packet ,after i capture it using sniffex , all fields are printed correctly, hence i assume that the structure of the sent packet is such that the server can understand it.
When I connect to the webserver using browser, the SYN/ACK is received successfully.
Another related query: how do I set the filter such that I get packets relating to this conversation (b/w my pc and webserver) only
I am using UBUNTU 14.04
EDIT: The c file with which I am trying to send the packet
#define __USE_BSD /* use bsd'ish ip header */
#include <sys/socket.h> /* these headers are for a Linux system, but */
#include <netinet/in.h> /* the names on other systems are easy to guess.. */
#include <netinet/ip.h>
#define __FAVOR_BSD /* use bsd'ish tcp header */
#include <netinet/tcp.h>
#include <unistd.h>
#include<stdio.h>
#include<stdlib.h>
#include<memory.h>
#include<errno.h>
#include<sys/socket.h>
#include<sys/types.h>
#define P 80 /* lets flood the sendmail port */
unsigned short /* this function generates header checksums */
csum (unsigned short *buf, int nwords)
{
unsigned long sum;
for (sum = 0; nwords > 0; nwords--)
sum += *buf++;
sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16);
return ~sum;
}
int
main (void)
{
int s = socket (AF_INET, SOCK_RAW, IPPROTO_TCP);
printf("s=%d\n",s); /* open raw socket */
char datagram[4096]; /* this buffer will contain ip header, tcp header,
and payload. we'll point an ip header structure
at its beginning, and a tcp header structure after
that to write the header values into it */
struct ip *iph = (struct ip *) datagram;
struct tcphdr *tcph = (struct tcphdr *) (datagram + sizeof (struct ip));
struct sockaddr_in sin;
/* the sockaddr_in containing the dest. address is used
in sendto() to determine the datagrams path */
sin.sin_family = AF_INET;
sin.sin_port = htons (P);/* you byte-order >1byte header values to network
byte order (not needed on big endian machines) */
sin.sin_addr.s_addr = inet_addr ("xxx.xxx.xxx.xxx");
memset (datagram, 0, 4096); /* zero out the buffer */
/* we'll now fill in the ip/tcp header values, see above for explanations */
iph->ip_hl = 5;
iph->ip_v = 4;
iph->ip_tos = 0;
iph->ip_len = sizeof (struct ip) + sizeof (struct tcphdr); /* no payload */
iph->ip_id = htonl (54321); /* the value doesn't matter here */
iph->ip_off = 0;
iph->ip_ttl = 255;
iph->ip_p = 6;
iph->ip_sum = 0; /* set it to 0 before computing the actual checksum later */
iph->ip_src.s_addr = inet_addr ("xxx.xxx.xxx.xxx");/* SYN's can be blindly spoofed */
iph->ip_dst.s_addr = sin.sin_addr.s_addr;
tcph->th_sport = htons (2000); /* arbitrary port */
tcph->th_dport = htons (P);
tcph->th_seq = random();/* in a SYN packet, the sequence is a random */
tcph->th_ack = 0;/* number, and the ack sequence is 0 in the 1st packet */
tcph->th_x2 = 5;
tcph->th_off = 5; /* first and only tcp segment */
tcph->th_flags = TH_SYN; /* initial connection request */
tcph->th_win = htonl (65535); /* maximum allowed window size */
tcph->th_sum = 0;/* if you set a checksum to zero, your kernel's IP stack
should fill in the correct checksum during transmission */
tcph->th_urp = 0;
iph->ip_sum = csum ((unsigned short *) datagram, iph->ip_len >> 1);
/* finally, it is very advisable to do a IP_HDRINCL call, to make sure
that the kernel knows the header is included in the data, and doesn't
insert its own header into the packet before our data */
/* lets do it the ugly way.. */
int one = 1;
// const int *val = &one;
if (setsockopt (s, IPPROTO_IP, IP_HDRINCL, &one, sizeof (one)) < 0)
printf ("Warning: Cannot set HDRINCL!\terrno = %d\n",errno);
// while (1)
// {
if (sendto (s, /* our socket */
datagram, /* the buffer containing headers and data */
iph->ip_len, /* total length of our datagram */
0, /* routing flags, normally always 0 */
(struct sockaddr *) &sin, /* socket addr, just like in */
sizeof (sin)) < 0) /* a normal send() */
printf ("error\n");
else
printf ("SUCCESS\n\n\n\n");
//}
char buffer[8192];
memset (buffer, 0, 8192);
int n;
//while(n=read (s, buffer, 8192) > 0)
//{
//printf("n=%d\n",n);
//printf ("Caught tcp packet: %s\n", buffer);
//memset (buffer, 0, 8192);
//}
return 0;
}
[Summary of chat session]
In addition to the iph->ip_off issue, you may need to compute the TCP checksum yourself (your O/S may not do it for you). Useful info is here: http://www.tcpipguide.com/free/t_TCPChecksumCalculationandtheTCPPseudoHeader-2.htm and http://www.netfor2.com/tcpsum.htm
Also tcph->th_seq = htonl(23456); may be useful.
I'm trying write a C program that sends an UDP packet to a given IP adress and waits for an ICMP response of a router telling that the time to live expired. It's kept very simple because I just want to understand the mechanism at first. What I need is someone checking my code giving feedback on what's wrong and what's missing. I'm very unexperienced at C-programming but I have to do it as an assignment - giving my best to understand it...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/ip.h>
// The packet length
#define PCKT_LEN 8192
// Source IP, source port, target IP, target port from the command line arguments
int main(int argc, char *argv[])
{
int send, recv, resp;
int ttl = 1; // Time to live
char buffer[PCKT_LEN];
// Destination address
struct sockaddr_in dst;
// ICMP Response
struct msghdr icmp;
memset(buffer, 0, PCKT_LEN);
// Create a raw socket with UDP protocol
if ((send = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
{
printf("Could not process socket() [send].\n");
return EXIT_FAILURE;
}
// ICMP Socket
if ((recv = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0)
{
printf("Could not process socket() [recv].\n");
return EXIT_FAILURE;
}
dst.sin_family = AF_INET;
dst.sin_addr.s_addr = inet_addr("74.125.39.106");
dst.sin_port = htons(60001);
// Define time to life
if(setsockopt(send, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl)) < 0)
{
printf("Could not process setsockopt().\n");
return EXIT_FAILURE;
}
if(sendto(send, buffer, sizeof(buffer), 0, (struct sockaddr *) &dst, sizeof(dst)) < 0)
{
printf("Could not process sendto().\n");
return EXIT_FAILURE;
}
if((resp = recvmsg(recv, (struct msghdr *) &icmp, IP_RECVERR|MSG_ERRQUEUE)) < 0 )
{
printf("Could not process recvmsg().\n");
return EXIT_FAILURE;
}
close(send);
close(recv);
return 0;
}
I keep receiving "Could not process recvmsg()." and I have no clue anymore what else to try. I'd like to receive an ICMP answer and read its senders IP-Address.
Looking forward to helpfull hints.
Best regards!
I normally use
recvfrom(serversock, buf, 100, 0, (struct sockaddr *)&rcv,&size);
printf("Received packet from %s:%d\nData: %s\n\n", inet_ntoa(rcv.sin_addr), ntohs(rcv.sin_port), buf);