LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-25-2007, 06:48 PM   #1
fei
Member
 
Registered: Jun 2003
Distribution: Ubuntu, Debian
Posts: 40

Rep: Reputation: 15
Problem with raw IP packets


I'm trying to write a raw socket program where client send a raw IP packet to server and then the server reply a raw IP packet to the client.

Below is the source code of my program. Currently, client can send raw IP packets to the server, but when the server send a reply raw packet to the client, the client can never be able to receive it. I can't understand why. Can you please help with this????

Thanks in advance.


Code:
#include <sys/types.h>	/* basic system data types */
#include <sys/socket.h>	/* basic socket definitions */
#include <sys/time.h>	/* timeval{} for select() */
#include <time.h>		/* timespec{} for pselect() */
#include <netinet/in.h>	/* sockaddr_in{} and other Internet defns */
#include <arpa/inet.h>	/* inet(3) functions */
#include <errno.h>
#include <fcntl.h>		/* for nonblocking */
#include <netdb.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>	/* for S_xxx file mode constants */
#include <sys/uio.h>		/* for iovec{} and readv/writev */
#include <unistd.h>
#include <sys/wait.h>
#include <sys/un.h>		/* for Unix domain sockets */

#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>

#include <sys/ioctl.h>
#include <net/if.h>

#define PORT 999
#define IP_PROTOCOL 253

#define PACKET_NUM 24
#define DATAGRAM_SIZE 512


unsigned short 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 (int argc, char *argv[]) {
	
	int sk = socket(AF_INET, SOCK_RAW, IP_PROTOCOL);
	
	//contains ip header, payload
	//do not include Ethernet head and CRC (kernel will deal with Ethernet head and CRC)
	char datagram[DATAGRAM_SIZE];
	struct ip *iph = (struct ip *) datagram;
	char *payload  = datagram + sizeof(struct ip);

	struct sockaddr_in cli_sin, cli02;
	struct sockaddr_in serv_sin, serv02;


	if(argc != 2) {
		printf("usage: %s -s|-c\n", argv[0]);
		exit(1);
	}	
	
	memset (datagram, 0, sizeof(datagram));	/* zero out the buffer */

	cli_sin.sin_family = AF_INET;
	cli_sin.sin_port = htons (PORT);
	//client has to run on this machine
	cli_sin.sin_addr.s_addr = inet_addr("192.168.0.113");//client ip address
	//cli_sin.sin_addr.s_addr = inet_addr ("127.0.0.1");
	
	serv_sin.sin_family = AF_INET;
	serv_sin.sin_port = htons (PORT);
	//server has to run on this machine
	serv_sin.sin_addr.s_addr = inet_addr("192.168.0.111");//server ip address
	//serv_sin.sin_addr.s_addr = inet_addr ("127.0.0.1");


	iph->ip_hl = 5;
	iph->ip_v = 4;
	iph->ip_tos = 0;
	iph->ip_len = sizeof(datagram);
	iph->ip_id = 54321;
	iph->ip_off = 0;
	iph->ip_ttl = 255;
	iph->ip_p = IP_PROTOCOL;
	iph->ip_sum = 0;

	{
		int on = 1;
		if (setsockopt (sk, IPPROTO_IP, IP_HDRINCL, &on, sizeof (on)) < 0)
			printf ("Warning: Cannot set HDRINCL!\n");
	}


	//if this is client
	if (strcmp(argv[1], "-c") == 0) {

		while (1) {
			static int counter = 0;		
			ssize_t n = 0;
			socklen_t len = DATAGRAM_SIZE;
			
			payload[0] = 'A';
			
			iph->ip_src.s_addr = cli_sin.sin_addr.s_addr;
			iph->ip_dst.s_addr = serv_sin.sin_addr.s_addr;
			
			//no need to do checksum
			//iph->ip_sum = csum ((unsigned short *) datagram, iph->ip_len >> 1);
			
			printf("client sending packet number: %d\n", counter);
			
			if (sendto(sk, datagram, iph->ip_len, 0, 
				(struct sockaddr *) &serv_sin, sizeof (serv_sin)) < 0) 
			{
				printf("client error sendto\n");
			}
			
			n = recvfrom(sk, datagram, sizeof(datagram), 0,
						 (struct sockaddr *)&serv_sin, &len);			 
			if ( n < 0) {
				printf("server error recvfrom\n");	
			}
			printf("Client: ip_p: %d,  n: %d, datagram: %d\n", 
				   (int)iph->ip_p, n, payload[0]);
			
			
			counter++;
			if (counter >= PACKET_NUM) {
				break;	
			}
		}//end of while (1) loop

	
	//end of client	
	
	} else { // server
		socklen_t len = DATAGRAM_SIZE;
		ssize_t n = 0;
		//char buf[DATAGRAM_SIZE];
		
		len = sizeof(cli_sin);
		//struct ip *serv_iph = (struct ip *)buf;

		
		for (; ;) {
			n = recvfrom(sk, datagram, sizeof(datagram), 0,
						 (struct sockaddr *)&cli_sin, &len);
			if ( n < 0) {
				printf("server error recvfrom\n");	
			}
		
			printf("Server: ip_p: %d,  n: %d, datagram: %d\n", 
				   (int)iph->ip_p, n, payload[0]);
			
			payload[0] += 1;
			iph->ip_id++;
			iph->ip_src.s_addr = serv02.sin_addr.s_addr;
			iph->ip_dst.s_addr = cli02.sin_addr.s_addr;
			if (sendto(sk, datagram, iph->ip_len, 0, 
				(struct sockaddr *) &cli_sin, sizeof (cli_sin)) < 0) 
			{
				printf("server error sendto\n");
			}
			
			printf("server, after sending a reply to client\n");
			
		}
		
	} 
	
	return 0;
}
 
