grub/grub-core/net/tftp.c

176 lines
4.1 KiB
C
Raw Normal View History

#include <grub/misc.h>
#include <grub/net/tftp.h>
#include <grub/net/udp.h>
#include <grub/net/ip.h>
#include <grub/net/ethernet.h>
#include <grub/net/netbuff.h>
#include <grub/net.h>
#include <grub/mm.h>
#include <grub/dl.h>
#include <grub/file.h>
static grub_err_t
tftp_open (struct grub_file *file, const char *filename)
{
struct tftphdr *tftph;
char *rrq;
2011-04-01 08:42:34 +00:00
int i;
int rrqlen;
2011-04-01 08:42:34 +00:00
int hdrlen;
char open_data[1500];
struct grub_net_buff nb;
tftp_data_t data = grub_malloc (sizeof *data);
grub_err_t err;
2011-04-01 08:42:34 +00:00
file->device->net->socket->data = (void *) data;
nb.head = open_data;
nb.end = open_data + sizeof (open_data);
grub_netbuff_clear (&nb);
2011-04-01 08:42:34 +00:00
grub_netbuff_reserve (&nb,1500);
grub_netbuff_push (&nb,sizeof (*tftph));
2010-09-22 22:45:39 +00:00
2011-04-01 08:42:34 +00:00
tftph = (struct tftphdr *) nb.data;
rrq = (char *) tftph->u.rrq;
rrqlen = 0;
2010-09-22 22:45:39 +00:00
tftph->opcode = grub_cpu_to_be16 (TFTP_RRQ);
grub_strcpy (rrq, filename);
rrqlen += grub_strlen (filename) + 1;
rrq += grub_strlen (filename) + 1;
grub_strcpy (rrq,"octet");
rrqlen += grub_strlen ("octet") + 1;
rrq += grub_strlen ("octet") + 1;
grub_strcpy (rrq,"blksize");
rrqlen += grub_strlen("blksize") + 1;
rrq += grub_strlen ("blksize") + 1;
grub_strcpy (rrq,"1024");
rrqlen += grub_strlen ("1024") + 1;
rrq += grub_strlen ("1024") + 1;
grub_strcpy (rrq,"tsize");
rrqlen += grub_strlen ("tsize") + 1;
rrq += grub_strlen ("tsize") + 1;
grub_strcpy (rrq,"0");
rrqlen += grub_strlen ("0") + 1;
2010-09-22 22:45:39 +00:00
rrq += grub_strlen ("0") + 1;
hdrlen = sizeof (tftph->opcode) + rrqlen;
2011-04-01 08:42:34 +00:00
grub_netbuff_unput (&nb, nb.tail - (nb.data + hdrlen));
file->device->net->socket->out_port = TFTP_SERVER_PORT;
2010-09-22 22:45:39 +00:00
2011-04-01 08:42:34 +00:00
err = grub_net_send_udp_packet (file->device->net->socket, &nb);
2010-09-22 22:45:39 +00:00
if (err)
return err;
2011-04-01 08:42:34 +00:00
/* Receive OACK packet. */
for ( i = 0; i < 3; i++)
2010-09-22 22:45:39 +00:00
{
2011-04-01 08:42:34 +00:00
grub_net_pool_cards (100);
if (grub_errno)
return grub_errno;
if (file->device->net->socket->status != 0)
break;
/* Retry. */
//err = grub_net_send_udp_packet (file->device->net->socket, &nb);
// if (err)
// return err;
2010-09-22 22:45:39 +00:00
}
2011-04-01 08:42:34 +00:00
if (file->device->net->socket->status == 0)
return grub_error (GRUB_ERR_TIMEOUT,"Time out opening tftp.");
file->size = data->file_size;
2010-09-22 22:45:39 +00:00
return GRUB_ERR_NONE;
}
2011-04-01 08:42:34 +00:00
static grub_err_t
tftp_receive (grub_net_socket_t sock, struct grub_net_buff *nb)
{
struct tftphdr *tftph;
2011-04-01 08:42:34 +00:00
char nbdata[128];
tftp_data_t data = sock->data;
grub_err_t err;
2011-04-01 08:42:34 +00:00
char *ptr;
struct grub_net_buff nb_ack;
nb_ack.head = nbdata;
nb_ack.end = nbdata + sizeof (nbdata);
2011-04-01 08:42:34 +00:00
tftph = (struct tftphdr *) nb->data;
2010-09-22 22:45:39 +00:00
switch (grub_be_to_cpu16 (tftph->opcode))
{
2011-04-01 08:42:34 +00:00
case TFTP_OACK:
for (ptr = nb->data; ptr < nb->tail; )
{
if (grub_memcmp (ptr, "tsize\0", sizeof ("tsize\0") - 1) == 0)
data->file_size = grub_strtoul (ptr + sizeof ("tsize\0") - 1, 0, 0);
while (ptr < nb->tail && *ptr)
ptr++;
ptr++;
}
sock->status = 1;
data->block = 0;
grub_netbuff_clear(nb);
break;
case TFTP_DATA:
2011-04-01 08:42:34 +00:00
grub_netbuff_pull (nb,sizeof (tftph->opcode) + sizeof (tftph->u.data.block));
if (grub_be_to_cpu16 (tftph->u.data.block) == data->block + 1)
{
data->block++;
if (nb->tail - nb->data < 1024)
sock->status = 2;
}
else
grub_netbuff_clear(nb);
break;
case TFTP_ERROR:
2011-04-01 08:42:34 +00:00
grub_netbuff_clear (nb);
2010-09-22 22:45:39 +00:00
return grub_error (GRUB_ERR_IO, (char *)tftph->u.err.errmsg);
2011-04-01 08:42:34 +00:00
break;
}
2011-04-01 08:42:34 +00:00
grub_netbuff_clear (&nb_ack);
grub_netbuff_reserve (&nb_ack,128);
grub_netbuff_push (&nb_ack,sizeof (tftph->opcode) + sizeof (tftph->u.ack.block));
2011-04-01 08:42:34 +00:00
tftph = (struct tftphdr *) nb_ack.data;
2010-09-22 22:45:39 +00:00
tftph->opcode = grub_cpu_to_be16 (TFTP_ACK);
2011-04-01 08:42:34 +00:00
tftph->u.ack.block = data->block;
2011-04-01 08:42:34 +00:00
err = grub_net_send_udp_packet (sock, &nb_ack);
return err;
}
static grub_err_t
tftp_close (struct grub_file *file __attribute__ ((unused)))
{
return 0;
}
2011-04-01 08:42:34 +00:00
static struct grub_net_app_protocol grub_tftp_protocol =
{
.name = "tftp",
.open = tftp_open,
.read = tftp_receive,
.close = tftp_close
};
GRUB_MOD_INIT (tftp)
{
grub_net_app_level_register (&grub_tftp_protocol);
}
GRUB_MOD_FINI (tftp)
{
grub_net_app_level_unregister (&grub_tftp_protocol);
}