Receiving broadcast packets using packet socket


 
Thread Tools Search this Thread
Top Forums Programming Receiving broadcast packets using packet socket
# 1  
Old 04-12-2010
Receiving broadcast packets using packet socket

Hello

I try to send DHCP RENEW packets to the network and receive the responses. I broadcast the packet and I can see that it's successfully sent using Wireshark. But I have difficulties receiving the responses.I use packet sockets to catch the packets. I can see that there are responses to my RENEW packet using Wireshark, but my function 'packet_receive_renew' sometimes catch the packets but sometimes it can not catch the packets. I set the file descriptor using FDSET but the 'select' in my code can not realize that there are new packets for that file descriptor and timeout occurs. I couldn't make it clear that why it sometimes catches the packets and sometimes doesn't.
Anybody have an idea?
Thanks in advance.

Here's the receive function.

Code:
int packet_receive_renew(struct client_info* info)
{
	int fd;
	struct sockaddr_ll sock, si_other;
	struct sockaddr_in si_me;
	fd_set rfds;
	struct timeval tv;
	time_t start, end;
	int bcast = 1;

	int ret = 0, try = 0;
	char buf[1500] = {'\0'};
	uint8_t tmp[BUFLEN] = {'\0'};
	struct dhcp_packet pkt;
	socklen_t slen = sizeof(si_other);
	struct dhcps* new_dhcps;
	
	memset((char *) &si_me, 0, sizeof(si_me));
	memset((char *) &si_other, 0, sizeof(si_other));
	memset(&pkt, 0, sizeof(struct dhcp_packet));
	
#define SERVER_AND_CLIENT_PORTS  ((67 << 16) + 68)

	static const struct sock_filter filter_instr[] = {
		/* check for udp */
		BPF_STMT(BPF_LD|BPF_B|BPF_ABS, 9),
		BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, IPPROTO_UDP, 0, 4),     /* L5, L1, is UDP? */
		/* skip IP header */
		BPF_STMT(BPF_LDX|BPF_B|BPF_MSH, 0),                     /* L5: */
		/* check udp source and destination ports */
		BPF_STMT(BPF_LD|BPF_W|BPF_IND, 0),
		BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, SERVER_AND_CLIENT_PORTS, 0, 1), /* L3, L4 */
		/* returns */
		BPF_STMT(BPF_RET|BPF_K, 0x0fffffff ),                   /* L3: pass */
		BPF_STMT(BPF_RET|BPF_K, 0),                             /* L4: reject */
	};

	static const struct sock_fprog filter_prog = {
		.len = sizeof(filter_instr) / sizeof(filter_instr[0]),
		/* casting const away: */
		.filter = (struct sock_filter *) filter_instr,
	};
	
#ifdef DEBUG
	printf("opening raw socket on ifindex %d\n", info->interf.if_index);
#endif
	if (-1==(fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP))))
	{
		perror("packet_receive_renew::socket");
		return -1;
	}
#ifdef DEBUG
	printf("got raw socket fd %d\n", fd);
#endif
		
	/* Use only if standard ports are in use */
	/* Ignoring error (kernel may lack support for this) */
	if (-1==setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter_prog, sizeof(filter_prog)))
		perror("packet_receive_renew::setsockopt");

	sock.sll_family = AF_PACKET;
	sock.sll_protocol = htons(ETH_P_IP);
	//sock.sll_pkttype = PACKET_BROADCAST;
	sock.sll_ifindex = info->interf.if_index;
	if (-1 == bind(fd, (struct sockaddr *) &sock, sizeof(sock))) {
		perror("packet_receive_renew::bind");
		close(fd);
		return -3;
	}
	
	if (-1 == setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &bcast, sizeof(bcast))) {
		perror("packet_receive_renew::setsockopt");
		close(fd);
		return -1;
	}
	
	FD_ZERO(&rfds);
	FD_SET(fd, &rfds);
	tv.tv_sec = TIMEOUT;
	tv.tv_usec = 0;
	ret = time(&start);
	if (-1 == ret) {
		perror("packet_receive_renew::time");
		close(fd);
		return -1;
	}

	while(1) {
		ret = select(fd + 1, &rfds, NULL, NULL, &tv);
		time(&end);
		if (TOTAL_PENDING <= (end - start)) {
#ifdef DEBUG
			fprintf(stderr, "End receiving\n");
#endif
			break;
		}
		if (-1 == ret)
		{
			perror("packet_receive_renew::select");
			close(fd);
			return -4;
		}
		else if (ret) {
			new_dhcps = (struct dhcps*)calloc(1, sizeof(struct dhcps));
			if (-1 == recvfrom(fd, buf, 1500, 0, (struct sockaddr*)&si_other, &slen)) {
				perror("packet_receive_renew::recvfrom");
				close(fd);
				return -4;
			}
			deref_packet((unsigned char*)buf, &pkt, info);
			if (-1!=(ret=get_option_val(pkt.options, DHO_DHCP_SERVER_IDENTIFIER, tmp))) {
				sprintf((char*)tmp, "%d.%d.%d.%d", tmp[0],tmp[1],tmp[2],tmp[3]);
#ifdef DEBUG
				fprintf(stderr, "Received renew from %s\n", tmp);
#endif
			}
			else
			{
#ifdef DEBUG
				fprintf(stderr, "Couldnt get DHO_DHCP_SERVER_IDENTIFIER%s\n", tmp);
#endif
				close(fd);
				return -5;
			}
			new_dhcps->dhcps_addr = strdup((char*)tmp);
			
			//add to list
			if (info->dhcps_list)
				info->dhcps_list->next = new_dhcps;
			else
				info->dhcps_list = new_dhcps;
			new_dhcps->next = NULL;
		}
		else {
			try++;
			tv.tv_sec = TOTAL_PENDING - try * TIMEOUT;
			tv.tv_usec = 0;
#ifdef DEBUG
			fprintf(stderr, "Timeout occured\n");
#endif
		}
	}
