mirror of
https://git.savannah.gnu.org/git/grub.git
synced 2026-10-29 10:39:22 +00:00
Adaptation for the new protocols and interface structs.
implementation of receive in the protocols. Also all unwanted packets are discarded.
This commit is contained in:
1 parent
c3639ae731
commit
60cdb895da
9 files changed
+505
-226
No files matched your search
+191
-32
@@ -4,42 +4,94 @@
|
||||
#include <grub/net/ip.h>
|
||||
#include <grub/net/ethernet.h>
|
||||
#include <grub/net/netbuff.h>
|
||||
#include <grub/net/protocol.h>
|
||||
#include <grub/net/ieee1275/interface.h>
|
||||
#include <grub/ieee1275/ieee1275.h>
|
||||
#include <grub/time.h>
|
||||
#include <grub/net/interface.h>
|
||||
#include <grub/net/type_net.h>
|
||||
#include <grub/net.h>
|
||||
#include <grub/mm.h>
|
||||
|
||||
int block,rrq_count=0;
|
||||
struct {
|
||||
int block_size;
|
||||
int size;
|
||||
} tftp_file;
|
||||
|
||||
|
||||
char *get_tok_val(char **tok, char **val, char **str_opt,char *end);
|
||||
void process_option(char *tok, char *val);
|
||||
|
||||
char *get_tok_val(char **tok, char **val,char **str_opt,char *end)
|
||||
{
|
||||
char *p = *str_opt;
|
||||
*tok = p;
|
||||
p += grub_strlen(p) + 1;
|
||||
|
||||
if(p > end)
|
||||
return NULL;
|
||||
|
||||
*val = p;
|
||||
p += grub_strlen(p) + 1;
|
||||
*str_opt = p;
|
||||
return *tok;
|
||||
}
|
||||
|
||||
void process_option(char *tok, char *val)
|
||||
{
|
||||
if (!grub_strcmp(tok,"blksize"))
|
||||
{
|
||||
tftp_file.block_size = grub_strtoul (val,NULL,0);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!grub_strcmp(tok,"tsize"))
|
||||
{
|
||||
tftp_file.size = grub_strtoul (val,NULL,0);
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//void tftp_open (char *options);
|
||||
|
||||
/*send read request*/
|
||||
static grub_err_t
|
||||
send_tftp_rr (struct grub_net_interface *inf, struct grub_net_protstack *protstack,struct grub_net_buff *nb)
|
||||
tftp_open (struct grub_net_network_layer_interface *inf __attribute((unused)),
|
||||
struct grub_net_protocol_stack *protstack,struct grub_net_buff *nb, char *filename)
|
||||
{
|
||||
/*Start TFTP header*/
|
||||
|
||||
struct tftphdr *tftph;
|
||||
char *rrq;
|
||||
int rrqlen;
|
||||
int hdrlen;
|
||||
grub_err_t err;
|
||||
struct udp_interf *udp_interf;
|
||||
struct grub_net_application_transport_interface *app_interface = (struct grub_net_application_transport_interface *) protstack->interface;
|
||||
|
||||
if((err = grub_netbuff_push (nb,sizeof(*tftph))) != GRUB_ERR_NONE)
|
||||
return err;
|
||||
app_interface = (struct grub_net_application_transport_interface *) protstack->interface;
|
||||
grub_netbuff_push (nb,sizeof (*tftph));
|
||||
|
||||
udp_interf = (struct udp_interf *) app_interface->data;
|
||||
udp_interf->src = TFTP_CLIENT_PORT + rrq_count++;
|
||||
grub_printf("open tfpt udp_port = %d\n",udp_interf->src);
|
||||
udp_interf->dst = TFTP_SERVER_PORT;
|
||||
tftph = (struct tftphdr *) nb->data;
|
||||
|
||||
rrq = (char *) tftph->u.rrq;
|
||||
rrqlen = 0;
|
||||
|
||||
tftph->opcode = TFTP_RRQ;
|
||||
grub_strcpy (rrq,inf->path);
|
||||
rrqlen += grub_strlen (inf->path) + 1;
|
||||
rrq += grub_strlen (inf->path) + 1;
|
||||
grub_strcpy (rrq,filename);
|
||||
rrqlen += grub_strlen (filename) + 1;
|
||||
rrq += grub_strlen (filename) + 1;
|
||||
/*passar opcoes como parametro ou usar default?*/
|
||||
|
||||
grub_strcpy (rrq,"octet");
|
||||
rrqlen += grub_strlen ("octet") + 1;
|
||||
rrq += grub_strlen ("octet") + 1;
|
||||
|
||||
//grub_strcpy (rrq,"netascii");
|
||||
//rrqlen += grub_strlen ("netascii") + 1;
|
||||
//rrq += grub_strlen ("netascii") + 1;
|
||||
|
||||
grub_strcpy (rrq,"blksize");
|
||||
rrqlen += grub_strlen("blksize") + 1;
|
||||
rrq += grub_strlen ("blksize") + 1;
|
||||
@@ -59,39 +111,146 @@ send_tftp_rr (struct grub_net_interface *inf, struct grub_net_protstack *protsta
|
||||
|
||||
grub_netbuff_unput (nb,nb->tail - (nb->data+hdrlen));
|
||||
|
||||
return protstack->next->prot->send(inf,protstack->next,nb);
|
||||
app_interface->trans_prot->send (inf,protstack->interface,nb);
|
||||
/*Receive OACK*/
|
||||
return app_interface->app_prot->recv(inf,protstack,nb);
|
||||
}
|
||||
|
||||
/*
|
||||
int send_tftp_ack(int block, int port){
|
||||
|
||||
tftp_t pckt;
|
||||
int pcktlen;
|
||||
pckt.opcode = TFTP_ACK;
|
||||
pckt.u.ack.block = block;
|
||||
pcktlen = sizeof (pckt.opcode) + sizeof (pckt.u.ack.block);
|
||||
|
||||
port = 4;
|
||||
return 0;// send_udp_packet (&pckt,pcktlen,TFTP_CLIENT_PORT,port);
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
static struct grub_net_protocol grub_tftp_protocol =
|
||||
static grub_err_t
|
||||
tftp_receive (struct grub_net_network_layer_interface *inf __attribute((unused)),
|
||||
struct grub_net_protocol_stack *protstack,struct grub_net_buff *nb)
|
||||
{
|
||||
.name = "tftp",
|
||||
.open = send_tftp_rr
|
||||
struct tftphdr *tftph;
|
||||
char *token,*value,*temp;
|
||||
struct grub_net_application_transport_interface *app_interface =
|
||||
(struct grub_net_application_transport_interface *) protstack->interface;
|
||||
|
||||
app_interface->trans_prot->recv (inf,protstack->interface,nb);
|
||||
|
||||
tftph = (struct tftphdr *) nb->data;
|
||||
switch (tftph->opcode)
|
||||
{
|
||||
case TFTP_OACK:
|
||||
/*process oack packet*/
|
||||
temp = (char *) tftph->u.oack.data;
|
||||
while(get_tok_val(&token,&value,&temp,nb->tail))
|
||||
{
|
||||
grub_printf("tok = <%s> val = <%s>\n",token,value);
|
||||
process_option(token,value);
|
||||
}
|
||||
|
||||
//buff_clean
|
||||
nb->data = nb->tail;
|
||||
grub_printf("OACK---------------------------------------------------------\n");
|
||||
grub_printf("block_size=%d\n",tftp_file.block_size);
|
||||
grub_printf("file_size=%d\n",tftp_file.size);
|
||||
grub_printf("OACK---------------------------------------------------------\n");
|
||||
block = 0;
|
||||
break;
|
||||
case TFTP_DATA:
|
||||
grub_netbuff_pull (nb,sizeof (tftph->opcode) + sizeof (tftph->u.data.block));
|
||||
if (tftph->u.data.block == block + 1)
|
||||
block = tftph->u.data.block;
|
||||
else
|
||||
grub_netbuff_clear(nb);
|
||||
break;
|
||||
case TFTP_ERROR:
|
||||
nb->data = nb->tail;
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;// tftp_send_ack (inf,protstack,nb,tftph->u.data.block);
|
||||
/*remove tftp header and return.
|
||||
nb should now contain only the payload*/
|
||||
}
|
||||
|
||||
static grub_err_t
|
||||
tftp_send_ack (struct grub_net_network_layer_interface *inf __attribute((unused)),
|
||||
struct grub_net_protocol_stack *protstack,struct grub_net_buff *nb)
|
||||
{
|
||||
struct tftphdr *tftph;
|
||||
struct grub_net_application_transport_interface *app_interface = (struct grub_net_application_transport_interface *) protstack->interface;
|
||||
|
||||
nb->data = nb->tail = nb->end;
|
||||
|
||||
grub_netbuff_push (nb,sizeof (tftph->opcode) + sizeof (tftph->u.ack.block));
|
||||
|
||||
tftph = (struct tftphdr *) nb->data;
|
||||
tftph->opcode = TFTP_ACK;
|
||||
tftph->u.ack.block = block;
|
||||
|
||||
return app_interface->trans_prot->send (inf,protstack->interface,nb);
|
||||
}
|
||||
|
||||
static int tftp_file_size (struct grub_net_network_layer_interface* inf ,
|
||||
struct grub_net_protocol_stack *protocol_stack , struct grub_net_buff *nb ,char *filename )
|
||||
{
|
||||
|
||||
tftp_open (inf, protocol_stack,nb, filename);
|
||||
return tftp_file.size;
|
||||
}
|
||||
|
||||
static grub_err_t
|
||||
tftp_close (struct grub_net_network_layer_interface *inf __attribute((unused)),
|
||||
struct grub_net_protocol_stack *protstack __attribute((unused)),struct grub_net_buff *nb __attribute((unused)))
|
||||
{
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct grub_net_application_transport_interface grub_net_tftp_app_trans_interf;
|
||||
static struct grub_net_transport_network_interface grub_net_tftp_trans_net_interf;
|
||||
static struct grub_net_network_link_interface grub_net_tftp_net_link_interf;
|
||||
static struct grub_net_protocol_stack grub_net_tftp_stack =
|
||||
{
|
||||
.name = "tftp",
|
||||
.id = GRUB_NET_TFTP_ID,
|
||||
.interface = (void *) &grub_net_tftp_app_trans_interf
|
||||
|
||||
};
|
||||
|
||||
static struct grub_net_application_layer_protocol grub_tftp_protocol =
|
||||
{
|
||||
.name = "tftp",
|
||||
.id = GRUB_NET_TFTP_ID,
|
||||
.open = tftp_open,
|
||||
.recv = tftp_receive,
|
||||
.send_ack = tftp_send_ack,
|
||||
.get_file_size = tftp_file_size,
|
||||
.close = tftp_close
|
||||
};
|
||||
|
||||
static struct udp_interf tftp_udp_interf;
|
||||
|
||||
void tftp_ini(void)
|
||||
{
|
||||
grub_protocol_register (&grub_tftp_protocol);
|
||||
|
||||
ethernet_ini ();
|
||||
ipv4_ini ();
|
||||
udp_ini ();
|
||||
grub_net_application_layer_protocol_register (&grub_tftp_protocol);
|
||||
grub_net_stack_register (&grub_net_tftp_stack);
|
||||
|
||||
grub_net_tftp_app_trans_interf.app_prot = &grub_tftp_protocol;
|
||||
grub_net_tftp_app_trans_interf.trans_prot = grub_net_transport_layer_protocol_get (GRUB_NET_UDP_ID);
|
||||
grub_net_tftp_app_trans_interf.inner_layer = &grub_net_tftp_trans_net_interf;
|
||||
grub_net_tftp_app_trans_interf.data = &tftp_udp_interf;
|
||||
|
||||
grub_net_tftp_trans_net_interf.trans_prot = grub_net_tftp_app_trans_interf.trans_prot;
|
||||
grub_net_tftp_trans_net_interf.net_prot = grub_net_network_layer_protocol_get (GRUB_NET_IPV4_ID);
|
||||
grub_net_tftp_trans_net_interf.inner_layer = &grub_net_tftp_net_link_interf;
|
||||
|
||||
grub_net_tftp_net_link_interf.net_prot = grub_net_tftp_trans_net_interf.net_prot;
|
||||
grub_net_tftp_net_link_interf.link_prot = grub_net_link_layer_protocol_get (GRUB_NET_ETHERNET_ID) ;
|
||||
|
||||
|
||||
}
|
||||
|
||||
void tftp_fini(void)
|
||||
{
|
||||
grub_protocol_unregister (&grub_tftp_protocol);
|
||||
|
||||
|
||||
grub_net_application_layer_protocol_unregister (&grub_tftp_protocol);
|
||||
}
|
||||
/*
|
||||
int read_tftp_pckt (grub_uint16_t port, void *buffer, int &buff_len){
|
||||
|
||||
Reference in new issue
Block a user