00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #ifndef lint
00037 static const char rcsid[] = "$Id: uptime.c,v 1.14 2003-12-11 00:49:11 wcc Exp $";
00038 #endif
00039
00040 #define MODULE_NAME "uptime"
00041 #define MAKING_UPTIME
00042 #include "uptime.h"
00043 #include "lib/eggdrop/module.h"
00044 #include "modules/server/server.h"
00045 #include <netdb.h>
00046 #include <netinet/in.h>
00047 #include <arpa/inet.h>
00048 #include <unistd.h>
00049 #include <fcntl.h>
00050 #include <sys/types.h>
00051 #include <sys/socket.h>
00052 #include <fcntl.h>
00053 #include <sys/stat.h>
00054 #include <unistd.h>
00055
00056 #define start uptime_LTX_start
00057
00058 typedef struct PackUp
00059 {
00060 int regnr;
00061 int pid;
00062 int type;
00063 unsigned long cookie;
00064 unsigned long uptime;
00065 unsigned long ontime;
00066 unsigned long now2;
00067 unsigned long sysup;
00068 char string[3];
00069
00070 } PackUp;
00071
00072 PackUp upPack;
00073
00074 static Function *server_funcs = NULL;
00075 static eggdrop_t *egg = NULL;
00076
00077 char *uptime_host = NULL;
00078 int uptimeport = 9969;
00079 int hours=0;
00080 int uptimesock;
00081 int uptimecount;
00082 unsigned long uptimeip;
00083 unsigned long uptimecookie;
00084 time_t uptimelast;
00085 char uptime_version[50]="";
00086
00087 static void uptime_report(int idx, int details)
00088 {
00089 }
00090
00091
00092 unsigned long get_ip()
00093 {
00094 struct hostent *hp;
00095 IP ip;
00096 struct in_addr *in;
00097
00098
00099 if (uptime_host[0]) {
00100 if ((uptime_host[strlen(uptime_host) - 1] >= '0') && (uptime_host[strlen(uptime_host) - 1] <= '9'))
00101 return (IP) inet_addr(uptime_host);
00102 }
00103 hp = gethostbyname(uptime_host);
00104 if (hp == NULL)
00105 return -1;
00106 in = (struct in_addr *) (hp->h_addr_list[0]);
00107 ip = (IP) (in->s_addr);
00108 return ip;
00109 }
00110
00111 int init_uptime(void)
00112 {
00113 struct sockaddr_in sai;
00114 char temp[50]="";
00115 upPack.regnr = 0;
00116 upPack.pid = htonl(getpid());
00117 upPack.type = htonl(UPTIME_EGGDROP);
00118 upPack.cookie = 0;
00119 upPack.uptime = htonl(online_since);
00120 uptimecookie = rand();
00121 uptimecount = 0;
00122 uptimelast = 0;
00123 uptimeip = -1;
00124
00125
00126 strcpy(temp,ver);
00127 splitc(uptime_version,temp,' ');
00128 strcpy(uptime_version,temp);
00129
00130 if ((uptimesock = socket(AF_INET,SOCK_DGRAM,0)) < 0) {
00131 putlog(LOG_DEBUG, "*", "init_uptime socket returned <0 %d",uptimesock);
00132 return((uptimesock = -1));
00133 }
00134 memset(&sai,0,sizeof(sai));
00135 sai.sin_addr.s_addr = INADDR_ANY;
00136 sai.sin_family = AF_INET;
00137 if (bind(uptimesock,(struct sockaddr*)&sai,sizeof(sai)) < 0) {
00138 close(uptimesock);
00139 return((uptimesock = -1));
00140 }
00141 fcntl(uptimesock,F_SETFL,O_NONBLOCK | fcntl(uptimesock,F_GETFL));
00142 return(0);
00143 }
00144
00145
00146 int send_uptime(void)
00147 {
00148 struct sockaddr_in sai;
00149 struct stat st;
00150 PackUp *mem;
00151 int len;
00152
00153 uptimecookie = (uptimecookie + 1) * 18457;
00154 upPack.cookie = htonl(uptimecookie);
00155 upPack.now2 = htonl(time(NULL));
00156 if (stat("/proc",&st) < 0)
00157 upPack.sysup = 0;
00158 else
00159 upPack.sysup = htonl(st.st_ctime);
00160 upPack.uptime = htonl(online_since);
00161 upPack.ontime = htonl(server_online);
00162 uptimecount++;
00163 if (((uptimecount & 0x7) == 0) || (uptimeip == -1)) {
00164 uptimeip = get_ip();
00165 if (uptimeip == -1)
00166 return -2;
00167 }
00168 len = sizeof(upPack) + strlen(myname) + strlen(dcc[servidx].host) + strlen(uptime_version);
00169 mem = (PackUp*)malloc(len);
00170 memcpy(mem,&upPack,sizeof(upPack));
00171 sprintf(mem->string,"%s %s %s",myname,dcc[servidx].host,uptime_version);
00172 memset(&sai,0,sizeof(sai));
00173 sai.sin_family = AF_INET;
00174 sai.sin_addr.s_addr = uptimeip;
00175 sai.sin_port = htons(uptimeport);
00176 len = sendto(uptimesock,(void*)mem,len,0,(struct sockaddr*)&sai,sizeof(sai));
00177 free(mem);
00178 return len;
00179 }
00180
00181 void check_hourly() {
00182 hours++;
00183 if (hours==6) {
00184 send_uptime();
00185 hours=0;
00186 }
00187 }
00188
00189 static int uptime_set_send(struct userrec *u, int idx, char *par)
00190 {
00191 dprintf(idx,"Nick %s Ontime %lu Version %s Result %d\n", myname, online_since,
00192 uptime_version, send_uptime());
00193 return 1;
00194 }
00195
00196 static cmd_t mydcc[] =
00197 {
00198 {"usetsend", "", uptime_set_send, NULL},
00199 {0, 0, 0, 0}
00200 };
00201
00202 static tcl_strings mystrings[] =
00203 {
00204 {0, 0, 0, 0}
00205 };
00206
00207 static tcl_ints myints[] =
00208 {
00209 {0, 0}
00210 };
00211
00212 static char *uptime_close()
00213 {
00214 rem_tcl_strings(mystrings);
00215 rem_tcl_ints(myints);
00216 rem_builtins("dcc", mydcc);
00217 free(uptime_host);
00218 close(uptimesock);
00219 del_hook(HOOK_HOURLY, (Function) check_hourly);
00220 module_undepend(MODULE_NAME);
00221 return NULL;
00222 }
00223
00224 EXPORT_SCOPE char *start();
00225
00226 static Function uptime_table[] =
00227 {
00228 (Function) start,
00229 (Function) uptime_close,
00230 (Function) 0,
00231 (Function) uptime_report,
00232 };
00233
00234 char *start(eggdrop_t *eggdrop)
00235 {
00236 egg = eggdrop;
00237
00238 if (!module_depend(MODULE_NAME, "eggdrop", 107, 0)) {
00239 module_undepend(MODULE_NAME);
00240 return _("This module requires eggdrop1.7.0 or later");
00241 }
00242 if (!(server_funcs = module_depend(MODULE_NAME, "server", 1, 0)))
00243 return _("You need the server module to use the uptime module.");
00244 module_register(MODULE_NAME, uptime_table, 1, 1);
00245 add_tcl_strings(mystrings);
00246 add_tcl_ints(myints);
00247 add_builtins("dcc", mydcc);
00248 add_hook(HOOK_HOURLY, (Function) check_hourly);
00249 uptime_host = strdup(UPTIME_HOST);
00250 init_uptime();
00251 return NULL;
00252 }