File: OptFQDN.cpp

package info (click to toggle)
dibbler 0.7.3-1.3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 11,148 kB
  • ctags: 8,720
  • sloc: cpp: 54,863; sh: 9,389; ansic: 8,659; yacc: 2,570; makefile: 1,061; lex: 842; perl: 49; xml: 6
file content (174 lines) | stat: -rw-r--r-- 3,621 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
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
/*
* Dibbler - a portable DHCPv6
*
* authors: Adrien CLERC, Bahattin DEMIRPLAK, Gatant ELEOUET
*          Mickal GURIN, Lionel GUILMIN, Laurline PROVOST
*          from ENSEEIHT, Toulouse, France
*
* released under GNU GPL v2 licence
*
* $Id: OptFQDN.cpp,v 1.8 2008-11-11 20:55:46 thomson Exp $
*
*/

#include "Portable.h"
#include "OptFQDN.h"
#include "Logger.h"

TOptFQDN::TOptFQDN(string domain, TMsg* parent)
		:TOpt(OPTION_FQDN, parent) {
    fqdn = domain;
    flag_N = false;
    flag_S = false;
    flag_O = false; // This flag is always off in client messages.

    Valid = true;
}

TOptFQDN::TOptFQDN(char * &buf, int &bufsize, TMsg* parent)
		:TOpt(OPTION_FQDN, parent) {
    this->Valid = false;
    // Extracting flags...
    unsigned char flags = *buf;
    this->flag_N = flags & FQDN_N;
    this->flag_S = flags & FQDN_S;
    this->flag_O = flags & FQDN_O;
    buf += 1;
    bufsize -= 1;

    //Extracting domain name
    fqdn = "";
    if ( bufsize <= 255 ) {
	unsigned char tmplength = *buf;
	if (tmplength>bufsize) 
	{
	    Log(Warning) << "Malformed FQDN option: domain name encoding is invalid. "
			 << "(Is this message sent by Microsoft? Tell them to fix the FQDN option.)" << LogEnd;
	    Valid = false;
	    return;
	}

	buf++;
	while (tmplength != 0) {
	    fqdn.append(buf, tmplength);
	    buf += tmplength;
	    bufsize -= tmplength;
	    tmplength = *buf;
	    if (tmplength>bufsize) 
	    {
		Log(Warning) << "Malformed FQDN option: domain name encoding is invalid."
			     << "(Is this message sent by Microsoft? Tell them to fix the FQDN option.)" << LogEnd;
		Valid = false;
		return;
	    }
	    buf++;
	    if ( tmplength != 0 ) {
		fqdn.append(".");
	    }
	}
	buf++;
	bufsize--;
	Valid = true;
    }
    Log(Debug) << "FQDN: FQDN option received: fqdn name=" << fqdn << LogEnd;
}

TOptFQDN::~TOptFQDN() {
	return;
}

void TOptFQDN::setNFlag(bool flag) {
	flag_N = flag;
}

void TOptFQDN::setSFlag(bool flag) {
	flag_S = flag;
}

void TOptFQDN::setOFlag(bool flag) {
	flag_O = flag;
}

string TOptFQDN::getFQDN() {
	return fqdn;
}

/** 
 * @brief returns option size
 *
 * Each dot will be removed from the string, and replaced with a length < 63
 * The first length and the final 0 will increased the fqdn string length by 2
 * We also have to add 4 for the header (option type and size) and 1 for the flags.
 * 2 + 1 + 4 = 7
 * 
 * 
 * 
 * @return size of the option (without option header)
 */
int TOptFQDN::getSize() {
        if (fqdn.length())
	    return fqdn.length() + 7;
	else
	    return 6;
}

char * TOptFQDN::storeSelf(char *buffer) {
	// Type and size
	*(short*)buffer = htons(OptType);
	buffer+=2;
	*(short*)buffer = htons(getSize()-4);
	buffer+=2;
//Flag Initialization
	*buffer = 0;
	if (flag_N) {
		*buffer += FQDN_N;
	}
	if (flag_S) {
		*buffer += FQDN_S;
	}
	if (flag_O) {
		*buffer += FQDN_O;
	}
	buffer++;

	//FQDN data :)
	if ( fqdn.length() != 0 ) {
		string copy = "";
		copy += fqdn;
	        std::string::size_type dotpos = copy.find('.', 0);
		while(dotpos != string::npos) {
			*buffer = dotpos;
			buffer++;
			memcpy(buffer, copy.c_str(), dotpos);
			buffer += dotpos;
			copy.assign(copy, dotpos + 1, copy.length());
			dotpos = copy.find('.', 0);
		}

		*buffer = copy.length();
		buffer++;
		memcpy(buffer, copy.c_str(), copy.length());
		buffer += copy.length();
	}
	*buffer = 0;
	//buffer++;

	return buffer;
}

bool TOptFQDN::isValid() {
	//TODO Check the validity of this option
	return Valid;
}

bool TOptFQDN::getNFlag( ) {
	return flag_N;
}

bool TOptFQDN::getSFlag( ) {
	return flag_S;
}

bool TOptFQDN::getOFlag( ) {
	return flag_O;
}