dnstest.c
2.65 KB
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
/*----------------------------------------------------------------
* dnstest.c - DNS test for libnet
*----------------------------------------------------------------
* libnet is (c) Copyright Chad Catlett and George Foot 1997-1998
*
* Please look in `docs' for details, documentation and
* distribution conditions.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined TARGET_MSVC || defined TARGET_MINGW32
#include <windows.h>
#define sleep(x) Sleep ((x)*1000)
#else
#include <unistd.h>
#endif
#include <libnet.h>
void list_drivers (NET_DRIVERNAME *drvs, char *title) {
printf ("\nDriver list: %s\n", title);
while (drvs->name) {
printf ("\t%d\t%s\n", drvs->num, drvs->name);
drvs++;
}
printf ("\n");
}
int in_list (NET_DRIVERNAME *drvs, int x) {
while (drvs->name) {
if (x == drvs->num) return 1;
drvs++;
}
return 0;
}
int main (void) {
NET_DRIVERNAME *drivers;
char buffer[NET_MAX_ADDRESS_LENGTH], buffer2[NET_MAX_ADDRESS_LENGTH];
char *ch;
int not_ok, x, choice;
NET_DRIVERLIST avail;
net_init();
net_loadconfig (NULL);
printf ("Detecting available drivers...\n");
avail = net_detectdrivers (net_drivers_all);
printf ("Getting detected driver names...\n");
drivers = net_getdrivernames (avail);
do {
list_drivers (drivers, "Available drivers");
printf ("Please choose a driver from the above list.\n");
fgets (buffer, 10, stdin);
choice = atoi (buffer);
} while (!in_list (drivers, choice));
free (drivers);
avail = net_driverlist_create();
net_driverlist_add (avail, choice);
if (!net_initdrivers (avail)) {
printf("Error initialising driver.\n");
exit (1);
}
do {
printf ("Enter address to prepare:\n");
fgets (buffer, sizeof buffer, stdin);
if (feof (stdin)) break;
ch = strchr (buffer, '\n');
if (!ch) {
printf ("Too long for this lame program...\n");
while (1) {
char ch = getchar();
if (ch == EOF || ch == '\n') break;
}
} else {
NET_PREPADDR_DATA *data;
*ch = '\0';
printf ("Converting...\n");
data = net_prepareaddress (choice, buffer, buffer2);
if (!data) {
printf ("Failed to initiate\n");
} else {
int x;
printf ("Waiting...");
do {
printf (".");
fflush(stdout);
sleep (1);
x = net_poll_prepareaddress (data);
} while (!x);
printf ("\n");
if (x < 0) {
printf ("Failed to convert address\n");
} else {
printf ("%s => %s\n", buffer, buffer2);
}
}
}
} while (!feof (stdin));
printf ("Quitting...\n");
return 0;
}