Old 01-26-2007, 02:29 PM   #2
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 01-26-2007, 04:59 PM   #3
phoenix_benu
LQ Newbie
 
Registered: Jan 2007
Location: Croatia
Distribution: Debian, Suse, Mandrake (on old computer)
Posts: 9

Rep: Reputation: 0
Post perhaps this could help you

Here is some code I have once written that sniffs packets on
specified interface.
Perhaps this could help you:

Code:
Code:
/*
   Linux packet sniffer.
 */

/* netear.c */

#include <netpacket/packet.h>
#include <netinet/ip_icmp.h>
#include <netinet/udp.h>
#include <netinet/tcp.h>
#include <netinet/ip.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>

#define ALERT "Hustone, we have a problem!"

#define ETH_P_ALL 0x0003
#define ETH_P_IP 0x0800

#define SOL_ICMP 0x1

#define MAX_BUFFER 4096

/* options bit mask:
   ########
   00000[tcp][udp][icmp]
 */

#define OPT_TCP 0x4
#define OPT_UDP 0x2
#define OPT_ICMP 0x1
#define OPT_ALL (OPT_TCP | OPT_UDP | OPT_ICMP)

int usage(const char *s);

/* find interface index of interface iface */
int getifindex(int rawsock,const char *iface);

/* create raw socket */
int creatrawsock(void);

/* enable promiscuouse mode */
int setpromiscuous(int rawsock,int ifindex);

/* do sniffing function */
/* rawsock is raw socket we sniff on,
   ifname is name of interface,
   logf is logging file,
   opts are options
  */
void dosniff(int rawsock,char *ifname,char *logf,unsigned char opts);

int main(int argc, char **argv)
{
	int arg;
	char *iface=NULL; /* interface */
	char *logf=NULL; /* log file */
	unsigned char options=0; /* bit mask */
	int ifindex;

	int rawsock;

	struct sockaddr_ll sll;

	if(argc<2) {
		fprintf(stderr,"%s Too few arguments\n",ALERT);
		exit(1);
	}
	for(arg=1;arg<argc;++arg) {
		if(argv[arg][0]!='-') {
			iface=argv[arg];
		} else if(strlen(argv[arg])>2) {
			fprintf(stderr,"%s Invalid option \"%s\"\n",ALERT,argv[arg]);
			exit(1);
		} else {
			switch(argv[arg][1]) {
				case 'h':
					usage(argv[0]);
					exit(0);
					break;
				case 't':
					options|=OPT_TCP;
					break;
				case 'u':
					options|=OPT_UDP;
					break;
				case 'i':
					options|=OPT_ICMP;
					break;
				case 'l':
					++arg;
					logf=argv[arg];
					break;
				default:
					fprintf(stderr,"%s Invalid option \"%s\"\n",ALERT,argv[arg]);
					exit(1);
			}
		}
	}
	if(options==0) {
		options=OPT_ALL;
	}
	if(iface==NULL) {
		fprintf(stderr,"%s Interface missing\n",ALERT);
		exit(1);
	}

	rawsock=creatrawsock();
	if(rawsock<0) {
		perror("raw socket");
		exit(1);
	}

	ifindex=getifindex(rawsock,iface);

	if(ifindex<0) {
		perror("getifindex");
		close(rawsock);
		exit(1);
	}

	/* this raw socket will receive all ethernet packets */
	memset(&sll,0, sizeof(sll));
	sll.sll_family=AF_PACKET;
	sll.sll_ifindex=ifindex;
	sll.sll_protocol=htons(ETH_P_ALL);

	/* bind rawsock to the interface */
	if(bind(rawsock,(struct sockaddr *)&sll,sizeof(sll))<0) {
		perror("bind");
		close(rawsock);
		exit(1);
	}

	/* enable promiscuous mode */
	if(setpromiscuous(rawsock,ifindex)<0) {
		perror("setpromiscuous");
		close(rawsock);
		exit(1);
	}

	/* sniff */
	dosniff(rawsock,iface,logf,options);

	return 0;
}

