Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
085ba9a
fix377
Aug 13, 2018
47945cc
sanitize struct
Aug 13, 2018
663f8e6
doc fix
Aug 13, 2018
e7d5904
make get_ip() static
Aug 13, 2018
954b1eb
rand() -> random()
Aug 26, 2018
30b0114
cleanup comment
Oct 1, 2018
a2b3e74
Fix struct hack / flexible array member, requires merged #660
Oct 1, 2018
ba54ca7
Merge remote-tracking branch 'upstream/develop' into fix377
Oct 1, 2018
6fafbb5
fix
Oct 1, 2018
368f8e8
fix
Oct 1, 2018
957c665
fix #664
Oct 1, 2018
b2a7d4a
enhance fix #664 / convert some (const) variables to #defines
Oct 1, 2018
6446d18
Merge remote-tracking branch 'upstream/develop' into fix377
Oct 12, 2018
98556db
Merge remote-tracking branch 'upstream/develop' into fix377
Oct 13, 2018
f44dfad
cleanup includes
Oct 13, 2018
57d7528
sizeof -> offsetof; added comment; my_memcpy -> memcpy
Oct 13, 2018
496d5ce
tab -> space, format
Oct 13, 2018
5e89995
gethostbyname -> getaddrinfo(); prepare fr ipv6 ;); static functions
Oct 14, 2018
5eee5f3
Merge remote-tracking branch 'upstream/develop' into fix377
michaelortmann Jun 27, 2019
6099b88
Cleanup sizeof
michaelortmann Sep 12, 2019
d6ec239
Merge remote-tracking branch 'upstream/develop' into fix377
michaelortmann Nov 21, 2019
6b59921
Merge remote-tracking branch 'upstream/develop' into fix377
michaelortmann Nov 26, 2019
b276159
Cleanup
michaelortmann Nov 26, 2019
a9966e0
Cleanup
michaelortmann Nov 26, 2019
26e58f0
undo my stupidity
michaelortmann Nov 26, 2019
e25c926
Merge remote-tracking branch 'upstream/develop' into fix377
michaelortmann Oct 23, 2021
4686ec0
simplify, we dont need this string copy
michaelortmann Oct 23, 2021
6040550
update module minor version
michaelortmann Oct 23, 2021
d97efcd
Merge remote-tracking branch 'upstream/develop' into fix377
michaelortmann Sep 15, 2022
b48ca7b
Enhance logging
michaelortmann Sep 15, 2022
115bd64
Merge remote-tracking branch 'upstream/develop' into fix377
Jan 3, 2024
563b60f
Fix include
Jan 3, 2024
c011c54
Merge remote-tracking branch 'upstream/develop' into fix377
michaelortmann Jul 10, 2024
f68ed14
Fix include / --disable-tls
michaelortmann Jul 10, 2024
ca853a2
Merge remote-tracking branch 'upstream/develop' into fix377
michaelortmann Jul 21, 2024
e974e01
Merge remote-tracking branch 'upstream/develop' into fix377
michaelortmann Aug 1, 2024
6a15f30
Resolve conflicts
michaelortmann Oct 17, 2025
e5ec86b
Make struct packed
michaelortmann Oct 17, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
162 changes: 71 additions & 91 deletions src/mod/uptime.mod/uptime.c
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
/*
* This module reports uptime statistics to the uptime contest web
* site at https://www.eggheads.org/uptime/. The
* purpose for this is to see how your bot rates against many others (including EnergyMechs
* and Eggdrops) -- It is a fun little project, jointly run by Eggheads.org and EnergyMech.net.
* This module reports uptime statistics to the uptime contest web site at
* https://www.eggheads.org/uptime/. The purpose for this is to see how your
* bot rates against many others (including EnergyMechs and Eggdrops) -- It is
* a fun little project, jointly run by Eggheads.org and EnergyMech.net.
*
* If you don't like being a part of it please just unload this module.
*
* Also for bot developers feel free to modify this code to make it a part of your bot and
* e-mail webmaster@eggheads.org for more information on registering your bot type. See how
* your bot's stability rates against ours and ours against yours <g>.
* Also for bot developers feel free to modify this code to make it a part of
* your bot and e-mail webmaster@eggheads.org for more information on
* registering your bot type. See how your bot's stability rates against ours
* and ours against yours <g>.
*/
/*
* Copyright (C) 2001 proton
* Copyright (C) 2001 - 2025 Eggheads Development Team
* Copyright (C) 2001 - 2024 Eggheads Development Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
Expand All @@ -32,41 +33,36 @@
#define MODULE_NAME "uptime"
#define MAKING_UPTIME

#include <fcntl.h>
#include <netdb.h>
#include <stddef.h>
#include <sys/stat.h>
#include "uptime.h"
#include "src/mod/module.h"
#include "../server.mod/server.h"
#include <netdb.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>

#define UPDATE_INTERVAL (12 * 60) /* random(0..12) hours: ~6 hour average. */

