[go: up one dir, main page]

File: Requestor.cpp

package info (click to toggle)
dibbler 1.0.1-1
  • links: PTS
  • area: main
  • in suites: buster, stretch
  • size: 13,308 kB
  • ctags: 10,470
  • sloc: cpp: 60,323; ansic: 12,233; sh: 11,951; yacc: 3,418; lex: 969; makefile: 940; perl: 319; xml: 116; python: 74
file content (184 lines) | stat: -rw-r--r-- 5,218 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
 * Dibbler - a portable DHCPv6
 *
 * author: Tomasz Mrugalski <thomson@klub.com.pl>
 *
 * Released under GNU GPL v2 licence
 *
 */

#include <string.h>
#include <stdlib.h>
#include "Portable.h"
#include "ReqCfgMgr.h"
#include "Portable.h"
#include "IfaceMgr.h"
#include "ReqTransMgr.h"
#include "Logger.h"

#ifdef WIN32
#include <winsock2.h>
#include <Ws2tcpip.h>
#endif

using namespace std;

void printHelp()
{
    cout << "Usage:" << endl
         << "-i IFACE - send query using iface inteface, e.g. -i eth0" << endl
         << "-addr ADDR - query about address, e.g. -addr 2000::43" << endl
         << "-duid DUID - query about DUID, e.g. -duid 00:11:22:33:44:55:66:77:88" << endl
         << "-timeout 10 - query timeout, specified in seconds" << endl
         << "-dstaddr 2000::1 - destination address (by default it is ff02::1:2)" << endl;
}

bool parseCmdLine(ReqCfgMgr *a, int argc, char *argv[])
{
    char * addr    = 0;
    char * duid    = 0;
    char * iface   = 0;
    char * dstaddr = 0;
    int timeout  = 60; // default timeout value
    for (int i=1; i<argc; i++) {
        if (!strncmp(argv[i],"-addr", 5)) {
            if (argc==i) {
                Log(Error) << "Unable to parse command-line. -addr used, but actual address is missing." << LogEnd;
                return false;
            }
            addr = argv[++i];
            continue;
        }
        if (!strncmp(argv[i],"-duid", 5)) {
            if (argc==i) {
                Log(Error) << "Unable to parse command-line. -duid used, but actual DUID is missing." << LogEnd;
                return false;
            }
            duid = argv[++i];
            continue;
        }
        if (!strncmp(argv[i],"-i", 2)) {
            if (argc==i) {
                Log(Error) << "Unable to parse command-line. -i used, but actual interface name is missing." << LogEnd;
                return false;
            }
            iface = argv[++i];
            continue;
        }
        if (!strncmp(argv[i],"-timeout", 8)) {
            if (argc==i) {
                Log(Error) << "Unable to parse command-line. -timeout used, but actual timeout value is missing." << LogEnd;
                return false;
            }
            timeout = atoi(argv[++i]);
            continue;
        }
        if (!strncmp(argv[i],"-dstaddr", 7)) {
            if (argc==i) {
                Log(Error) << "Unable to parse command-line. -dstaddr used, but actual destination address is missing." << LogEnd;
            }
            dstaddr = argv[++i];
            continue;
        }
        if (!strncmp(argv[i], "--help", 5) || !strncmp(argv[i], "-h", 5) || !strncmp(argv[i], "/help", 5) ||
            !strncmp(argv[i], "-?", 2) || !strncmp(argv[i], "/?",2)) {
            return false;
        }

        Log(Error) << "Unable to parse command-line parameter: " << argv[i] << LogEnd;
        Log(Error) << "Please use -h for help." << LogEnd;
        return false;
    }

    if (!addr && !duid) {
        Log(Error) << "Both address and DUID not defined." << LogEnd;
        return false;
    }
    if (addr && duid) {
        Log(Error) << "Both address and DUID defined." << LogEnd;
        return false;
    }

    if (!iface) {
        Log(Error) << "Interface not defined. Please use -i command-line switch." << LogEnd;
        return false;
    }

    a->addr  = addr;
    a->duid  = duid;
    a->iface = iface;
    a->timeout= timeout;
    a->dstaddr = dstaddr;
    return true;
}

int initWin()
{
#ifdef WIN32
    WSADATA wsaData;
        if( WSAStartup( MAKEWORD( 2, 2 ), &wsaData )) {
        cout << "Unable to load WinSock 2.2 library." << endl;
                return -1;
        }
#endif
    return 0;
}

int main(int argc, char *argv[])
{
    ReqCfgMgr a;

    initWin();

    srand(static_cast<unsigned int>(time(NULL))); // Windows

    logger::setLogName("Requestor");
        logger::Initialize((char*)REQLOG_FILE);

    cout << DIBBLER_COPYRIGHT1 << " (REQUESTOR)" << endl;
    cout << DIBBLER_COPYRIGHT2 << endl;
    cout << DIBBLER_COPYRIGHT3 << endl;
    cout << DIBBLER_COPYRIGHT4 << endl;
    cout << endl;

    if (!parseCmdLine(&a, argc, argv)) {
        Log(Crit) << "Aborted. Invalid command-line parameters or help called." << LogEnd;
        printHelp();
        return -1;
    }

    TIfaceMgr   * ifaceMgr = new TIfaceMgr(REQIFACEMGR_FILE, true);
    ReqTransMgr * transMgr = new ReqTransMgr(ifaceMgr);

    transMgr->SetParams(&a);

    if (!transMgr->BindSockets()) {
        Log(Crit) << "Aborted. Socket binding failed." << LogEnd;
        return LOWLEVEL_ERROR_BIND_FAILED;
    }

    if (!transMgr->SendMsg()) {
        Log(Crit) << "Aborted. Message transmission failed." << LogEnd;
        return LOWLEVEL_ERROR_SOCKET;
    }

	if (!transMgr->WaitForRsp()) {

    }

    delete transMgr;

    return LOWLEVEL_NO_ERROR;
}


/* linker workarounds: dummy functions */
extern "C"
{
    void *hmac_sha (const char *buffer, size_t len, char *key, size_t key_len, char *resbuf, int type) { return 0; }
    void *hmac_md5 (const char *buffer, size_t len, char *key, size_t key_len, char *resbuf) { return 0; }
}

#ifndef WIN32
//unsigned getDigestSize(enum DigestTypes type) { return 0; }
#endif