int usage(const char *s)
{
	return fprintf(stderr,"usage: %s [OPTIONS] INTERFACE\n"
						   "OPTIONS\n"
						   "   -h   prints this help\n"
						   "   -t   sniff tcp packets (by default sniff all tcp, udp, icmp)\n"
						   "   -u   sniff udp packets (by default sniff all tcp, udp, icmp)\n"
						   "   -i   sniff icmp packets (by default sniff all tcp, udp, icmp)\n"
						   "   -l LOGFILE   set log file (default stdout)\n"
						   "\n"
						   "INTERFACE\n"
						   "   interface name to sniff\n"
						   "\n",
						   s);
}

int getifindex(int rawsock, const char *iface)
{
	struct ifreq ifr;
	int ret;

	memset(&ifr,0,sizeof(ifr));
	strncpy(ifr.ifr_name,iface,sizeof(ifr.ifr_name));

	ret=ioctl(rawsock,SIOCGIFINDEX,&ifr);

	if(ret<0) {
		return ret;
	}

	return ifr.ifr_ifindex;
}

int creatrawsock(void)
{
	return socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_ALL));
}

int setpromiscuous(int rawsock, int ifindex)
{
	struct packet_mreq mr;

	memset(&mr,0,sizeof(mr));
	mr.mr_ifindex=ifindex;
	mr.mr_type=PACKET_MR_PROMISC;

	return setsockopt(rawsock,SOL_PACKET,PACKET_ADD_MEMBERSHIP,&mr,sizeof(mr));
}

void dosniff(int rawsock,char *ifname,char *logf,unsigned char opts)
{
	unsigned char foo[MAX_BUFFER];
	struct sockaddr_ll from;
	int foolen, fromlen;
	FILE *f=stdout;

	struct icmphdr *icmp;
	struct udphdr *udp;
	struct tcphdr *tcp;
	struct iphdr *ip;

	struct in_addr src,dest;

	unsigned short sport, dport;

	if(logf) {
		f=fopen(logf,"a");
		if(f==NULL) {
			fprintf(stderr,"%s Cannot open (append) log file \"%s\"\n",ALERT,logf);
			close(rawsock);
			exit(1);
		}
		fseek(f,0,SEEK_END);
	}

	while(1) {
		fromlen=sizeof(from);

		foolen=recvfrom(rawsock,foo,MAX_BUFFER,0,(struct sockaddr*)&from,&fromlen);
		if(foolen<0) {
			perror("recvfrom");
			close(rawsock);
			if(f!=stdout) {
				fclose(f);
			}
			exit(1);
		}

		/* skip duplicate packets on the loopback interface */
		if( strcmp(ifname,"lo")==0 && from.sll_pkttype==PACKET_OUTGOING) {
			continue;
		}

		/* verify family */
		if(from.sll_family != AF_PACKET) {
			fprintf(f,"%s socket family is \"%d\", should be \"%d\" \"AF_PACKET\"\n",
					ALERT,from.sll_family, AF_PACKET);
		}

		/* only standard IPv4 packets */
		if(ntohs(from.sll_protocol)!=ETH_P_IP) {
			continue;
		}

		/* ip header */
		ip=(struct iphdr*)foo;

		src.s_addr=ip->saddr;
		dest.s_addr=ip->daddr;

		switch(ip->protocol) {
			case SOL_TCP:
				if(!(opts & OPT_TCP)) {
					break;
				}
				fprintf(f,"TCP:");
				tcp=(struct tcphdr *)((void*)ip+sizeof(struct iphdr));
				sport=ntohs(tcp->source);
				dport=ntohs(tcp->dest);

			common:
				fprintf(f,"%15s:%-5d -> ",
						inet_ntoa(src),sport);
				fprintf(f,"%15s:%-5d %4d\n",
						inet_ntoa(dest),dport,
						foolen);
				break;
			case SOL_UDP:
				if(!(opts & OPT_UDP)) {
					break;
				}
				fprintf(f,"UDP:");
				udp=(struct udphdr *)((void*)ip+sizeof(struct iphdr));
				sport=ntohs(udp->source);
				dport=ntohs(udp->dest);
				goto common;
			case SOL_ICMP:
				if(!(opts & OPT_ICMP)) {
					break;
				}
				fprintf(f,"ICMP:");
				icmp=(struct icmphdr *)((void*)ip+sizeof(struct iphdr));

				fprintf(f,"%15s -> ",
						inet_ntoa(src));
				fprintf(f,"%15s type %d code %d    %4d\n",
						inet_ntoa(dest),icmp->type,icmp->code,foolen);
				break;
			default:
				fprintf(f,"%s unsupported IP protocol %d\n",ALERT,ip->protocol);
				break;
		}
	}
}

