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 #ifdef HAVE_NETPACKET_PACKET_H
32 #include <netpacket/packet.h>
33 #elif defined(HAVE_LINUX_IF_PACKET_H)
34 #include <linux/if_packet.h>
37 #ifdef HAVE_ASM_TYPES_H
38 #include <asm/types.h>
41 #ifdef HAVE_SYS_IOCTL_H
42 #include <sys/ioctl.h>
49 #ifdef HAVE_NET_IF_ARP_H
50 #include <net/if_arp.h>
56 static FILE *debugFile;
63 vfprintf(stderr, fmt, pvar);
74 vfprintf(stderr, fmt, pvar);
84 vfprintf(stderr, fmt, pvar);
100 init_pr_log(const char *prefix, int level)
111 pr_log(void *arg, char *fmt, ...)
115 vfprintf(debugFile, fmt, ap);
120 strlcpy(char *dest, const char *src, size_t len)
122 size_t ret = strlen(src);
128 strncpy(dest, src, len - 1);
136 xstrdup(const char *s)
138 char *ret = strdup(s);
147 get_time(struct timeval *tv)
149 return gettimeofday(tv, NULL);
153 term_handler(int signum)
158 static void usage(void);
160 int main(int argc, char *argv[])
163 PPPoEConnection *conn;
165 signal(SIGINT, term_handler);
166 signal(SIGTERM, term_handler);
168 conn = malloc(sizeof(PPPoEConnection));
174 memset(conn, 0, sizeof(PPPoEConnection));
177 conn->discoveryTimeout = PADI_TIMEOUT;
178 conn->discoveryAttempts = MAX_PADI_ATTEMPTS;
180 while ((opt = getopt(argc, argv, "I:D:VUQS:C:W:t:a:h")) > 0) {
183 conn->serviceName = xstrdup(optarg);
186 conn->acName = xstrdup(optarg);
189 if (sscanf(optarg, "%d", &conn->discoveryTimeout) != 1) {
190 fprintf(stderr, "Illegal argument to -t: Should be -t timeout\n");
193 if (conn->discoveryTimeout < 1) {
194 conn->discoveryTimeout = 1;
198 if (sscanf(optarg, "%d", &conn->discoveryAttempts) != 1) {
199 fprintf(stderr, "Illegal argument to -a: Should be -a attempts\n");
202 if (conn->discoveryAttempts < 1) {
203 conn->discoveryAttempts = 1;
207 if(conn->hostUniq.length) {
208 fprintf(stderr, "-U and -W are mutually exclusive\n");
211 pid_t pid = getpid();
212 conn->hostUniq.type = htons(TAG_HOST_UNIQ);
213 conn->hostUniq.length = htons(sizeof(pid));
214 memcpy(conn->hostUniq.payload, &pid, sizeof(pid));
218 if(conn->hostUniq.length) {
219 fprintf(stderr, "-U and -W are mutually exclusive\n");
222 if (!parseHostUniq(optarg, &conn->hostUniq)) {
223 fprintf(stderr, "Invalid host-uniq argument: %s\n", optarg);
230 debugFile = fopen(optarg, "w");
232 fprintf(stderr, "Could not open %s: %s\n",
233 optarg, strerror(errno));
236 fprintf(debugFile, "pppoe-discovery from pppd %s\n", VERSION);
239 conn->ifName = xstrdup(optarg);
254 /* default interface name */
256 conn->ifName = xstrdup("eth0");
258 conn->sessionSocket = -1;
260 conn->discoverySocket = openInterface(conn->ifName, Eth_PPPOE_Discovery, conn->myEth);
261 if (conn->discoverySocket < 0) {
262 perror("Cannot create PPPoE discovery socket");
277 fprintf(stderr, "Usage: pppoe-discovery [options]\n");
278 fprintf(stderr, "Options:\n");
279 fprintf(stderr, " -I if_name -- Specify interface (default eth0)\n");
280 fprintf(stderr, " -D filename -- Log debugging information in filename.\n");
282 " -t timeout -- Initial timeout for discovery packets in seconds\n"
283 " -a attempts -- Number of discovery attempts\n"
284 " -V -- Print version and exit.\n"
285 " -Q -- Quit Mode: Do not print access concentrator names\n"
286 " -S name -- Set desired service name.\n"
287 " -C name -- Set desired access concentrator name.\n"
288 " -U -- Use Host-Unique to allow multiple PPPoE sessions.\n"
289 " -W hexvalue -- Set the Host-Unique to the supplied hex string.\n"
290 " -h -- Print usage information.\n");
291 fprintf(stderr, "\npppoe-discovery from pppd " VERSION "\n");