#include "utils.h"
static int quit = 0;
+static unsigned char macaddr[6];
static void usage(FILE *out)
{
- fprintf(out, "usage: mctap [-hdp] [-P PIDFILE] [-D TAPNAME] MCASTGROUP PORT\n");
+ fprintf(out, "usage: mctap [-hdpk] [-P PIDFILE] [-D TAPNAME] MCASTGROUP PORT\n");
}
static __attribute__ ((unused)) char *formataddress(struct sockaddr *arg, socklen_t arglen)
} else {
if(sizeof(buf) - ret < sizeof(pi)) {
/* Drop */
+ } else if((ret < 12) || !memcmp(macaddr, buf + 6, 6)) {
+ /* Drop looped back */
} else {
memmove(buf + sizeof(pi), buf, ret);
pi.flags = 0;
strncpy(rb.ifr_name, name, IFNAMSIZ);
if(ioctl(fd, TUNSETIFF, &rb))
return(-1);
+ if(ioctl(fd, SIOCGIFHWADDR, &rb))
+ return(-1);
+ memcpy(macaddr, rb.ifr_hwaddr.sa_data, 6);
return(fd);
}
}
}
+static void killrunning(char *pidfile)
+{
+ FILE *pidfd;
+ int pid;
+
+ if((pidfd = fopen(pidfile, "r")) == NULL) {
+ fprintf(stderr, "mctab -k: could not read PID file %s: %s\n", pidfile, strerror(errno));
+ exit(1);
+ }
+ fscanf(pidfd, "%i", &pid);
+ if(kill(pid, SIGTERM)) {
+ fprintf(stderr, "mctab -k: could not kill %i: %s\n", pid, strerror(errno));
+ exit(1);
+ }
+ fclose(pidfd);
+}
+
int main(int argc, char **argv)
{
int c;
int port;
char *tapname;
char *pidfile;
- int daemonize;
+ int daemonize, killold;
struct sockaddr_in dst;
FILE *pidfd;
tapname = "mctap";
- daemonize = 0;
+ daemonize = killold = 0;
pidfile = NULL;
- while((c = getopt(argc, argv, "hD:dpP:")) >= 0) {
+ while((c = getopt(argc, argv, "hD:dpP:k")) >= 0) {
switch(c) {
case 'D':
tapname = optarg;
case 'P':
pidfile = optarg;
break;
+ case 'k':
+ killold = 1;
+ if(pidfile == NULL)
+ pidfile = (void *)-1;
+ break;
case 'h':
usage(stdout);
return(0);
}
if(pidfile == (void *)-1)
pidfile = sprintf2("/var/run/mctap.%s.pid", tapname);
+ if(killold) {
+ killrunning(pidfile);
+ return(0);
+ }
if(argc - optind < 2) {
usage(stderr);
exit(1);
dst.sin_family = AF_INET;
dst.sin_addr = group;
dst.sin_port = htons(port);
+ syslog(LOG_INFO, "bridge created with MAC %02x:%02x:%02x:%02x:%02x:%02x", macaddr[0], macaddr[1], macaddr[2], macaddr[3], macaddr[4], macaddr[5]);
bridge(sock, tap, (struct sockaddr *)&dst, sizeof(dst));
syslog(LOG_INFO, "exiting");