Last edited by phoenix_benu; 01-26-2007 at 05:08 PM.
 
Old 01-26-2007, 05:27 PM   #4
tovis
Member
 
Registered: Jan 2007
Location: Budapest
Distribution: Debian
Posts: 74

Rep: Reputation: 15
When last time I write raw packet sender program there were problems with check sum. Check packets using ethereal or some other sniffer which can indicate check sum and some other errors.
 
Old 01-28-2007, 02:37 PM   #5
fei
Member
 
Registered: Jun 2003
Distribution: Ubuntu, Debian
Posts: 40

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by phoenix_benu
Here is some code I have once written that sniffs packets on
specified interface.
Perhaps this could help you:

What I want to do is to build a IP packet myself and then send it using raw socket interface between client and server. I have problems when send a IP packet which is build myself. Can you please help me with this?? Thanks!!!

Fei
 
Old 01-29-2007, 02:15 AM   #6
tovis
Member
 
Registered: Jan 2007
Location: Budapest
Distribution: Debian
Posts: 74

Rep: Reputation: 15
I'm not shure, but when I do similar prog, I was always calculate checksums. I think you should recalculate checksum for your packet when you change IP fields and send back the packet. Yes, kernel deal with some check sums but there are more then one - I do not remember which is not calculated by the kernel - if you left it as zero kernel will fill it, but if you put there something then kernel will leave it untouched, even if it wrong!
 
Old 01-30-2007, 04:49 AM   #7
phoenix_benu
LQ Newbie
 
Registered: Jan 2007
Location: Croatia
Distribution: Debian, Suse, Mandrake (on old computer)
Posts: 9

Rep: Reputation: 0
Post

Hi!

I didn't have much time but I managed to change your code so that it works.
Changes are in setting ip addresses into sockaddr_in's and ip header values
inside loops.
You were also using cli02 and serv02 but never setting values to them.

Because I am not network guru I can not tell you why this code works
after placing setting ip header fileds inside loops.

Hope this helps you.

Code:
Code:
#include <sys/types.h>	/* basic system data types */
#include <sys/socket.h>	/* basic socket definitions */
#include <sys/time.h>	/* timeval{} for select() */
#include <time.h>		/* timespec{} for pselect() */
#include <netinet/in.h>	/* sockaddr_in{} and other Internet defns */
#include <arpa/inet.h>	/* inet(3) functions */
#include <errno.h>
#include <fcntl.h>		/* for nonblocking */
#include <netdb.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>	/* for S_xxx file mode constants */
#include <sys/uio.h>		/* for iovec{} and readv/writev */
#include <unistd.h>
#include <sys/wait.h>
#include <sys/un.h>		/* for Unix domain sockets */

#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>

#include <sys/ioctl.h>
#include <net/if.h>

