src/mod/uptime.mod/uptime.c File Reference

#include "uptime.h"
#include "../module.h"
#include "../server.mod/server.h"
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
Include dependency graph for uptime.c:

Go to the source code of this file.

Data Structures

struct  PackUp

Defines

#define MODULE_NAME   "uptime"
#define MAKING_UPTIME

Functions

void check_secondly (void)
void check_minutely (void)
static int uptime_expmem ()
static void uptime_report (int idx, int details)
unsigned long get_ip ()
int init_uptime (void)
int send_uptime (void)
static char * uptime_close ()
char * uptime_start (Function *)

Variables

PackUp upPack
static Functionglobal = 0
static int minutes = 0
static int seconds = 0
static int next_seconds = 0
static int next_minutes = 0
static int update_interval = 720
static time_t next_update = 0
static int uptimesock
static int uptimecount
static unsigned long uptimeip
static char uptime_version [48] = ""
static Function uptime_table []

Define Documentation

#define MAKING_UPTIME

Definition at line 34 of file uptime.c.

#define MODULE_NAME   "uptime"

Definition at line 33 of file uptime.c.


Function Documentation

void check_minutely ( void   ) 

Definition at line 235 of file uptime.c.

References add_hook, check_secondly(), del_hook, HOOK_MINUTELY, HOOK_SECONDLY, minutes, and next_minutes.

Referenced by check_secondly(), and uptime_start().

00236 {
00237   minutes++;
00238   if (minutes >= next_minutes) {
00239     /* We're down to zero minutes.  Now do the seconds. */
00240     del_hook(HOOK_MINUTELY, (Function) check_minutely);
00241     add_hook(HOOK_SECONDLY, (Function) check_secondly);
00242   }
00243 }

Here is the call graph for this function:

Here is the caller graph for this function:

void check_secondly ( void   ) 

Definition at line 245 of file uptime.c.

References add_hook, check_minutely(), del_hook, HOOK_MINUTELY, HOOK_SECONDLY, minutes, next_minutes, next_seconds, next_update, NULL, seconds, send_uptime(), time_t, and update_interval.

Referenced by check_minutely().

00246 {
00247   seconds++;
00248   if (seconds >= next_seconds) {  /* DING! */
00249     del_hook(HOOK_SECONDLY, (Function) check_secondly);
00250 
00251     send_uptime();
00252 
00253     minutes = 0; /* Reset for the next countdown. */
00254     seconds = 0;
00255     next_minutes = rand() % update_interval;
00256     next_seconds = rand() % 59;
00257     next_update = (time_t) ((time(NULL) / 60 * 60) + (next_minutes * 60) +
00258       next_seconds);
00259 
00260     /* Go back to checking every minute. */
00261     add_hook(HOOK_MINUTELY, (Function) check_minutely);
00262   }
00263 }

Here is the call graph for this function:

Here is the caller graph for this function:

unsigned long get_ip (  ) 

Definition at line 115 of file uptime.c.

References NULL, and uptime_host.

Referenced by send_uptime().

00116 {
00117   struct hostent *hp;
00118   IP ip;
00119   struct in_addr *in;
00120 
00121   /* could be pre-defined */
00122   if (uptime_host[0]) {
00123     if ((uptime_host[strlen(uptime_host) - 1] >= '0') &&
00124         (uptime_host[strlen(uptime_host) - 1] <= '9'))
00125       return (IP) inet_addr(uptime_host);
00126   }
00127   hp = gethostbyname(uptime_host);
00128   if (hp == NULL)
00129     return -1;
00130   in = (struct in_addr *) (hp->h_addr_list[0]);
00131   ip = (IP) (in->s_addr);
00132   return ip;
00133 }

Here is the caller graph for this function:

int init_uptime ( void   ) 

Definition at line 135 of file uptime.c.

References egg_memset, LOG_DEBUG, newsplit, next_minutes, next_seconds, next_update, NULL, PackUp::packets_sent, PackUp::pid, putlog, PackUp::regnr, strncpyz, time_t, PackUp::type, update_interval, PackUp::uptime, uptime_type, uptime_version, uptimecount, uptimeip, uptimesock, and ver.

