Plan 9 from Bell Labs’s /usr/web/sources/extra/9hist/ip/igmp.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


## diffname ip/igmp.c 1997/0423
## diff -e /dev/null /n/emeliedump/1997/0423/sys/src/brazil/ip/igmp.c
0a
#include "u.h"
#include "../port/lib.h"
#include "mem.h"
#include "dat.h"
#include "fns.h"
#include "../port/error.h"

#include "ip.h"

enum
{
	IGMP_IPHDRSIZE	= 20,		/* size of ip header */
	IGMP_HDRSIZE	= 8,		/* size of IGMP header */
	IP_IGMPPROTO	= 2,

	IGMPquery	= 1,
	IGMPreport	= 2,

	MSPTICK		= 100,
	MAXTIMEOUT	= 10000/MSPTICK,	/* at most 10 secs for a response */
};

typedef struct IGMPpkt IGMPpkt;
struct IGMPpkt
{
	/* ip header */
	byte	vihl;		/* Version and header length */
	byte	tos;		/* Type of service */
	byte	len[2];		/* packet length (including headers) */
	byte	id[2];		/* Identification */
	byte	frag[2];	/* Fragment information */
	byte	Unused;	
	byte	proto;		/* Protocol */
	byte	cksum[2];	/* checksum of ip portion */
	byte	src[Ipaddrlen];		/* Ip source */
	byte	dst[Ipaddrlen];		/* Ip destination */

	/* igmp header */
	byte	vertype;	/* version and type */
	byte	unused;
	byte	igmpcksum[2];		/* checksum of igmp portion */
	byte	group[Ipaddrlen];	/* multicast group */
};

/*
 *  lists for group reports
 */
typedef struct IGMPrep IGMPrep;
struct IGMPrep
{
	IGMPrep		*next;
	Media		*m;
	int		ticks;
	Multicast	*multi;
};

typedef struct IGMP IGMP;
struct IGMP
{
	Lock;
	Rendez	r;
	Rendez	r0;
	IGMPrep	*reports;
};

IGMP igmpalloc;

	Proto	igmp;
extern	Fs	fs;

void
igmpsendreport(Media *m, byte *addr)
{
	IGMPpkt *p;
	Block *bp;

	bp = allocb(sizeof(IGMPpkt));
	if(bp == nil)
		return;
	p = (IGMPpkt*)bp->wp;
	bp->wp += sizeof(IGMPpkt);
	memset(bp->rp, 0, sizeof(IGMPpkt));
	hnputl(p->src, Mediagetaddr(m));
	hnputl(p->dst, Ipallsys);
	p->vertype = (1<<4) | IGMPreport;
	p->proto = IP_IGMPPROTO;
	memmove(p->group, addr, Ipaddrlen);
	hnputs(p->igmpcksum, ptclcsum(bp, IGMP_IPHDRSIZE, IGMP_HDRSIZE));
	netlog(Logigmp, "igmpreport %I\n", p->group);
	ipoput(bp, 0, 1);	/* TTL of 1 */
}

static int
isreport(void *a)
{
	USED(a);
	return igmpalloc.reports != 0;
}


void
igmpproc(void *a)
{
	IGMPrep *rp, **lrp;
	Multicast *mp, **lmp;
	byte ip[Ipaddrlen];

	USED(a);

	for(;;){
		sleep(&igmpalloc.r, isreport, 0);
		for(;;){
			lock(&igmpalloc);

			if(igmpalloc.reports == nil)
				break;
	
			/* look for a single report */
			lrp = &igmpalloc.reports;
			mp = nil;
			for(rp = *lrp; rp; rp = *lrp){
				rp->ticks++;
				lmp = &rp->multi;
				for(mp = *lmp; mp; mp = *lmp){
					if(rp->ticks >= mp->timeout){
						*lmp = mp->next;
						break;
					}
					lmp = &mp->next;
				}
				if(mp != nil)
					break;

				if(rp->multi != nil){
					lrp = &rp->next;
					continue;
				} else {
					*lrp = rp->next;
					free(rp);
				}
			}
			unlock(&igmpalloc);

			if(mp){
				/* do a single report and try again */
				hnputl(ip, mp->addr);
				igmpsendreport(rp->m, ip);
				free(mp);
				continue;
			}

			tsleep(&igmpalloc.r0, return0, 0, MSPTICK);
		}
		unlock(&igmpalloc);
	}

}

