2 * Perform PPPoE discovery
4 * Copyright (C) 2000-2001 by Roaring Penguin Software Inc.
5 * Copyright (C) 2004 Marco d'Itri <md@linux.it>
7 * This program may be distributed according to the terms of the GNU
8 * General Public License, version 2 or (at your option) any later version.
30 static FILE *debugFile;
37 vfprintf(stderr, fmt, pvar);
48 vfprintf(stderr, fmt, pvar);
58 vfprintf(stderr, fmt, pvar);
74 init_pr_log(const char *prefix, int level)
85 pr_log(void *arg, char *fmt, ...)
89 vfprintf(debugFile, fmt, ap);
94 strlcpy(char *dest, const char *src, size_t len)
96 size_t ret = strlen(src);
102 strncpy(dest, src, len - 1);
110 xstrdup(const char *s)
112 char *ret = strdup(s);
121 get_time(struct timeval *tv)
123 return gettimeofday(tv, NULL);
127 term_handler(int signum)
132 static void usage(void);
134 int main(int argc, char *argv[])
137 PPPoEConnection *conn;
139 signal(SIGINT, term_handler);
140 signal(SIGTERM, term_handler);
142 conn = malloc(sizeof(PPPoEConnection));
148 memset(conn, 0, sizeof(PPPoEConnection));
151 conn->discoveryTimeout = PADI_TIMEOUT;
152 conn->discoveryAttempts = MAX_PADI_ATTEMPTS;
154 while ((opt = getopt(argc, argv, "I:D:VUQS:C:W:t:a:h")) > 0) {
157 conn->serviceName = xstrdup(optarg);
160 conn->acName = xstrdup(optarg);
163 if (sscanf(optarg, "%d", &conn->discoveryTimeout) != 1) {
164 fprintf(stderr, "Illegal argument to -t: Should be -t timeout\n");
167 if (conn->discoveryTimeout < 1) {
168 conn->discoveryTimeout = 1;
172 if (sscanf(optarg, "%d", &conn->discoveryAttempts) != 1) {
173 fprintf(stderr, "Illegal argument to -a: Should be -a attempts\n");
176 if (conn->discoveryAttempts < 1) {
177 conn->discoveryAttempts = 1;
181 if(conn->hostUniq.length) {
182 fprintf(stderr, "-U and -W are mutually exclusive\n");
185 pid_t pid = getpid();
186 conn->hostUniq.type = htons(TAG_HOST_UNIQ);
187 conn->hostUniq.length = htons(sizeof(pid));
188 memcpy(conn->hostUniq.payload, &pid, sizeof(pid));
192 if(conn->hostUniq.length) {
193 fprintf(stderr, "-U and -W are mutually exclusive\n");
196 if (!parseHostUniq(optarg, &conn->hostUniq)) {
197 fprintf(stderr, "Invalid host-uniq argument: %s\n", optarg);
204 debugFile = fopen(optarg, "w");
206 fprintf(stderr, "Could not open %s: %s\n",
207 optarg, strerror(errno));
210 fprintf(debugFile, "pppoe-discovery from pppd %s\n", PPPD_VERSION);
213 conn->ifName = xstrdup(optarg);
228 if (optind != argc) {
229 fprintf(stderr, "%s: extra argument '%s'\n", argv[0], argv[optind]);
235 fprintf(stderr, "Interface was not specified\n");
239 conn->sessionSocket = -1;
241 conn->discoverySocket = openInterface(conn->ifName, Eth_PPPOE_Discovery, conn->myEth);
242 if (conn->discoverySocket < 0) {
243 perror("Cannot create PPPoE discovery socket");
258 fprintf(stderr, "Usage: pppoe-discovery [options]\n");
259 fprintf(stderr, "Options:\n");
260 fprintf(stderr, " -I if_name -- Specify interface (mandatory option)\n");
261 fprintf(stderr, " -D filename -- Log debugging information in filename.\n");
263 " -t timeout -- Initial timeout for discovery packets in seconds\n"
264 " -a attempts -- Number of discovery attempts\n"
265 " -V -- Print version and exit.\n"
266 " -Q -- Quit Mode: Do not print access concentrator names\n"
267 " -S name -- Set desired service name.\n"
268 " -C name -- Set desired access concentrator name.\n"
269 " -U -- Use Host-Unique to allow multiple PPPoE sessions.\n"
270 " -W hexvalue -- Set the Host-Unique to the supplied hex string.\n"
271 " -h -- Print usage information.\n");
272 fprintf(stderr, "\npppoe-discovery from pppd " PPPD_VERSION "\n");