|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <unistd.h> |
| 4 | +#include <string.h> |
| 5 | +#include <ctype.h> |
| 6 | +#include <pthread.h> |
| 7 | +#include "iplist.h" |
| 8 | +#include "watchip.h" |
| 9 | +#include "dnsquery.h" |
| 10 | +#include "dnsupdate.h" |
| 11 | + |
| 12 | +static int |
| 13 | +filter_add(struct filter_list **fp, char *descriptor) |
| 14 | +{ |
| 15 | +char *p; |
| 16 | + |
| 17 | + while (p = strchr(descriptor, ',')) { |
| 18 | + *p = 0; |
| 19 | + if (p > (descriptor+1)) { |
| 20 | + if (!watchip_filter_add(fp, descriptor)) return 0; |
| 21 | + } |
| 22 | + descriptor = p+1; |
| 23 | + } |
| 24 | + if (*descriptor) { |
| 25 | + if (!watchip_filter_add(fp, descriptor)) return 0; |
| 26 | + } |
| 27 | + return 1; |
| 28 | +} |
| 29 | + |
| 30 | +int |
| 31 | +main(int argc, char **argv) |
| 32 | +{ |
| 33 | +struct watchip *w; |
| 34 | +struct iplist *ipl; |
| 35 | +int err = 0; |
| 36 | +int c; |
| 37 | +int filter_sense = 0; |
| 38 | +int enable4 = 1; |
| 39 | +int enable6 = 1; |
| 40 | +int verbose = 0; |
| 41 | +int default_family = 1; |
| 42 | +int default_ttl = -1; |
| 43 | +int max6_ttl = 86400; |
| 44 | +int use_valid = 0; |
| 45 | +char *hostname = NULL; |
| 46 | +struct filter_list *intf_filter = NULL; |
| 47 | + |
| 48 | + while ((c = getopt(argc, argv, "46t:m:Vvh:i:")) != EOF) { |
| 49 | + switch (c) { |
| 50 | + case 'i': |
| 51 | + c = (optarg[0] == '!') ? -1 : 1; |
| 52 | + if (filter_sense != 0 && (c != filter_sense)) { |
| 53 | + fprintf(stderr, |
| 54 | + "%s: cannot mix \"-i include\" with \"-i !exclude\"\n", |
| 55 | + argv[0] |
| 56 | + ); |
| 57 | + err = 1; |
| 58 | + break; |
| 59 | + } |
| 60 | + filter_sense = c; |
| 61 | + if (!filter_add( |
| 62 | + &intf_filter, |
| 63 | + optarg + ((filter_sense == -1) ? 1 : 0) |
| 64 | + )) return 2; |
| 65 | + break; |
| 66 | + case '4': |
| 67 | + if (default_family) { |
| 68 | + default_family = 0; |
| 69 | + enable6 = 0; |
| 70 | + } |
| 71 | + enable4 = 1; |
| 72 | + break; |
| 73 | + case '6': |
| 74 | + if (default_family) { |
| 75 | + default_family = 0; |
| 76 | + enable4 = 0; |
| 77 | + } |
| 78 | + enable6 = 1; |
| 79 | + break; |
| 80 | + case 't': |
| 81 | + if (isdigit(optarg[0])) { |
| 82 | + default_ttl = atoi(optarg); |
| 83 | + } else { |
| 84 | + err = 1; |
| 85 | + } |
| 86 | + break; |
| 87 | + case 'm': |
| 88 | + if (isdigit(optarg[0])) { |
| 89 | + max6_ttl = atoi(optarg); |
| 90 | + } else { |
| 91 | + err = 1; |
| 92 | + } |
| 93 | + break; |
| 94 | + case 'V': |
| 95 | + use_valid = 1; |
| 96 | + break; |
| 97 | + case 'v': |
| 98 | + verbose = 1; |
| 99 | + break; |
| 100 | + case 'h': |
| 101 | + hostname = optarg; |
| 102 | + break; |
| 103 | + default: |
| 104 | + err = 1; |
| 105 | + break; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + dnsquery_set_verbose(verbose); |
| 110 | + |
| 111 | + if (enable4 && (default_ttl == -1)) { |
| 112 | + err = 1; |
| 113 | + fprintf(stderr, "%s: When IPv4 is enabled, a default TTL is required\n" |
| 114 | + "%s: Use either -6 to disable IPv4 or -t to specify DNS TTL\n", |
| 115 | + argv[0], argv[0]); |
| 116 | + } |
| 117 | + if ((!enable6) && use_valid) { |
| 118 | + fprintf(stderr, "%s: warning: -V only makes sense when IPv6 is enabled\n", |
| 119 | + argv[0]); |
| 120 | + } |
| 121 | + if ((!enable4) && (default_ttl != -1)) { |
| 122 | + fprintf(stderr, "%s: warning: -t only makes sense when IPv4 is enabled\n", |
| 123 | + argv[0]); |
| 124 | + } |
| 125 | + if (err || (optind == argc)) { |
| 126 | + fprintf(stderr, "Usage: %s [-4|6] [-v] [-V] [-t DNS_ttl] [-m DNS_ttl] [-h hostname] \\\n" |
| 127 | + " [-i [!]interface[,interface]] -- nsupdate command line\n" |
| 128 | + " -4|6: Enable IPv4 or IPv6 (default is both)\n" |
| 129 | + " -V: Use valid lifetime as DNS TTL instead of prefered lifetime\n" |
| 130 | + " -v: verbose\n" |
| 131 | + " -i: consider only named interfaces (with !, consider all except named)\n" |
| 132 | + " -t: Specify DNS TTL to use for IPv4 addresses\n" |
| 133 | + " -m: Specify DNS TTL to use for IPv6 addresses with infinite lifetime\n" |
| 134 | + " -h: Override the local hostname\n" |
| 135 | + "nsupdate comand line is typically \"nsupdate -k keyfile\"\n", |
| 136 | + argv[0] |
| 137 | + ); |
| 138 | + return 2; |
| 139 | + } |
| 140 | + |
| 141 | + if (!(ipl = iplist_new(verbose, enable4, enable6))) { |
| 142 | + return 1; |
| 143 | + } |
| 144 | + |
| 145 | + if (!(w = watchip_start( |
| 146 | + ipl, enable4, enable6, use_valid, default_ttl, max6_ttl, |
| 147 | + filter_sense, intf_filter |
| 148 | + ))) { |
| 149 | + return 1; |
| 150 | + } |
| 151 | + |
| 152 | + if (!(dnsupdate_start(ipl, verbose, hostname, argv + optind))) { |
| 153 | + return 1; |
| 154 | + } |
| 155 | + |
| 156 | + watchip(w); |
| 157 | + |
| 158 | + return 0; |
| 159 | +} |
0 commit comments