3 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include <net/socket.h>
25 unsigned tftp_client_connect (char *address
, unsigned short port
, char *file
, unsigned *l
)
30 sockaddr_in serverSock
;
32 // Check info about remote computer
33 if ((host
= gethostbyname (address
)) == NULL
) {
34 printf ("tftp -> wrong address\n");
39 if ((sock
= socket (AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
)) == -1) {
40 printf ("tftp -> cant create socket\n");
44 // Zaplníme strukturu sockaddr_in
45 // 1) Rodina protokolù
46 serverSock
.sin_family
= AF_INET
;
47 // 2) èíslo portu, ke kterému se pøipojíme
48 serverSock
.sin_port
= swap16 (port
);
49 // 3) Nastavení IP adresy, ke které se pøipojíme
50 memcpy (&(serverSock
.sin_addr
), host
->h_addr
, host
->h_length
);
52 if (connect (sock
, (sockaddr
*) &serverSock
, sizeof (serverSock
)) == -1) {
53 printf ("tftp -> cant connect to server\n");
59 char *buf
= (char *) kmalloc (sizeof (char) * (len
+ 64));
64 /* Read request for tftp server */
65 unsigned short opcode
= swap16 (1);
67 unsigned char tsize
= '0';
69 memcpy (buf
, (char *) &opcode
, 2);
70 memcpy (buf
+2, file
, len
+1);
71 memcpy (buf
+3+len
, "octet", 6);
72 memcpy (buf
+9+len
, "blksize", 8);
73 memcpy (buf
+17+len
, "1024", 5);
74 memcpy (buf
+22+len
, "tsize", 6);
75 memcpy (buf
+28+len
, (char *) &tsize
, 1);
79 int ret
= send (sock
, buf
, 30+len
, 0);
87 ret
= recv (sock
, buf
, 30+len
, 0);
92 unsigned short opcode2
= 0;
94 memcpy (&opcode2
, buf
, 2);
96 if (swap16 (opcode2
) != 6) {
97 printf ("tftp -> server send error message: %s\n", buf
+4);
103 unsigned blksize2
= atoi (buf
+10);
105 if (blksize2
!= 1024) {
123 unsigned tftp_client_read (int sock
, char *buf
, unsigned len
)
127 unsigned short block
= 0;
130 /* acknowledgement opcode */
131 unsigned short opcode
= swap16 (4);
135 memcpy (b
, (char *) &opcode
, 2);
136 memcpy (b
+2, (char *) &i
, 2);
138 if ((send (sock
, b
, 4, 0)) == -1) {
139 printf ("tftp -> something go wrong with sending acknowledgement packet");
146 for (l
= 0; l
< len
; l
+= 1024) {
147 ret
= recv (sock
, b
, 1100, 0);
150 memcpy (buf
+l
, b
+4, 1024);
154 memcpy (b
, (char *) &opcode
, 2);
155 memcpy (b
+2, (char *) &i
, 2);
157 if ((send (sock
, b
, 4, 0)) == -1) {
158 printf ("tftp -> something go wrong with sending acknowledgement packet");