-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTFTP_Packet.cpp
140 lines (116 loc) · 3.22 KB
/
TFTP_Packet.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* File: TFTP_Packet.cpp
* Author: gonzalo
*
* Created on May 23, 2016, 10:56 AM
*/
#include <iostream>
#include <string>
#include <strings.h>
#include <apr-1.0/apr_general.h>
#include <apr-1.0/apr_network_io.h>
#include "TFTP_Packet.h"
#include "Hanabi_Ack_Packet.h"
//using namespace std;
TFTP_Packet::TFTP_Packet(long unsigned int size, const char * data) : size_pck(size) {
packet_data = new char [size];
if(data != NULL && size)
memcpy(packet_data, data, size);
}
TFTP_Packet::~TFTP_Packet() {
delete[] packet_data;
}
TFTP_Packet::TFTP_Packet(const TFTP_Packet& other)
{
size_pck = other.size_pck;
packet_data = new char[size_pck];
if(packet_data != NULL)
{
memcpy(packet_data,other.packet_data,size_pck);
tftp_packet_type = other.tftp_packet_type;
}
}
TFTP_Packet::TFTP_Packet()
{
size_pck = 0;
packet_data=NULL;//NULL pero en c++ se usa 0 pq son ezzzpeciales
}
/*
* Envia un paquete usando apr al socket dado, devuelve apr_status_t
*
* Input:
* apr_socket_t *sock: puntero al socket de red al que se quiera mandar el paquete
*
* Return Value:
* apr_status_t de poder enviar correcta devuele APR_SUCCESS de lo contrario algun tipo de error de APR
*
*
*/
apr_status_t TFTP_Packet::send_packet(apr_socket_t *sock)
{
apr_status_t rv;
unsigned long int len_send=size_pck;
std::cout << "sending packet length "<<size_pck<<std::endl;
rv = apr_socket_send(sock, packet_data, &len_send); //simplemente mandamos al socket "sock" el string "req_hdr" del largo len. Es an�logo para cualquier bloque de bytes que queramos mandar.
if (rv != APR_SUCCESS)
std::cout << "CANNOT send info\n" ;
else if(len_send!=size_pck) // nose si podria pasar
std::cout << "Incomplete packet sent(like your birth) :D\n";
else
std::cout <<"Packet has been sent"<<std::endl;
return rv;
}
/*
* Envia un paquete usando apr al socket dado, devuelve apr_status_t
*
* Input:
* apr_socket_t *sock: puntero al socket de donde se quiera levantar el paquete
*
* Return Value:
* bool: true(1) si recibio un paquete. False(0) sino recibio o se produjo un error(ej; allocacion de memoria)
*
*
*/
/*
bool TFTP_Packet::recieve_pck(apr_socket_t *sock)
{
bool packet_picked_up=false;
char buf[MAX_PACKET_SIZE+1];
long unsigned int len_to_pickup=MAX_PACKET_SIZE+1;
apr_status_t rv = apr_socket_recv(sock, buf, &len_to_pickup);//levanto el buffer de red.
if(rv==APR_SUCCESS && len_to_pickup > 0 && len_to_pickup<=MAX_PACKET_SIZE )
{
if(packet_data!=NULL)//evitamos memoryleaks como unos champions
delete[] packet_data;
size_pck=len_to_pickup;
packet_data= new char[size_pck];
if(packet_data!=NULL)
{
memcpy(packet_data,buf,size_pck);
packet_picked_up=true;
std::cout<<"Packet recieved"<<std::endl;
}
}
//else
// std::cout<<"No packet recieved"<<std::endl;e
return packet_picked_up;
}*/
#include <stdio.h>
void TFTP_Packet::print_packet(void)
{
if(size_pck)
{
std::cout<< "0x";
for(int i = 0 ; i < size_pck ; i++)
printf("%X ",(unsigned char)packet_data[i]);
printf("\n");
}
}
const char * TFTP_Packet::get_data_pck(void)
{
return (const char *) this->packet_data; //Read Only. LOOKY BUT NO TOUCHY, cunt.
}
long unsigned int TFTP_Packet::size(void)
{
return this->size_pck;
}