1 /* tftp_setup_ctrl.c: setup ctrl structure */
2 
3 /*
4  * Copyright (C) 1999 Uwe Ohse
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20 
21 #include "config.h"
22 #include <stdlib.h>
23 #include <netinet/in.h>
24 #include <string.h>
25 #include "tftplib.h"
26 
27 struct tftplib_ctrl *
tftp_setup_ctrl(struct tftplib_ctrl * old,size_t segsize)28 tftp_setup_ctrl(struct tftplib_ctrl *old, size_t segsize)
29 {
30 	if (!old) {
31 		old=(struct tftplib_ctrl *)malloc(sizeof(struct tftplib_ctrl));
32 		if (!old) return 0;
33 		memset(old,0,sizeof(*old));
34 		old->segsize=512; /* default */
35 		old->netascii=0;
36 		old->timeout=5;
37 		old->retries=5;
38 		old->first_packet_length=0; /* tftp_open() adjusts this */
39 		old->filefd=-1;
40 		old->sendbuf.buf=(char *) malloc(old->segsize+TFTP_OFFSET);
41 		old->recvbuf.buf=(char *) malloc(old->segsize+TFTP_OFFSET);
42 		if (!old->sendbuf.buf || !old->recvbuf.buf) {
43 			free(old->sendbuf.buf);
44 			free(old->recvbuf.buf);
45 			free(old);
46 			return 0;
47 		}
48 	}
49 	if (segsize!=0 && old->segsize!=segsize) {
50 		void *v,*u;
51 		u=realloc(old->sendbuf.buf,segsize+TFTP_OFFSET);
52 		v=realloc(old->recvbuf.buf,segsize+TFTP_OFFSET);
53 		if (!u || !v) {
54 			if (u) free(u); else free(old->sendbuf.buf);
55 			if (v) free(v); else free(old->recvbuf.buf);
56 			free(old);
57 			return 0;
58 		}
59 		old->sendbuf.buf=(char *) u;
60 		old->recvbuf.buf=(char *) v;
61 		old->segsize=segsize;
62 	}
63 	return old;
64 }
65 
66