aboutsummaryrefslogtreecommitdiff
path: root/lib/wes/wes.cpp
blob: b8d469d279a79269be0187074be6789484a0d35f (plain)
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
/* ****************************************************************************
 * wes/wes.cpp -- méthodes de la classe `wes` héritée de `wes_base`.
 * Copyright (C) 2018 Thomas Touhey <thomas@touhey.fr>
 *
 * This project is governed by the CeCILL license under French law and
 * abiding by the rules of distribution of free software. You can use,
 * modify and/or redistribute the software under the terms of the
 * CeCILL license as circulated by CEA, CNRS and INRIA at the
 * following URL: "http://www.cecill.info".
 *
 * As a counterpart to the access to the source code and rights to copy,
 * modify and redistribute granted by the license, users are provided only
 * with a limited warranty and the project's author, the holder of the
 * economic rights, and the successive licensors have only limited liability.
 *
 * In this respect, the user's attention is drawn to the risks associated
 * with loading, using, modifying and/or developing or reproducing the
 * software by the user in light of its specific status of free software,
 * that may mean that it is complicated to manipulate, and that also therefore
 * means that it is reserved for developers and experienced professionals
 * having in-depth computer knowledge. Users are therefore encouraged to load
 * and test the software's suitability as regards their requirements in
 * conditions enabling the security of their systems and/or data to be
 * ensured and, more generally, to use and operate it in the same conditions
 * as regards security.
 *
 * The fact that you are presently reading this means that you have had
 * knowledge of the CeCILL license and that you accept its terms.
 * ************************************************************************* */
#include "../internals.hpp"

using namespace wesh;

/* ---
 * Utilitaires.
 * --- */

static int decode_ipv4(const char *s, unsigned char ip[4])
{
	int i;

	ip[0] = 0;
	ip[1] = 0;
	ip[2] = 0;
	ip[3] = 0;

	for (i = 0; *s; s++) switch (*s) {
		case '1': case '2': case '3':
		case '4': case '5': case '6':
		case '7': case '8': case '9':
		          case '0':

			ip[i] = static_cast<unsigned char>(ip[i] * 10 + (*s - '0'));
			break;

		case '.':
			if (++i >= 4)
				return (1);
			break;

		default:
			return (1);
	}

	if (i < 3) {
		ip[3] = ip[i];
		ip[i] = 0;
	}

	return (0);
}

/* ---
 * Récupération de la ressource.
 * --- */

void wes::gather(const unsigned char ip[4],
	std::string http_username, std::string http_password,
	std::string ftp_username, std::string ftp_password)
{
	wespoptions_t args;
	wespret_with_id_t *resp;
	wespret_t ret;
	char hu[http_username.length() + 1],
		hp[http_password.length() + 1],
		fu[ftp_username.length() + 1],
		fp[ftp_password.length() + 1];

	if (this->id != 0)
		throw already_created_exception();

	strcpy(hu, http_username.c_str());
	strcpy(hp, http_password.c_str());
	strcpy(fu, ftp_username.c_str());
	strcpy(fp, ftp_password.c_str());

	args.type = WESPTYPE_IP;
	args.wespoptions_t_u.ip.addr.type = WESPIPTYPE_4;
	args.wespoptions_t_u.ip.addr.wespip_t_u.addr4[0] = ip[0];
	args.wespoptions_t_u.ip.addr.wespip_t_u.addr4[1] = ip[1];
	args.wespoptions_t_u.ip.addr.wespip_t_u.addr4[2] = ip[2];
	args.wespoptions_t_u.ip.addr.wespip_t_u.addr4[3] = ip[3];
	args.wespoptions_t_u.ip.http_username = hu;
	args.wespoptions_t_u.ip.http_password = hp;
	args.wespoptions_t_u.ip.ftp_username = fu;
	args.wespoptions_t_u.ip.ftp_password = fp;

	resp = gather_1(&args, clnt(this));
	if (!resp)
		throw connexion_exception();

	ret = resp->ret;
	if (ret != WESPRET_OK)
		throw_exception_using_ret(ret);

	this->id = resp->id;
}

void wes::gather(const std::string ip,
	std::string http_username, std::string http_password,
	std::string ftp_username, std::string ftp_password)
{
	this->gather(ip.c_str(), http_username, http_password,
		ftp_username, ftp_password);
}

void wes::gather(const char *ip_string,
	std::string http_username, std::string http_password,
	std::string ftp_username, std::string ftp_password)
{
	unsigned char ip[4];

	decode_ipv4(ip_string, ip);
	this->gather(ip, http_username, http_password, ftp_username, ftp_password);
}

void wes::gather(const unsigned char ip[4],
	std::string username, std::string password)
{
	this->gather(ip, username, password, username, password);
}

void wes::gather(const std::string ip,
	std::string username, std::string password)
{
	this->gather(ip.c_str(), username, password, username, password);
}

void wes::gather(const char *ip,
	std::string username, std::string password)
{
	this->gather(ip, username, password, username, password);
}

void wes::gather(const unsigned char ip[4])
{
	this->gather(ip, "admin", "wes", "adminftp", "wesftp");
}

void wes::gather(const std::string ip)
{
	this->gather(ip.c_str());
}

void wes::gather(const char *ip_string)
{
	unsigned char ip[4];

	decode_ipv4(ip_string, ip);
	this->gather(ip);
}