#include <sys/cdefs.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <arpa/tftp.h>
#include <assert.h>
#include <err.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include "tftp.h"
#include "tftp-file.h"
#include "tftp-utils.h"
#include "tftp-io.h"
#include "tftp-transfer.h"
#include "tftp-options.h"
int
xmitfile(int peer, char *port, int fd, char *name, char *mode)
{
char recvbuffer[MAXPKTSIZE];
struct sockaddr_storage serv;
struct tftp_stats tftp_stats;
struct tftphdr *rp;
struct servent *se;
int n, i, ret = 0;
uint16_t block, portn;
stats_init(&tftp_stats);
memset(&serv, 0, sizeof(serv));
rp = (struct tftphdr *)recvbuffer;
if ((se = getservbyname(port ? port : "tftp", "udp")) != NULL)
portn = se->s_port;
else
portn = htons(atoi(port));
if (portn == 0) {
printf("Invalid port '%s'.\n", port ? port : "tftp");
return (-1);
}
((struct sockaddr_in *)&peer_sock)->sin_port = portn;
for (i = 0; i < 12; i++) {
struct sockaddr_storage from;
if (debug & DEBUG_SIMPLE)
printf("Sending %s\n", name);
n = send_wrq(peer, name, mode);
if (n > 0) {
printf("Cannot send WRQ packet\n");
return (-1);
}
n = receive_packet(peer, recvbuffer,
MAXPKTSIZE, &from, timeoutpacket);
if (n >= 0) {
((struct sockaddr_in *)&peer_sock)->sin_port =
((struct sockaddr_in *)&from)->sin_port;
break;
}
if (n == RP_TIMEOUT) {
printf("Try %d, didn't receive answer from remote.\n",
i + 1);
continue;
}
break;
}
if (i == 12) {
printf("Transfer timed out.\n");
return (-1);
}
if (rp->th_opcode == ERROR) {
printf("Got ERROR, aborted\n");
return (-1);
}
if (rp->th_opcode == OACK) {
if (!options_rfc_enabled) {
printf("Got OACK while options are not enabled!\n");
send_error(peer, EBADOP);
return (-1);
}
parse_options(peer, rp->th_stuff, n + 2);
}
if (read_init(fd, NULL, mode) < 0) {
warn("read_init()");
return (-1);
}
block = 1;
if (tftp_send(peer, &block, &tftp_stats) != 0)
ret = -1;
read_close();
if (tftp_stats.amount > 0)
printstats("Sent", verbose, &tftp_stats);
return (ret);
}
int
recvfile(int peer, char *port, int fd, char *name, char *mode)
{
char recvbuffer[MAXPKTSIZE];
struct tftp_stats tftp_stats;
struct tftphdr *rp;
struct servent *se;
int n, i, ret = 0;
uint16_t block, portn;
stats_init(&tftp_stats);
rp = (struct tftphdr *)recvbuffer;
if ((se = getservbyname(port ? port : "tftp", "udp")) != NULL)
portn = se->s_port;
else
portn = htons(atoi(port));
if (portn == 0) {
printf("Invalid port '%s'.\n", port ? port : "tftp");
return (-1);
}
((struct sockaddr_in *)&peer_sock)->sin_port = portn;
for (i = 0; i < 12; i++) {
struct sockaddr_storage from;
if (debug & DEBUG_SIMPLE)
printf("Requesting %s\n", name);
n = send_rrq(peer, name, mode);
if (n > 0) {
printf("Cannot send RRQ packet\n");
return (-1);
}
n = receive_packet(peer, recvbuffer,
MAXPKTSIZE, &from, timeoutpacket);
if (n >= 0) {
((struct sockaddr_in *)&peer_sock)->sin_port =
((struct sockaddr_in *)&from)->sin_port;
break;
}
if (n == RP_TIMEOUT) {
printf("Try %d, didn't receive answer from remote.\n",
i + 1);
continue;
}
break;
}
if (i == 12) {
printf("Transfer timed out.\n");
return (-1);
}
if (rp->th_opcode == ERROR) {
tftp_log(LOG_ERR, "Error code %d: %s", rp->th_code, rp->th_msg);
return (-1);
}
if (write_init(fd, NULL, mode) < 0) {
warn("write_init");
return (-1);
}
if (rp->th_opcode == OACK) {
if (!options_rfc_enabled) {
printf("Got OACK while options are not enabled!\n");
send_error(peer, EBADOP);
return (-1);
}
parse_options(peer, rp->th_stuff, n + 2);
n = send_ack(peer, 0);
if (n > 0) {
printf("Cannot send ACK on OACK.\n");
return (-1);
}
block = 0;
if (tftp_receive(peer, &block, &tftp_stats, NULL, 0) != 0)
ret = -1;
} else {
block = 1;
if (tftp_receive(peer, &block, &tftp_stats, rp, n) != 0)
ret = -1;
}
if (tftp_stats.amount > 0)
printstats("Received", verbose, &tftp_stats);
return (ret);
}