1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
RIOT/sys/net/application_layer/uhcp/uhcpc.c

56 lines
1.5 KiB
C
Raw Normal View History

/*
* 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>
#include "net/af.h"
2016-11-13 14:54:02 +01:00
#include "net/sock/udp.h"
#include "net/uhcp.h"
#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);
/* 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);
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);
if (res > 0) {
2016-11-13 14:54:02 +01:00
uhcp_handle_udp(buf, res, remote.addr.ipv6, remote.port, iface);
xtimer_sleep(60);
}
else {
2016-11-13 14:54:02 +01:00
puts("uhcp_client(): no reply received");
}
}
}