tftp-options.c (207614) | tftp-options.c (213099) |
---|---|
1/* 2 * Copyright (C) 2008 Edwin Groothuis. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. --- 10 unchanged lines hidden (view full) --- 19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 */ 25 26#include <sys/cdefs.h> | 1/* 2 * Copyright (C) 2008 Edwin Groothuis. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. --- 10 unchanged lines hidden (view full) --- 19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 */ 25 26#include <sys/cdefs.h> |
27__FBSDID("$FreeBSD: head/libexec/tftpd/tftp-options.c 207614 2010-05-04 13:07:40Z imp $"); | 27__FBSDID("$FreeBSD: head/libexec/tftpd/tftp-options.c 213099 2010-09-24 10:40:17Z marius $"); |
28 29#include <sys/socket.h> 30#include <sys/types.h> 31#include <sys/sysctl.h> 32#include <sys/stat.h> 33 34#include <netinet/in.h> 35#include <arpa/tftp.h> --- 38 unchanged lines hidden (view full) --- 74 * - Logging is done as errors. After all, the server shouldn't 75 * return rubbish. 76 * - The handler returns if there is a serious problem with the 77 * values submitted in the option. 78 * - Sending the EBADOP packets is done by the handler. 79 */ 80 81int | 28 29#include <sys/socket.h> 30#include <sys/types.h> 31#include <sys/sysctl.h> 32#include <sys/stat.h> 33 34#include <netinet/in.h> 35#include <arpa/tftp.h> --- 38 unchanged lines hidden (view full) --- 74 * - Logging is done as errors. After all, the server shouldn't 75 * return rubbish. 76 * - The handler returns if there is a serious problem with the 77 * values submitted in the option. 78 * - Sending the EBADOP packets is done by the handler. 79 */ 80 81int |
82option_tsize(int peer, struct tftphdr *tp, int mode, struct stat *stbuf) | 82option_tsize(int peer __unused, struct tftphdr *tp __unused, int mode, 83 struct stat *stbuf) |
83{ 84 85 if (options[OPT_TSIZE].o_request == NULL) 86 return (0); 87 88 if (mode == RRQ) 89 asprintf(&options[OPT_TSIZE].o_reply, 90 "%ju", stbuf->st_size); --- 63 unchanged lines hidden (view full) --- 154 options[OPT_ROLLOVER].o_reply); 155 156 return (0); 157} 158 159int 160option_blksize(int peer) 161{ | 84{ 85 86 if (options[OPT_TSIZE].o_request == NULL) 87 return (0); 88 89 if (mode == RRQ) 90 asprintf(&options[OPT_TSIZE].o_reply, 91 "%ju", stbuf->st_size); --- 63 unchanged lines hidden (view full) --- 155 options[OPT_ROLLOVER].o_reply); 156 157 return (0); 158} 159 160int 161option_blksize(int peer) 162{ |
162 int *maxdgram; 163 char maxbuffer[100]; | 163 u_long maxdgram; |
164 size_t len; 165 166 if (options[OPT_BLKSIZE].o_request == NULL) 167 return (0); 168 169 /* maximum size of an UDP packet according to the system */ | 164 size_t len; 165 166 if (options[OPT_BLKSIZE].o_request == NULL) 167 return (0); 168 169 /* maximum size of an UDP packet according to the system */ |
170 len = sizeof(maxbuffer); | 170 len = sizeof(maxdgram); |
171 if (sysctlbyname("net.inet.udp.maxdgram", | 171 if (sysctlbyname("net.inet.udp.maxdgram", |
172 maxbuffer, &len, NULL, 0) < 0) { | 172 &maxdgram, &len, NULL, 0) < 0) { |
173 tftp_log(LOG_ERR, "sysctl: net.inet.udp.maxdgram"); 174 return (acting_as_client ? 1 : 0); 175 } | 173 tftp_log(LOG_ERR, "sysctl: net.inet.udp.maxdgram"); 174 return (acting_as_client ? 1 : 0); 175 } |
176 maxdgram = (int *)maxbuffer; | |
177 178 int size = atoi(options[OPT_BLKSIZE].o_request); 179 if (size < BLKSIZE_MIN || size > BLKSIZE_MAX) { 180 if (acting_as_client) { 181 tftp_log(LOG_ERR, 182 "Invalid blocksize (%d bytes), aborting", 183 size); 184 send_error(peer, EBADOP); 185 return (1); 186 } else { 187 tftp_log(LOG_WARNING, 188 "Invalid blocksize (%d bytes), ignoring request", 189 size); 190 return (0); 191 } 192 } 193 | 176 177 int size = atoi(options[OPT_BLKSIZE].o_request); 178 if (size < BLKSIZE_MIN || size > BLKSIZE_MAX) { 179 if (acting_as_client) { 180 tftp_log(LOG_ERR, 181 "Invalid blocksize (%d bytes), aborting", 182 size); 183 send_error(peer, EBADOP); 184 return (1); 185 } else { 186 tftp_log(LOG_WARNING, 187 "Invalid blocksize (%d bytes), ignoring request", 188 size); 189 return (0); 190 } 191 } 192 |
194 if (size > *maxdgram) { | 193 if (size > (int)maxdgram) { |
195 if (acting_as_client) { 196 tftp_log(LOG_ERR, 197 "Invalid blocksize (%d bytes), " 198 "net.inet.udp.maxdgram sysctl limits it to " | 194 if (acting_as_client) { 195 tftp_log(LOG_ERR, 196 "Invalid blocksize (%d bytes), " 197 "net.inet.udp.maxdgram sysctl limits it to " |
199 "%d bytes.\n", size, *maxdgram); | 198 "%d bytes.\n", size, maxdgram); |
200 send_error(peer, EBADOP); 201 return (1); 202 } else { 203 tftp_log(LOG_WARNING, 204 "Invalid blocksize (%d bytes), " 205 "net.inet.udp.maxdgram sysctl limits it to " | 199 send_error(peer, EBADOP); 200 return (1); 201 } else { 202 tftp_log(LOG_WARNING, 203 "Invalid blocksize (%d bytes), " 204 "net.inet.udp.maxdgram sysctl limits it to " |
206 "%d bytes.\n", size, *maxdgram); 207 size = *maxdgram; | 205 "%d bytes.\n", size, maxdgram); 206 size = maxdgram; |
208 /* No reason to return */ 209 } 210 } 211 212 asprintf(&options[OPT_BLKSIZE].o_reply, "%d", size); 213 segsize = size; 214 pktsize = size + 4; 215 if (debug&DEBUG_OPTIONS) 216 tftp_log(LOG_DEBUG, "Setting blksize to '%s'", 217 options[OPT_BLKSIZE].o_reply); 218 219 return (0); 220} 221 222int | 207 /* No reason to return */ 208 } 209 } 210 211 asprintf(&options[OPT_BLKSIZE].o_reply, "%d", size); 212 segsize = size; 213 pktsize = size + 4; 214 if (debug&DEBUG_OPTIONS) 215 tftp_log(LOG_DEBUG, "Setting blksize to '%s'", 216 options[OPT_BLKSIZE].o_reply); 217 218 return (0); 219} 220 221int |
223option_blksize2(int peer) | 222option_blksize2(int peer __unused) |
224{ | 223{ |
225 int *maxdgram; 226 char maxbuffer[100]; | 224 u_long maxdgram; |
227 int size, i; 228 size_t len; 229 230 int sizes[] = { 231 8, 16, 32, 64, 128, 256, 512, 1024, 232 2048, 4096, 8192, 16384, 32768, 0 233 }; 234 235 if (options[OPT_BLKSIZE2].o_request == NULL) 236 return (0); 237 238 /* maximum size of an UDP packet according to the system */ | 225 int size, i; 226 size_t len; 227 228 int sizes[] = { 229 8, 16, 32, 64, 128, 256, 512, 1024, 230 2048, 4096, 8192, 16384, 32768, 0 231 }; 232 233 if (options[OPT_BLKSIZE2].o_request == NULL) 234 return (0); 235 236 /* maximum size of an UDP packet according to the system */ |
239 len = sizeof(maxbuffer); | 237 len = sizeof(maxdgram); |
240 if (sysctlbyname("net.inet.udp.maxdgram", | 238 if (sysctlbyname("net.inet.udp.maxdgram", |
241 maxbuffer, &len, NULL, 0) < 0) { | 239 &maxdgram, &len, NULL, 0) < 0) { |
242 tftp_log(LOG_ERR, "sysctl: net.inet.udp.maxdgram"); 243 return (acting_as_client ? 1 : 0); 244 } | 240 tftp_log(LOG_ERR, "sysctl: net.inet.udp.maxdgram"); 241 return (acting_as_client ? 1 : 0); 242 } |
245 maxdgram = (int *)maxbuffer; | |
246 247 size = atoi(options[OPT_BLKSIZE2].o_request); 248 for (i = 0; sizes[i] != 0; i++) { 249 if (size == sizes[i]) break; 250 } 251 if (sizes[i] == 0) { 252 tftp_log(LOG_INFO, 253 "Invalid blocksize2 (%d bytes), ignoring request", size); 254 return (acting_as_client ? 1 : 0); 255 } 256 | 243 244 size = atoi(options[OPT_BLKSIZE2].o_request); 245 for (i = 0; sizes[i] != 0; i++) { 246 if (size == sizes[i]) break; 247 } 248 if (sizes[i] == 0) { 249 tftp_log(LOG_INFO, 250 "Invalid blocksize2 (%d bytes), ignoring request", size); 251 return (acting_as_client ? 1 : 0); 252 } 253 |
257 if (size > *maxdgram) { | 254 if (size > (int)maxdgram) { |
258 for (i = 0; sizes[i+1] != 0; i++) { | 255 for (i = 0; sizes[i+1] != 0; i++) { |
259 if (*maxdgram < sizes[i+1]) break; | 256 if ((int)maxdgram < sizes[i+1]) break; |
260 } 261 tftp_log(LOG_INFO, 262 "Invalid blocksize2 (%d bytes), net.inet.udp.maxdgram " | 257 } 258 tftp_log(LOG_INFO, 259 "Invalid blocksize2 (%d bytes), net.inet.udp.maxdgram " |
263 "sysctl limits it to %d bytes.\n", size, *maxdgram); | 260 "sysctl limits it to %d bytes.\n", size, maxdgram); |
264 size = sizes[i]; 265 /* No need to return */ 266 } 267 268 asprintf(&options[OPT_BLKSIZE2].o_reply, "%d", size); 269 segsize = size; 270 pktsize = size + 4; 271 if (debug&DEBUG_OPTIONS) 272 tftp_log(LOG_DEBUG, "Setting blksize2 to '%s'", 273 options[OPT_BLKSIZE2].o_reply); 274 275 return (0); 276} 277 278/* 279 * Append the available options to the header 280 */ 281uint16_t | 261 size = sizes[i]; 262 /* No need to return */ 263 } 264 265 asprintf(&options[OPT_BLKSIZE2].o_reply, "%d", size); 266 segsize = size; 267 pktsize = size + 4; 268 if (debug&DEBUG_OPTIONS) 269 tftp_log(LOG_DEBUG, "Setting blksize2 to '%s'", 270 options[OPT_BLKSIZE2].o_reply); 271 272 return (0); 273} 274 275/* 276 * Append the available options to the header 277 */ 278uint16_t |
282make_options(int peer, char *buffer, uint16_t size) { | 279make_options(int peer __unused, char *buffer, uint16_t size) { |
283 int i; 284 char *value; 285 const char *option; 286 uint16_t length; 287 uint16_t returnsize = 0; 288 289 if (!options_rfc_enabled) return (0); 290 --- 100 unchanged lines hidden --- | 280 int i; 281 char *value; 282 const char *option; 283 uint16_t length; 284 uint16_t returnsize = 0; 285 286 if (!options_rfc_enabled) return (0); 287 --- 100 unchanged lines hidden --- |