2016-03-30 12:28:39 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 Kaspar Schleiser <kaspar@schleiser.de>
|
|
|
|
*
|
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser
|
|
|
|
* General Public License v2.1. See the file LICENSE in the top level
|
|
|
|
* directory for more details.
|
|
|
|
*/
|
|
|
|
|
2016-11-13 14:54:02 +01:00
|
|
|
#include <arpa/inet.h>
|
2016-03-30 12:28:39 +02:00
|
|
|
|
|
|
|
#include "net/af.h"
|
2016-11-13 14:54:02 +01:00
|
|
|
#include "net/sock/udp.h"
|
|
|
|
#include "net/uhcp.h"
|
2016-03-30 12:28:39 +02:00
|
|
|
#include "xtimer.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Request prefix from uhcp server
|
|
|
|
*
|
|
|
|
* Never returns.
|
|
|
|
* Calls @c uhcp_handle_prefix() when a prefix or prefix change is received.
|
|
|
|
*
|
|
|
|
* @param[in] iface interface to request prefix on
|
|
|
|
*/
|
|
|
|
void uhcp_client(uhcp_iface_t iface)
|
|
|
|
{
|
2016-11-13 14:54:02 +01:00
|
|
|
sock_udp_t sock;
|
|
|
|
sock_udp_ep_t local = { .family=AF_INET6, .port=UHCP_PORT, .netif=iface };
|
|
|
|
sock_udp_ep_t req_target = { .family=AF_INET6, .port=UHCP_PORT, .netif=iface };
|
|
|
|
sock_udp_ep_t remote;
|
|
|
|
|
|
|
|
inet_pton(AF_INET6, "ff15::abcd", req_target.addr.ipv6);
|
2016-03-30 12:28:39 +02:00
|
|
|
|
|
|
|
/* prepare UHCP header */
|
|
|
|
uhcp_req_t req;
|
|
|
|
uhcp_hdr_set(&req.hdr, UHCP_REQ);
|
|
|
|
req.prefix_len = 64;
|
|
|
|
|
|
|
|
/* create listening socket */
|
2016-11-13 14:54:02 +01:00
|
|
|
int res = sock_udp_create(&sock, &local, NULL, 0);
|
2016-03-30 12:28:39 +02:00
|
|
|
|
|
|
|
uint8_t buf[sizeof(uhcp_push_t) + 16];
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
puts("uhcp_client(): sending REQ...");
|
2016-11-13 14:54:02 +01:00
|
|
|
sock_udp_send(&sock, &req, sizeof(uhcp_req_t), &req_target);
|
|
|
|
res = sock_udp_recv(&sock, buf, sizeof(buf), 10U*SEC_IN_USEC, &remote);
|
2016-03-30 12:28:39 +02:00
|
|
|
if (res > 0) {
|
2016-11-13 14:54:02 +01:00
|
|
|
uhcp_handle_udp(buf, res, remote.addr.ipv6, remote.port, iface);
|
2016-03-30 12:28:39 +02:00
|
|
|
xtimer_sleep(60);
|
|
|
|
}
|
|
|
|
else {
|
2016-11-13 14:54:02 +01:00
|
|
|
puts("uhcp_client(): no reply received");
|
2016-03-30 12:28:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|