This repository has been archived by the owner on Feb 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
tftpcommon.cpp
executable file
·93 lines (60 loc) · 2.25 KB
/
tftpcommon.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include "tftpcommon.h"
int TFTPCommon::sendACK(SocketUDP sock, unsigned short block,SockAddr &addr){
unsigned short* data=new unsigned short[2];
data[0]=htons(0x0004);
data[1]=htons(block);
return sock.sendTo((void*)data,sizeof(short)*2,addr);
}
int TFTPCommon::sendData(SocketUDP sock, unsigned short block, void *data, size_t data_size, SockAddr &addr){
if(data_size>512)
data_size=512;
unsigned char* data_s=new unsigned char[4+data_size];
data_s[0]=0x00;
data_s[1]=0x03;
data_s[2]=(unsigned char)((0x00FF)&(block>>8));
data_s[3]=(unsigned char)((0x00FF)&(block));
memcpy(data_s+4,data,data_size);
return sock.sendTo((void*)data_s,sizeof(char)*4+data_size,addr);
}
int TFTPCommon::sendError(SocketUDP sock, int errtype, SockAddr &addr){
unsigned char* data_s=new unsigned char[6];
data_s[0]=0x00;
data_s[1]=0x05;
data_s[2]=(unsigned char)((0x000000FF)&(errtype>>8));
data_s[3]=(unsigned char)((0x000000FF)&(errtype));
data_s[4]='X';//Pendientes las cadenas de error
data_s[5]=0x00;
return sock.sendTo((void*)data_s,sizeof(char)*5,addr);
}
int TFTPCommon::sendRRQ(SocketUDP sock, const char *filename, int mode, SockAddr &addr){
unsigned char* data_s=new unsigned char[15+strlen(filename)];
data_s[0]=0x00;
data_s[1]=0x01;
strcpy((char*)(data_s+2),filename);
if(mode==OCTET){
strcpy((char*)(data_s+3+strlen(filename)),"octet");
}
else if(mode==NETASCII){
strcpy((char*)(data_s+3+strlen(filename)),"netascii");
}
else return -1;
return sock.sendTo((void*)data_s,sizeof(char)*(4+strlen(filename)+(mode==OCTET?5:8)),addr);
}
int TFTPCommon::sendWRQ(SocketUDP sock, const char *filename, int mode, SockAddr &addr){
unsigned char* data_s=new unsigned char[15+strlen(filename)];
data_s[0]=0x00;
data_s[1]=0x02;
strcpy((char*)(data_s+2),filename);
if(mode==OCTET){
strcpy((char*)(data_s+3+strlen(filename)),"octet");
}
else if(mode==NETASCII){
strcpy((char*)(data_s+3+strlen(filename)),"netascii");
}
else return -1;
return sock.sendTo((void*)data_s,sizeof(char)*(4+strlen(filename)+(mode==OCTET?5:8)),addr);
}
TFTPCommon::TFTPCommon(QObject *parent):
QObject(parent)
{
}