Copyright (C) 2003 Pekka Riikonen
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
#include "silcincludes.h"
#include "silcclient.h"
#include "silcmap.h"
/* Write the HTML data file of the gathered data from the connection. */
bool silc_map_writehtml(SilcMap map, SilcMapConnection mapconn)
{
FILE *fp;
char *hostname;
char filename[256];
/* Generate data filename. First configure hostname is the filename */
silc_dlist_start(mapconn->hostnames);
hostname = silc_dlist_get(mapconn->hostnames);
memset(filename, 0, sizeof(filename));
snprintf(filename, sizeof(filename) - 1, "%s_%d.html", hostname,
mapconn->port);
/* Open for writing */
fp = fopen(filename, "w+");
if (!fp) {
fprintf(stderr, "Could not open file '%s'\n", filename);
return FALSE;
}
/* Write the HTML page */
fprintf(fp, "\n");
fprintf(fp, "
\n");
/* General stuff */
fprintf(fp, "\n");
silc_dlist_start(mapconn->hostnames);
while ((hostname = silc_dlist_get(mapconn->hostnames)) != SILC_LIST_END)
fprintf(fp, "Hostname | : | %s |
\n", hostname);
silc_dlist_start(mapconn->ips);
while ((hostname = silc_dlist_get(mapconn->ips)) != SILC_LIST_END)
fprintf(fp, "IP | : | %s |
\n", hostname);
fprintf(fp, "Port | : | %d |
\n", mapconn->port);
fprintf(fp, "Country | : | %s |
\n", mapconn->country);
fprintf(fp, "City | : | %s |
\n", mapconn->city);
fprintf(fp, "Admin | : | %s |
\n", mapconn->admin);
fprintf(fp, "
\n");
/* Public key */
if (mapconn->public_key) {
SilcPublicKey public_key;
SilcPublicKeyIdentifier ident;
char *fingerprint, *babbleprint;
unsigned char *pk;
SilcUInt32 pk_len;
SilcPKCS pkcs;
SilcUInt32 key_len = 0;
FILE *pd;
unsigned char *pdd;
fprintf(fp, "
\n");
fprintf(fp, "Public Key:
\n");
fprintf(fp, "\n");
if (silc_pkcs_load_public_key(mapconn->public_key, &public_key,
SILC_PKCS_FILE_PEM) == FALSE)
if (silc_pkcs_load_public_key(mapconn->public_key, &public_key,
SILC_PKCS_FILE_BIN) == FALSE) {
fprintf(stderr, "Could not load public key file `%s'\n",
mapconn->public_key);
return FALSE;
}
ident = silc_pkcs_decode_identifier(public_key->identifier);
pk = silc_pkcs_public_key_encode(public_key, &pk_len);
fingerprint = silc_hash_fingerprint(NULL, pk, pk_len);
babbleprint = silc_hash_babbleprint(NULL, pk, pk_len);
if (silc_pkcs_alloc(public_key->name, &pkcs)) {
key_len = silc_pkcs_public_key_set(pkcs, public_key);
silc_pkcs_free(pkcs);
}
fprintf(fp, " ");
fprintf(fp, "Public key file | : | %s |
\n",
mapconn->public_key, mapconn->public_key);
fprintf(fp, " ");
fprintf(fp, "Algorithm | : | %s |
\n", public_key->name);
if (key_len) {
fprintf(fp, " ");
fprintf(fp, "Key length (bits) | : | %d |
\n", (unsigned int)key_len);
}
if (ident->realname) {
fprintf(fp, " ");
fprintf(fp, "Real name | : | %s |
\n", ident->realname);
}
if (ident->username) {
fprintf(fp, " ");
fprintf(fp, "Username | : | %s |
\n", ident->username);
}
if (ident->host) {
fprintf(fp, " ");
fprintf(fp, "Hostname | : | %s |
\n", ident->host);
}
if (ident->email) {
fprintf(fp, " ");
fprintf(fp, "Email | : | %s |
\n", ident->email);
}
if (ident->org) {
fprintf(fp, " ");
fprintf(fp, "Organization | : | %s |
\n", ident->org);
}
if (ident->country) {
fprintf(fp, " ");
fprintf(fp, "Country | : | %s |
\n", ident->country);
}
fprintf(fp, " ");
fprintf(fp, "Fingerprint (SHA1) | : | %s |
\n", fingerprint);
fprintf(fp, " ");
fprintf(fp, "Babbleprint (SHA1) | : | %s |
\n", babbleprint);
fprintf(fp, "
\n");
pd = fopen(mapconn->public_key, "r");
if (!pd)
return FALSE;
pk_len = silc_file_size(mapconn->public_key);
pdd = silc_calloc(pk_len + 1, sizeof(*pdd));
if (!pdd)
return FALSE;
fread(pdd, pk_len, 1, pd);
fprintf(fp, "\n");
fprintf(fp, "%s", pdd);
fprintf(fp, "
\n");
fclose(pd);
silc_free(pdd);
silc_free(fingerprint);
silc_free(babbleprint);
silc_free(pk);
silc_pkcs_public_key_free(public_key);
silc_pkcs_free_identifier(ident);
}
/* Description */
if (mapconn->description && mapconn->description[0]) {
fprintf(fp, "
\n");
fprintf(fp, "Description:
\n");
fprintf(fp, "%s
\n", mapconn->description);
}
/* Status */
if (mapconn->connect) {
fprintf(fp, "
\n");
fprintf(fp, "Server status:
\n");
if (mapconn->down)
fprintf(fp,
"Server is currently down and unreachable. "
"Please try again later to connect the server.
\n");
else
fprintf(fp,
"Server is up and running
\n");
}
if (mapconn->connect && !mapconn->down) {
int days, hours, mins, secs, uptime;
uptime = mapconn->data.uptime;
days = uptime / (24 * 60 * 60);
uptime -= days * (24 * 60 * 60);
hours = uptime / (60 * 60);
uptime -= hours * (60 * 60);
mins = uptime / 60;
uptime -= mins * 60;
secs = uptime;
/* Statistics */
fprintf(fp, "
\n");
fprintf(fp, "Server statistics:
\n");
fprintf(fp, "\n");
if (mapconn->starttime) {
fprintf(fp, " ");
fprintf(fp, "Server start time | : | %s | |
\n",
silc_get_time(mapconn->data.starttime));
}
if (mapconn->uptime) {
fprintf(fp, " ");
fprintf(fp, "Server uptime | : | %d days %d hours %d mins %d secs | |
\n",
days, hours, mins, secs);
}
if (mapconn->clients) {
fprintf(fp, " ");
fprintf(fp, "Local clients | : | %ld |
\n",
mapconn->data.clients);
}
if (mapconn->channels) {
fprintf(fp, " ");
fprintf(fp, "Local channels | : | %ld |
\n",
mapconn->data.channels);
}
if (mapconn->server_ops) {
fprintf(fp, " ");
fprintf(fp, "Local server operators | : | %ld |
\n",
mapconn->data.server_ops);
}
if (mapconn->router_ops) {
fprintf(fp, " ");
fprintf(fp, "Local router operators | : | %ld |
\n",
mapconn->data.router_ops);
}
if (mapconn->cell_clients) {
fprintf(fp, " ");
fprintf(fp, "Cell clients | : | %ld |
\n",
mapconn->data.cell_clients);
}
if (mapconn->cell_channels) {
fprintf(fp, " ");
fprintf(fp, "Cell channels | : | %ld |
\n",
mapconn->data.cell_channels);
}
if (mapconn->cell_servers) {
fprintf(fp, " ");
fprintf(fp, "Cell servers | : | %ld |
\n",
mapconn->data.cell_servers);
}
if (mapconn->all_clients) {
fprintf(fp, " ");
fprintf(fp, "All SILC clients | : | %ld |
\n",
mapconn->data.all_clients);
}
if (mapconn->all_channels) {
fprintf(fp, " ");
fprintf(fp, "All SILC channels | : | %ld |
\n",
mapconn->data.all_channels);
}
if (mapconn->all_servers) {
fprintf(fp, " ");
fprintf(fp, "All SILC servers | : | %ld |
\n",
mapconn->data.all_servers);
}
if (mapconn->all_routers) {
fprintf(fp, " ");
fprintf(fp, "All SILC routers | : | %ld |
\n",
mapconn->data.all_routers);
}
if (mapconn->all_server_ops) {
fprintf(fp, " ");
fprintf(fp, "All SILC server operators | : | %ld |
\n",
mapconn->data.all_server_ops);
}
if (mapconn->all_router_ops) {
fprintf(fp, " ");
fprintf(fp, "All SILC router operators | : | %ld |
\n",
mapconn->data.all_router_ops);
}
fprintf(fp, "
\n");
}
/* motd */
if (mapconn->motd) {
fprintf(fp, "
\n");
fprintf(fp, "Message of the Day:
\n");
fprintf(fp, "%s\n", mapconn->data.motd);
}
fprintf(fp, "
\n");
fclose(fp);
return TRUE;
}
/* Write the HTML index file that lists all servers. */
bool silc_map_writehtml_index(SilcMap map)
{
SilcMapConnection mapconn;
char *hostname, *ip, *class;
FILE *fp;
/* Open for writing */
fp = fopen(map->writehtml.filename, "w+");
if (!fp) {
fprintf(stderr, "Could not open file '%s'\n", map->writehtml.filename);
return FALSE;
}
/* Produce a simple HTML index file of all servers */
class = map->writehtml.text ? map->writehtml.text : "silcmap";
fprintf(fp, "\n");
fprintf(fp, "
\n");
fprintf(fp, "
\n", class);
fprintf(fp,
"\n"
"\n"
"\n"
"\n"
"\n"
"\n"
"
\n", class, class, class, class, class);
silc_dlist_start(map->conns);
while ((mapconn = silc_dlist_get(map->conns)) != SILC_LIST_END) {
silc_dlist_start(mapconn->hostnames);
hostname = silc_dlist_get(mapconn->hostnames);
silc_dlist_start(mapconn->ips);
ip = silc_dlist_get(mapconn->ips);
fprintf(fp,
"\n"
" %s | \n"
" %s | \n"
" %d | \n"
" %s | \n"
" %s | \n"
"
\n", class,
mapconn->html_url ? mapconn->html_url : hostname, mapconn->port,
hostname, class, ip, class, mapconn->port, class,
mapconn->country, class, mapconn->admin);
}
fprintf(fp, "
\n");
return TRUE;
}
/* Creates a HTML map file, which can be used to allow user to click
URLs on the image at the specified locations. */
bool silc_map_writemaphtml(SilcMap map)
{
SilcMapConnection mapconn;
SilcMapCommand cmd;
char *hostname, url[256];
FILE *fp;
int xx , yy, w, h;
/* Open for writing */
fp = fopen(map->writemaphtml.filename, "w+");
if (!fp) {
fprintf(stderr, "Could not open file '%s'\n", map->writemaphtml.filename);
return FALSE;
}
/* The target may be portion of the original map, so we must make the
new coordinates relative to the new map. */
xx = map->writemaphtml.x;
yy = map->writemaphtml.y;
memset(url, 0, sizeof(url));
fprintf(fp, "\n");
fprintf(fp, "
\n");
fprintf(fp, "\n",
map->writemaphtml.text);
fprintf(fp, "
\n");
fclose(fp);
return TRUE;
}