2010-09-16 19:57:31 +00:00
|
|
|
#include <grub/net.h>
|
|
|
|
#include <grub/net/udp.h>
|
|
|
|
#include <grub/net/ip.h>
|
|
|
|
#include <grub/net/netbuff.h>
|
|
|
|
#include <grub/time.h>
|
|
|
|
|
2011-07-02 20:13:33 +00:00
|
|
|
grub_net_socket_t
|
|
|
|
grub_net_udp_open (char *server,
|
|
|
|
grub_uint16_t out_port,
|
|
|
|
grub_err_t (*recv_hook) (grub_net_socket_t sock,
|
|
|
|
struct grub_net_buff *nb,
|
|
|
|
void *data),
|
|
|
|
void *recv_hook_data)
|
|
|
|
{
|
|
|
|
grub_err_t err;
|
|
|
|
grub_net_network_level_address_t addr;
|
|
|
|
struct grub_net_network_level_interface *inf;
|
|
|
|
grub_net_network_level_address_t gateway;
|
|
|
|
grub_net_socket_t socket;
|
|
|
|
static int in_port = 25300;
|
|
|
|
|
|
|
|
err = grub_net_resolve_address (server, &addr);
|
|
|
|
if (err)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
err = grub_net_route_address (addr, &gateway, &inf);
|
|
|
|
if (err)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
socket = grub_zalloc (sizeof (*socket));
|
|
|
|
if (socket == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
socket->x_out_port = out_port;
|
|
|
|
socket->x_inf = inf;
|
|
|
|
socket->x_out_nla = addr;
|
|
|
|
socket->x_in_port = in_port++;
|
|
|
|
socket->x_status = GRUB_NET_SOCKET_START;
|
|
|
|
socket->recv_hook = recv_hook;
|
|
|
|
socket->recv_hook_data = recv_hook_data;
|
|
|
|
|
|
|
|
grub_net_socket_register (socket);
|
|
|
|
|
|
|
|
return socket;
|
|
|
|
}
|
|
|
|
|
2010-09-16 19:57:31 +00:00
|
|
|
grub_err_t
|
2011-06-08 00:59:53 +00:00
|
|
|
grub_net_send_udp_packet (const grub_net_socket_t socket,
|
|
|
|
struct grub_net_buff *nb)
|
2010-09-16 19:57:31 +00:00
|
|
|
{
|
|
|
|
struct udphdr *udph;
|
2011-06-18 23:20:53 +00:00
|
|
|
grub_err_t err;
|
2010-09-16 19:57:31 +00:00
|
|
|
|
2011-06-24 19:51:57 +00:00
|
|
|
err = grub_netbuff_push (nb, sizeof (*udph));
|
|
|
|
if (err)
|
2011-06-18 23:20:53 +00:00
|
|
|
return err;
|
2011-06-08 00:59:53 +00:00
|
|
|
|
|
|
|
udph = (struct udphdr *) nb->data;
|
2011-07-02 20:13:33 +00:00
|
|
|
udph->src = grub_cpu_to_be16 (socket->x_in_port);
|
|
|
|
udph->dst = grub_cpu_to_be16 (socket->x_out_port);
|
2010-09-16 19:57:31 +00:00
|
|
|
|
|
|
|
/* No chechksum. */
|
2011-06-08 00:59:53 +00:00
|
|
|
udph->chksum = 0;
|
2010-09-16 19:57:31 +00:00
|
|
|
udph->len = grub_cpu_to_be16 (nb->tail - nb->data);
|
2011-06-08 00:59:53 +00:00
|
|
|
|
2011-07-02 20:13:33 +00:00
|
|
|
return grub_net_send_ip_packet (socket->x_inf, &(socket->x_out_nla), nb);
|
2010-09-16 19:57:31 +00:00
|
|
|
}
|
|
|
|
|
2011-06-08 00:59:53 +00:00
|
|
|
grub_err_t
|
2011-06-24 18:35:25 +00:00
|
|
|
grub_net_recv_udp_packet (struct grub_net_buff * nb,
|
|
|
|
struct grub_net_network_level_interface * inf)
|
2010-09-16 19:57:31 +00:00
|
|
|
{
|
2011-04-01 08:12:44 +00:00
|
|
|
struct udphdr *udph;
|
|
|
|
grub_net_socket_t sock;
|
2011-07-02 18:11:29 +00:00
|
|
|
grub_err_t err;
|
2011-04-01 08:12:44 +00:00
|
|
|
udph = (struct udphdr *) nb->data;
|
2011-07-02 18:11:29 +00:00
|
|
|
err = grub_netbuff_pull (nb, sizeof (*udph));
|
|
|
|
if (err)
|
|
|
|
return err;
|
2010-09-16 19:57:31 +00:00
|
|
|
|
2011-06-08 00:59:53 +00:00
|
|
|
FOR_NET_SOCKETS (sock)
|
|
|
|
{
|
2011-07-02 20:13:33 +00:00
|
|
|
if (grub_be_to_cpu16 (udph->dst) == sock->x_in_port
|
|
|
|
&& inf == sock->x_inf && sock->recv_hook)
|
2011-06-08 00:59:53 +00:00
|
|
|
{
|
2011-07-02 20:13:33 +00:00
|
|
|
if (sock->x_status == GRUB_NET_SOCKET_START)
|
|
|
|
{
|
|
|
|
sock->x_out_port = grub_be_to_cpu16 (udph->src);
|
|
|
|
sock->x_status = GRUB_NET_SOCKET_ESTABLISHED;
|
|
|
|
}
|
2011-06-08 00:59:53 +00:00
|
|
|
|
|
|
|
/* App protocol remove its own reader. */
|
2011-07-02 20:13:33 +00:00
|
|
|
sock->recv_hook (sock, nb, sock->recv_hook_data);
|
2011-06-08 00:59:53 +00:00
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
grub_netbuff_free (nb);
|
2010-09-16 19:57:31 +00:00
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
}
|