Referenced by uptime_start().

00136 {
00137   struct sockaddr_in sai;
00138   char x[64], *z = x;
00139 
00140   upPack.regnr = 0;  /* unused */
00141   upPack.pid = 0;    /* must set this later */
00142   upPack.type = htonl(uptime_type);
00143   upPack.packets_sent = 0; /* reused (abused?) to send our packet count */
00144   upPack.uptime = 0; /* must set this later */
00145   uptimecount = 0;
00146   uptimeip = -1;
00147 
00148   strncpyz(x, ver, sizeof x);
00149   newsplit(&z);
00150   strncpyz(uptime_version, z, sizeof uptime_version);
00151 
00152   if ((uptimesock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
00153     putlog(LOG_DEBUG, "*", "init_uptime socket returned < 0 %d", uptimesock);
00154     return ((uptimesock = -1));
00155   }
00156   egg_memset(&sai, 0, sizeof(sai));
00157   sai.sin_addr.s_addr = INADDR_ANY;
00158   sai.sin_family = AF_INET;
00159   if (bind(uptimesock, (struct sockaddr *) &sai, sizeof(sai)) < 0) {
00160     close(uptimesock);
00161     return ((uptimesock = -1));
00162   }
00163   fcntl(uptimesock, F_SETFL, O_NONBLOCK | fcntl(uptimesock, F_GETFL));
00164 
00165   next_minutes = rand() % update_interval; /* Initial update delay */
00166   next_seconds = rand() % 59;
00167   next_update = (time_t) ((time(NULL) / 60 * 60) + (next_minutes * 60) +
00168     next_seconds);
00169 
00170   return 0;
00171 }

Here is the caller graph for this function:

int send_uptime ( void   ) 

Definition at line 174 of file uptime.c.

References botnetnick, dcc, egg_bzero, findanyidx, _module_entry::funcs, get_ip(), module_find, my_memcpy, nfree, nmalloc, PackUp::now2, NULL, online_since, PackUp::ontime, PackUp::packets_sent, PackUp::pid, serv, server_funcs, server_online, strncpyz, PackUp::sysup, UHOSTLEN, PackUp::uptime, uptime_port, uptime_version, uptimecount, uptimeip, and uptimesock.

Referenced by check_secondly().

00175 {
00176   struct sockaddr_in sai;
00177   struct stat st;
00178   PackUp *mem;
00179   int len, servidx;
00180   char servhost[UHOSTLEN] = "none";
00181   module_entry *me;
00182 
00183   if (uptimeip == -1) {
00184     uptimeip = get_ip();
00185     if (uptimeip == -1)
00186       return -2;
00187   }
00188 
00189   uptimecount++;
00190   upPack.packets_sent = htonl(uptimecount); /* Tell the server how many
00191                            uptime packets we've sent. */
00192   upPack.now2 = htonl(time(NULL));
00193   upPack.ontime = 0;
00194 
00195   if ((me = module_find("server", 1, 0))) {
00196     Function *server_funcs = me->funcs;
00197 
00198     if (server_online) {
00199       servidx = findanyidx(serv);
00200       strncpyz(servhost, dcc[servidx].host, sizeof servhost);
00201       upPack.ontime = htonl(server_online);
00202     }
00203   }
00204 
00205   if (!upPack.pid)
00206     upPack.pid = htonl(getpid());
00207 
00208   if (!upPack.uptime)
00209     upPack.uptime = htonl(online_since);
00210 
00211   if (stat("/proc", &st) < 0)
00212     upPack.sysup = 0;
00213   else
00214     upPack.sysup = htonl(st.st_ctime);
00215 
00216   len = sizeof(upPack) + strlen(botnetnick) + strlen(servhost) +
00217         strlen(uptime_version);
00218   mem = (PackUp *) nmalloc(len);
00219   egg_bzero(mem, len); /* mem *should* be completely filled before it's
00220                              * sent to the server.  But belt-and-suspenders
00221                              * is always good.
00222                              */
00223   my_memcpy(mem, &upPack, sizeof(upPack));
00224   sprintf(mem->string, "%s %s %s", botnetnick, servhost, uptime_version);
00225   egg_bzero(&sai, sizeof(sai));
00226   sai.sin_family = AF_INET;
00227   sai.sin_addr.s_addr = uptimeip;
00228   sai.sin_port = htons(uptime_port);
00229   len = sendto(uptimesock, (void *) mem, len, 0, (struct sockaddr *) &sai,
00230                sizeof(sai));
00231   nfree(mem);
00232   return len;
00233 }

Here is the call graph for this function:

Here is the caller graph for this function:

static char* uptime_close (  )  [static]

Definition at line 265 of file uptime.c.

00266 {
00267   return "You cannot unload the uptime module "
00268          "(doing so will reset your stats).";
00269 }

static int uptime_expmem (  )  [static]

Definition at line 93 of file uptime.c.

00094 {
00095   return 0;
00096 }

static void uptime_report ( int  idx,
int  details 
) [static]

Definition at line 98 of file uptime.c.

References dprintf, int, next_update, NULL, and uptimecount.

00099 {
00100   int delta_seconds;
00101   char *next_update_at;
00102 
00103   if (details) {
00104     delta_seconds = (int) (next_update - time(NULL));
00105     next_update_at = ctime(&next_update);
00106     next_update_at[strlen(next_update_at) - 1] = 0;
00107 
00108     dprintf(idx, "      %d uptime packet%s sent\n", uptimecount,
00109             (uptimecount != 1) ? "s" : "");
00110     dprintf(idx, "      Approximately %-.2f hours until next update "
00111             "(at %s)\n", delta_seconds / 3600.0, next_update_at);
00112   }
00113 }

char * uptime_start ( Function global_funcs  ) 

Definition at line 280 of file uptime.c.

References add_help_reference, add_hook, check_minutely(), global, HOOK_MINUTELY, init_uptime(), module_depend, MODULE_NAME, module_register, module_undepend, NULL, and uptime_table.

00281 {
00282   if (global_funcs) {
00283     global = global_funcs;
00284 
00285     module_register(MODULE_NAME, uptime_table, 1, 3);
00286     if (!module_depend(MODULE_NAME, "eggdrop", 106, 11)) {
00287       module_undepend(MODULE_NAME);
00288       return "This module requires Eggdrop 1.6.11 or later.";
00289     }
00290 
00291     add_help_reference("uptime.help");
00292     add_hook(HOOK_MINUTELY, (Function) check_minutely);
00293     init_uptime();
00294   }
00295   return NULL;
00296 }

Here is the call graph for this function:


Variable Documentation

Function* global = 0 [static]

Definition at line 77 of file uptime.c.

int minutes = 0 [static]

Definition at line 79 of file uptime.c.

Referenced by check_minutely(), and check_secondly().

int next_minutes = 0 [static]

Definition at line 82 of file uptime.c.

Referenced by check_minutely(), check_secondly(), and init_uptime().

int next_seconds = 0 [static]

Definition at line 81 of file uptime.c.

Referenced by check_secondly(), and init_uptime().

time_t next_update = 0 [static]

Definition at line 84 of file uptime.c.

Referenced by check_secondly(), init_uptime(), and uptime_report().

int seconds = 0 [static]

Definition at line 80 of file uptime.c.

Referenced by check_secondly().

int update_interval = 720 [static]

Definition at line 83 of file uptime.c.

Referenced by check_secondly(), and init_uptime().

Definition at line 75 of file uptime.c.

Function uptime_table[] [static]
Initial value:

Definition at line 273 of file uptime.c.

Referenced by uptime_start().

char uptime_version[48] = "" [static]

Definition at line 88 of file uptime.c.

Referenced by init_uptime(), and send_uptime().

int uptimecount [static]

Definition at line 86 of file uptime.c.

Referenced by init_uptime(), send_uptime(), and uptime_report().

unsigned long uptimeip [static]

Definition at line 87 of file uptime.c.

Referenced by init_uptime(), and send_uptime().

int uptimesock [static]

Definition at line 85 of file uptime.c.

Referenced by init_uptime(), and send_uptime().


Generated on 7 Sep 2016 for Eggdrop by  doxygen 1.6.1