-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtftp_def.c
184 lines (164 loc) · 4.38 KB
/
tftp_def.c
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
/* hey emacs! -*- Mode: C; c-file-style: "k&r"; indent-tabs-mode: nil -*- */
/*
* tftp_def.c
*
* $Id: tftp_def.c,v 1.15 2004/02/13 03:16:09 jp Exp $
*
* Copyright (c) 2000 Jean-Pierre Lefebvre <[email protected]>
* and Remi Lefebvre <[email protected]>
*
* atftp is free software; you can redistribute them and/or modify them
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "tftp_def.h"
#include "options.h"
#include "logger.h"
/*
* This is the default option structure, that must be used
* for initialisation.
*/
// FIXME: is there a way to use TIMEOUT and SEGSIZE here?
struct tftp_opt tftp_default_options[OPT_NUMBER] = {
{ "filename", "", 0, 1}, /* file to transfer */
{ "mode", "octet", 0, 1}, /* mode for transfer */
{ "tsize", "0", 0, 1 }, /* RFC1350 options. See RFC2347, */
{ "timeout", "5", 0, 1 }, /* 2348, 2349, 2090. */
{ "blksize", "512", 0, 1 }, /* This is the default option */
{ "multicast", "", 0, 1 }, /* structure */
{ "", "", 0, 0}
};
/* Error message defined in RFC1350. */
char *tftp_errmsg[9] = {
"Undefined error code",
"File not found",
"Access violation",
"Disk full or allocation exceeded",
"Illegal TFTP operation",
"Unknown transfer ID",
"File already exists",
"No such user",
"Failure to negotiate RFC1782 options",
};
/*
* Compute the difference of two timeval structs handling wrap around.
* The result is retruned in *res.
* Return value are:
* 1 if t1 > t0
* 0 if t1 = t0
* -1 if t1 < t0
*/
int timeval_diff(struct timeval *res, struct timeval *t1, struct timeval *t0)
{
res->tv_sec = t1->tv_sec - t0->tv_sec;
res->tv_usec = t1->tv_usec - t0->tv_usec;
if (res->tv_sec > 0)
{
if (res->tv_usec >= 0)
{
return 1;
}
else
{
res->tv_sec -= 1;
res->tv_usec += 1000000;
return 1;
}
}
else if (res->tv_sec < 0)
{
if (res->tv_usec > 0)
{
res->tv_sec += 1;
res->tv_usec -= 1000000;
return -1;
}
else if (res->tv_usec <= 0);
{
return -1;
}
}
else
{
if (res->tv_usec > 0)
return 1;
else if (res->tv_usec < 0)
return -1;
else
return 0;
}
}
/*
* Print a string in engineering notation.
*
* IN:
* value: value to print
* string: if NULL, the function print to stdout, else if print
* to the string.
* format: format string for printf.
*/
int print_eng(double value, char *string, int size, char *format)
{
char suffix[] = {'f', 'p', 'n', 'u', 'm', 0, 'k', 'M', 'G', 'T', 'P'};
double tmp;
double div = 1e-15;
int i;
for (i = 0; i < 11; i++)
{
tmp = value / div;
if ((tmp > 1.0) && (tmp < 1000.0))
break;
div *= 1000.0;
}
if (string)
snprintf(string, size, format, tmp, suffix[i]);
else
printf(format, tmp, suffix[i]);
return OK;
}
/*
* This is a strncpy function that take care of string NULL termination
*/
inline char *Strncpy(char *to, const char *from, size_t size)
{
to[size-1] = '\000';
return strncpy(to, from, size - 1);
}
/*
* gethostbyname replacement that is reentrant. This function is copyied
* from the libc manual.
*/
int Gethostbyname(char *addr, struct hostent *host)
{
struct hostent *hp;
char *tmpbuf;
size_t tmpbuflen;
int res;
int herr;
tmpbuflen = 1024;
if ((tmpbuf = (char *)malloc(tmpbuflen)) == NULL)
return ERR;
res = gethostbyname_r(addr, host, tmpbuf, tmpbuflen, &hp, &herr);
free(tmpbuf);
/* Check for errors. */
if (res != 0)
{
logger(LOG_ERR, "%s: %d: gethostbyname_r: %s",
__FILE__, __LINE__, strerror(herr));
return ERR;
}
if (hp != host)
{
logger(LOG_ERR, "%s: %d: abnormal return value",
__FILE__, __LINE__);
return ERR;
}
return OK;
}