1 /* tftp_prepare.c: common "first packet" thing for client routines */
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 <sys/types.h>
23 #include <sys/ioctl.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 
27 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <netdb.h>
30 #include "no_tftp.h"
31 #ifdef HAVE_ARPA_INET_H
32 #include <arpa/inet.h>
33 #endif
34 
35 #include <syslog.h>
36 #include <errno.h>
37 #include <unistd.h>
38 #include <string.h>
39 #include <signal.h>
40 #include "timselsysdep.h"
41 #include "str_ulong.h"
42 #include "str2num.h"
43 #include "tftplib.h"
44 
45 /*
46  *
47  */
48 size_t
tftp_prepare_rq(int type,const char * server,unsigned short port,const char * remotefilename,struct sockaddr_in * s_in,int * may_get_oack,struct tftplib_ctrl * flags)49 tftp_prepare_rq(int type, const char *server, unsigned short port, const char *remotefilename, struct sockaddr_in *s_in,
50 	int *may_get_oack, struct tftplib_ctrl *flags)
51 {
52     struct hostent *hp;
53     size_t l;
54 	size_t sendlength;
55 	struct in_addr ina;
56 
57 	if (0==inet_aton(server,&ina)) {
58 	    hp = gethostbyname (server);
59 	    if (hp == NULL) { int e=errno;syslog(LOG_ERR,"can't resolve hostname");errno=e; return 0; }
60 	    memcpy (&s_in->sin_addr, hp->h_addr, hp->h_length);
61 	} else {
62 	    memcpy (&s_in->sin_addr, &ina, sizeof(ina));
63 	}
64     s_in->sin_port = port;
65 
66     /* so far, so good ... now do that protocol startup */
67 
68     /*
69      *  2 bytes     string    1 byte     string   1 byte
70      *  ------------------------------------------------
71      *  | Opcode |  Filename  |   0  |    Mode    |   0  |
72      *  ------------------------------------------------
73     */
74     flags->sendbuf.hdr->th_opcode=htons(type);
75 	sendlength=2;
76 
77     l=strlen(remotefilename)+1; /* include \0 */
78 	if (sendlength+l>512) goto toolong;
79     memcpy(flags->sendbuf.buf+2,remotefilename,l);
80     sendlength+=l;
81     if (flags->netascii)  {
82 		if (sendlength+9>512) goto toolong;
83         memcpy(flags->sendbuf.buf+sendlength,"netascii",9); /* \0  */
84         sendlength+=9;
85     } else {
86 		if (sendlength+6>512) goto toolong;
87         memcpy(flags->sendbuf.buf+sendlength,"octet",6); /* \0  */
88         sendlength+=6;
89     }
90     if (flags->segsize!=512) {
91         char buf[STR_ULONG];
92 		if (sendlength+8>512) goto toolong;
93         memcpy(flags->sendbuf.buf+sendlength,"blksize",8); /* \0  */
94         sendlength+=8;
95 
96         l=str_ulong(buf,flags->segsize)+1; /* \0 */
97 		if (sendlength+l>512) goto toolong;
98         memcpy(flags->sendbuf.buf+sendlength,buf,l);
99         sendlength+=l;
100 		*may_get_oack=1;
101     }
102     if (flags->timeout!=5) {
103         char buf[STR_ULONG];
104 		if (sendlength+8>512) goto toolong;
105         memcpy(flags->sendbuf.buf+sendlength,"timeout",8); /* \0  */
106         sendlength+=8;
107         l=str_ulong(buf,flags->timeout)+1;
108 		if (sendlength+l>512) goto toolong;
109         memcpy(flags->sendbuf.buf+sendlength,buf,l);
110         sendlength+=l;
111 		*may_get_oack=1;
112     }
113     if (flags->revision) {
114 		if (sendlength+9>512) goto toolong;
115         memcpy(flags->sendbuf.buf+sendlength,"revision",9); /* \0  */
116         sendlength+=9;
117         l=strlen(flags->revision)+1;
118 		if (sendlength+l>512) goto toolong;
119         memcpy(flags->sendbuf.buf+sendlength,flags->revision,l);
120         sendlength+=l;
121 		*may_get_oack=1;
122     }
123 	if (flags->send_garbage) {
124 		if (sendlength+2>512) goto toolong;
125 		memcpy(flags->sendbuf.buf+sendlength,"AZ",2);
126 		sendlength+=2;
127 	}
128 	return sendlength;
129   toolong:
130     { int e=errno;syslog(LOG_ERR,"?RQ packet would be too long");errno=e; return 0; }
131     return 0;
132 }
133