diff --git a/modules/pico_dhcp_client.c b/modules/pico_dhcp_client.c index 948cbd83a..42a87e8f7 100644 --- a/modules/pico_dhcp_client.c +++ b/modules/pico_dhcp_client.c @@ -510,6 +510,8 @@ static int recv_ack(struct pico_dhcp_client_cookie *dhcpc, uint8_t *buf) 0 }; + struct pico_ipv4_link *l; + pico_dhcp_client_recv_params(dhcpc, opt); if ((dhcpc->event != PICO_DHCP_MSG_ACK) || !dhcpc->server_id.addr || !dhcpc->netmask.addr || !dhcpc->lease_time) return -1; @@ -523,7 +525,15 @@ static int recv_ack(struct pico_dhcp_client_cookie *dhcpc, uint8_t *buf) /* close the socket used for address (re)acquisition */ pico_socket_close(dhcpc->s); dhcpc->s = NULL; + + /* Delete all the links before adding the address */ pico_ipv4_link_del(dhcpc->dev, address); + l = pico_ipv4_link_by_dev(dhcpc->dev); + while(l) { + pico_ipv4_link_del(dhcpc->dev, l->address); + l = pico_ipv4_link_by_dev_next(dhcpc->dev, l); + } + pico_ipv4_link_add(dhcpc->dev, dhcpc->address, dhcpc->netmask); dbg("DHCP client: renewal time (T1) %u\n", (unsigned int)dhcpc->t1_time); diff --git a/modules/pico_ipv4.c b/modules/pico_ipv4.c index d00544b0b..77b7f1962 100644 --- a/modules/pico_ipv4.c +++ b/modules/pico_ipv4.c @@ -1495,6 +1495,8 @@ int pico_ipv4_link_add(struct pico_device *dev, struct pico_ip4 address, struct pico_ipv4_route_add(network, netmask, gateway, 1, new); pico_ipv4_to_string(ipstr, new->address.addr); dbg("Assigned ipv4 %s to device %s\n", ipstr, new->dev->name); + if (default_bcast_route.link == NULL) + default_bcast_route.link = new; return 0; } @@ -1560,6 +1562,8 @@ int pico_ipv4_link_del(struct pico_device *dev, struct pico_ip4 address) pico_ipv4_cleanup_routes(found); pico_tree_delete(&Tree_dev_link, found); + if (default_bcast_route.link == found) + default_bcast_route.link = NULL; PICO_FREE(found); return 0; diff --git a/modules/pico_sntp_client.c b/modules/pico_sntp_client.c index a3f60af9e..e52b8f47c 100644 --- a/modules/pico_sntp_client.c +++ b/modules/pico_sntp_client.c @@ -1,5 +1,5 @@ /********************************************************************* - PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved. + PicoTCP. Copyright (c) 2012-2015 Altran Intelligent Systems. Some rights reserved. See LICENSE and COPYING for usage. Author: Toon Stegen @@ -13,6 +13,7 @@ #include "pico_ipv6.h" #include "pico_dns_client.h" #include "pico_tree.h" +#include "pico_stack.h" #ifdef PICO_SUPPORT_SNTP_CLIENT @@ -20,6 +21,7 @@ /* #define sntp_dbg dbg */ #define SNTP_VERSION 4 +#define PICO_SNTP_MAXBUF (1400) /* Sntp mode */ #define SNTP_MODE_CLIENT 3 @@ -63,6 +65,7 @@ struct sntp_server_ns_cookie struct pico_socket *sock; /* Socket which contains the cookie */ void (*cb_synced)(pico_err_t status); /* Callback function for telling the user wheter/when the time is synchronised */ + struct pico_timer *timer; /* Timer that will signal timeout */ }; /* global variables */ @@ -110,6 +113,7 @@ static void pico_sntp_cleanup(struct sntp_server_ns_cookie *ck, pico_err_t statu sntp_dbg("FREE!\n"); PICO_FREE(ck->hostname); PICO_FREE(ck); + } /* Extracts the current time from a server sntp packet*/ @@ -145,7 +149,7 @@ static int pico_sntp_parse(char *buf, struct sntp_server_ns_cookie *ck) static void pico_sntp_client_wakeup(uint16_t ev, struct pico_socket *s) { struct sntp_server_ns_cookie *ck = (struct sntp_server_ns_cookie *)s->priv; - char recvbuf[1400]; + char *recvbuf; int read = 0; uint32_t peer; uint16_t port; @@ -159,10 +163,15 @@ static void pico_sntp_client_wakeup(uint16_t ev, struct pico_socket *s) if (ev == PICO_SOCK_EV_RD) { ck->rec = 1; /* receive while data available in socket buffer */ + recvbuf = PICO_ZALLOC(PICO_SNTP_MAXBUF); + if (!recvbuf) + return; do { - read = pico_socket_recvfrom(s, recvbuf, 1400, &peer, &port); + read = pico_socket_recvfrom(s, recvbuf, PICO_SNTP_MAXBUF, &peer, &port); } while(read > 0); pico_sntp_parse(recvbuf, s->priv); + pico_timer_cancel(ck->timer); + PICO_FREE(recvbuf); } /* socket is closed */ else if(ev == PICO_SOCK_EV_CLOSE) { @@ -209,7 +218,7 @@ static void pico_sntp_send(struct pico_socket *sock, union pico_address *dst) return; } - pico_timer_add(5000, sntp_receive_timeout, ck); + ck->timer = pico_timer_add(5000, sntp_receive_timeout, ck); header.vn = SNTP_VERSION; header.mode = SNTP_MODE_CLIENT; /* header.trs_ts.frac = long_be(0ul); */ @@ -256,6 +265,7 @@ static void dnsCallback(char *ip, void *arg) sock = pico_socket_open(ck->proto, PICO_PROTO_UDP, &pico_sntp_client_wakeup); if (!sock) return; + sock->priv = ck; ck->sock = sock; if ((pico_socket_bind(sock, &sntp_inaddr_any, &any_port) == 0)) { @@ -330,18 +340,21 @@ int pico_sntp_sync(const char *sntp_server, void (*cb_synced)(pico_err_t status) ck6->cb_synced = cb_synced; sntp_dbg("Resolving AAAA %s\n", ck6->hostname); retval6 = pico_dns_client_getaddr6(sntp_server, &dnsCallback, ck6); - - PICO_FREE(ck6->hostname); - PICO_FREE(ck6); + if (retval6 != 0) { + PICO_FREE(ck6->hostname); + PICO_FREE(ck6); + return -1; + } #endif sntp_dbg("Resolving A %s\n", ck->hostname); retval = pico_dns_client_getaddr(sntp_server, &dnsCallback, ck); - - PICO_FREE(ck->hostname); - PICO_FREE(ck); - - return (!retval || !retval6)? 0: (-1); + if (retval != 0) { + PICO_FREE(ck->hostname); + PICO_FREE(ck); + return -1; + } + return 0; } /* user function to get the current time */ diff --git a/test/unit/modunit_pico_sntp_client.c b/test/unit/modunit_pico_sntp_client.c index 6a604997f..cf089f332 100644 --- a/test/unit/modunit_pico_sntp_client.c +++ b/test/unit/modunit_pico_sntp_client.c @@ -95,6 +95,11 @@ struct pico_timer *pico_timer_add(pico_time expire, void (*timer)(pico_time, voi return NULL; } +void pico_timer_cancel(struct pico_timer *t) +{ + +} + START_TEST(tc_timestamp_convert) { struct pico_sntp_ts ts;