#define PORT 999
#define IP_PROTOCOL 253

#define PACKET_NUM 5
#define DATAGRAM_SIZE 512

#define SERVER_IP "192.168.176.128"
#define CLIENT_IP "192.168.176.1"


unsigned short 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 (int argc, char *argv[]) {
	
	int sk = socket(AF_INET, SOCK_RAW, IP_PROTOCOL);
	
	//contains ip header, payload
	//do not include Ethernet head and CRC (kernel will deal with Ethernet head and CRC)
	char datagram[DATAGRAM_SIZE];
	struct ip *iph = (struct ip *) datagram;
	char *payload  = datagram + sizeof(struct ip);

	struct sockaddr_in cli_sin;
	struct sockaddr_in serv_sin;


	if(argc != 2) {
		printf("usage: %s -s|-c\n", argv[0]);
		exit(1);
	}	

  memset (datagram, 0, sizeof(datagram)); /* zero out the buffer */

	cli_sin.sin_family = AF_INET;
	cli_sin.sin_port = htons (PORT);
	//client has to run on this machine
	//cli_sin.sin_addr.s_addr = inet_addr("192.168.0.113");//client ip address
	cli_sin.sin_addr.s_addr = inet_addr (CLIENT_IP);
	
	serv_sin.sin_family = AF_INET;
	serv_sin.sin_port = htons (PORT);
	//server has to run on this machine
	//serv_sin.sin_addr.s_addr = inet_addr("192.168.0.111");//server ip address
	serv_sin.sin_addr.s_addr = inet_addr (SERVER_IP);

	{
		int on = 1;
		if (setsockopt (sk, IPPROTO_IP, IP_HDRINCL, &on, sizeof (on)) < 0)
			printf ("Warning: Cannot set HDRINCL!\n");
	}


	//if this is client
	if (strcmp(argv[1], "-c") == 0) {
    payload[0] = 'A';
    iph->ip_id = 1;
		while (1) {
			static int counter = 0;
			ssize_t n = 0;
			socklen_t len = DATAGRAM_SIZE;
			
      iph->ip_hl = 5;
      iph->ip_v = 4;
      iph->ip_tos = 0;
      iph->ip_len = sizeof(datagram);
      iph->ip_off = 0;
      iph->ip_ttl = 255;
      iph->ip_p = IP_PROTOCOL;
      iph->ip_sum = 0;
      iph->ip_src.s_addr = cli_sin.sin_addr.s_addr;
      iph->ip_dst.s_addr = serv_sin.sin_addr.s_addr;

      serv_sin.sin_family = AF_INET;
      serv_sin.sin_port = htons (PORT);
      serv_sin.sin_addr.s_addr = inet_addr (SERVER_IP);

			//no need to do checksum
			//iph->ip_sum = csum ((unsigned short *) datagram, iph->ip_len >> 1);
			
			printf("client sending packet number: %d\n", counter);
			
      printf("src: %d.%d.%d.%d, dst: %d.%d.%d.%d\n",
        iph->ip_src.s_addr & 0xff,
        (iph->ip_src.s_addr >> 8) & 0xff,
        (iph->ip_src.s_addr >> 16) & 0xff,
        (iph->ip_src.s_addr >> 24) & 0xff,
        iph->ip_dst.s_addr & 0xff,
        (iph->ip_dst.s_addr >> 8) & 0xff,
        (iph->ip_dst.s_addr >> 16) & 0xff,
        (iph->ip_dst.s_addr >> 24) & 0xff);
			if (sendto(sk, datagram, iph->ip_len, 0,
				(struct sockaddr *) &serv_sin, sizeof (serv_sin)) < 0) 
			{
				printf("client error sendto\n");
			}
			
      serv_sin.sin_family = AF_INET;
      serv_sin.sin_port = htons (PORT);
      serv_sin.sin_addr.s_addr = inet_addr (SERVER_IP);

    	n = recvfrom(sk, datagram, sizeof(datagram), 0,
						 (struct sockaddr *)&serv_sin, &len);			 
			if ( n < 0) {
				printf("client error recvfrom\n");
			}
			printf("Client: ip_p: %d,  n: %d, datagram: %d (%c), id: %d\n", 
				   (int)iph->ip_p, n, payload[0], payload[0], iph->ip_id);
      printf("port: %d\n", ntohs(serv_sin.sin_port));
      printf("src: %d.%d.%d.%d, dst: %d.%d.%d.%d\n",
        iph->ip_src.s_addr & 0xff,
        (iph->ip_src.s_addr >> 8) & 0xff,
        (iph->ip_src.s_addr >> 16) & 0xff,
        (iph->ip_src.s_addr >> 24) & 0xff,
        iph->ip_dst.s_addr & 0xff,
        (iph->ip_dst.s_addr >> 8) & 0xff,
        (iph->ip_dst.s_addr >> 16) & 0xff,
        (iph->ip_dst.s_addr >> 24) & 0xff);
      
			counter++;
			if (counter >= PACKET_NUM) {
				break;	
			}
		}//end of while (1) loop

	
	//end of client	
	
	} else { // server
		socklen_t len = DATAGRAM_SIZE;
		ssize_t n = 0;
		//char buf[DATAGRAM_SIZE];
		
		len = sizeof(cli_sin);
		//struct ip *serv_iph = (struct ip *)buf;

		for (; ;) {
      cli_sin.sin_family = AF_INET;
      cli_sin.sin_port = htons (PORT);
      cli_sin.sin_addr.s_addr = inet_addr (CLIENT_IP);

			n = recvfrom(sk, datagram, sizeof(datagram), 0,
						 (struct sockaddr *)&cli_sin, &len);
			if ( n < 0) {
				printf("server error recvfrom\n");	
			}

      printf("port: %d\n", ntohs(serv_sin.sin_port));

      cli_sin.sin_family = AF_INET;
      cli_sin.sin_port = htons (PORT);
      cli_sin.sin_addr.s_addr = inet_addr (CLIENT_IP);
		
			printf("Server: ip_p: %d,  n: %d, datagram: %d, id: %d\n", 
				   (int)iph->ip_p, n, payload[0], iph->ip_id);
			
			payload[0] += 1;
			iph->ip_id++;
			iph->ip_src.s_addr = serv_sin.sin_addr.s_addr;
			iph->ip_dst.s_addr = cli_sin.sin_addr.s_addr;

      iph->ip_hl = 5;
      iph->ip_v = 4;
      iph->ip_tos = 0;
      iph->ip_len = sizeof(datagram);
      iph->ip_off = 0;
      iph->ip_ttl = 255;
      iph->ip_p = IP_PROTOCOL;
      iph->ip_sum = 0;

			printf("Server: ip_p: %d,  n: %d, datagram: %d (%c), id: %d\n", 
				   (int)iph->ip_p, n, payload[0], payload[0], iph->ip_id);
      printf("src: %d.%d.%d.%d, dst: %d.%d.%d.%d\n",
        iph->ip_src.s_addr & 0xff,
        (iph->ip_src.s_addr >> 8) & 0xff,
        (iph->ip_src.s_addr >> 16) & 0xff,
        (iph->ip_src.s_addr >> 24) & 0xff,
        iph->ip_dst.s_addr & 0xff,
        (iph->ip_dst.s_addr >> 8) & 0xff,
        (iph->ip_dst.s_addr >> 16) & 0xff,
        (iph->ip_dst.s_addr >> 24) & 0xff);

      printf("port: %d\n", ntohs(serv_sin.sin_port));

			if (sendto(sk, datagram, iph->ip_len, 0, 
				(struct sockaddr *) &cli_sin, sizeof (cli_sin)) < 0) 
			{
				printf("server error sendto\n");
			}
			
			printf("server, after sending a reply to client\n");
			
		}
		
	} 
	
	return 0;
}
 
Old 01-30-2007, 07:17 AM   #8
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 57
Checksums can be added directly by the hardware. Sometimes while looking at outgoing packet with a sniffer, you will realize that these checksum are zero. The hardware (PHY or MAC component) will in fine put it on the wire.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Packets gets repeated in raw sockets??????? fpfernando Programming 5 03-08-2006 02:20 AM
How to Capture Raw Packets (no Decode) with PCAP kidskc Programming 1 11-02-2005 04:54 PM
raw packets bassdemon Programming 7 01-28-2005 12:23 AM
Problem sniffing with raw socket warned Linux - Networking 2 08-26-2004 03:07 AM
RAW packets not permitted? (mandrake 9.1) core Linux - Networking 2 08-16-2003 07:48 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration