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
|
/* Nessuslib -- the Nessus Library
* Copyright (C) 1998 Renaud Deraison
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#define EXPORTING
#include <includes.h>
#include "system.h"
#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif
ExtFunc
void * emalloc(size)
size_t size;
{
void * ptr;
/*
* Just for our personal safety, we increase the
* size by one
*/
if((int)size < 0)
{
fprintf(stderr, "[%d] Won't allocate a pointer of size %ld !\n", getpid(), size);
exit(1);
}
size++;
/*
* If no memory can be allocated, then wait a little.
* It's very likely that another nessusd child will free
* the size of memory we need. So we make 10 attempts,
* and if nothing happens, then we exit
*/
ptr = malloc(size);
if(!ptr){
int i;
for(i=0; (i<5) && ptr == NULL ;i++)
{
waitpid(0, NULL, WNOHANG);
usleep(5000);
ptr = malloc(size);
}
if( ptr == NULL )
{
fprintf(stderr, "[%d] Could not allocate a pointer of size %ld !\n", getpid(), size);
exit(1);
}
}
bzero(ptr, size);
return(ptr);
}
ExtFunc char *
estrdup(str)
const char * str;
{
char * buf;
int len;
if (!str) return NULL;
len = strlen(str);
buf = emalloc(len + 1);
memcpy(buf, str, len);
buf[len] = '\0';
return buf;
}
ExtFunc void
efree(ptr)
void * ptr;
{
char ** p = ptr;
if(p && *p){
free(*p);
*p=NULL;
}
}
ExtFunc void *
erealloc(ptr, size)
void * ptr;
size_t size;
{
void * ret;
if ( (int)size < 0 )
{
fprintf(stderr, "Won't realloc() a pointer of size %ld !\n", size);
exit (1);
}
ret = realloc(ptr, size);
if(!ret)
{
fprintf(stderr, "Could not realloc() a pointer of size %ld !\n",
size);
exit (1);
}
return ret;
}
ExtFunc size_t
estrlen(s,n)
const char * s;
size_t n;
{
size_t i;
for(i = 0; (*(s+i) != '\0' && i < n); i++);
return i;
}
#ifndef HAVE_INET_ATON
/*
* Coming straight from Fyodor's Nmap
*/
int
inet_aton(cp, addr)
register const char *cp;
struct in_addr *addr;
{
register unsigned int val; /* changed from u_long --david */
register int base, n;
register char c;
u_int parts[4];
register u_int *pp = parts;
c = *cp;
for (;;) {
/*
* Collect number up to ``.''.
* Values are specified as for C:
* 0x=hex, 0=octal, isdigit=decimal.
*/
if (!isdigit((int)c))
return (0);
val = 0; base = 10;
if (c == '0') {
c = *++cp;
if (c == 'x' || c == 'X')
base = 16, c = *++cp;
else
base = 8;
}
for (;;) {
if (isascii((int)c) && isdigit((int)c)) {
val = (val * base) + (c - '0');
c = *++cp;
} else if (base == 16 && isascii((int)c) && isxdigit((int)c)) {
val = (val << 4) |
(c + 10 - (islower((int)c) ? 'a' : 'A'));
c = *++cp;
} else
break;
}
if (c == '.') {
/*
* Internet format:
* a.b.c.d
* a.b.c (with c treated as 16 bits)
* a.b (with b treated as 24 bits)
*/
if (pp >= parts + 3)
return (0);
*pp++ = val;
c = *++cp;
} else
break;
}
/*
* Check for trailing characters.
*/
if (c != '\0' && (!isascii((int)c) || !isspace((int)c)))
return (0);
/*
* Concoct the address according to
* the number of parts specified.
*/
n = pp - parts + 1;
switch (n) {
case 0:
return (0); /* initial nondigit */
case 1: /* a -- 32 bits */
break;
case 2: /* a.b -- 8.24 bits */
if (val > 0xffffff)
return (0);
val |= parts[0] << 24;
break;
case 3: /* a.b.c -- 8.8.16 bits */
if (val > 0xffff)
return (0);
val |= (parts[0] << 24) | (parts[1] << 16);
break;
case 4: /* a.b.c.d -- 8.8.8.8 bits */
if (val > 0xff)
return (0);
val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
break;
}
if (addr)
addr->s_addr = htonl(val);
return (1);
}
#endif
|