[BusyBox] HELP: New applet compilation error.
Nick Fedchik
fnm at fusion.ukrsat.com
Mon Dec 9 03:59:03 UTC 2002
Hi, All!
I need a few help notes from more experienced peoples.
I try to made a new applet - nameif (original from net-tools 1.60).
I read docs/new-applet-HOWTO.txt and made approriated changes (+/-
according to the latest code changes from cvs). Patch is attached.
So here is my error message:
# make
gcc -I./include -Wall -Wstrict-prototypes -Wshadow -Os -march=i386
-mpreferred-stack-boundary=2 -falign-functions=0 -falign-jumps=0
-falign-loops=0 -fomit-frame-pointer -D_GNU_SOURCE -c -o
applets/usage.o applets/usage.c
In file included from applets/usage.c:8:
include/applets.h:372: syntax error at '#' token
include/applets.h:372: parse error before "define"
make: *** [applets/usage.o] Error 1
#
Here is fragment from include/applets.h:372:
...
371: #ifdef CONFIG_NAMEIF
372: APPLET(nameif, nameif_main, _BB_DIR_SBIN, _BB_SUID_NEVER)
373: #endif
...
This lines looks like other - I just copy another three existing lines
and made approriated changes.
Can anybody hint me how to solve this problem?
--
Nick Fedchik <fnm at fusion.ukrsat.com>
FNM3-RIPE (-UANIC), UkrSat ISP
-------------- next part --------------
diff -urN busybox/include/applets.h busybox.nameif/include/applets.h
--- busybox/include/applets.h 2002-12-02 01:04:04.000000000 +0200
+++ busybox.nameif/include/applets.h 2002-12-09 11:52:11.000000000 +0200
@@ -371,6 +371,9 @@
#ifdef CONFIG_NC
APPLET(nc, nc_main, _BB_DIR_USR_BIN, _BB_SUID_NEVER)
#endif
+#ifdef CONFIG_NAMEIF
+ APPLET(nameif, nameif_main, _BB_DIR_SBIN, _BB_SUID_NEVER)
+#endif
#ifdef CONFIG_NETSTAT
APPLET(netstat, netstat_main, _BB_DIR_BIN, _BB_SUID_NEVER)
#endif
diff -urN busybox/include/usage.h busybox.nameif/include/usage.h
--- busybox/include/usage.h 2002-12-05 22:56:31.000000000 +0200
+++ busybox.nameif/include/usage.h 2002-12-09 12:05:17.000000000 +0200
@@ -1449,6 +1449,19 @@
#define mv_example_usage \
"$ mv /tmp/foo /bin/bar\n"
+#define nameif_trivial_usage \
+ "[OPTIONS] [{IFNAME MACADDR}]"
+#define nameif_full_usage \
+ "Nameif renaming network interface while it in the down state.\n\n" \
+ "Options:\n" \
+ "\t-c FILE\t\tUse another configuration file (default is /etc/mactab)\n" \
+ "\t-s\t\tUse syslog.\n" \
+ "\t\t\tnew_interface_name interface_macaddress\n" \
+#define nameif_example_usage \
+ "$ nameif -s dmz0 00:A0:C9:8C:F6:3F\n" \
+ " or\n" \
+ "$ nameif -c /etc/my_mactab_file\n" \
+
#define nc_trivial_usage \
"[OPTIONS] [IP] [port]"
#define nc_full_usage \
diff -urN busybox/networking/Config.in busybox.nameif/networking/Config.in
--- busybox/networking/Config.in 2002-12-07 01:11:30.000000000 +0200
+++ busybox.nameif/networking/Config.in 2002-12-09 11:35:28.000000000 +0200
@@ -17,6 +17,13 @@
help
Please submit a patch to add help text for this item.
+config CONFIG_NAMEIF
+ bool "nameif"
+ default n
+ help
+ Naming Interfaces based on MAC address.
+ Interfaces must be in the down state.
+
config CONFIG_IFCONFIG
bool "ifconfig"
default n
diff -urN busybox/networking/Makefile.in busybox.nameif/networking/Makefile.in
--- busybox/networking/Makefile.in 2002-12-02 01:04:05.000000000 +0200
+++ busybox.nameif/networking/Makefile.in 2002-12-09 11:32:42.000000000 +0200
@@ -32,6 +32,7 @@
NETWORKING-$(CONFIG_IPLINK) += iplink.o
NETWORKING-$(CONFIG_IPROUTE) += iproute.o
NETWORKING-$(CONFIG_IPTUNNEL) += iptunnel.o
+NETWORKING-$(CONFIG_NAMEIF) += nameif.o
NETWORKING-$(CONFIG_NC) += nc.o
NETWORKING-$(CONFIG_NETSTAT) += netstat.o
NETWORKING-$(CONFIG_NSLOOKUP) += nslookup.o
diff -urN busybox/networking/nameif.c busybox.nameif/networking/nameif.c
--- busybox/networking/nameif.c 1970-01-01 03:00:00.000000000 +0300
+++ busybox.nameif/networking/nameif.c 2002-12-09 12:23:19.000000000 +0200
@@ -0,0 +1,322 @@
+/*
+ * nameif.c - Naming Interfaces based on MAC address for busybox.
+ *
+ * Writen 2000 by Andi Kleen.
+ * Busybox port 2002 by Nick Fedchik <nick at fedchik.org.ua>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * 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 have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+ * 02111-1307 USA
+ *
+ */
+#include <stdio.h>
+#include <getopt.h>
+#include <sys/syslog.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <ctype.h>
+#include <string.h>
+#include <stdarg.h>
+#include <sys/socket.h>
+#include <sys/ioctl.h>
+#include <net/if.h>
+#include <linux/sockios.h>
+#include <errno.h>
+
+#include "busybox.h"
+
+const char default_conf[] = "/etc/mactab";
+const char *fname = default_conf;
+int use_syslog;
+int ctl_sk = -1;
+
+void err(char *msg)
+{
+ if (use_syslog) {
+ syslog(LOG_ERR, "%s: %m", msg);
+ } else {
+ perror(msg);
+ }
+ exit(1);
+}
+
+void complain(char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ if (use_syslog) {
+ vsyslog(LOG_ERR, fmt, ap);
+ } else {
+ vfprintf(stderr, fmt, ap);
+ fputc('\n', stderr);
+ }
+ va_end(ap);
+ exit(1);
+}
+
+void warning(char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ if (use_syslog) {
+ vsyslog(LOG_ERR, fmt, ap);
+ } else {
+ vfprintf(stderr, fmt, ap);
+ fputc('\n', stderr);
+ }
+ va_end(ap);
+}
+
+int parsemac(char *str, unsigned char *mac)
+{
+ char *s;
+
+ while ((s = strsep(&str, ":")) != NULL) {
+ unsigned byte;
+
+ if (sscanf(s, "%x", &byte) != 1 || byte > 0xff)
+ return -1;
+ *mac++ = byte;
+ }
+ return 0;
+}
+
+void *xmalloc(unsigned sz)
+{
+ void *p = calloc(sz, 1);
+
+ if (!p)
+ errno = ENOMEM, err("xmalloc");
+ return p;
+}
+
+void opensock(void)
+{
+ if (ctl_sk < 0)
+ ctl_sk = socket(PF_INET, SOCK_DGRAM, 0);
+}
+
+#ifndef ifr_newname
+#define ifr_newname ifr_ifru.ifru_slave
+#endif
+
+int setname(char *oldname, char *newname)
+{
+ struct ifreq ifr;
+
+ opensock();
+ memset(&ifr, 0, sizeof(struct ifreq));
+ strcpy(ifr.ifr_name, oldname);
+ strcpy(ifr.ifr_newname, newname);
+ return ioctl(ctl_sk, SIOCSIFNAME, &ifr);
+}
+
+int getmac(char *name, unsigned char *mac)
+{
+ int r;
+ struct ifreq ifr;
+
+ opensock();
+ memset(&ifr, 0, sizeof(struct ifreq));
+ strcpy(ifr.ifr_name, name);
+ r = ioctl(ctl_sk, SIOCGIFHWADDR, &ifr);
+ memcpy(mac, ifr.ifr_hwaddr.sa_data, 6);
+ return r;
+}
+
+struct change {
+ struct change *next, **pprev;
+ char ifname[IFNAMSIZ + 1];
+ unsigned char mac[6];
+};
+struct change *clist;
+
+struct change *lookupmac(unsigned char *mac)
+{
+ struct change *ch;
+
+ for (ch = clist; ch; ch = ch->next)
+ if (!memcmp(ch->mac, mac, 6))
+ return ch;
+ return NULL;
+}
+
+int addchange(char *p, struct change *ch, char *pos)
+{
+ if (strchr(ch->ifname, ':'))
+ warning(_("alias device %s at %s probably has no mac"),
+ ch->ifname, pos);
+ if (parsemac(p, ch->mac) < 0)
+ complain(_("cannot parse MAC `%s' at %s"), p, pos);
+ if (clist)
+ clist->pprev = &ch->next;
+ ch->next = clist;
+ ch->pprev = &clist;
+ clist = ch;
+ return 0;
+}
+
+void readconf(void)
+{
+ char *line;
+ size_t linel;
+ int linenum;
+ FILE *ifh;
+ char *p;
+ int n;
+
+ ifh = fopen(fname, "r");
+ if (!ifh)
+ complain(_("opening configuration file %s: %s"), fname,
+ strerror(errno));
+
+ line = NULL;
+ linel = 0;
+ linenum = 1;
+ while (getdelim(&line, &linel, '\n', ifh) > 0) {
+ struct change *ch = xmalloc(sizeof(struct change));
+ char pos[20];
+
+ sprintf(pos, _("line %d"), linenum);
+
+ if ((p = strchr(line, '#')) != NULL)
+ *p = '\0';
+ p = line;
+ while (isspace(*p))
+ ++p;
+ if (*p == '\0')
+ continue;
+ n = strcspn(p, " \t");
+ if (n > IFNAMSIZ)
+ complain(_("interface name too long at line %d"), line);
+ memcpy(ch->ifname, p, n);
+ ch->ifname[n] = 0;
+ p += n;
+ p += strspn(p, " \t");
+ n = strspn(p, "0123456789ABCDEFabcdef:");
+ p[n] = 0;
+ addchange(p, ch, pos);
+ linenum++;
+ }
+ fclose(ifh);
+}
+
+struct option lopt[] = {
+ {"syslog", 0, NULL, 's'},
+ {"config-file", 1, NULL, 'c'},
+ {"help", 0, NULL, '?'},
+ {NULL},
+};
+
+int nameif_main(int argc, char **argv)
+{
+ FILE *ifh;
+ char *p;
+ int n;
+ int linenum;
+ char *line = NULL;
+ size_t linel = 0;
+
+ for (;;) {
+ int c = getopt_long(argc, argv, "c:s", lopt, NULL);
+
+ if (c == -1)
+ break;
+ switch (c) {
+ default:
+ case '?':
+ show_usage();
+ case 'c':
+ fname = optarg;
+ break;
+ case 's':
+ use_syslog = 1;
+ break;
+ }
+ }
+
+ if (use_syslog)
+ openlog("nameif", 0, LOG_LOCAL0);
+
+ while (optind < argc) {
+ struct change *ch = xmalloc(sizeof(struct change));
+ char pos[30];
+
+ if ((argc - optind) & 1)
+ usage();
+ if (strlen(argv[optind]) + 1 > IFNAMSIZ)
+ complain(_("interface name `%s' too long"), argv[optind]);
+ strcpy(ch->ifname, argv[optind]);
+ optind++;
+ sprintf(pos, _("argument %d"), optind);
+ addchange(argv[optind], ch, pos);
+ optind++;
+ }
+
+ if (!clist || fname != default_conf)
+ readconf();
+
+ ifh = fopen("/proc/net/dev", "r");
+ if (!ifh)
+ complain(_("open of /proc/net/dev: %s"), strerror(errno));
+
+
+ linenum = 0;
+ while (getdelim(&line, &linel, '\n', ifh) > 0) {
+ struct change *ch;
+ unsigned char mac[6];
+
+ if (linenum++ < 2)
+ continue;
+
+ p = line;
+ while (isspace(*p))
+ ++p;
+ n = strcspn(p, ": \t");
+ p[n] = 0;
+
+ if (n > IFNAMSIZ - 1)
+ complain(_("interface name `%s' too long"), p);
+
+ if (getmac(p, mac) < 0)
+ continue;
+
+ ch = lookupmac(mac);
+ if (!ch)
+ continue;
+
+ *ch->pprev = ch->next;
+ if (strcmp(p, ch->ifname)) {
+ if (setname(p, ch->ifname) < 0)
+ complain(_("cannot change name of %s to %s: %s"),
+ p, ch->ifname, strerror(errno));
+ }
+ free(ch);
+ }
+ fclose(ifh);
+
+ while (clist) {
+ struct change *ch = clist;
+
+ clist = clist->next;
+ warning(_("interface '%s' not found"), ch->ifname);
+ free(ch);
+ }
+
+ if (use_syslog)
+ closelog();
+ return 0;
+}
More information about the busybox
mailing list