void
igmpiput(Media *m, Block *bp)
{
	int n;
	IGMPpkt *ghp;
	Ipaddr group;
	IGMPrep *rp, **lrp;
	Multicast *mp, **lmp;

	ghp = (IGMPpkt*)(bp->rp);
	netlog(Logigmp, "igmpiput: %d %I\n", ghp->vertype, ghp->group);

	n = blocklen(bp);
	if(n < IGMP_IPHDRSIZE+IGMP_HDRSIZE){
		netlog(Logigmp, "igmpiput: bad len\n");
		goto error;
	}
	if((ghp->vertype>>4) != 1){
		netlog(Logigmp, "igmpiput: bad igmp type\n");
		goto error;
	}
	if(ptclcsum(bp, IGMP_IPHDRSIZE, IGMP_HDRSIZE)){
		netlog(Logigmp, "igmpiput: checksum error %I\n", ghp->src);
		goto error;
	}

	group = nhgetl(ghp->group);
	
	lock(&igmpalloc);
	switch(ghp->vertype & 0xf){
	case IGMPquery:
		/*
		 *  start reporting groups that we're a member of.
		 */
		for(rp = igmpalloc.reports; rp; rp = rp->next)
			if(rp->m == m)
				break;
		if(rp != nil)
			break;	/* already reporting */

		mp = Mediacopymulti(m);
		if(mp == nil)
			break;

		rp = malloc(sizeof(*rp));
		if(rp == 0)
			break;

		rp->m = m;
		rp->multi = mp;
		rp->ticks = 0;
		for(; mp; mp = mp->next)
			mp->timeout = nrand(MAXTIMEOUT);
		rp->next = igmpalloc.reports;
		igmpalloc.reports = rp;

		wakeup(&igmpalloc.r);

		break;
	case IGMPreport:
		/*
		 *  find report list for this medium
		 */
		lrp = &igmpalloc.reports;
		for(rp = *lrp; rp; rp = *lrp){
			if(rp->m == m)
				break;
			lrp = &rp->next;
		}
		if(rp == nil)
			break;

		/*
		 *  if someone else has reported a group,
		 *  we don't have to.
		 */
		lmp = &rp->multi;
		for(mp = *lmp; mp; mp = *lmp){
			if(mp->addr == group){
				*lmp = mp->next;
				free(mp);
				break;
			}
			lmp = &mp->next;
		}

		break;
	}
	unlock(&igmpalloc);

error:
	freeb(bp);
}

void
igmpinit(Fs *fs)
{
	igmp.name = "igmp";
	igmp.kick = nil;
	igmp.connect = nil;
	igmp.announce = nil;
	igmp.ctl = nil;
	igmp.state = nil;
	igmp.close = nil;
	igmp.rcv = igmpiput;
	igmp.ipproto = IP_IGMPPROTO;
	igmp.nc = 0;
	igmp.ptclsize = 0;

	igmpreportfn = igmpsendreport;
	kproc("igmpproc", igmpproc, 0);

	Fsproto(fs, &igmp);
}
.
## diffname ip/igmp.c 1997/0815
## diff -e /n/emeliedump/1997/0423/sys/src/brazil/ip/igmp.c /n/emeliedump/1997/0815/sys/src/brazil/ip/igmp.c
106c
	byte ip[IPaddrlen];
.
87c
	memmove(p->group, addr, IPaddrlen);
.
42c
	byte	group[IPaddrlen];	/* multicast group */
.
35,36c
	byte	src[IPaddrlen];		/* Ip source */
	byte	dst[IPaddrlen];		/* Ip destination */
.
## diffname ip/igmp.c 1997/0916
## diff -e /n/emeliedump/1997/0815/sys/src/brazil/ip/igmp.c /n/emeliedump/1997/0916/sys/src/brazil/ip/igmp.c
263a
	igmp.stats = igmpstats;
.
252a
int
igmpstats(char *buf, int len)
{
	return snprint(buf, len, "\trcvd %d %d\n\tsent %d %d\n",
		stats.inqueries, stats.inreports,
		stats.outqueries, stats.outreports);
}

.
221a
		stats.inreports++;
.
192a
		stats.inqueries++;
.
89a
	stats.outreports++;
.
70a
static struct Stats
{
	ulong 	inqueries;
	ulong	outqueries;
	ulong	inreports;
	ulong	outreports;
} stats;

.
## diffname ip/igmp.c 1997/1104
## diff -e /n/emeliedump/1997/0916/sys/src/brazil/ip/igmp.c /n/emeliedump/1997/1104/sys/src/brazil/ip/igmp.c
214c
		if(rp == nil)
.
## diffname ip/igmp.c 1999/0817
## diff -e /n/emeliedump/1997/1104/sys/src/brazil/ip/igmp.c /n/emeliedump/1999/0817/sys/src/brazil/ip/igmp.c
99c
	ipoput(bp, 0, 1, DFLTTOS);	/* TTL of 1 */
.
## diffname ip/igmp.c 2001/0623
## diff -e /n/emeliedump/1999/0817/sys/src/brazil/ip/igmp.c /n/emeliedump/2001/0623/sys/src/9/ip/igmp.c
169c
igmpiput(Media *m, Ipifc*, Block *bp)
.
## diffname ip/igmp.c 2002/0507
## diff -e /n/emeliedump/2001/0623/sys/src/9/ip/igmp.c /n/emeliedump/2002/0507/sys/src/9/ip/igmp.c
169c
igmpiput(Media *m, Ipifc *, Block *bp)
.
99c
	ipoput4(bp, 0, 1, DFLTTOS);	/* TTL of 1 */
.
88a
	p->vihl = IP_VER4;
.
## diffname ip/igmp.c 2002/0712
## diff -e /n/emeliedump/2002/0507/sys/src/9/ip/igmp.c /n/emeliedump/2002/0712/sys/src/9/ip/igmp.c
277d
## diffname ip/igmp.c 2003/0308
## diff -e /n/emeliedump/2002/0712/sys/src/9/ip/igmp.c /n/emeliedump/2003/0308/sys/src/9/ip/igmp.c
100c
	ipoput4(bp, 0, 1, DFLTTOS, nil);	/* TTL of 1 */
.
## diffname ip/igmp.c 2003/0407
## diff -e /n/emeliedump/2003/0308/sys/src/9/ip/igmp.c /n/emeliedump/2003/0407/sys/src/9/ip/igmp.c
162c
			tsleep(&up->sleep, return0, 0, MSPTICK);
.
62d

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].