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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
|
/*
* $Id: utils.c,v 1.47 2006/10/12 14:21:23 desrod Exp $
*
* utils.c: misc. stuff for dealing with packets.
*
* Portions Copyright (c) 1996, D. Jeff Dionne.
* Portions Copyright (c) 1996, Kenneth Albanowski
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <string.h>
#include "pi-debug.h"
#include "pi-source.h"
void pi_timeout_to_timespec(int timeout, struct timespec *ts);
void get_pilot_rate(int *establishrate, int *establishhighrate);
int pi_timespec_to_timeout(const struct timespec *ts);
int pi_timeout_expired(const struct timespec *ts);
size_t palm_strftime(char *s, size_t max, const char *fmt,
const struct tm *tm);
/* this routine ruthlessly stolen verbatim from Brian J. Swetland */
/***********************************************************************
*
* Function: crc16
*
* Summary: Implementation of the CRC16 Cyclic Redundancy Check
*
* Parameters: None
*
* Returns: CRC + NULL
*
***********************************************************************/
int crc16(unsigned char *ptr, int count)
{
int crc,
i;
crc = 0;
while (--count >= 0) {
crc = crc ^ (int) *ptr++ << 8;
for (i = 0; i < 8; ++i)
if (crc & 0x8000)
crc = crc << 1 ^ 0x1021;
else
crc = crc << 1;
}
return (crc & 0xFFFF);
}
void get_pilot_rate(int *establishrate, int *establishhighrate)
{
/* Default PADP connection rate */
char *rate_env = getenv("PILOTRATE");
if (rate_env) {
/* Establish high rate */
if (rate_env[0] == 'H') {
*establishrate = atoi(rate_env + 1);
*establishhighrate = 1;
} else {
*establishrate = atoi(rate_env);
*establishhighrate = 0;
}
}
else {
*establishrate = -1;
}
}
#ifndef HAVE_STRDUP
char *strdup(const char *s)
{
char *result;
size_t size = strlen(s) + 1;
if (!(result = malloc(size))) {
return NULL;
}
memcpy(result, s, size);
return result;
}
#endif
/* Borrowed from GNU sh-utils, and then probably from GNU libc */
#ifndef HAVE_PUTENV
#if HAVE_GNU_LD
# define environ __environ
#else
extern char **environ;
#endif
/* Put STRING, which is of the form "NAME=VALUE", in the environment */
int putenv(const char *string)
{
const char *const name_end = strchr(string, '=');
register size_t size;
register char **ep;
if (name_end == NULL) {
/* Remove the variable from the environment. */
size = strlen(string);
for (ep = environ; *ep != NULL; ++ep)
if (!strncmp(*ep, string, size)
&& (*ep)[size] == '=') {
while (ep[1] != NULL) {
ep[0] = ep[1];
++ep;
}
*ep = NULL;
return 0;
}
}
size = 0;
for (ep = environ; *ep != NULL; ++ep)
if (!strncmp(*ep, string, name_end - string)
&& (*ep)[name_end - string] == '=')
break;
else
++size;
if (*ep == NULL) {
static char **last_environ = NULL;
char **new_environ =
(char **) malloc((size + 2) * sizeof(char *));
if (new_environ == NULL)
return -1;
(void) memcpy((void *) new_environ, (void *) environ,
size * sizeof(char *));
new_environ[size] = (char *) string;
new_environ[size + 1] = NULL;
if (last_environ != NULL)
free((void *) last_environ);
last_environ = new_environ;
environ = new_environ;
} else
*ep = (char *) string;
return 0;
}
#endif
#ifdef OS2
/* Replacement version of getenv(), because the one in the EMX 0.9c, fix03
dist appears to be busted when called from inside a DLL. (MJJ) */
char *getenv(const char *envar)
{
APIRET rc;
unsigned char *envstring;
/* just call the OS/2 function directly */
rc = DosScanEnv(envar, &envstring);
if (rc)
return NULL;
else
return envstring;
}
#endif
#ifndef HAVE_INET_ATON
/***********************************************************************
*
* Function: inet_aton
*
* Summary: Manipulate our network address information
*
* Parameters: None
*
* Returns: Nothing
*
***********************************************************************/
int inet_aton(const char *cp, struct in_addr *addr)
{
register u_long val;
register int base;
register int n;
register char c;
u_int parts[4];
register u_int *pp = parts;
for (;;) {
/* Collect number up to ``.''. Values are specified as for
C: 0x=hex, 0=octal, other=decimal. */
val = 0;
base = 10;
if (*cp == '0') {
if (*++cp == 'x' || *cp == 'X')
base = 16, cp++;
else
base = 8;
}
while ((c = *cp) != '\0') {
if (isascii(c) && isdigit(c)) {
val = (val * base) + (c - '0');
cp++;
continue;
}
if (base == 16 && isascii(c) && isxdigit(c)) {
val =
(val << 4) + (c + 10 -
(islower(c) ? 'a' :
'A'));
cp++;
continue;
}
break;
}
if (*cp == '.') {
/* 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 || val > 0xff)
return (0);
*pp++ = val, cp++;
} else
break;
}
/* Check for trailing characters. */
if (*cp && (!isascii(*cp) || !isspace(*cp)))
return (0);
/* Concoct the address according to the number of parts specified. */
n = pp - parts + 1;
switch (n) {
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
char *printlong(unsigned long val)
{
static char buf[5];
set_long(buf, val);
buf[4] = 0;
return buf;
}
unsigned long makelong(char *c)
{
int l = strlen(c);
char c2[4];
if (l >= 4)
return get_long(c);
memset(c2, ' ', 4);
memcpy(c2, c, (size_t)l);
return get_long(c2);
}
double get_float(void *buffer)
{
unsigned char *buf = buffer;
unsigned long frac = get_long(buf);
int expr = get_sshort(buf + 4),
sign = get_byte(buf + 6);
/* if (sign) f = frac; else f = -frac; return ldexp(f, exp); */
return ldexp(sign ? (double) frac : -(double) frac, expr);
}
void set_float(void *buffer, double value)
{
int expr,
sign;
unsigned char *buf = buffer;
unsigned long frac;
double r;
/* Take absolute */
if (value < 0) {
sign = 0;
value = -value;
} else
sign = 0xFF;
/* Convert mantissa to 32-bit integer, and take exponent */
r = ldexp(frexp(value, &expr), 32);
frac = (unsigned long)r;
expr -= 32;
/* Store values in buffer */
set_long(buf, frac);
set_sshort(buf + 4, expr);
set_byte(buf + 6, sign);
set_byte(buf + 7, 0);
}
int compareTm(struct tm *a, struct tm *b)
{
int date;
date = a->tm_year - b->tm_year;
if (date)
return date;
date = a->tm_mon - b->tm_mon;
if (date)
return date;
date = a->tm_mday - b->tm_mday;
if (date)
return date;
date = a->tm_hour - b->tm_hour;
if (date)
return date;
date = a->tm_min - b->tm_min;
if (date)
return date;
date = a->tm_sec - b->tm_sec;
return date;
}
void pi_timeout_to_timespec(int timeout, struct timespec *ts)
{
/* convert a timeout value (in milliseconds) to an absolute timespec */
struct timeval now;
gettimeofday(&now, NULL);
ts->tv_sec = now.tv_sec + (long)(timeout / 1000);
ts->tv_nsec = (now.tv_usec + ((long)timeout % 1000) * 1000) * 1000;
if (ts->tv_nsec >= 1000000000) {
ts->tv_nsec -= 1000000000;
ts->tv_sec++;
}
}
int pi_timespec_to_timeout(const struct timespec *ts)
{
/* convert an absolute timespec to a timeout value (in milliseconds) from now
* returns a negative if the timeout expired already
*/
struct timeval now;
gettimeofday(&now, NULL);
return (int)(((double)ts->tv_sec * 1000.0 + (double)ts->tv_nsec / 1000000.0) -
((double)now.tv_sec * 1000.0 + (double)now.tv_usec / 1000.0));
}
int pi_timeout_expired(const struct timespec *ts)
{
return pi_timespec_to_timeout(ts) <= 0;
}
/* Fix some issues with some locales reporting 2 or 4 digit years */
size_t palm_strftime(char *s, size_t max, const char *fmt,
const struct tm *tm) {
return strftime(s, max, fmt, tm);
}
/* vi: set ts=8 sw=4 sts=4 noexpandtab: cin */
/* ex: set tabstop=4 expandtab: */
/* Local Variables: */
/* indent-tabs-mode: t */
/* c-basic-offset: 8 */
/* End: */
|