I'm looking for an example of an TFTP client running on Linux platform. I'm trying do this problem using boost::asio here's the code:
void packi16 (char *buf, unsigned short int i){ //change the host order to network byte order (16bit)
i = htons(i);
memcpy(buf,&i,2);
}
int main()
{
boost::asio::io_service service;
boost::asio::ip::udp::socket sock(service);
sock.open(boost::asio::ip::udp::v4());
boost::asio::ip::udp::endpoint receiver_ep(boost::asio::ip::address::from_string("127.0.0.1"), 69);
char filename[] = "first";
char mode[] = "netascii";
int filename_size = (sizeof(filename)/sizeof(*filename));
int mode_size = (sizeof(mode)/sizeof(*mode));
char *packet;
packet = (char *)malloc((17)*sizeof(char));
packi16(packet,1); // 1 opcode for read request
memcpy(packet+2,filename,filename_size-1);
memset(packet+1+filename_size, '\0', 1);
memcpy(packet+2+filename_size, mode, mode_size-1);
memset(packet+1+filename_size+mode_size, '\0', 1);
sock.send_to(boost::asio::buffer(packet, 17), receiver_ep);
char buff[512];
boost::asio::ip::udp::endpoint sender_ep;
sock.receive_from(boost::asio::buffer(buff), sender_ep);
std::cout << buff;
return 0;
}
The problem is in program no error but code don't print anything although in the TFTP server i have this file : first. I use sniffer (wireshark) for see what's going on in my request packet, i capture packet made in tftp client in ubuntu then capture my program capture and it seems my program build correct packet.
Thanks m.s., Prabhu for your comments.