~ubuntu-branches/ubuntu/trusty/slof/trusty-proposed

« back to all changes in this revision

Viewing changes to clients/net-snk/app/netlib/tftp.c

  • Committer: Package Import Robot
  • Author(s): Adam Conrad
  • Date: 2015-01-23 12:18:53 UTC
  • mfrom: (6.1.1 utopic-proposed)
  • Revision ID: package-import@ubuntu.com-20150123121853-or6a6rkrzor2peku
Tags: 20140630+dfsg-1ubuntu1~14.04
Backport wholesale to trusty to fix network boot issues (LP: #1374568)

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 
20
20
#include <ethernet.h>
21
21
#include <ipv4.h>
22
 
//#include <ipv6.h>
 
22
#include <ipv6.h>
23
23
#include <udp.h>
24
24
 
25
25
//#define __DEBUG__
26
26
 
27
27
#define MAX_BLOCKSIZE 1428
28
 
#define BUFFER_LEN 2048
29
 
#define ACK_BUFFER_LEN 256
30
 
#define READ_BUFFER_LEN 256
 
28
#define BUFFER_LEN 256
31
29
 
32
30
#define ENOTFOUND 1
33
31
#define EACCESS   2
46
44
#define OACK  6
47
45
 
48
46
/* Local variables */
 
47
static unsigned char packet[BUFFER_LEN];
49
48
static unsigned char  *buffer = NULL;
50
49
static unsigned short block = 0;
51
50
static unsigned short blocksize;
92
91
send_rrq(void)
93
92
{
94
93
        int ip_len = 0;
95
 
        //int ip6_payload_len    = 0;
 
94
        int ip6_payload_len    = 0;
96
95
        unsigned short udp_len = 0;
97
96
        unsigned char mode[] = "octet";
98
 
        unsigned char packet[READ_BUFFER_LEN];
99
97
        char *ptr            = NULL;
100
98
        struct iphdr *ip     = NULL;
101
 
        //struct ip6hdr *ip6   = NULL;
 
99
        struct ip6hdr *ip6   = NULL;
102
100
        struct udphdr *udph  = NULL;
103
101
        struct tftphdr *tftp = NULL;
104
102
 
105
 
        memset(packet, 0, READ_BUFFER_LEN);
 
103
        memset(packet, 0, BUFFER_LEN);
106
104
 
107
105
        if (4 == ip_version) {
108
106
                ip = (struct iphdr *) packet;
113
111
                fill_iphdr ((uint8_t *) ip, ip_len, IPTYPE_UDP, 0,
114
112
                            fn_ip->server_ip);
115
113
        }
116
 
/*
117
114
        else if (6 == ip_version) {
118
115
                ip6 = (struct ip6hdr *) packet;
119
116
                udph = (struct udphdr *) (ip6 + 1);
125
122
                             &(fn_ip->server_ip6)); 
126
123
 
127
124
        }
128
 
*/
129
125
        udp_len = htons(sizeof(struct udphdr)
130
126
                              + strlen((char *) fn_ip->filename) + strlen((char *) mode) + 4
131
127
                              + strlen("blksize") + strlen(blocksize_str) + 2);
164
160
send_ack(int blckno, unsigned short dport)
165
161
{
166
162
        int ip_len             = 0;
167
 
        //int ip6_payload_len    = 0;
 
163
        int ip6_payload_len    = 0;
168
164
        unsigned short udp_len = 0;
169
 
        unsigned char packet[ACK_BUFFER_LEN];
170
165
        struct iphdr *ip     = NULL;
171
 
        //struct ip6hdr *ip6   = NULL;
 
166
        struct ip6hdr *ip6   = NULL;
172
167
        struct udphdr *udph  = NULL;
173
168
        struct tftphdr *tftp = NULL;
174
169
 
175
 
        memset(packet, 0, ACK_BUFFER_LEN);
 
170
        memset(packet, 0, BUFFER_LEN);
176
171
 
177
172
        if (4 == ip_version) {
178
173
                ip = (struct iphdr *) packet;
181
176
                fill_iphdr ((uint8_t *) ip, ip_len, IPTYPE_UDP, 0,
182
177
                            fn_ip->server_ip);
183
178
        }
184
 
/*
185
179
        else if (6 == ip_version) {
186
180
                ip6 = (struct ip6hdr *) packet;
187
181
                udph = (struct udphdr *) (ip6 + 1);
191
185
                fill_ip6hdr ((uint8_t *) ip6, ip6_payload_len, IPTYPE_UDP, get_ipv6_address(),
192
186
                             &(fn_ip->server_ip6));
193
187
        }
194
 
*/
195
188
        udp_len = htons(sizeof(struct udphdr) + 4);
196
189
        fill_udphdr ((uint8_t *) udph, udp_len, htons(2001), htons(dport));
197
190
 
218
211
send_error(int error_code, unsigned short dport)
219
212
{
220
213
        int ip_len             = 0;
221
 
        //int ip6_payload_len    = 0;
 
214
        int ip6_payload_len    = 0;
222
215
        unsigned short udp_len = 0;
223
 
        unsigned char packet[256];
224
 
        //struct ip6hdr *ip6   = NULL;
 
216
        struct ip6hdr *ip6   = NULL;
225
217
        struct iphdr *ip     = NULL;
226
218
        struct udphdr *udph  = NULL;
227
219
        struct tftphdr *tftp = NULL;
228
220
 
229
 
        memset(packet, 0, 256);
 
221
        memset(packet, 0, BUFFER_LEN);
230
222
 
231
223
        if (4 == ip_version) {
232
224
                ip = (struct iphdr *) packet;
235
227
                fill_iphdr ((uint8_t *) ip, ip_len, IPTYPE_UDP, 0,
236
228
                            fn_ip->server_ip);
237
229
        }
238
 
/*
239
230
        else if (6 == ip_version) {
240
231
                ip6 = (struct ip6hdr *) packet;
241
232
                udph = (struct udphdr *) (ip6 + 1);
245
236
                fill_ip6hdr ((uint8_t *) ip6, ip6_payload_len, IPTYPE_UDP, get_ipv6_address(),
246
237
                            &(fn_ip->server_ip6));
247
238
        }
248
 
*/
249
239
        udp_len = htons(sizeof(struct udphdr) + 5);
250
240
        fill_udphdr ((uint8_t *) udph, udp_len, htons(2001), htons(dport));
251
241
 
346
336
 *               ERRORCODE if error occurred 
347
337
 */
348
338
int32_t
349
 
handle_tftp(uint8_t *packet, int32_t packetsize) 
 
339
handle_tftp(uint8_t *pkt, int32_t packetsize) 
350
340
{
351
341
        struct udphdr *udph;
352
342
        struct tftphdr *tftp;
358
348
#ifndef __DEBUG__
359
349
        print_progress(0, received_len);
360
350
#endif
361
 
        udph = (struct udphdr *) packet;
 
351
        udph = (struct udphdr *) pkt;
362
352
        tftp = (struct tftphdr *) ((void *) udph + sizeof(struct udphdr));
363
353
        set_timer(TICKS_SEC);
364
354
 
365
355
#ifdef __DEBUG__
366
 
        dump_package(packet, packetsize);
 
356
        dump_package(pkt, packetsize);
367
357
#endif
368
358
 
369
359
        port_number = udph->uh_sport;
370
360
        if (tftp->th_opcode == htons(OACK)) {
371
361
                /* an OACK means that the server answers our blocksize request */
372
 
                blocksize = get_blksize(packet, packetsize);
 
362
                blocksize = get_blksize(pkt, packetsize);
373
363
                if (!blocksize || blocksize > MAX_BLOCKSIZE) {
374
364
                        send_error(8, port_number);
375
365
                        tftp_errno = -8;