Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 1 | #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 Schuymer | 909146d | 2005-01-24 20:41:04 +0000 | [diff] [blame] | 10 | static 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 Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 25 | int main(int argc, char *argv[]) |
| 26 | { |
| 27 | char *arguments, *pos; |
| 28 | int i, writefd, len = 0; |
| 29 | |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 30 | if (argc > EBTD_ARGC_MAX) { |
Bart De Schuymer | 9ef397e | 2005-01-23 13:14:09 +0000 | [diff] [blame] | 31 | 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 Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 35 | return -1; |
| 36 | } else if (argc == 1) { |
Bart De Schuymer | 9ef397e | 2005-01-23 13:14:09 +0000 | [diff] [blame] | 37 | fprintf(stderr, "At least one argument is needed.\n"); |
Bart De Schuymer | 0f060c2 | 2005-02-19 19:01:15 +0000 | [diff] [blame] | 38 | print_help(); |
| 39 | exit(0); |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 40 | } |
| 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 Schuymer | 9ef397e | 2005-01-23 13:14:09 +0000 | [diff] [blame] | 47 | 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 Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 50 | "with a higher value for EBTD_CMDLINE_MAXLN.\n", |
| 51 | EBTD_CMDLINE_MAXLN, len); |
| 52 | return -1; |
| 53 | } |
| 54 | |
Bart De Schuymer | 909146d | 2005-01-24 20:41:04 +0000 | [diff] [blame] | 55 | 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 Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 64 | if (!(arguments = (char *)malloc(len))) { |
Bart De Schuymer | 9ef397e | 2005-01-23 13:14:09 +0000 | [diff] [blame] | 65 | fprintf(stderr, "ebtablesu: out of memory.\n"); |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 66 | return -1; |
| 67 | } |
| 68 | |
Bart De Schuymer | 909146d | 2005-01-24 20:41:04 +0000 | [diff] [blame] | 69 | 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 Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 76 | pos = arguments; |
| 77 | for (i = 0; i < argc; i++) { |
| 78 | strcpy(pos, argv[i]); |
Bart De Schuymer | 83a1b0f | 2005-09-28 19:36:17 +0000 | [diff] [blame] | 79 | pos += strlen(argv[i]); |
| 80 | *(pos++) = ' '; |
Bart De Schuymer | 6622a01 | 2005-01-19 21:09:05 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | *(pos-1) = '\n'; |
| 84 | if (write(writefd, arguments, len) == -1) { |
| 85 | perror("write"); |
| 86 | return -1; |
| 87 | } |
| 88 | return 0; |
| 89 | } |