#ifdef DEBUG	
	close(fd);
	printf("close fd:%d\n", fd);
#endif
	return 0;
}



---------- Post updated at 07:45 AM ---------- Previous update was at 03:30 AM ----------

I've solved the problem. I think it was an timing issue. I opened the listening socket and bind it before sending the message. So, it can catch the message without problem. Thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help RCon Packets Socket Bash

Hi and thank you in advance for taking the time to help. As I have less than 5 posts it said I had to remove the links you will need to help so good luck! I am having issues building a packet and sending it to a Minecraft RCon server. This is the Minecraft RCon Protocol information can be... (6 Replies)
Discussion started by: Stuperfied
6 Replies

2. AIX

Packet loss coming with big packet size ping

(5 Replies)
Discussion started by: Vishal_dba
5 Replies

3. Programming

Raw Socket Programming - Efficient Packet Sniffer

Hi, I have the requirement to sniff packets from the Ethernet card on my Linux machine and process it and feed it to a RANAP protocol stack. So far I have written the raw packet sniffer and successfully sniffing packets and do little processing. However, for huge number of packets ... (9 Replies)
Discussion started by: rstnsrr
9 Replies

4. Programming

Why am i getting these strange packets while running my packet capture module written in c.?

I have made an packet capture application running on intel machine, it is capturing packets with src address- 17.0.0.0 destination ip- 66.0.0.0, source port- 0, destination port- 0, and protocol- 0 what does these packets mean ? The code written to interpreter captured bytes is given below.... (5 Replies)
Discussion started by: arunpushkar
5 Replies

5. IP Networking

Packets sent from Linux TCP socket

Hello, Our software is using a TCP socket (AF_INET, SOCK_STREAM) to communicate with an Ethernet device. When we send a message, the message object writes itself in full onto the socket's stream buffer before the software invokes send() from socket.h. I'm still researching, but have 2... (1 Reply)
Discussion started by: bix_20002000
1 Replies

6. Programming

Receiving JPEG packet from camera

I am trying to receive a packet of data as bytes in C, but the picture is getting messed up. I am using fwrite to append bytes to jpg file, but the append or write does not seem to be appending jpg correctly. Packet 1 data comes in append to file Packet 2 data comes in append to file ...... (12 Replies)
Discussion started by: photon
12 Replies

7. Programming

How to broadcast a message across the network using Socket programming in C??

My problem definition is ,I have to send a message from one node in a network and it has to be broadcasted to all other nodes in the network.The program what I have given below will be running in all the nodes in the network.The same program should be capable of sending(broadcasting) and receiving.... (1 Reply)
Discussion started by: vigneshinbox
1 Replies

8. Programming

problem receiving data from TCP socket

Hi all, I'm writing a socket program which sends a structure from one machine to another. When I run my client first time it runs well, however after the first time I couldn't receive all the data inside the structure (it is like, half of the array is received and the other half is not set). I... (1 Reply)
Discussion started by: SaTYR
1 Replies

9. IP Networking

Problem Receiving the first OSPF packet

I trying to send and receive OSPF packets. I am using RAW Sockets(socket(AF_INET, SOCK_RAW, IPPROTO_OSPF)) to do this. I am successfully able to send an OSPF Hello packet however I am not able to receive a OSPF packet if I have not sent an OSPF packet earlier on the RAW SOCKET. Scenario: ... (3 Replies)
Discussion started by: cosmic_egg
3 Replies

10. Programming

sendto in packet socket

Hi, I have created a packet socket (PF_PACKET, SOCK_DGRAM, htons(ETH_P_ARP)) to catch the ARP packets coming to my machine and send appropriate reply. Now I'm able to recieve the ARP requests using recvfrom but don't know how to send the reply. I looked into man page but I'm not able to... (5 Replies)
Discussion started by: Rakesh Ranjan
5 Replies
Login or Register to Ask a Question