RE: Multicast listening
RE: Multicast listening
- Subject: RE: Multicast listening
- From: "Alex Tarter" <email@hidden>
- Date: Wed, 9 Jan 2008 15:29:42 -0000
- Thread-topic: Multicast listening
OK here is the code I'm using, I'm effectively setting up a raw udp
packet, then when I receive a multicast packet I respond with my created
udp packet.
Sure enough when I run wireshark I can see (using ifconfig) that the
PROMISC flag is set, and when I stop wireshark it resets the flag. Is
there a way that I can set the flag in my program, I have looked
everywhere on the net for a way to do it, but apple has stopped you
using the ifconfig en0 promisc command (even if you're running as su).
Thanks,
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in_systm.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/udp.h>
#include <arpa/inet.h>
#define MAXBUFLEN 100
using namespace std;
struct psd_udp {
struct in_addr src;
struct in_addr dst;
unsigned char pad;
unsigned char proto;
unsigned short udp_len;
struct udphdr udp;
char data_buf[500];
};
unsigned short in_cksum (unsigned short *addr, int len) {
int nleft = len;
int sum = 0;
unsigned short *w = addr;
unsigned short answer = 0;
while (nleft > 1) {
sum += *w++;
nleft -= 2;
}
if (nleft == 1) {
*(unsigned char *) (&answer) = *(unsigned char *) w;
sum += answer;
}
sum = (sum >> 16) + (sum & 0xFFFF);
sum += (sum >> 16);
answer = ~sum;
return (answer);
}
unsigned short in_cksum_udp(int src, int dst, unsigned short *addr,
const int len) {
struct psd_udp buf;
//u_char *tempbuf;
int length = len;
memset (&buf, 0, sizeof(buf));
//tempbuf = (u_char *)malloc(length + 12);
//memset (tempbuf, 0, length + 12);
buf.src.s_addr = src;
buf.dst.s_addr = dst;
buf.pad = 0;
buf.proto = IPPROTO_UDP;
buf.udp_len = htons(length);
memcpy(&(buf.udp), addr, length);
for (int i = 0; i < (length - 8); i++) {
buf.data_buf[i] = 0;
}
//memcpy(tempbuf, &buf, sizeof(buf));
return in_cksum((unsigned short *)&buf, length + 12);
}
int main (int argc, char * const argv[]) {
struct ip ip0;
struct udphdr udp0;
int sd0, buffer_sfd;
const int on = 1;
struct sockaddr_in sin0, my_sin, buffer_sin;
u_char *packet0;
struct ip_mreq stmreq0;
char buf[MAXBUFLEN];
int iTmp, numbytes;
socklen_t addr_len;
int PCKT_LEN0 = 0;
PCKT_LEN0 = atoi(argv[1]) + 28;
//Set up circuit
packet0 = (u_char *)malloc(PCKT_LEN0);
ip0.ip_hl = 0x5;
ip0.ip_v = 0x4;
ip0.ip_tos = 0x0;
ip0.ip_len = PCKT_LEN0;
ip0.ip_id = htons(12830);
ip0.ip_off = 0x0;
ip0.ip_ttl = 64;
ip0.ip_p = IPPROTO_UDP;
ip0.ip_sum = 0x0;
ip0.ip_src.s_addr = inet_addr("172.20.255.30");
ip0.ip_dst.s_addr = inet_addr("224.25.0.10");
ip0.ip_sum = in_cksum((unsigned short *) &ip0, sizeof(ip0));
memcpy(packet0, &ip0, sizeof(ip0));
udp0.uh_sport = htons(45512);
udp0.uh_dport = htons(45513);
udp0.uh_ulen = htons(PCKT_LEN0 - 20);
udp0.uh_sum = 0;
udp0.uh_sum = in_cksum_udp(ip0.ip_src.s_addr, ip0.ip_dst.s_addr,
(unsigned short *)&udp0, PCKT_LEN0 - 20);
memcpy(packet0 + 20, &udp0, sizeof(udp0));
if ((sd0 = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) {
perror("raw socket0");
exit(1);
}
if (setsockopt(sd0, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on)) <
0) {
perror("setsockopt0");
exit(1);
}
if ((buffer_sfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("buffer empty socket");
exit(1);
}
iTmp = 1;
setsockopt(buffer_sfd, SOL_SOCKET, SO_REUSEADDR, (char *)&iTmp,
sizeof(iTmp));
my_sin.sin_family = AF_INET;
my_sin.sin_port = htons(48640);
my_sin.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(buffer_sfd, (struct sockaddr *)&my_sin, sizeof(my_sin))
< 0) {
perror("bind");
exit(1);
}
stmreq0.imr_multiaddr.s_addr = inet_addr("239.192.12.0");
stmreq0.imr_interface.s_addr = INADDR_ANY;
setsockopt(buffer_sfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char
*)&stmreq0, sizeof(stmreq0));
addr_len = sizeof(buffer_sin);
//----------------------------------------------------------------------
-----------------------
while (1) {
memset(buf, '\0', sizeof(buf));
if((numbytes = recvfrom(buffer_sfd, buf, MAXBUFLEN-1, 0,
(struct sockaddr *)&buffer_sin, &addr_len)) < 0) {
perror("recvfrom");
exit(1);
}
if (numbytes < 24) {
cout << "Not sent enough: " << numbytes << endl;
} else {
if (sendto(sd0, packet0, PCKT_LEN0, 0, (struct
sockaddr *)&sin0, sizeof(struct sockaddr)) < 0) {
perror("sendto0");
exit(1);
}
}
}
close(sd0);
return 0;
}
Alex Tarter
KTP Associate
Ultra Electronics
Sonar & Communication Systems
Tel: +44 (0)20 8813 4527
Mobile: +44 (0)772 018 2267
www.ultra-electronics.com
-----Original Message-----
From: Hamish Allan [mailto:email@hidden]
Sent: 09 January 2008 15:08
To: Alex Tarter
Cc: Mohacsi Janos; email@hidden
Subject: Re: Multicast listening
On Jan 9, 2008 2:27 PM, Alex Tarter <email@hidden> wrote:
> Thanks for those suggestions, but I'm already doing all of the
commands
> that are listed.
It would probably be most useful if you gave us a version of your code
which included every function call relating to the socket. In
particular, the setsockopt()s are critical!
Hamish
This e-mail from Ultra Electronics Limited and any attachments to it are confidential to the intended recipient and may also be privileged. If you have received it in error please notify the sender and delete it from your system. If you are not the intended recipient you must not copy it or use it for any purpose nor disclose or distribute its contents to any other person. All communications may be subject to interception or monitoring for operational and/or security purposes. Please rely on your own virus checking as the sender cannot accept any liability for any damage arising from any bug or virus infection. Ultra Electronics Limited is a company registered in England and Wales, registration number 2830644. The address of its registered office is 417 Bridport Road, Greenford, Middlesex, UB6 8UA.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden