5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2003 - 2004 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
20 #include "silcincludes.h"
21 #include "silcclient.h"
24 /* Write the HTML data file of the gathered data from the connection. */
26 bool silc_map_writehtml(SilcMap map, SilcMapConnection mapconn)
30 char filename[256], line[128];
33 /* Generate data filename. First configure hostname is the filename */
34 silc_dlist_start(mapconn->hostnames);
35 hostname = silc_dlist_get(mapconn->hostnames);
36 memset(filename, 0, sizeof(filename));
37 snprintf(filename, sizeof(filename) - 1, "%s_%d.html", hostname,
40 /* Open for writing */
41 fp = fopen(filename, "w+");
43 fprintf(stderr, "Could not open file '%s'\n", filename);
47 /* Write the HTML page */
49 fprintf(fp, "<!-- Automatically generated by silcmap -->\n");
50 fprintf(fp, "<br /><hr ><br />\n");
54 fprintf(fp, "<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\">\n");
55 silc_dlist_start(mapconn->hostnames);
56 while ((hostname = silc_dlist_get(mapconn->hostnames)) != SILC_LIST_END)
57 fprintf(fp, "<tr><td><b>Hostname</b></td><td> :</td><td> %s</td></tr>\n", hostname);
59 silc_dlist_start(mapconn->ips);
60 while ((hostname = silc_dlist_get(mapconn->ips)) != SILC_LIST_END)
61 fprintf(fp, "<tr><td><b>IP</b></td><td> :</td><td> %s</td></tr>\n", hostname);
63 fprintf(fp, "<tr><td><b>Port</b></td><td> :</td><td> %d</td></tr>\n", mapconn->port);
64 fprintf(fp, "<tr><td><b>Country</b></td><td> :</td><td> %s</td></tr>\n", mapconn->country);
65 fprintf(fp, "<tr><td><b>City</b></td><td> :</td><td> %s</td></tr>\n", mapconn->city);
66 fprintf(fp, "<tr><td><b>Admin</b></td><td> :</td><td> %s</td></tr>\n", mapconn->admin);
67 fprintf(fp, "</table>\n");
70 if (mapconn->public_key) {
71 SilcPublicKey public_key;
72 SilcPublicKeyIdentifier ident;
73 char *fingerprint, *babbleprint;
77 SilcUInt32 key_len = 0;
81 fprintf(fp, " <br /><hr ><br />\n");
82 fprintf(fp, "<b>Public Key:</b> <br />\n");
83 fprintf(fp, "<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\">\n");
85 if (silc_pkcs_load_public_key(mapconn->public_key, &public_key,
86 SILC_PKCS_FILE_PEM) == FALSE)
87 if (silc_pkcs_load_public_key(mapconn->public_key, &public_key,
88 SILC_PKCS_FILE_BIN) == FALSE) {
89 fprintf(stderr, "Could not load public key file `%s'\n",
94 ident = silc_pkcs_decode_identifier(public_key->identifier);
95 pk = silc_pkcs_public_key_encode(public_key, &pk_len);
96 fingerprint = silc_hash_fingerprint(NULL, pk, pk_len);
97 babbleprint = silc_hash_babbleprint(NULL, pk, pk_len);
99 if (silc_pkcs_alloc(public_key->name, &pkcs)) {
100 key_len = silc_pkcs_public_key_set(pkcs, public_key);
101 silc_pkcs_free(pkcs);
104 fprintf(fp, "<tr><td> ");
105 fprintf(fp, "Public key file</td><td> :</td><td> <a href=\"%s\">%s</a></td></tr>\n",
106 mapconn->public_key, mapconn->public_key);
107 fprintf(fp, "<tr><td> ");
108 fprintf(fp, "Algorithm</td><td> :</td><td> %s</td></tr>\n", public_key->name);
110 fprintf(fp, "<tr><td> ");
111 fprintf(fp, "Key length</td><td> :</td><td> %d bits</td></tr>\n", (unsigned int)key_len);
113 if (ident->realname) {
114 fprintf(fp, "<tr><td> ");
115 fprintf(fp, "Real name</td><td> :</td><td> %s</td></tr>\n", ident->realname);
117 if (ident->username) {
118 fprintf(fp, "<tr><td> ");
119 fprintf(fp, "Username</td><td> :</td><td> %s</td></tr>\n", ident->username);
122 fprintf(fp, "<tr><td> ");
123 fprintf(fp, "Hostname</td><td> :</td><td> %s</td></tr>\n", ident->host);
126 fprintf(fp, "<tr><td> ");
127 fprintf(fp, "Email</td><td> :</td><td> %s</td></tr>\n", ident->email);
130 fprintf(fp, "<tr><td> ");
131 fprintf(fp, "Organization</td><td> :</td><td> %s</td></tr>\n", ident->org);
133 if (ident->country) {
134 fprintf(fp, "<tr><td> ");
135 fprintf(fp, "Country</td><td> :</td><td> %s</td></tr>\n", ident->country);
137 fprintf(fp, "<tr><td> ");
138 fprintf(fp, "Fingerprint</td><td> :</td><td> <tt>%s</tt></td></tr>\n", fingerprint);
139 fprintf(fp, "<tr><td> ");
140 fprintf(fp, "Babbleprint</td><td> :</td><td> <tt>%s</tt></td></tr>\n", babbleprint);
141 fprintf(fp, "</table>\n");
143 pd = fopen(mapconn->public_key, "r");
147 pk_len = silc_file_size(mapconn->public_key);
148 pdd = silc_calloc(pk_len + 2, sizeof(*pdd));
151 fread(pdd, pk_len, 1, pd);
154 fprintf(fp, "<br /><tt><small>\n");
156 while (silc_gets(line, sizeof(line) - 1, pdd, pk_len + 1, &begin) != EOF)
157 fprintf(fp, "%s<br />\n", line);
158 fprintf(fp, "</small></tt><br />\n");
162 silc_free(fingerprint);
163 silc_free(babbleprint);
165 silc_pkcs_public_key_free(public_key);
166 silc_pkcs_free_identifier(ident);
170 if (mapconn->description && mapconn->description[0]) {
171 fprintf(fp, "<hr ><br />\n");
172 fprintf(fp, "<b>Description:</b> <br />\n");
173 fprintf(fp, "%s<br /> <br />\n", mapconn->description);
177 if (mapconn->connect) {
178 fprintf(fp, "<hr ><br />\n");
179 fprintf(fp, "<b>Server status:</b> <br />\n");
182 "Server is currently down and unreachable. "
183 "Please try again later to connect the server.<br />\n");
186 "Server is up and running<br />\n");
189 if (mapconn->connect && !mapconn->down) {
190 int days, hours, mins, secs, uptime;
192 uptime = mapconn->data.uptime;
193 days = uptime / (24 * 60 * 60);
194 uptime -= days * (24 * 60 * 60);
195 hours = uptime / (60 * 60);
196 uptime -= hours * (60 * 60);
202 fprintf(fp, "<br />\n");
203 fprintf(fp, "<b>Server statistics:</b> <br />\n");
204 fprintf(fp, "<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\">\n");
205 if (mapconn->starttime) {
206 fprintf(fp, "<tr><td> ");
207 fprintf(fp, "Server start time</td><td> :</td><td> %s<td></tr>\n",
208 silc_get_time(mapconn->data.starttime));
210 if (mapconn->uptime) {
211 fprintf(fp, "<tr><td> ");
212 fprintf(fp, "Server uptime</td><td> :</td><td> %d days %d hours %d mins %d secs<td></tr>\n",
213 days, hours, mins, secs);
215 if (mapconn->clients) {
216 fprintf(fp, "<tr><td> ");
217 fprintf(fp, "Local clients</td><td> :</td><td> %ld</td></tr>\n",
218 (unsigned long)mapconn->data.clients);
220 if (mapconn->channels) {
221 fprintf(fp, "<tr><td> ");
222 fprintf(fp, "Local channels</td><td> :</td><td> %ld</td></tr>\n",
223 (unsigned long)mapconn->data.channels);
225 if (mapconn->server_ops) {
226 fprintf(fp, "<tr><td> ");
227 fprintf(fp, "Local server operators</td><td> :</td><td> %ld</td></tr>\n",
228 (unsigned long)mapconn->data.server_ops);
230 if (mapconn->router_ops) {
231 fprintf(fp, "<tr><td> ");
232 fprintf(fp, "Local router operators</td><td> :</td><td> %ld</td></tr>\n",
233 (unsigned long)mapconn->data.router_ops);
235 if (mapconn->cell_clients) {
236 fprintf(fp, "<tr><td> ");
237 fprintf(fp, "Cell clients</td><td> :</td><td> %ld</td></tr>\n",
238 (unsigned long)mapconn->data.cell_clients);
240 if (mapconn->cell_channels) {
241 fprintf(fp, "<tr><td> ");
242 fprintf(fp, "Cell channels</td><td> :</td><td> %ld</td></tr>\n",
243 (unsigned long)mapconn->data.cell_channels);
245 if (mapconn->cell_servers) {
246 fprintf(fp, "<tr><td> ");
247 fprintf(fp, "Cell servers</td><td> :</td><td> %ld</td></tr>\n",
248 (unsigned long)mapconn->data.cell_servers);
250 if (mapconn->all_clients) {
251 fprintf(fp, "<tr><td> ");
252 fprintf(fp, "All SILC clients</td><td> :</td><td> %ld</td></tr>\n",
253 (unsigned long)mapconn->data.all_clients);
255 if (mapconn->all_channels) {
256 fprintf(fp, "<tr><td> ");
257 fprintf(fp, "All SILC channels</td><td> :</td><td> %ld</td></tr>\n",
258 (unsigned long)mapconn->data.all_channels);
260 if (mapconn->all_servers) {
261 fprintf(fp, "<tr><td> ");
262 fprintf(fp, "All SILC servers</td><td> :</td><td> %ld</td></tr>\n",
263 (unsigned long)mapconn->data.all_servers);
265 if (mapconn->all_routers) {
266 fprintf(fp, "<tr><td> ");
267 fprintf(fp, "All SILC routers</td><td> :</td><td> %ld</td></tr>\n",
268 (unsigned long)mapconn->data.all_routers);
270 if (mapconn->all_server_ops) {
271 fprintf(fp, "<tr><td> ");
272 fprintf(fp, "All SILC server operators</td><td> :</td><td> %ld</td></tr>\n",
273 (unsigned long)mapconn->data.all_server_ops);
275 if (mapconn->all_router_ops) {
276 fprintf(fp, "<tr><td> ");
277 fprintf(fp, "All SILC router operators</td><td> :</td><td> %ld</td></tr>\n",
278 (unsigned long)mapconn->data.all_router_ops);
280 fprintf(fp, "</table>\n");
284 if (mapconn->motd && mapconn->data.motd) {
285 fprintf(fp, " <br /><hr ><br />\n");
286 fprintf(fp, "<b>Message of the Day:</b> <br />\n");
288 fprintf(fp, "<br /><tt><small>\n");
290 while (silc_gets(line, sizeof(line) - 1, mapconn->data.motd,
291 strlen(mapconn->data.motd), &begin) != EOF)
292 fprintf(fp, "%s<br />\n", line);
293 fprintf(fp, "</small></tt>\n");
296 fprintf(fp, "<br />\n");
302 /* Write the HTML index file that lists all servers. */
304 bool silc_map_writehtml_index(SilcMap map)
306 SilcMapConnection mapconn;
307 char *hostname, *ip, *class;
310 /* Open for writing */
311 fp = fopen(map->writehtml.filename, "w+");
313 fprintf(stderr, "Could not open file '%s'\n", map->writehtml.filename);
317 /* Produce a simple HTML index file of all servers */
318 class = map->writehtml.text ? map->writehtml.text : "silcmap";
320 fprintf(fp, "<!-- Automatically generated by silcmap -->\n");
321 fprintf(fp, "<br />\n");
322 fprintf(fp, "<table cellspacing=\"0\" cellpadding=\"0\" "
323 "class=\"%s\" border=\"0\">\n", class);
326 "<td align=\"center\" class=\"%s_header\"><b>Hostname</b></td>\n"
327 "<td align=\"center\" class=\"%s_header\"><b>IPv4 Address</b></td>\n"
328 "<td align=\"center\" class=\"%s_header\"><b>Port</b></td>\n"
329 "<td align=\"center\" class=\"%s_header\"><b>Country</b></td>\n"
330 "<td align=\"center\" class=\"%s_header\"><b>Oper</b></td>\n"
331 "</tr>\n", class, class, class, class, class);
333 silc_dlist_start(map->conns);
334 while ((mapconn = silc_dlist_get(map->conns)) != SILC_LIST_END) {
336 silc_dlist_start(mapconn->hostnames);
337 hostname = silc_dlist_get(mapconn->hostnames);
338 silc_dlist_start(mapconn->ips);
339 ip = silc_dlist_get(mapconn->ips);
341 fprintf(fp, "<tr>\n");
342 if (mapconn->html_url)
344 "<td align = \"center\" class=\"%s\"> <a href=\"%s\">%s</a></td>\n", class, mapconn->html_url, hostname);
347 "<td align = \"center\" class=\"%s\"> <a href=\"%s_%d.html\">%s</a></td>\n", class, hostname, mapconn->port, hostname);
349 "<td align = \"center\" class=\"%s\"> %s</td>\n"
350 "<td align = \"center\" class=\"%s\"> %d</td>\n"
351 "<td align = \"center\" class=\"%s\"> %s</td>\n"
352 "<td align = \"center\" class=\"%s\"> %s</td>\n"
354 class, ip, class, mapconn->port, class,
355 mapconn->country, class, mapconn->admin);
358 fprintf(fp, "</table><br />\n");
363 /* Creates a HTML map file, which can be used to allow user to click
364 URLs on the image at the specified locations. */
366 bool silc_map_writemaphtml(SilcMap map)
368 SilcMapConnection mapconn;
369 SilcMapCommand cmd, c;
370 char *hostname, url[256];
372 int i, xx , yy, w, h;
374 for (i = 0; i < map->writemaphtml_count; i++) {
375 c = &map->writemaphtml[i];
377 /* Open for writing */
378 fp = fopen(c->filename, "w+");
380 fprintf(stderr, "Could not open file '%s'\n", c->filename);
384 /* The target may be portion of the original map, so we must make the
385 new coordinates relative to the new map. */
389 memset(url, 0, sizeof(url));
391 fprintf(fp, "<!-- Automatically generated by silcmap -->\n");
392 fprintf(fp, "<img src=\"%s\" usemap=\"#map\" class=\"silcmap\">\n",
394 fprintf(fp, "<map name=\"map\">\n");
396 silc_dlist_start(map->conns);
397 while ((mapconn = silc_dlist_get(map->conns)) != SILC_LIST_END) {
398 memset(url, 0, sizeof(url));
399 if (mapconn->html_url && mapconn->html_url[0]) {
400 silc_strncat(url, sizeof(url), mapconn->html_url,
401 strlen(mapconn->html_url));
403 silc_dlist_start(mapconn->hostnames);
404 hostname = silc_dlist_get(mapconn->hostnames);
405 snprintf(url, sizeof(url) - 1, "%s_%d.html", hostname, mapconn->port);
408 /* Print the positions of various items on the map into the map file */
409 silc_dlist_start(mapconn->commands);
410 while ((cmd = silc_dlist_get(mapconn->commands)) != SILC_LIST_END) {
411 if (cmd->draw_text) {
412 w = strlen(cmd->text) * 5;
413 h = map->font.height - 2;
415 "<area shape=\"rect\" coords=\"%d,%d,%d,%d\" href=\"%s\">\n",
416 (int)(cmd->x - xx), (int)(cmd->y - yy), w, h, url);
419 if (cmd->draw_circle) {
422 "<area shape=\"circle\" coords=\"%d,%d,%d\" href=\"%s\">\n",
423 (int)(cmd->x - xx), (int)(cmd->y - yy), w, url);
425 w = strlen(cmd->text) * 5;
426 h = map->font.height - 2;
428 "<area shape=\"rect\" coords=\"%d,%d,%d,%d\" href=\"%s\">\n",
429 (int)(cmd->x - xx + cmd->lposx),
430 (int)(cmd->y - yy - cmd->lposy),
431 (int)(cmd->x - xx + cmd->lposx + w),
432 (int)(cmd->y - yy - cmd->lposy + h), url);
436 if (cmd->draw_rectangle) {
440 "<area shape=\"rect\" coords=\"%d,%d,%d,%d\" href=\"%s\">\n",
441 (int)(cmd->x - xx), (int)(cmd->y - yy),
442 (int)(cmd->x - xx + w), (int)(cmd->y - yy + h), url);
444 w = strlen(cmd->text) * 5;
445 h = map->font.height - 2;
447 "<area shape=\"rect\" coords=\"%d,%d,%d,%d\" href=\"%s\">\n",
448 (int)(cmd->x - xx + cmd->lposx),
449 (int)(cmd->y - yy - cmd->lposy),
450 (int)(cmd->x - xx + cmd->lposx + w),
451 (int)(cmd->y - yy - cmd->lposy + h), url);
457 fprintf(fp, "</map>\n");
464 /* Writes the server uptime reliablity data file. */
466 bool silc_map_writerel(SilcMap map, SilcMapConnection mapconn)
469 int try = 0, success = 0;
470 char f[256], *hostname;
472 /* Generate data filename */
473 memset(f, 0, sizeof(f));
474 silc_dlist_start(mapconn->hostnames);
475 hostname = silc_dlist_get(mapconn->hostnames);
476 snprintf(f, sizeof(f) - 1, "%s_%d.rel", hostname, mapconn->port);
478 /* Read the current data */
481 fscanf(fp, "%d:%d", &try, &success);
485 /* Update the data */
487 success = (mapconn->down == FALSE ? success + 1 : success);
489 /* Write the data file */
492 fprintf(stderr, "Could not open file '%s'\n", map->writerel.filename);
495 fprintf(fp, "%d:%d", try, success);
501 /* Writes the servers' uptime reliability graph as HTML page. */
503 bool silc_map_writerelhtml(SilcMap map)
505 SilcMapConnection mapconn;
506 char *hostname, *class;
509 /* Open for writing */
510 fp = fopen(map->writerel.filename, "w+");
512 fprintf(stderr, "Could not open file '%s'\n", map->writerel.filename);
516 /* Produce the reliability graph as HTML file. */
517 class = map->writerel.text ? map->writerel.text : "silcmap";
519 fprintf(fp, "<!-- Automatically generated by silcmap -->\n");
520 fprintf(fp, "<br />\n");
521 fprintf(fp, "<table cellspacing=\"0\" cellpadding=\"0\" "
522 "class=\"%s\" border=\"0\">\n", class);
525 "<td align=\"center\" class=\"%s_header\"><b>Server</b></td>\n"
526 "<td colspan=\"2\" align=\"center\" class=\"%s_header\"><b>Reliability</b></td>\n"
527 "</tr>\n", class, class);
529 silc_dlist_start(map->conns);
530 while ((mapconn = silc_dlist_get(map->conns)) != SILC_LIST_END) {
532 int try = 0, success = 0;
535 silc_dlist_start(mapconn->hostnames);
536 hostname = silc_dlist_get(mapconn->hostnames);
539 memset(f, 0, sizeof(f));
540 snprintf(f, sizeof(f) - 1, "%s_%d.rel", hostname, mapconn->port);
543 fscanf(dp, "%d:%d", &try, &success);
547 /* Count the reliability */
549 rel = ((double)success / (double)try) * (double)160.0;
551 fprintf(fp, "<tr>\n");
552 if (mapconn->html_url)
554 "<td align = \"center\" class=\"%s\"> <a href=\"%s\">%s</a></td>\n", class, mapconn->html_url, hostname);
557 "<td align = \"center\" class=\"%s\"> <a href=\"%s_%d.html\">%s</a></td>\n", class, hostname, mapconn->port, hostname);
559 "<td class=\"%s\" width=\"160\">"
560 "<table style=\"border: solid 1px black; width: 160px;\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"%s\"><tr>"
561 "<td style=\"width: %fpx; height: 10px;\" bgcolor=\"gray\"></td>"
562 "<td style=\"width: %fpx; height: 10px;\" bgcolor=\"white\"></td>"
563 "</tr></table></td>\n"
564 "<td class=\"%s\">%.2f%% - score: %d</td>\n"
566 class, class, rel, 160 - rel, class,
567 ((double)success / (double)try) * (double)100.0, success);
570 fprintf(fp, "</table><br />\n");