File: OptVendorSpecInfo.cpp

package info (click to toggle)
dibbler 1.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 13,352 kB
  • sloc: cpp: 60,323; ansic: 12,235; sh: 11,951; yacc: 3,418; lex: 969; makefile: 940; perl: 319; xml: 116; python: 74
file content (150 lines) | stat: -rw-r--r-- 3,963 bytes parent folder | download | duplicates (3)
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
/*
 * Dibbler - a portable DHCPv6
 *
 * authors: Tomasz Mrugalski <thomson@klub.com.pl>
 *          Marek Senderski <msend@o2.pl>
 *
 * released under GNU GPL v2 licence
 *
 */

#include <string.h>
#include <iostream>
#include <sstream>
#include "Portable.h"
#include "OptVendorSpecInfo.h"
#include "OptGeneric.h"
#include "DHCPConst.h"
#include "Logger.h"

#if defined(LINUX) || defined(BSD)
#include <arpa/inet.h>
#endif

TOptVendorSpecInfo::TOptVendorSpecInfo(uint16_t type, char * buf,  int n, TMsg* parent)
    :TOpt(type, parent), Vendor_(0)
{
    int optionCode = 0, optionLen = 0;
    if (n<4) {
	Log(Error) << "Unable to parse truncated vendor-spec info option." << LogEnd;
        Valid = false;
	return;
    }

    Vendor_ = readUint32(buf); // enterprise number
    buf += sizeof(uint32_t);
    n   -= sizeof(uint32_t);

    while (n>=4) {
        optionCode = readUint16(buf);
        buf += sizeof(uint16_t); n -= sizeof(uint16_t);
        optionLen  =  readUint16(buf);
        buf += sizeof(uint16_t); n -= sizeof(uint16_t);
        if (optionLen>n) {
            Log(Warning) << "Malformed vendor-spec info option. Suboption " << optionCode
                         << " truncated." << LogEnd;
            Valid = false;
            return;
        }

        SPtr<TOpt> opt = new TOptGeneric(optionCode, buf, optionLen, parent);
        addOption(opt);
        buf += optionLen;
        n   -= optionLen;
    }
    if (n) {
        Log(Warning) << "Extra " << n << " bytes, after parsing suboption " << optionCode
                     << " in vendor-spec info option." << LogEnd;
        Valid = false;
        return;
    }
    Valid = true;
}

TOptVendorSpecInfo::TOptVendorSpecInfo(uint16_t code, uint32_t enterprise, uint16_t sub_option_code,
                                       const char *data, int dataLen, TMsg* parent)
    :TOpt(code, parent), Vendor_(enterprise)
{
    createSuboption(sub_option_code, data, dataLen);
}

void TOptVendorSpecInfo::createSuboption(uint16_t sub_option_code, const char* data,
                                    size_t data_len) {
    if (sub_option_code) {
        SPtr<TOptGeneric> opt = new TOptGeneric(sub_option_code, data, data_len, Parent);
        addOption( (Ptr*) opt);
    }
}

TOptVendorSpecInfo::TOptVendorSpecInfo(uint16_t code, uint32_t enterprise, uint16_t sub_option_code,
                                       SPtr<TIPv6Addr> addr, TMsg* parent)
    :TOpt(code, parent), Vendor_(enterprise)
{
    createSuboption(sub_option_code, addr->getAddr(), 16);
}

TOptVendorSpecInfo::TOptVendorSpecInfo(uint16_t code, uint32_t enterprise, uint16_t sub_option_code,
                                       const std::string& str, TMsg* parent)
    :TOpt(code, parent), Vendor_(enterprise)
{
    createSuboption(sub_option_code, str.c_str(), str.length());
}

TOptVendorSpecInfo::~TOptVendorSpecInfo() 
{
}

size_t TOptVendorSpecInfo::getSize() {
    SPtr<TOpt> opt;
    unsigned int len = 8; // normal header(4) + enterprise(4)
    firstOption();
    while (opt = getOption()) {
        len += opt->getSize();
    }
    return len;
}

char * TOptVendorSpecInfo::storeSelf( char* buf)
{
    // option-code OPTION_VENDOR_OPTS (2 bytes long)
    buf = writeUint16(buf, OptType);

    // option-len size of total option-data
    buf = writeUint16(buf, getSize()-4);

    // enterprise-number (4 bytes long)
    buf = writeUint32(buf, Vendor_);

    SPtr<TOpt> opt;
    firstOption();
    while (opt = getOption())
    {
        buf = opt->storeSelf(buf);
    }
    
    return buf;
}

std::string TOptVendorSpecInfo::getPlain() {
    std::stringstream tmp;
    tmp << "vendor=" << Vendor_ << " ";

    SPtr<TOpt> opt;
    firstOption();
    while (opt = getOption())
    {
	tmp << opt->getOptType() << "=";
	tmp << opt->getPlain() << " ";
    }
    return tmp.str();
}

bool TOptVendorSpecInfo::isValid() const
{
    return true;
}

uint32_t TOptVendorSpecInfo::getVendor()
{
    return Vendor_;
}