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.
31 static FILE *debugFile;
38 vfprintf(stderr, fmt, pvar);
49 vfprintf(stderr, fmt, pvar);
59 vfprintf(stderr, fmt, pvar);
75 init_pr_log(const char *prefix, int level)
86 pr_log(void *arg, char *fmt, ...)
90 vfprintf(debugFile, fmt, ap);
95 strlcpy(char *dest, const char *src, size_t len)
97 size_t ret = strlen(src);
103 strncpy(dest, src, len - 1);
111 xstrdup(const char *s)
113 char *ret = strdup(s);
122 get_time(struct timeval *tv)
124 return gettimeofday(tv, NULL);
128 term_handler(int signum)
133 static void usage(void);
135 int main(int argc, char *argv[])
138 PPPoEConnection *conn;
140 signal(SIGINT, term_handler);
141 signal(SIGTERM, term_handler);
143 conn = malloc(sizeof(PPPoEConnection));
149 memset(conn, 0, sizeof(PPPoEConnection));
152 conn->discoveryTimeout = PADI_TIMEOUT;
153 conn->discoveryAttempts = MAX_PADI_ATTEMPTS;
155 while ((opt = getopt(argc, argv, "I:D:VUQS:C:W:t:a:h")) > 0) {
158 conn->serviceName = xstrdup(optarg);
161 conn->acName = xstrdup(optarg);
164 if (sscanf(optarg, "%d", &conn->discoveryTimeout) != 1) {
165 fprintf(stderr, "Illegal argument to -t: Should be -t timeout\n");
168 if (conn->discoveryTimeout < 1) {
169 conn->discoveryTimeout = 1;
173 if (sscanf(optarg, "%d", &conn->discoveryAttempts) != 1) {
174 fprintf(stderr, "Illegal argument to -a: Should be -a attempts\n");
177 if (conn->discoveryAttempts < 1) {
178 conn->discoveryAttempts = 1;
182 if(conn->hostUniq.length) {
183 fprintf(stderr, "-U and -W are mutually exclusive\n");
186 pid_t pid = getpid();
187 conn->hostUniq.type = htons(TAG_HOST_UNIQ);
188 conn->hostUniq.length = htons(sizeof(pid));
189 memcpy(conn->hostUniq.payload, &pid, sizeof(pid));
193 if(conn->hostUniq.length) {
194 fprintf(stderr, "-U and -W are mutually exclusive\n");
197 if (!parseHostUniq(optarg, &conn->hostUniq)) {
198 fprintf(stderr, "Invalid host-uniq argument: %s\n", optarg);
205 debugFile = fopen(optarg, "w");
207 fprintf(stderr, "Could not open %s: %s\n",
208 optarg, strerror(errno));
211 fprintf(debugFile, "pppoe-discovery from pppd %s\n", PPPD_VERSION);
214 conn->ifName = xstrdup(optarg);
229 if (optind != argc) {
230 fprintf(stderr, "%s: extra argument '%s'\n", argv[0], argv[optind]);
236 fprintf(stderr, "Interface was not specified\n");
240 conn->sessionSocket = -1;
242 conn->discoverySocket = openInterface(conn->ifName, Eth_PPPOE_Discovery, conn->myEth);
243 if (conn->discoverySocket < 0) {
244 perror("Cannot create PPPoE discovery socket");
259 fprintf(stderr, "Usage: pppoe-discovery [options]\n");
260 fprintf(stderr, "Options:\n");
261 fprintf(stderr, " -I if_name -- Specify interface (mandatory option)\n");
262 fprintf(stderr, " -D filename -- Log debugging information in filename.\n");
264 " -t timeout -- Initial timeout for discovery packets in seconds\n"
265 " -a attempts -- Number of discovery attempts\n"
266 " -V -- Print version and exit.\n"
267 " -Q -- Quit Mode: Do not print access concentrator names\n"
268 " -S name -- Set desired service name.\n"
269 " -C name -- Set desired access concentrator name.\n"
270 " -U -- Use Host-Unique to allow multiple PPPoE sessions.\n"
271 " -W hexvalue -- Set the Host-Unique to the supplied hex string.\n"
272 " -h -- Print usage information.\n");
273 fprintf(stderr, "\npppoe-discovery from pppd " PPPD_VERSION "\n");