1/*
2 * Perform PPPoE discovery
3 *
4 * Copyright (C) 2000-2001 by Roaring Penguin Software Inc.
5 * Copyright (C) 2004 Marco d'Itri <md@linux.it>
6 *
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.
9 *
10 */
11
12#include <stdio.h>
13#include <stdlib.h>
14#include <unistd.h>
15#include <errno.h>
16#include <string.h>
17
18#include "pppoe.h"
19
20char *xstrdup(const char *s);
21void usage(void);
22
23void die(int status)
24{
25	exit(status);
26}
27
28int main(int argc, char *argv[])
29{
30    int opt;
31    PPPoEConnection *conn;
32
33    conn = malloc(sizeof(PPPoEConnection));
34    if (!conn)
35	fatalSys("malloc");
36
37    memset(conn, 0, sizeof(PPPoEConnection));
38
39    while ((opt = getopt(argc, argv, "I:D:VUAS:C:h")) > 0) {
40	switch(opt) {
41	case 'S':
42	    conn->serviceName = xstrdup(optarg);
43	    break;
44	case 'C':
45	    conn->acName = xstrdup(optarg);
46	    break;
47	case 'U':
48	    conn->useHostUniq = 1;
49	    break;
50	case 'D':
51	    conn->debugFile = fopen(optarg, "w");
52	    if (!conn->debugFile) {
53		fprintf(stderr, "Could not open %s: %s\n",
54			optarg, strerror(errno));
55		exit(1);
56	    }
57	    fprintf(conn->debugFile, "pppoe-discovery %s\n", VERSION);
58	    break;
59	case 'I':
60	    conn->ifName = xstrdup(optarg);
61	    break;
62	case 'A':
63	    /* this is the default */
64	    break;
65	case 'V':
66	case 'h':
67	    usage();
68	    exit(0);
69	default:
70	    usage();
71	    exit(1);
72	}
73    }
74
75    /* default interface name */
76    if (!conn->ifName)
77	conn->ifName = strdup("eth0");
78
79    conn->discoverySocket = -1;
80    conn->sessionSocket = -1;
81    conn->printACNames = 1;
82
83    discovery(conn);
84    exit(0);
85}
86
87void rp_fatal(char const *str)
88{
89    char buf[1024];
90
91    printErr(str);
92    sprintf(buf, "pppoe-discovery: %.256s", str);
93    exit(1);
94}
95
96void fatalSys(char const *str)
97{
98    char buf[1024];
99    int i = errno;
100
101    sprintf(buf, "%.256s: %.256s", str, strerror(i));
102    printErr(buf);
103    sprintf(buf, "pppoe-discovery: %.256s: %.256s", str, strerror(i));
104    exit(1);
105}
106
107void sysErr(char const *str)
108{
109    rp_fatal(str);
110}
111
112char *xstrdup(const char *s)
113{
114    register char *ret = strdup(s);
115    if (!ret)
116	sysErr("strdup");
117    return ret;
118}
119
120void usage(void)
121{
122    fprintf(stderr, "Usage: pppoe-discovery [options]\n");
123    fprintf(stderr, "\nVersion " VERSION "\n");
124}
125