summaryrefslogtreecommitdiff
path: root/other/3wahas/common.c
diff options
context:
space:
mode:
Diffstat (limited to 'other/3wahas/common.c')
-rw-r--r--other/3wahas/common.c296
1 files changed, 296 insertions, 0 deletions
diff --git a/other/3wahas/common.c b/other/3wahas/common.c
new file mode 100644
index 0000000..6f8f591
--- /dev/null
+++ b/other/3wahas/common.c
@@ -0,0 +1,296 @@
1
2#include <sys/time.h>
3#include <netinet/in.h>
4#include <unistd.h>
5#include <pthread.h>
6#include <time.h>
7#include <stdarg.h>
8#include <stdio.h>
9#include <string.h>
10#include <stdlib.h>
11#include <unistd.h>
12#include "common.h"
13
14#ifdef DEBUG
15void
16debugp (char *filename, const char *str, ...)
17{
18 FILE *fp; /* temporary file pointer */
19 va_list vl;
20
21 fp = fopen (filename, "a");
22 if (fp == NULL)
23 return;
24
25 va_start (vl, str);
26 vfprintf (fp, str, vl);
27 va_end (vl);
28
29 fclose (fp);
30
31 return;
32}
33
34void
35hexdump (char *filename, unsigned char *data, unsigned int amount)
36{
37 FILE *fp; /* temporary file pointer */
38 unsigned int dp, p; /* data pointer */
39 const char trans[] = "................................ !\"#$%&'()*+,-./0123456789"
40 ":;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklm"
41 "nopqrstuvwxyz{|}~...................................."
42 "....................................................."
43 "........................................";
44
45 fp = fopen (filename, "a");
46 if (fp == NULL)
47 return;
48
49 fprintf (fp, "\n-packet-\n");
50
51 for (dp = 1; dp <= amount; dp++) {
52 fprintf (fp, "%02x ", data[dp-1]);
53 if ((dp % 8) == 0)
54 fprintf (fp, " ");
55 if ((dp % 16) == 0) {
56 fprintf (fp, "| ");
57 p = dp;
58 for (dp -= 16; dp < p; dp++)
59 fprintf (fp, "%c", trans[data[dp]]);
60 fflush (fp);
61 fprintf (fp, "\n");
62 }
63 fflush (fp);
64 }
65 if ((amount % 16) != 0) {
66 p = dp = 16 - (amount % 16);
67 for (dp = p; dp > 0; dp--) {
68 fprintf (fp, " ");
69 if (((dp % 8) == 0) && (p != 8))
70 fprintf (fp, " ");
71 fflush (fp);
72 }
73 fprintf (fp, " | ");
74 for (dp = (amount - (16 - p)); dp < amount; dp++)
75 fprintf (fp, "%c", trans[data[dp]]);
76 fflush (fp);
77 }
78 fprintf (fp, "\n");
79
80 fclose (fp);
81 return;
82}
83
84#endif
85
86
87/* m_random
88 *
89 * return a random number between `lowmark' and `highmark'
90 */
91
92int
93m_random (int lowmark, int highmark)
94{
95 long int rnd;
96
97 /* flip/swap them in case user messed up
98 */
99 if (lowmark > highmark) {
100 lowmark ^= highmark;
101 highmark ^= lowmark;
102 lowmark ^= highmark;
103 }
104 rnd = lowmark;
105
106 srandom ((unsigned int) time (NULL));
107 rnd += (random () % (highmark - lowmark));
108
109 /* this is lame, i know :)
110 */
111 return (rnd);
112}
113
114
115/* set_tv
116 *
117 * initializes a struct timeval pointed to by `tv' to a second value of
118 * `seconds'
119 *
120 * return in any case
121 */
122
123void
124set_tv (struct timeval *tv, int seconds)
125{
126 tv->tv_sec = seconds;
127 tv->tv_usec = 0;
128
129 return;
130}
131
132
133/* xstrupper
134 *
135 * uppercase a string `str'
136 *
137 * return in any case
138 */
139
140void
141xstrupper (char *str)
142{
143 for (; *str != '\0'; ++str) {
144 if (*str >= 'a' && *str <= 'z') {
145 *str -= ('a' - 'A');
146 }
147 }
148
149 return;
150}
151
152
153/* concating snprintf
154 *
155 * determines the length of the string pointed to by `os', appending formatted
156 * string to a maximium length of `len'.
157 *
158 */
159
160void
161scnprintf (char *os, size_t len, const char *str, ...)
162{
163 va_list vl;
164 char *ostmp = os + strlen (os);
165
166 va_start (vl, str);
167 vsnprintf (ostmp, len - strlen (os) - 1, str, vl);
168 va_end (vl);
169
170 return;
171}
172
173unsigned long int
174tdiff (struct timeval *old, struct timeval *new)
175{
176 unsigned long int time1;
177
178 if (new->tv_sec >= old->tv_sec) {
179 time1 = new->tv_sec - old->tv_sec;
180 if ((new->tv_usec - 500000) >= old->tv_usec)
181 time1++;
182 } else {
183 time1 = old->tv_sec - new->tv_sec;
184 if ((old->tv_usec - 500000) >= new->tv_usec)
185 time1++;
186 }
187
188 return (time1);
189}
190
191
192/* ipv4_print
193 *
194 * padding = 0 -> don't padd
195 * padding = 1 -> padd with zeros
196 * padding = 2 -> padd with spaces
197 */
198
199char *
200ipv4_print (char *dest, struct in_addr in, int padding)
201{
202 unsigned char *ipp;
203
204 ipp = (unsigned char *) &in.s_addr;
205
206 strcpy (dest, "");
207
208 switch (padding) {
209 case (0):
210 sprintf (dest, "%d.%d.%d.%d", ipp[0], ipp[1], ipp[2], ipp[3]);
211 break;
212 case (1):
213 sprintf (dest, "%03d.%03d.%03d.%03d", ipp[0], ipp[1], ipp[2], ipp[3]);
214 break;
215 case (2):
216 sprintf (dest, "%3d.%3d.%3d.%3d", ipp[0], ipp[1], ipp[2], ipp[3]);
217 break;
218 default:
219 break;
220 }
221
222 return (dest);
223}
224
225
226void *
227xrealloc (void *m_ptr, size_t newsize)
228{
229 void *n_ptr;
230
231 n_ptr = realloc (m_ptr, newsize);
232 if (n_ptr == NULL) {
233 fprintf (stderr, "realloc failed\n");
234 exit (EXIT_FAILURE);
235 }
236
237 return (n_ptr);
238}
239
240
241char *
242xstrdup (char *str)
243{
244 char *b;
245
246 b = strdup (str);
247 if (b == NULL) {
248 fprintf (stderr, "strdup failed\n");
249 exit (EXIT_FAILURE);
250 }
251
252 return (b);
253}
254
255
256void *
257xcalloc (int factor, size_t size)
258{
259 void *bla;
260
261 bla = calloc (factor, size);
262
263 if (bla == NULL) {
264 fprintf (stderr, "no memory left\n");
265 exit (EXIT_FAILURE);
266 }
267
268 return (bla);
269}
270
271/* source by dk
272 */
273
274char *
275allocncat (char **to, char *from, size_t len)
276{
277 int rlen = strlen (from);
278 int null = *to == NULL;
279
280 len = rlen < len ? rlen : len;
281 *to = realloc (*to, (null ? 0 : strlen (*to)) + len + 1);
282 if (null)
283 **to = '\0';
284
285 if (*to == NULL)
286 perror ("no memory: ");
287
288 return (strncat (*to, from, len));
289}
290
291char *
292alloccat (char **to, char *from)
293{
294 return (allocncat (to, from, strlen (from)));
295}
296