summaryrefslogtreecommitdiff
path: root/packages/ppp/ppp-2.4.1/ppp-tdbread.patch
blob: 80232ac25f55707c42802cf4be93f6a34d123b22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
diff -Nur ppp-2.4.1/pppd/Makefile.linux myppp/ppp-2.4.1/pppd/Makefile.linux
--- ppp-2.4.1/pppd/Makefile.linux	2006-09-14 14:52:54.000000000 +0200
+++ ppp-2.4.1/pppd/Makefile.linux	2006-09-14 14:55:44.000000000 +0200
@@ -17,7 +17,7 @@
 	   auth.o options.o demand.o utils.o sys-linux.o ipxcp.o multilink.o \
 	   tdb.o tty.o
 
-all: pppd
+all: pppd tdbread
 
 #
 # include dependancies if present and backup if as a header file
@@ -114,9 +114,10 @@
 
 INSTALL= install
 
-install: pppd
+install: pppd tdbread
 	mkdir -p $(BINDIR) $(MANDIR)
 	$(INSTALL) -c -m 555 pppd $(BINDIR)/pppd
+	$(INSTALL) -c -m 555 tdbread $(BINDIR)/tdbread
 	if chgrp pppusers $(BINDIR)/pppd 2>/dev/null; then \
 	  chmod o-rx,u+s $(BINDIR)/pppd; fi
 	$(INSTALL) -c -m 444 pppd.8 $(MANDIR)/man8
@@ -124,8 +125,11 @@
 pppd: $(PPPDOBJS)
 	$(CC) $(CFLAGS) $(LDFLAGS) -o pppd $(PPPDOBJS) $(LIBS)
 
+tdbread: tdbread.o tdb.o
+	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ 
+
 clean:
-	rm -f $(PPPDOBJS) pppd *~ #* core
+	rm -f $(PPPDOBJS) tdbread.o tdbread pppd *~ #* core
 
 depend:
 	$(CPP) -M $(CFLAGS) $(PPPDSRCS) >.depend
diff -Nur ppp-2.4.1/pppd/tdbread.c myppp/ppp-2.4.1/pppd/tdbread.c
--- ppp-2.4.1/pppd/tdbread.c	1970-01-01 01:00:00.000000000 +0100
+++ ppp-2.4.1/pppd/tdbread.c	2006-09-14 14:52:32.000000000 +0200
@@ -0,0 +1,153 @@
+/**
+ * @file    tdbread.c
+ * @author  Thomas Geffert <geffert@4g-systems.com>
+ * @date    Thu Sep 14 10:28:31 2006
+ *
+ * @brief  Small program to extract information from pppd.tbd database.
+ *         You can get information about a specific ppp process with its pid
+ *         or view all keys available in the database.
+ */
+
+/*
+ * (c) COPYRIGHT 2006 by 4G Systems GmbH Germany
+ *
+ * Redistribution and use in source and binary forms are permitted
+ * provided that the above copyright notice and this paragraph are
+ * duplicated in all such forms AND provided that this software or
+ * any derived work is only used as part of the PPP daemon (pppd)
+ * and related utilities.
+ * The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * Note: this software is also available under the Gnu Public License
+ * version 2 or later.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <fcntl.h>
+#include <getopt.h>
+#include <signal.h>				/* needed for tdb.h starting with ppp-2.4.3 */
+
+#include "tdb.h"
+#include "pppd.h"
+#include "pathnames.h"
+
+/**
+ * Callback function for tdb_traverse: show a key and its associated data
+ *
+ * @param tdb pointer to database
+ * @param key hash key
+ * @param dbuf data belonging to key
+ * @param state unused data pointer
+ *
+ * @return 0 if success, 1 to stop calling function
+ */
+static int show(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
+{
+	printf("%.*s: \"%.*s\"\n", key.dsize, key.dptr, dbuf.dsize, dbuf.dptr);
+	return 0;
+}
+
+/**
+ * Parse command line option. Option is used to sepcify for which ppp process
+ * information should be shown.
+ *
+ * @param argc  number of options
+ * @param argv  pointer to array with options
+ *
+ * @return empty key if no valid option found, or key selected by config option
+ */
+TDB_DATA parse_options(int argc, char **argv)
+{
+	TDB_DATA key = { NULL, 0 };
+	static char keyname[32] = { 0 };
+	int c;
+	while (1) {
+		int option_index = 0;
+		static struct option long_options[] = {
+			{"pid", 1, 0, 'p'}, {"device", 1, 0, 'd'}, {"ifname", 1, 0, 'i'},
+			{"ipremote", 1, 0, 'r'}, {"help", 0, 0, 'h'}, {0, 0, 0, 0}
+		};
+
+		c = getopt_long (argc, argv, "p:d:i:r:h", long_options, &option_index);
+		if (c == -1) {
+			if ( optind<argc ) {
+				c = '?'; // force display of usage
+			} else {
+				break;
+			}
+		}
+
+		switch (c) {
+		case 'p':
+			snprintf(keyname, sizeof(keyname), "PPPD_PID=%s", optarg);
+			break;
+		case 'i':
+			snprintf(keyname, sizeof(keyname), "IFNAME=%s", optarg);
+			break;
+		case 'd':
+			snprintf(keyname, sizeof(keyname), "DEVICE=%s", optarg);
+			break;
+		case 'r':
+			snprintf(keyname, sizeof(keyname), "IPREMOTE=%s", optarg);
+			break;
+		case '?':
+		case 'h':
+			fprintf(stderr, "Usage: tdbread [--pid pid|--device devname|--ifname ifname|--ipremote ipremote]\n"
+					" If several options are given, only the last one is used.\n");
+			exit(1);
+			break;
+		}
+	}
+
+	if ( *keyname != 0 ) {
+		key.dptr = (char *) keyname;
+		key.dsize = strlen(keyname);
+	}
+
+	return key;
+}
+
+
+int main(int argc, char **argv) {
+	TDB_CONTEXT *pppdb;
+	int rc=1;
+
+	/* open database */
+	pppdb = tdb_open(_PATH_PPPDB, 0, 0, O_RDWR, 0644);
+	if (pppdb == NULL) {
+		fprintf(stderr, "Cannot open DB %s\n", _PATH_PPPDB);
+		return 1;
+	}
+
+	TDB_DATA key = parse_options(argc, argv);
+
+	if (key.dsize==0) {
+		tdb_traverse(pppdb, show, NULL);
+	} else {
+		if (tdb_exists(pppdb, key)) {
+			TDB_DATA key2;
+			/* value of pppd_pid entry points to entry with real info */
+			key2 = tdb_fetch(pppdb, key);
+			if (tdb_exists(pppdb, key2)) {
+				TDB_DATA data;
+				data = tdb_fetch(pppdb, key2);
+				printf("%.*s\n", data.dsize, data.dptr);
+				rc=0;
+			} else {
+				fprintf(stderr, "No data found for %.*s\n", key2.dsize, key2.dptr);
+			}
+		} else {
+			fprintf(stderr, "Key %.*s not found\n", key.dsize, key.dptr);
+		}
+	}
+
+	tdb_close(pppdb);
+
+	return rc;
+}