summaryrefslogtreecommitdiffstats
path: root/ax25/bpqparms.c
blob: 83734a8590f0537f5f30c065096d85da48b3f2da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
   bpqparms.c

   Copyright 1996, by Joerg Reuter jreuter@poboxes.com

   This program is free software; you can redistribute it and/or modify
   it under the terms of the (modified) GNU General Public License
   delivered with the LinuX kernel source.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should find a copy of the GNU General Public License in
   /usr/src/linux/COPYING;

*/

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <termios.h>
#include <string.h>
#include <ctype.h>
#include <sys/ioctl.h>

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

#ifdef __GLIBC__
#include <net/ethernet.h> /* is this really needed ??  */
#endif

#include <linux/bpqether.h> /* xlz - dammit, we need this again */

#include <config.h>

void usage(void)
{
		fprintf(stderr, "usage   : bpqparms dev -d address [-a address]\n");
		fprintf(stderr, "examples: bpqparms bpq0 -d 00:80:AD:1B:05:26\n");
		fprintf(stderr, "          bpqparms bpq0 -d broadcast -a 00:80:AD:1B:05:26\n");
		exit(1);
}

int get_hwaddr(unsigned char *k, char *s)
{
	unsigned char broadcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
	unsigned int  eth[ETH_ALEN];
	int n;

	if (strcmp(s, "default") == 0 || strcmp(s, "broadcast") == 0) {
		memcpy(k, broadcast, ETH_ALEN);
	} else {
		n = sscanf(s, "%x:%x:%x:%x:%x:%x",
		&eth[0], &eth[1], &eth[2], &eth[3], &eth[4], &eth[5]);

		if (n < 6)
			return 1;

		for (n = 0; n < ETH_ALEN; n++)
			k[n] = eth[n];
	}

	return 0;
}

int main(int argc, char **argv)
{
	int fd;
	int cmd, flag;
	struct ifreq ifr;
	char dev[40];
	struct bpq_ethaddr addr;

	flag = 0;

	while ((cmd = getopt(argc, argv, "d:a:vVh")) != EOF) {
		switch (cmd) {
		case 'd':
			flag |= 1;
			if (get_hwaddr(addr.destination, optarg)) {
				fprintf(stderr, "bpqparms: invalid 'destination' address %s\n", optarg);
				return 1;
			}
			break;

		case 'a':
			flag |= 2;
			if (get_hwaddr(addr.accept, optarg)) {
				fprintf(stderr, "bpqparms: invalid 'accept' address %s\n", optarg);
				return 1;
			}
			break;

		case 'V':
			printf("bpqparms version " VERSION "\n");
			printf("Copyright 1996, Jörg Reuter (jreuter@poboxes.com)\n");
			printf("This program is free software; you can redistribute it and/or modify\n");
			printf("it under the terms of the GNU General Public License as published by\n");
			printf("the Free Software Foundation; either version 2 of the License, or\n");
			printf(" (at your option) any later version.\n\n");
			printf("This program is distributed in the hope that it will be useful,\n");
			printf("but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
			printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
			return 0;

		case 'v':
			printf("bpqparms: %s\n", VERSION);
			return 0;

		case 'h':
		case ':':
		case '?':
			usage();
		}
	}

	if (!(flag & 0x01) || optind+1 > argc)
		usage();

	strcpy(dev, argv[optind]);

	if ((flag & 0x02) == 0)
		memcpy(addr.accept, addr.destination, ETH_ALEN);

	fd = socket(AF_INET, SOCK_DGRAM, 0);

	strcpy(ifr.ifr_name, dev);
	ifr.ifr_data = (caddr_t) &addr;

	if (ioctl(fd, SIOCSBPQETHADDR, &ifr) < 0) {
		perror("bpqparms SIOCSBPQETHADDR");
		close(fd);
		return 1;
	}

	close(fd);

	return 0;
}