diff options
| author | Root THC | 2026-02-24 12:42:47 +0000 |
|---|---|---|
| committer | Root THC | 2026-02-24 12:42:47 +0000 |
| commit | c9cbeced5b3f2bdd7407e29c0811e65954132540 (patch) | |
| tree | aefc355416b561111819de159ccbd86c3004cf88 /other/ldistfp/src/common.c | |
| parent | 073fe4bf9fca6bf40cef2886d75df832ef4b6fca (diff) | |
initial
Diffstat (limited to 'other/ldistfp/src/common.c')
| -rw-r--r-- | other/ldistfp/src/common.c | 373 |
1 files changed, 373 insertions, 0 deletions
diff --git a/other/ldistfp/src/common.c b/other/ldistfp/src/common.c new file mode 100644 index 0000000..bae7ebc --- /dev/null +++ b/other/ldistfp/src/common.c | |||
| @@ -0,0 +1,373 @@ | |||
| 1 | |||
| 2 | #include <sys/types.h> | ||
| 3 | #include <sys/wait.h> | ||
| 4 | #include <sys/time.h> | ||
| 5 | #include <netinet/in.h> | ||
| 6 | #include <unistd.h> | ||
| 7 | #include <time.h> | ||
| 8 | #include <stdarg.h> | ||
| 9 | #include <stdio.h> | ||
| 10 | #include <string.h> | ||
| 11 | #include <stdlib.h> | ||
| 12 | #include <unistd.h> | ||
| 13 | #include "common.h" | ||
| 14 | |||
| 15 | |||
| 16 | #ifdef DEBUG | ||
| 17 | void | ||
| 18 | debugp (char *filename, const char *str, ...) | ||
| 19 | { | ||
| 20 | FILE *fp; /* temporary file pointer */ | ||
| 21 | va_list vl; | ||
| 22 | |||
| 23 | fp = fopen (filename, "a"); | ||
| 24 | if (fp == NULL) | ||
| 25 | return; | ||
| 26 | |||
| 27 | va_start (vl, str); | ||
| 28 | vfprintf (fp, str, vl); | ||
| 29 | va_end (vl); | ||
| 30 | |||
| 31 | fclose (fp); | ||
| 32 | |||
| 33 | return; | ||
| 34 | } | ||
| 35 | |||
| 36 | void | ||
| 37 | hexdump (char *filename, unsigned char *data, unsigned int amount) | ||
| 38 | { | ||
| 39 | FILE *fp; /* temporary file pointer */ | ||
| 40 | unsigned int dp, p; /* data pointer */ | ||
| 41 | const char trans[] = | ||
| 42 | "................................ !\"#$%&'()*+,-./0123456789" | ||
| 43 | ":;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklm" | ||
| 44 | "nopqrstuvwxyz{|}~...................................." | ||
| 45 | "....................................................." | ||
| 46 | "........................................"; | ||
| 47 | |||
| 48 | fp = fopen (filename, "a"); | ||
| 49 | if (fp == NULL) | ||
| 50 | return; | ||
| 51 | |||
| 52 | fprintf (fp, "\n-packet-\n"); | ||
| 53 | |||
| 54 | for (dp = 1; dp <= amount; dp++) { | ||
| 55 | fprintf (fp, "%02x ", data[dp-1]); | ||
| 56 | if ((dp % 8) == 0) | ||
| 57 | fprintf (fp, " "); | ||
| 58 | if ((dp % 16) == 0) { | ||
| 59 | fprintf (fp, "| "); | ||
| 60 | p = dp; | ||
| 61 | for (dp -= 16; dp < p; dp++) | ||
| 62 | fprintf (fp, "%c", trans[data[dp]]); | ||
| 63 | fflush (fp); | ||
| 64 | fprintf (fp, "\n"); | ||
| 65 | } | ||
| 66 | fflush (fp); | ||
| 67 | } | ||
| 68 | if ((amount % 16) != 0) { | ||
| 69 | p = dp = 16 - (amount % 16); | ||
| 70 | for (dp = p; dp > 0; dp--) { | ||
| 71 | fprintf (fp, " "); | ||
| 72 | if (((dp % 8) == 0) && (p != 8)) | ||
| 73 | fprintf (fp, " "); | ||
| 74 | fflush (fp); | ||
| 75 | } | ||
| 76 | fprintf (fp, " | "); | ||
| 77 | for (dp = (amount - (16 - p)); dp < amount; dp++) | ||
| 78 | fprintf (fp, "%c", trans[data[dp]]); | ||
| 79 | fflush (fp); | ||
| 80 | } | ||
| 81 | fprintf (fp, "\n"); | ||
| 82 | |||
| 83 | fclose (fp); | ||
| 84 | return; | ||
| 85 | } | ||
| 86 | |||
| 87 | #endif | ||
| 88 | |||
| 89 | |||
| 90 | /* z_fork | ||
| 91 | * | ||
| 92 | * fork and detach forked client completely to avoid zombies. | ||
| 93 | * taken from richard stevens excellent system programming book :) thanks, | ||
| 94 | * whereever you are now. | ||
| 95 | * | ||
| 96 | * caveat: the pid of the child has already died, it can just be used to | ||
| 97 | * differentiate between parent and not parent, the pid of the | ||
| 98 | * child is inaccessibly. | ||
| 99 | * | ||
| 100 | * return pid of child for old process | ||
| 101 | * return 0 for child | ||
| 102 | */ | ||
| 103 | |||
| 104 | pid_t | ||
| 105 | z_fork (void) | ||
| 106 | { | ||
| 107 | pid_t pid; | ||
| 108 | |||
| 109 | pid = fork (); | ||
| 110 | if (pid < 0) { | ||
| 111 | return (pid); | ||
| 112 | } else if (pid == 0) { | ||
| 113 | /* let the child fork again | ||
| 114 | */ | ||
| 115 | |||
| 116 | pid = fork (); | ||
| 117 | if (pid < 0) { | ||
| 118 | return (pid); | ||
| 119 | } else if (pid > 0) { | ||
| 120 | /* let the child and parent of the second child | ||
| 121 | * exit | ||
| 122 | */ | ||
| 123 | exit (EXIT_SUCCESS); | ||
| 124 | } | ||
| 125 | |||
| 126 | return (0); | ||
| 127 | } | ||
| 128 | |||
| 129 | waitpid (pid, NULL, 0); | ||
| 130 | |||
| 131 | return (pid); | ||
| 132 | } | ||
| 133 | |||
| 134 | |||
| 135 | /* m_random | ||
| 136 | * | ||
| 137 | * return a random number between `lowmark' and `highmark' | ||
| 138 | */ | ||
| 139 | |||
| 140 | int | ||
| 141 | m_random (int lowmark, int highmark) | ||
| 142 | { | ||
| 143 | long int rnd; | ||
| 144 | |||
| 145 | /* flip/swap them in case user messed up | ||
| 146 | */ | ||
| 147 | if (lowmark > highmark) { | ||
| 148 | lowmark ^= highmark; | ||
| 149 | highmark ^= lowmark; | ||
| 150 | lowmark ^= highmark; | ||
| 151 | } | ||
| 152 | rnd = lowmark; | ||
| 153 | |||
| 154 | rnd += (random () % (highmark - lowmark)); | ||
| 155 | |||
| 156 | /* this is lame, i know :) | ||
| 157 | */ | ||
| 158 | return (rnd); | ||
| 159 | } | ||
| 160 | |||
| 161 | |||
| 162 | /* set_tv | ||
| 163 | * | ||
| 164 | * initializes a struct timeval pointed to by `tv' to a second value of | ||
| 165 | * `seconds' | ||
| 166 | * | ||
| 167 | * return in any case | ||
| 168 | */ | ||
| 169 | |||
| 170 | void | ||
| 171 | set_tv (struct timeval *tv, int seconds) | ||
| 172 | { | ||
| 173 | tv->tv_sec = seconds; | ||
| 174 | tv->tv_usec = 0; | ||
| 175 | |||
| 176 | return; | ||
| 177 | } | ||
| 178 | |||
| 179 | |||
| 180 | /* xstrupper | ||
| 181 | * | ||
| 182 | * uppercase a string `str' | ||
| 183 | * | ||
| 184 | * return in any case | ||
| 185 | */ | ||
| 186 | |||
| 187 | void | ||
| 188 | xstrupper (char *str) | ||
| 189 | { | ||
| 190 | for (; *str != '\0'; ++str) { | ||
| 191 | if (*str >= 'a' && *str <= 'z') { | ||
| 192 | *str -= ('a' - 'A'); | ||
| 193 | } | ||
| 194 | } | ||
| 195 | |||
| 196 | return; | ||
| 197 | } | ||
| 198 | |||
| 199 | |||
| 200 | /* concating snprintf | ||
| 201 | * | ||
| 202 | * determines the length of the string pointed to by `os', appending formatted | ||
| 203 | * string to a maximium length of `len'. | ||
| 204 | * | ||
| 205 | */ | ||
| 206 | |||
| 207 | void | ||
| 208 | scnprintf (char *os, size_t len, const char *str, ...) | ||
| 209 | { | ||
| 210 | va_list vl; | ||
| 211 | char *ostmp = os + strlen (os); | ||
| 212 | |||
| 213 | va_start (vl, str); | ||
| 214 | vsnprintf (ostmp, len - strlen (os) - 1, str, vl); | ||
| 215 | va_end (vl); | ||
| 216 | |||
| 217 | return; | ||
| 218 | } | ||
| 219 | |||
| 220 | unsigned long int | ||
| 221 | tdiff (struct timeval *old, struct timeval *new) | ||
| 222 | { | ||
| 223 | unsigned long int time1; | ||
| 224 | |||
| 225 | if (new->tv_sec >= old->tv_sec) { | ||
| 226 | time1 = new->tv_sec - old->tv_sec; | ||
| 227 | if ((new->tv_usec - 500000) >= old->tv_usec) | ||
| 228 | time1++; | ||
| 229 | } else { | ||
| 230 | time1 = old->tv_sec - new->tv_sec; | ||
| 231 | if ((old->tv_usec - 500000) >= new->tv_usec) | ||
| 232 | time1++; | ||
| 233 | } | ||
| 234 | |||
| 235 | return (time1); | ||
| 236 | } | ||
| 237 | |||
| 238 | |||
| 239 | /* ipv4_print | ||
| 240 | * | ||
| 241 | * padding = 0 -> don't padd | ||
| 242 | * padding = 1 -> padd with zeros | ||
| 243 | * padding = 2 -> padd with spaces | ||
| 244 | */ | ||
| 245 | |||
| 246 | char * | ||
| 247 | ipv4_print (char *dest, struct in_addr in, int padding) | ||
| 248 | { | ||
| 249 | unsigned char *ipp; | ||
| 250 | |||
| 251 | ipp = (unsigned char *) &in.s_addr; | ||
| 252 | |||
| 253 | strcpy (dest, ""); | ||
| 254 | |||
| 255 | switch (padding) { | ||
| 256 | case (0): | ||
| 257 | sprintf (dest, "%d.%d.%d.%d", | ||
| 258 | ipp[0], ipp[1], ipp[2], ipp[3]); | ||
| 259 | break; | ||
| 260 | case (1): | ||
| 261 | sprintf (dest, "%03d.%03d.%03d.%03d", | ||
| 262 | ipp[0], ipp[1], ipp[2], ipp[3]); | ||
| 263 | break; | ||
| 264 | case (2): | ||
| 265 | sprintf (dest, "%3d.%3d.%3d.%3d", | ||
| 266 | ipp[0], ipp[1], ipp[2], ipp[3]); | ||
| 267 | break; | ||
| 268 | default: | ||
| 269 | break; | ||
| 270 | } | ||
| 271 | |||
| 272 | return (dest); | ||
| 273 | } | ||
| 274 | |||
| 275 | |||
| 276 | void * | ||
| 277 | xrealloc (void *m_ptr, size_t newsize) | ||
| 278 | { | ||
| 279 | void *n_ptr; | ||
| 280 | |||
| 281 | n_ptr = realloc (m_ptr, newsize); | ||
| 282 | if (n_ptr == NULL) { | ||
| 283 | fprintf (stderr, "realloc failed\n"); | ||
| 284 | exit (EXIT_FAILURE); | ||
| 285 | } | ||
| 286 | |||
| 287 | return (n_ptr); | ||
| 288 | } | ||
| 289 | |||
| 290 | |||
| 291 | char * | ||
| 292 | xstrdup (char *str) | ||
| 293 | { | ||
| 294 | char *b; | ||
| 295 | |||
| 296 | b = strdup (str); | ||
| 297 | if (b == NULL) { | ||
| 298 | fprintf (stderr, "strdup failed\n"); | ||
| 299 | exit (EXIT_FAILURE); | ||
| 300 | } | ||
| 301 | |||
| 302 | return (b); | ||
| 303 | } | ||
| 304 | |||
| 305 | |||
| 306 | void * | ||
| 307 | xcalloc (int factor, size_t size) | ||
| 308 | { | ||
| 309 | void *bla; | ||
| 310 | |||
| 311 | bla = calloc (factor, size); | ||
| 312 | |||
| 313 | if (bla == NULL) { | ||
| 314 | fprintf (stderr, "no memory left\n"); | ||
| 315 | exit (EXIT_FAILURE); | ||
| 316 | } | ||
| 317 | |||
| 318 | return (bla); | ||
| 319 | } | ||
| 320 | |||
| 321 | /* source by dk | ||
| 322 | */ | ||
| 323 | |||
| 324 | char * | ||
| 325 | allocncat (char **to, char *from, size_t len) | ||
| 326 | { | ||
| 327 | int rlen = strlen (from); | ||
| 328 | int null = (*to == NULL); | ||
| 329 | |||
| 330 | len = rlen < len ? rlen : len; | ||
| 331 | *to = xrealloc (*to, (null ? 0 : strlen (*to)) + len + 1); | ||
| 332 | if (null) | ||
| 333 | **to = '\0'; | ||
| 334 | |||
| 335 | return (strncat (*to, from, len)); | ||
| 336 | } | ||
| 337 | |||
| 338 | |||
| 339 | char * | ||
| 340 | alloccat (char **to, char *from) | ||
| 341 | { | ||
| 342 | return (allocncat (to, from, strlen (from))); | ||
| 343 | } | ||
| 344 | |||
| 345 | |||
| 346 | char * | ||
| 347 | file_load (char *pathname) | ||
| 348 | { | ||
| 349 | FILE * fp; | ||
| 350 | char * new; | ||
| 351 | |||
| 352 | if (pathname == NULL) | ||
| 353 | return (NULL); | ||
| 354 | |||
| 355 | fp = fopen (pathname, "r"); | ||
| 356 | if (fp == NULL) | ||
| 357 | return (NULL); | ||
| 358 | |||
| 359 | new = NULL; | ||
| 360 | while (feof (fp) == 0) { | ||
| 361 | unsigned long int rpos; | ||
| 362 | |||
| 363 | rpos = (new == NULL) ? 0 : strlen (new); | ||
| 364 | new = xrealloc (new, rpos + 1024); | ||
| 365 | memset (new + rpos, '\x00', 1024); | ||
| 366 | fread (new + rpos, sizeof (char), 1023, fp); | ||
| 367 | } | ||
| 368 | fclose (fp); | ||
| 369 | new = xrealloc (new, strlen (new) + 1); | ||
| 370 | |||
| 371 | return (new); | ||
| 372 | } | ||
| 373 | |||
