-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAS.h
More file actions
56 lines (47 loc) · 1.3 KB
/
Copy pathAS.h
File metadata and controls
56 lines (47 loc) · 1.3 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
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <string>
#include <sstream>
#include "Exceptions.h"
#ifndef __AS_H__
#define __AS_H__
class AS
{
public:
uint32_t ASno;
std::string hostname;
uint16_t portno;
union {
sockaddr_in saddr_in;
sockaddr saddr;
};
struct ASexception: public easyException {
ASexception(std::string s): easyException(s) {}
};
AS():ASno(99),portno(0) {}
//Construtor takes an AS number, hostname, portno, and whether to lookup the hostname
AS(uint32_t ASno, std::string hostname, uint16_t portno, bool lookup=true):
ASno(ASno),
hostname(hostname),
portno(portno)
{
if(ASno > 9) {
throw ASexception("invalid AS number");
}
//lookup the hostname
if(lookup) {
struct hostent * he;
he = gethostbyname( hostname.c_str() );
if ( he == NULL ) {
std::stringstream s;
s << "Unabled to lookup host '" << hostname << "'";
throw ASexception(s.str());
}
saddr_in.sin_family = AF_INET;
memcpy(&saddr_in.sin_addr, he->h_addr_list[0], 4);
saddr_in.sin_port = htons(portno);
}
}
};
#endif // __AS_H__