File: host.C

package info (click to toggle)
librudiments0 0.27-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 4,528 kB
  • ctags: 2,284
  • sloc: cpp: 14,657; sh: 7,547; ansic: 2,664; makefile: 945; xml: 15
file content (79 lines) | stat: -rw-r--r-- 2,032 bytes parent folder | download
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
// Copyright (c) 2002  David Muse
// See the file COPYING for more information

#include <rudiments/hostentry.h>
#include <stdio.h>
#include <sys/socket.h>

// this function takes addr[]={127,1,1,0} and returns "127.1.1.0"
char *getAddressString(int length, const char *addr) {
	char	*address=new char[(length*4)+1];
	address[0]=(char)NULL;
	for (int byte=0; byte<length; byte++) {
		sprintf(address,"%s%d",address,addr[byte]);
		if (byte<length-1) {
			sprintf(address,"%s.",address);
		}
	}
	return address;
}

int main(int argc, const char **argv) {


	// get the host information for "localhost"
	hostentry	he;
	he.initialize("localhost");

	// print the components individually
	printf("Individually...\n");
	printf("	Name: %s\n",he.getName());
	printf("	Alias list:\n");
	for (int i=0; he.getAliasList()[i]; i++) {
		printf("		%s\n",he.getAliasList()[i]);
	}
	printf("	Address type: %d\n",he.getAddressType());
	printf("	Address length: %d\n",he.getAddressLength());
	printf("	Address list:\n");
	for (int i=0; he.getAddressList()[i]; i++) {
		char	*addr=getAddressString(he.getAddressLength(),
						he.getAddressList()[i]);
		printf("		%s\n",addr);
		delete[] addr;
	}
	printf("\n");

	// use the built in print method
	printf("Built In...\n");
	he.print();
	printf("\n");



	// get the host information for "127.0.0.1"
	char	address[]={127,0,0,1};
	he.initialize(address,4,AF_INET);

	// print the components individually
	printf("Individually...\n");
	printf("	Name: %s\n",he.getName());
	printf("	Alias list:\n");
	for (int i=0; he.getAliasList()[i]; i++) {
		printf("		%s\n",he.getAliasList()[i]);
	}
	printf("	Address type: %d\n",he.getAddressType());
	printf("	Address length: %d\n",he.getAddressLength());
	printf("	Address list:\n");
	for (int i=0; he.getAddressList()[i]; i++) {
		char	*addr=getAddressString(he.getAddressLength(),
						he.getAddressList()[i]);
		printf("		%s\n",addr);
		delete[] addr;
	}
	printf("\n");

	// use the built in print method
	printf("Built In...\n");
	he.print();
	printf("\n");
}