1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
|
/* (C) 2006-2010 by folkert@vanheusden.com GPLv2 applies */
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <syslog.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
extern "C" {
#include "error.h"
}
#include "utils.h"
#ifdef _DEBUG
typedef struct
{
void *p;
char *descr;
int size;
} memlist;
memlist *pm = NULL;
int n_pm = 0;
void LOG(char *s, ...)
{
va_list ap;
FILE *fh = fopen("log.log", "a+");
if (!fh)
error_exit("can't access log.log");
va_start(ap, s);
vfprintf(fh, s, ap);
va_end(ap);
fclose(fh);
}
void dump_mem(int sig)
{
int loop;
signal(SIGHUP, dump_mem);
if (sig != SIGHUP)
error_exit("unexpected signal %d for dump_mem\n", sig);
LOG("%d elements of memory used\n", n_pm);
for(loop=0; loop<n_pm; loop++)
{
LOG("%06d] %p %d (%s)\n", loop, pm[loop].p, pm[loop].size, pm[loop].descr);
}
LOG("--- finished memory dump\n");
}
void remove_mem_element(void *p, char *what)
{
if (p)
{
int loop;
for(loop=0; loop<n_pm; loop++)
{
if (pm[loop].p == p)
{
int n_to_move;
n_to_move = (n_pm - loop) - 1;
memmove(&pm[loop], &pm[loop + 1], n_to_move * sizeof(memlist));
n_pm--;
loop=-1;
break;
}
}
if (loop != -1)
{
LOG("myfree: pointer %p (%s) not found\n", p, what);
}
if (n_pm == 0)
{
free(pm);
pm = NULL;
}
else
{
pm = (memlist *)realloc(pm, sizeof(memlist) * n_pm);
if (!pm) error_exit("failed to shrink memorylist to %d elements\n", n_pm);
}
}
}
void add_mem_element(void *p, int size, char *what)
{
pm = (memlist *)realloc(pm, sizeof(memlist) * (n_pm + 1));
if (!pm) error_exit("failed to grow memorylist from %d elements\n", n_pm);
pm[n_pm].p = p;
pm[n_pm].size = size;
pm[n_pm].descr = what;
n_pm++;
}
#endif
void myfree(void *p, char *what)
{
#ifdef _DEBUG
if (p)
remove_mem_element(p, what);
#endif
free(p);
}
void * myrealloc(void *oldp, int newsize, char *what)
{
#ifdef _DEBUG
void *dummy;
dummy = realloc(oldp, newsize);
if (!dummy)
error_exit("failed to reallocate to %d bytes for %s\n", newsize, what);
remove_mem_element(oldp, what);
add_mem_element(dummy, newsize, what);
signal(SIGHUP, dump_mem);
#else
/* ----------------------------------------------------
* add code for repeatingly retry? -> then configurable
* via configurationfile with number of retries and/or
* sleep
* ----------------------------------------------------
*/
void *dummy = realloc(oldp, newsize);
if (!dummy)
error_exit("failed to reallocate to %d bytes for %s\n", newsize, what);
#endif
return dummy;
}
void * mymalloc(int size, char *what)
{
void *dummy = myrealloc(NULL, size, what);
if (!dummy)
error_exit("failed to allocate %d bytes for %s\n", size, what);
return dummy;
}
char * mystrndup(char *in, int len)
{
#ifdef _DEBUG
char *dummy = (char *)mymalloc(len + 1, in);
if (!dummy)
error_exit("failed to copy string '%s': out of memory?\n", in);
memcpy(dummy, in, len + 1);
return dummy;
#else
char *dummy = strndup(in, len);
if (!dummy)
error_exit("failed to copy string '%s': out of memory?\n", in);
return dummy;
#endif
}
char * mystrdup(char *in)
{
return mystrndup(in, strlen(in));
}
int IRCREAD( server_t server_conn, char *buff, size_t len){
if( server_conn.ssl ){
return SSL_read( server_conn.ssl, buff, len );
}
return read( server_conn.fd, buff, len );
}
ssize_t IRCWRITE(server_t server_conn, char *whereto, size_t len)
{
ssize_t cnt=0;
while(len>0)
{
ssize_t rc;
if( server_conn.ssl ){
rc = SSL_write( server_conn.ssl, whereto, len);
}else{
rc = write(server_conn.fd, whereto, len);
}
if (rc == -1)
{
if (errno != EINTR && errno != EINPROGRESS && errno != EAGAIN)
{
syslog(LOG_INFO, "Error sending to IRC server: %m");
return -1;
}
}
else if (rc == 0)
{
return -1;
}
else
{
whereto += rc;
len -= rc;
cnt += rc;
}
}
return cnt;
}
ssize_t WRITE(int fd, char *whereto, size_t len)
{
ssize_t cnt=0;
while(len>0)
{
ssize_t rc;
rc = write(fd, whereto, len);
if (rc == -1)
{
if (errno != EINTR && errno != EINPROGRESS && errno != EAGAIN)
{
syslog(LOG_INFO, "Error sending to IRC server: %m");
return -1;
}
}
else if (rc == 0)
{
return -1;
}
else
{
whereto += rc;
len -= rc;
cnt += rc;
}
}
return cnt;
}
int get_filesize(char *filename)
{
struct stat buf;
if (stat(filename, &buf) == -1)
{
if (errno != ENOENT)
error_exit("stat failed for %s", filename);
return -1;
}
return buf.st_size;
}
time_t get_filechanged(char *filename)
{
struct stat buf;
if (stat(filename, &buf) == -1)
{
if (errno != ENOENT)
error_exit("stat failed for %s", filename);
return -1;
}
return buf.st_mtime;
}
void resolve_host(char *host, struct sockaddr_in *addr)
{
struct hostent *hostdnsentries;
hostdnsentries = gethostbyname(host);
if (hostdnsentries == NULL)
{
switch(h_errno)
{
case HOST_NOT_FOUND:
error_exit("The specified host is unknown.\n");
break;
case NO_ADDRESS:
error_exit("The requested name is valid but does not have an IP address.\n");
break;
case NO_RECOVERY:
error_exit("A non-recoverable name server error occurred.\n");
break;
case TRY_AGAIN:
error_exit("A temporary error occurred on an authoritative name server. Try again later.\n");
break;
default:
error_exit("Could not resolve %s for an unknown reason (%d)\n", host, h_errno);
}
}
/* create address structure */
addr -> sin_family = hostdnsentries -> h_addrtype;
addr -> sin_addr = incopy(hostdnsentries -> h_addr_list[0]);
}
int connect_to(char *h)
{
char *hoststr = mystrdup(h);
int fd;
char *colon = strchr(hoststr, ':');
int portnr = 33333;
struct sockaddr_in addr;
int keep_alive = 1;
if (colon)
{
*colon = 0x00;
portnr = atoi(colon + 1);
}
/* resolve */
memset(&addr, 0x00, sizeof(addr));
resolve_host(hoststr, &addr);
addr.sin_port = htons(portnr);
myfree(hoststr, "connect_to");
/* connect */
fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd == -1)
error_exit("problem creating socket");
if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (char *)&keep_alive, sizeof(keep_alive)) == -1)
error_exit("problem setting KEEPALIVE");
/* connect to peer */
if (connect(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr)) == 0)
{
/* connection made, return */
return fd;
}
close(fd);
return -1;
}
int write_pidfile(char *fname)
{
FILE *fh = fopen(fname, "w");
if (!fh)
error_exit("write_pidfile::fopen: failed creating file %s", fname);
fprintf(fh, "%i", getpid());
fclose(fh);
return 0;
}
|