/*
* regnr is unused; however, it must be here inorder for
* us to create a proper struct for the uptime server.
* regnr is unused; however, it must be here inorder for us to create a proper
* struct for the uptime server.
*
* "packets_sent" was originally defined as "cookie",
* however this field was deprecated and set to zero
* for most versions of the uptime client. It has been
* repurposed and renamed as of uptime v1.3 to reflect
* the number of packets the client thinks it has sent
* over the life of the module. Only the name has changed -
* the type (unsigned long) is still the same.
* "packets_sent" was originally defined as "cookie", however this field was
* deprecated and set to zero for most versions of the uptime client. It has
* been repurposed and renamed as of uptime v1.3 to reflect the number of
* packets the client thinks it has sent over the life of the module. Only the
* name has changed - the type is still the same.
*/

typedef struct PackUp {
int regnr;
int pid;
int type;
unsigned long packets_sent;
unsigned long uptime;
unsigned long ontime;
unsigned long now2;
unsigned long sysup;
char string[3];
typedef struct __attribute__((packed)) PackUp {
uint32_t regnr;
uint32_t pid;
uint32_t type;
uint32_t packets_sent;
uint32_t uptime;
uint32_t ontime;
uint32_t now2;
uint32_t sysup;
char string[FLEXIBLE_ARRAY_MEMBER];
} PackUp;

PackUp upPack;
Expand All @@ -77,15 +73,14 @@ static int minutes = 0;
static int seconds = 0;
static int next_seconds = 0;
static int next_minutes = 0;
static int update_interval = 720; /* rand(0..12) hours: ~6 hour average. */
static time_t next_update = 0;
static int uptimesock;
static int uptimecount;
static unsigned long uptimeip;
static char uptime_version[48] = "";

void check_secondly(void);
void check_minutely(void);
static void check_secondly(void);
static void check_minutely(void);

static int uptime_expmem()
{
Expand All @@ -95,47 +90,28 @@ static int uptime_expmem()
static void uptime_report(int idx, int details)
{
int delta_seconds;
char next_update_at[26];
char *next_update_at;

if (details) {
delta_seconds = (int) (next_update - time(NULL));
ctime_r(&next_update, next_update_at);
next_update_at[24] = 0;
dprintf(idx, " %d uptime packet%s sent\n", uptimecount,
next_update_at = ctime(&next_update);
next_update_at[strlen(next_update_at) - 1] = 0;

dprintf(idx, " %d uptime packet%s sent\n", uptimecount,
(uptimecount != 1) ? "s" : "");
dprintf(idx, " Approximately %-.2f hours until next update "
dprintf(idx, " Approximately %-.2f hours until next update "
"(at %s)\n", delta_seconds / 3600.0, next_update_at);
}
}

static unsigned long get_ip()
{
struct hostent *hp;
IP ip;
struct in_addr *in;

/* could be pre-defined */
if (uptime_host[0]) {
if ((uptime_host[strlen(uptime_host) - 1] >= '0') &&
(uptime_host[strlen(uptime_host) - 1] <= '9'))
return (IP) inet_addr(uptime_host);
}
hp = gethostbyname(uptime_host);
if (hp == NULL)
return -1;
in = (struct in_addr *) (hp->h_addr_list[0]);
ip = (IP) (in->s_addr);
return ip;
}

static int init_uptime(void)
{
struct sockaddr_in sai;
char x[64], *z = x;

upPack.regnr = 0; /* unused */
upPack.pid = 0; /* must set this later */
upPack.type = htonl(uptime_type);
upPack.type = htonl(UPTIME_TYPE);
upPack.packets_sent = 0; /* reused (abused?) to send our packet count */
upPack.uptime = 0; /* must set this later */
uptimecount = 0;
Expand All @@ -158,8 +134,8 @@ static int init_uptime(void)
}
fcntl(uptimesock, F_SETFL, O_NONBLOCK | fcntl(uptimesock, F_GETFL));

next_minutes = rand() % update_interval; /* Initial update delay */
next_seconds = rand() % 59;
next_minutes = random() % UPDATE_INTERVAL; /* Initial update delay */
next_seconds = random() % 59;
next_update = (time_t) ((time(NULL) / 60 * 60) + (next_minutes * 60) +
next_seconds);

Expand All @@ -169,17 +145,27 @@ static int init_uptime(void)

static int send_uptime(void)
{
struct sockaddr_in sai;
struct addrinfo hints, *res0;
int error;
struct stat st;
PackUp *mem;
int len, servidx;
char servhost[UHOSTLEN] = "none";
char *servhost = "none";
module_entry *me;

if (uptimeip == -1) {
uptimeip = get_ip();
if (uptimeip == -1)
return -2;
egg_bzero(&hints, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
error = getaddrinfo(UPTIME_HOST, UPTIME_PORT, &hints, &res0);
if (error) {
if (error == EAI_NONAME)
putlog(LOG_MISC, "*",
"send_uptime(): getaddrinfo(): hostname:port '%s:%s' not known",
UPTIME_HOST, UPTIME_PORT);
else
putlog(LOG_MISC, "*", "send_uptime(): getaddrinfo(): error = %s",
gai_strerror(error));
return -2;
}

uptimecount++;
Expand All @@ -193,7 +179,7 @@ static int send_uptime(void)

if (server_online) {
servidx = findanyidx(serv);
strlcpy(servhost, dcc[servidx].host, sizeof servhost);
servhost = dcc[servidx].host;
upPack.ontime = htonl(server_online);
}
}
Expand All @@ -209,26 +195,20 @@ static int send_uptime(void)
else
upPack.sysup = htonl(st.st_ctime);

len = sizeof(upPack) + strlen(botnetnick) + strlen(servhost) +
strlen(uptime_version);
len = offsetof(struct PackUp, string) + strlen(botnetnick) + strlen(servhost)
+ strlen(uptime_version) + 3; /* whitespace + whitespace + \0 */
mem = (PackUp *) nmalloc(len);
egg_bzero(mem, len); /* mem *should* be completely filled before it's
* sent to the server. But belt-and-suspenders
* is always good.
*/
memcpy(mem, &upPack, sizeof(upPack));
sprintf(mem->string, "%s %s %s", botnetnick, servhost, uptime_version);
egg_bzero(&sai, sizeof(sai));
sai.sin_family = AF_INET;
sai.sin_addr.s_addr = uptimeip;
sai.sin_port = htons(uptime_port);
len = sendto(uptimesock, (void *) mem, len, 0, (struct sockaddr *) &sai,
sizeof(sai));
len = sendto(uptimesock, (void *) mem, len, 0, res0->ai_addr,
res0->ai_addrlen);
if (len < 0)
putlog(LOG_DEBUG, "*", "send_uptime(): sendto(): %s", gai_strerror(error));
nfree(mem);
return len;
}

void check_minutely()
static void check_minutely()
{
minutes++;
if (minutes >= next_minutes) {
Expand All @@ -238,7 +218,7 @@ void check_minutely()
}
}

void check_secondly()
static void check_secondly()
{
seconds++;
if (seconds >= next_seconds) { /* DING! */
Expand All @@ -248,8 +228,8 @@ void check_secondly()

minutes = 0; /* Reset for the next countdown. */
seconds = 0;
next_minutes = rand() % update_interval;
next_seconds = rand() % 59;
next_minutes = random() % UPDATE_INTERVAL;
next_seconds = random() % 59;
next_update = (time_t) ((time(NULL) / 60 * 60) + (next_minutes * 60) +
next_seconds);

Expand Down Expand Up @@ -283,7 +263,7 @@ char *uptime_start(Function *global_funcs)
if (global_funcs) {
global = global_funcs;

module_register(MODULE_NAME, uptime_table, 1, 4);
module_register(MODULE_NAME, uptime_table, 1, 5);
if (!module_depend(MODULE_NAME, "eggdrop", 108, 0)) {
module_undepend(MODULE_NAME);
return "This module requires Eggdrop 1.8.0 or later.";
Expand Down
6 changes: 3 additions & 3 deletions src/mod/uptime.mod/uptime.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
#ifndef _EGG_MOD_UPTIME_UPTIME_H
#define _EGG_MOD_UPTIME_UPTIME_H

static const int uptime_type = 2;
static const int uptime_port = 9969;
static const char *uptime_host = "uptime.eggheads.org";
#define UPTIME_TYPE 2
#define UPTIME_HOST "uptime.eggheads.org"
#define UPTIME_PORT "9969"

#endif /* _EGG_MOD_UPTIME_UPTIME_H */
Loading