blob: 3cee145d4070bfbba87e9fc6cdb6a431f2959945 [file] [log] [blame]
Bart De Schuymer6622a012005-01-19 21:09:05 +00001#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <unistd.h>
5#include <sys/types.h>
6#include <sys/stat.h>
7#include <fcntl.h>
8#include <errno.h>
9
Bart De Schuymer909146d2005-01-24 20:41:04 +000010static void print_help()
11{
12 printf("ebtablesu v"PROGVERSION" ("PROGDATE")\n");
13 printf(
14"Usage:\n"
15"ebtablesu open table : copy the kernel table\n"
16"ebtablesu fopen table file : copy the table from the specified file\n"
17"ebtablesu free table : remove the table from memory\n"
18"ebtablesu commit table : commit the table to the kernel\n"
19"ebtablesu fcommit table file : commit the table to the specified file\n\n"
20"ebtablesu <ebtables options> : the ebtables specifications\n"
21" use spaces only to separate options and commands\n"
22"For the ebtables options, see\n# ebtables -h\nor\n# man ebtables\n"
23 );
24}
Bart De Schuymer6622a012005-01-19 21:09:05 +000025int main(int argc, char *argv[])
26{
27 char *arguments, *pos;
28 int i, writefd, len = 0;
29
Bart De Schuymer6622a012005-01-19 21:09:05 +000030 if (argc > EBTD_ARGC_MAX) {
Bart De Schuymer9ef397e2005-01-23 13:14:09 +000031 fprintf(stderr, "ebtablesd accepts at most %d arguments, %d "
32 "arguments were specified. If you need this many "
33 "arguments, recompile this tool with a higher value for"
34 " EBTD_ARGC_MAX.\n", EBTD_ARGC_MAX - 1, argc - 1);
Bart De Schuymer6622a012005-01-19 21:09:05 +000035 return -1;
36 } else if (argc == 1) {
Bart De Schuymer9ef397e2005-01-23 13:14:09 +000037 fprintf(stderr, "At least one argument is needed.\n");
Bart De Schuymer0f060c22005-02-19 19:01:15 +000038 print_help();
39 exit(0);
Bart De Schuymer6622a012005-01-19 21:09:05 +000040 }
41
42 for (i = 0; i < argc; i++)
43 len += strlen(argv[i]);
44 /* Don't forget '\0' */
45 len += argc;
46 if (len > EBTD_CMDLINE_MAXLN) {
Bart De Schuymer9ef397e2005-01-23 13:14:09 +000047 fprintf(stderr, "ebtablesd has a maximum command line argument "
48 "length of %d, an argument length of %d was received. "
49 "If a smaller length is unfeasible, recompile this tool "
Bart De Schuymer6622a012005-01-19 21:09:05 +000050 "with a higher value for EBTD_CMDLINE_MAXLN.\n",
51 EBTD_CMDLINE_MAXLN, len);
52 return -1;
53 }
54
Bart De Schuymer909146d2005-01-24 20:41:04 +000055 if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
56 if (argc != 2) {
57 fprintf(stderr, "%s does not accept options.\n", argv[1]);
58 return -1;
59 }
60 print_help();
61 exit(0);
62 }
63
Bart De Schuymer6622a012005-01-19 21:09:05 +000064 if (!(arguments = (char *)malloc(len))) {
Bart De Schuymer9ef397e2005-01-23 13:14:09 +000065 fprintf(stderr, "ebtablesu: out of memory.\n");
Bart De Schuymer6622a012005-01-19 21:09:05 +000066 return -1;
67 }
68
Bart De Schuymer909146d2005-01-24 20:41:04 +000069 if ((writefd = open(EBTD_PIPE, O_WRONLY, 0)) == -1) {
70 fprintf(stderr, "Could not open the pipe, perhaps ebtablesd is "
71 "not running or you don't have write permission (try "
72 "running as root).\n");
73 return -1;
74 }
75
Bart De Schuymer6622a012005-01-19 21:09:05 +000076 pos = arguments;
77 for (i = 0; i < argc; i++) {
78 strcpy(pos, argv[i]);
Bart De Schuymer83a1b0f2005-09-28 19:36:17 +000079 pos += strlen(argv[i]);
80 *(pos++) = ' ';
Bart De Schuymer6622a012005-01-19 21:09:05 +000081 }
82
83 *(pos-1) = '\n';
84 if (write(writefd, arguments, len) == -1) {
85 perror("write");
86 return -1;
87 }
88 return 0;
89}