From 12d42c5801b7b8a09047b30e16ed1f842700d133 Mon Sep 17 00:00:00 2001 From: goldsimon Date: Fri, 26 Mar 2010 14:20:39 +0000 Subject: [PATCH] unixsim: enable more warnings and fix the newly found warnings in ports/unix --- ports/unix/include/netif/list.h | 3 +- ports/unix/netif/list.c | 18 ++--- ports/unix/netif/tapif.c | 63 ++++++++-------- ports/unix/netif/tcpdump.c | 39 +++++----- ports/unix/netif/tunif.c | 67 +++++++++-------- ports/unix/netif/unixif.c | 108 ++++++++++++++-------------- ports/unix/proj/unixsim/Makefile | 11 ++- ports/unix/proj/unixsim/simhost.c | 12 ++-- ports/unix/proj/unixsim/simnode.c | 6 +- ports/unix/proj/unixsim/simrouter.c | 6 +- ports/unix/sys_arch.c | 83 +++++++++++---------- 11 files changed, 226 insertions(+), 190 deletions(-) diff --git a/ports/unix/include/netif/list.h b/ports/unix/include/netif/list.h index 38d598f..7d8728d 100644 --- a/ports/unix/include/netif/list.h +++ b/ports/unix/include/netif/list.h @@ -2,6 +2,8 @@ #ifndef __LIST_H__ #define __LIST_H__ +struct elem; + struct list { struct elem *first, *last; int size, elems; @@ -17,7 +19,6 @@ int list_push(struct list *list, void *data); void *list_pop(struct list *list); void *list_first(struct list *list); int list_elems(struct list *list); -int list_elems(struct list *list); void list_delete(struct list *list); int list_remove(struct list *list, void *elem); void list_map(struct list *list, void (* func)(void *arg)); diff --git a/ports/unix/netif/list.c b/ports/unix/netif/list.c index 6b72c8e..fd61f53 100644 --- a/ports/unix/netif/list.c +++ b/ports/unix/netif/list.c @@ -41,7 +41,7 @@ struct list * list_new(int size) { struct list *list; - list = malloc(sizeof(struct list)); + list = (struct list *)malloc(sizeof(struct list)); list->first = list->last = NULL; list->size = size; list->elems = 0; @@ -52,9 +52,9 @@ int list_push(struct list *list, void *data) { struct elem *elem; - + if (list->elems < list->size) { - elem = malloc(sizeof(struct elem)); + elem = (struct elem *)malloc(sizeof(struct elem)); elem->data = data; elem->next = NULL; if (list->last != NULL) { @@ -75,14 +75,14 @@ list_pop(struct list *list) { struct elem *elem; void *data; - - if (list->elems > 0) { + + if (list->elems > 0) { elem = list->first; if (elem == list->last) { list->last = elem->next; } list->first = elem->next; - + list->elems--; data = elem->data; @@ -116,7 +116,7 @@ int list_remove(struct list *list, void *elem) { struct elem *e, *p; - + p = NULL; for(e = list->first; e != NULL; e = e->next) { if (e->data == elem) { @@ -134,8 +134,8 @@ list_remove(struct list *list, void *elem) free(e); list->elems--; return 1; - } - p = e; + } + p = e; } return 0; } diff --git a/ports/unix/netif/tapif.c b/ports/unix/netif/tapif.c index 984a376..32be852 100644 --- a/ports/unix/netif/tapif.c +++ b/ports/unix/netif/tapif.c @@ -30,6 +30,8 @@ * */ +#include "netif/tapif.h" + #include #include #include @@ -55,6 +57,8 @@ #include "netif/tcpdump.h" #endif /* LWIP_DEBUG && LWIP_TCPDUMP */ +#define IFCONFIG_BIN "/sbin/ifconfig " + #if defined(linux) #include #include @@ -92,10 +96,10 @@ static void low_level_init(struct netif *netif) { struct tapif *tapif; - char buf[100]; + char buf[sizeof(IFCONFIG_ARGS) + sizeof(IFCONFIG_BIN) + 50]; + + tapif = (struct tapif *)netif->state; - tapif = netif->state; - /* Obtain MAC address from network interface. */ /* (We just fake an address...) */ @@ -107,7 +111,7 @@ low_level_init(struct netif *netif) tapif->ethaddr->addr[5] = 0x6; /* Do whatever else is needed to initialize interface. */ - + tapif->fd = open(DEVTAP, O_RDWR); LWIP_DEBUGF(TAPIF_DEBUG, ("tapif_init: fd %d\n", tapif->fd)); if(tapif->fd == -1) { @@ -131,12 +135,12 @@ low_level_init(struct netif *netif) } #endif /* Linux */ - snprintf(buf, sizeof(buf), "/sbin/ifconfig " IFCONFIG_ARGS, + sprintf(buf, IFCONFIG_BIN IFCONFIG_ARGS, ip4_addr1(&(netif->gw)), ip4_addr2(&(netif->gw)), ip4_addr3(&(netif->gw)), ip4_addr4(&(netif->gw))); - + LWIP_DEBUGF(TAPIF_DEBUG, ("tapif_init: system(\"%s\");\n", buf)); system(buf); sys_thread_new("tapif_thread", tapif_thread, netif, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO); @@ -161,21 +165,21 @@ low_level_output(struct netif *netif, struct pbuf *p) char *bufptr; struct tapif *tapif; - tapif = netif->state; -#if 0 + tapif = (struct tapif *)netif->state; +#if 0 if(((double)rand()/(double)RAND_MAX) < 0.2) { printf("drop output\n"); return ERR_OK; } -#endif +#endif /* initiate transfer(); */ - + bufptr = &buf[0]; - + for(q = p; q != NULL; q = q->next) { /* Send the data from the pbuf to the interface, one pbuf at a time. The size of the data in each pbuf is kept in the ->len - variable. */ + variable. */ /* send data from(q->payload, q->len); */ memcpy(bufptr, q->payload, q->len); bufptr += q->len; @@ -213,10 +217,10 @@ low_level_input(struct tapif *tapif) return NULL; } #endif - + /* We allocate a pbuf chain of pbufs from the pool. */ p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL); - + if(p != NULL) { /* We iterate over the pbuf chain until we have read the entire packet into the pbuf. */ @@ -234,20 +238,20 @@ low_level_input(struct tapif *tapif) /* drop packet(); */ } - return p; + return p; } /*-----------------------------------------------------------------------------------*/ -static void +static void tapif_thread(void *arg) { struct netif *netif; struct tapif *tapif; fd_set fdset; int ret; - - netif = arg; - tapif = netif->state; - + + netif = (struct netif *)arg; + tapif = (struct tapif *)netif->state; + while(1) { FD_ZERO(&fdset); FD_SET(tapif->fd, &fdset); @@ -282,15 +286,15 @@ tapif_input(struct netif *netif) struct pbuf *p; - tapif = netif->state; - + tapif = (struct tapif *)netif->state; + p = low_level_input(tapif); if(p == NULL) { LWIP_DEBUGF(TAPIF_DEBUG, ("tapif_input: low_level_input returned NULL\n")); return; } - ethhdr = p->payload; + ethhdr = (struct eth_hdr *)p->payload; switch(htons(ethhdr->type)) { /* IP or ARP packet? */ @@ -327,10 +331,11 @@ err_t tapif_init(struct netif *netif) { struct tapif *tapif; - - tapif = mem_malloc(sizeof(struct tapif)); - if (!tapif) - return ERR_MEM; + + tapif = (struct tapif *)mem_malloc(sizeof(struct tapif)); + if (!tapif) { + return ERR_MEM; + } netif->state = tapif; netif->name[0] = IFNAME0; netif->name[1] = IFNAME1; @@ -339,10 +344,10 @@ tapif_init(struct netif *netif) netif->mtu = 1500; /* hardware address length */ netif->hwaddr_len = 6; - + tapif->ethaddr = (struct eth_addr *)&(netif->hwaddr[0]); low_level_init(netif); - + return ERR_OK; } /*-----------------------------------------------------------------------------------*/ diff --git a/ports/unix/netif/tcpdump.c b/ports/unix/netif/tcpdump.c index 2177800..5929857 100644 --- a/ports/unix/netif/tcpdump.c +++ b/ports/unix/netif/tcpdump.c @@ -73,12 +73,13 @@ tcpdump(struct pbuf *p) if (file == NULL) { return; } -#ifdef IPv4 - iphdr = p->payload; +#ifdef IPv4 + iphdr = (struct ip_hdr *)p->payload; switch (IPH_PROTO(iphdr)) { - case IP_PROTO_TCP: +#if LWIP_TCP + case IP_PROTO_TCP: tcphdr = (struct tcp_hdr *)((char *)iphdr + IP_HLEN); - + pbuf_header(p, -IP_HLEN); if (inet_chksum_pseudo(p, (ip_addr_t *)&(iphdr->src), (ip_addr_t *)&(iphdr->dest), @@ -91,7 +92,7 @@ tcpdump(struct pbuf *p) IP_PROTO_TCP, p->tot_len));*/ fprintf(file, "!chksum "); } - + i = 0; if (TCPH_FLAGS(tcphdr) & TCP_SYN) { flags[i++] = 'S'; @@ -108,10 +109,10 @@ tcpdump(struct pbuf *p) if (i == 0) { flags[i++] = '.'; } - flags[i++] = 0; + flags[i++] = 0; + + - - fprintf(file, "%d.%d.%d.%d.%u > %d.%d.%d.%d.%u: ", (int)(ntohl(iphdr->src.addr) >> 24) & 0xff, (int)(ntohl(iphdr->src.addr) >> 16) & 0xff, @@ -124,7 +125,7 @@ tcpdump(struct pbuf *p) (int)(ntohl(iphdr->dest.addr) >> 0) & 0xff, ntohs(tcphdr->dest)); offset = TCPH_OFFSET(tcphdr) >> 4; - + len = ntohs(IPH_LEN(iphdr)) - offset * 4 - IP_HLEN; if (len != 0 || flags[0] != '.') { fprintf(file, "%s %u:%u(%u) ", @@ -139,16 +140,17 @@ tcpdump(struct pbuf *p) } fprintf(file, "wnd %u\n", ntohs(tcphdr->wnd)); - + fflush(file); - + pbuf_header(p, IP_HLEN); break; - +#endif /* LWIP_TCP */ + #if LWIP_UDP - case IP_PROTO_UDP: + case IP_PROTO_UDP: udphdr = (struct udp_hdr *)((char *)iphdr + IP_HLEN); - + pbuf_header(p, -IP_HLEN); if (inet_chksum_pseudo(p, (ip_addr_t *)&(iphdr->src), (ip_addr_t *)&(iphdr->dest), @@ -161,7 +163,7 @@ tcpdump(struct pbuf *p) IP_PROTO_TCP, p->tot_len));*/ fprintf(file, "!chksum "); } - + fprintf(file, "%d.%d.%d.%d.%u > %d.%d.%d.%d.%u: ", (int)(ntohl(iphdr->src.addr) >> 24) & 0xff, (int)(ntohl(iphdr->src.addr) >> 16) & 0xff, @@ -176,12 +178,15 @@ tcpdump(struct pbuf *p) fprintf(file, "U "); len = ntohs(IPH_LEN(iphdr)) - sizeof(struct udp_hdr) - IP_HLEN; fprintf(file, " %d\n", len); - + fflush(file); - + pbuf_header(p, IP_HLEN); break; #endif /* LWIP_UDP */ + default: + LWIP_DEBUGF(TCPDUMP_DEBUG, ("unhandled IP protocol: %d\n", (int)IPH_PROTO(iphdr))); + break; } #endif /* IPv4 */ diff --git a/ports/unix/netif/tunif.c b/ports/unix/netif/tunif.c index 33af594..27a0cd6 100644 --- a/ports/unix/netif/tunif.c +++ b/ports/unix/netif/tunif.c @@ -30,6 +30,8 @@ * */ +#include "netif/tunif.h" + #include #include #include @@ -58,6 +60,8 @@ #define TUNIF_DEBUG LWIP_DBG_OFF #endif +#define IFCONFIG_CALL "/sbin/ifconfig tun0 inet %d.%d.%d.%d %d.%d.%d.%d" + struct tunif { /* Add whatever per-interface state that is needed here. */ int fd; @@ -75,21 +79,21 @@ static void low_level_init(struct netif *netif) { struct tunif *tunif; - char buf[100]; + char buf[sizeof(IFCONFIG_CALL) + 50]; + + tunif = (struct tunif *)netif->state; - tunif = netif->state; - /* Obtain MAC address from network interface. */ /* Do whatever else is needed to initialize interface. */ - + tunif->fd = open("/dev/tun0", O_RDWR); LWIP_DEBUGF(TUNIF_DEBUG, ("tunif_init: fd %d\n", tunif->fd)); if (tunif->fd == -1) { perror("tunif_init"); exit(1); } - snprintf(buf, sizeof(buf), "/sbin/ifconfig tun0 inet %d.%d.%d.%d %d.%d.%d.%d", + sprintf(buf, IFCONFIG_CALL, ip4_addr1(&(netif->gw)), ip4_addr2(&(netif->gw)), ip4_addr3(&(netif->gw)), @@ -98,7 +102,7 @@ low_level_init(struct netif *netif) ip4_addr2(&(netif->ip_addr)), ip4_addr3(&(netif->ip_addr)), ip4_addr4(&(netif->ip_addr))); - + LWIP_DEBUGF(TUNIF_DEBUG, ("tunif_init: system(\"%s\");\n", buf)); system(buf); sys_thread_new("tunif_thread", tunif_thread, netif, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO); @@ -121,23 +125,25 @@ low_level_output(struct tunif *tunif, struct pbuf *p) struct pbuf *q; char buf[1500]; char *bufptr; - + int rnd_val; + /* initiate transfer(); */ - if (((double)rand()/(double)RAND_MAX) < 0.4) { + rnd_val = rand(); + if (((double)rnd_val/(double)RAND_MAX) < 0.4) { printf("drop\n"); return ERR_OK; } - - + + bufptr = &buf[0]; - + for(q = p; q != NULL; q = q->next) { /* Send the data from the pbuf to the interface, one pbuf at a time. The size of the data in each pbuf is kept in the ->len - variable. */ + variable. */ /* send data from(q->payload, q->len); */ - bcopy(q->payload, bufptr, q->len); + memcpy(q->payload, bufptr, q->len); bufptr += q->len; } @@ -173,10 +179,10 @@ low_level_input(struct tunif *tunif) return NULL; }*/ - + /* We allocate a pbuf chain of pbufs from the pool. */ p = pbuf_alloc(PBUF_LINK, len, PBUF_POOL); - + if (p != NULL) { /* We iterate over the pbuf chain until we have read the entire packet into the pbuf. */ @@ -186,7 +192,7 @@ low_level_input(struct tunif *tunif) available data in the pbuf is given by the q->len variable. */ /* read data into(q->payload, q->len); */ - bcopy(bufptr, q->payload, q->len); + memcpy(bufptr, q->payload, q->len); bufptr += q->len; } /* acknowledge that packet has been read(); */ @@ -194,7 +200,7 @@ low_level_input(struct tunif *tunif) /* drop packet(); */ } - return p; + return p; } /*-----------------------------------------------------------------------------------*/ static void @@ -204,10 +210,10 @@ tunif_thread(void *arg) struct tunif *tunif; fd_set fdset; int ret; - - netif = arg; - tunif = netif->state; - + + netif = (struct netif *)arg; + tunif = (struct tunif *)netif->state; + while (1) { FD_ZERO(&fdset); FD_SET(tunif->fd, &fdset); @@ -240,7 +246,7 @@ tunif_output(struct netif *netif, struct pbuf *p, struct tunif *tunif; LWIP_UNUSED_ARG(ipaddr); - tunif = netif->state; + tunif = (struct tunif *)netif->state; return low_level_output(tunif, p); @@ -263,8 +269,8 @@ tunif_input(struct netif *netif) struct pbuf *p; - tunif = netif->state; - + tunif = (struct tunif *)netif->state; + p = low_level_input(tunif); if (p == NULL) { @@ -295,16 +301,17 @@ err_t tunif_init(struct netif *netif) { struct tunif *tunif; - - tunif = mem_malloc(sizeof(struct tunif)); - if (!tunif) - return ERR_MEM; + + tunif = (struct tunif *)mem_malloc(sizeof(struct tunif)); + if (!tunif) { + return ERR_MEM; + } netif->state = tunif; netif->name[0] = IFNAME0; netif->name[1] = IFNAME1; netif->output = tunif_output; - - + + low_level_init(netif); return ERR_OK; } diff --git a/ports/unix/netif/unixif.c b/ports/unix/netif/unixif.c index acfa665..369c22c 100644 --- a/ports/unix/netif/unixif.c +++ b/ports/unix/netif/unixif.c @@ -69,7 +69,7 @@ struct unixif_buf { struct pbuf *p; unsigned short len, tot_len; - void *payload; + void *payload; }; struct unixif { @@ -81,7 +81,7 @@ struct unixif { /*-----------------------------------------------------------------------------------*/ static int -unix_socket_client(char *name) +unix_socket_client(const char *name) { int fd, len; struct sockaddr_un unix_addr; @@ -91,7 +91,7 @@ unix_socket_client(char *name) perror("unixif: unix_socket_client: socket"); return(-1); } - + /* fill socket address structure w/our address */ memset(&unix_addr, 0, sizeof(unix_addr)); unix_addr.sun_family = AF_UNIX; @@ -104,7 +104,7 @@ unix_socket_client(char *name) len = sizeof(unix_addr.sun_family) + strlen(unix_addr.sun_path) + 1; #endif /* linux */ - + unlink(unix_addr.sun_path); /* in case it already exists */ if (bind(fd, (struct sockaddr *) &unix_addr, sizeof(struct sockaddr_un)) < 0) { @@ -115,7 +115,7 @@ unix_socket_client(char *name) perror("unixif: unix_socket_client: socket"); return(-1); } - + /* fill socket address structure w/server's addr */ memset(&unix_addr, 0, sizeof(unix_addr)); unix_addr.sun_family = AF_UNIX; @@ -125,7 +125,7 @@ unix_socket_client(char *name) strlen(unix_addr.sun_path) + 1; unix_addr.sun_len = len; #else - len = sizeof(unix_addr.sun_family) + strlen(unix_addr.sun_path) + 1; + len = sizeof(unix_addr.sun_family) + strlen(unix_addr.sun_path) + 1; #endif /* linux */ if (connect(fd, (struct sockaddr *) &unix_addr, sizeof(struct sockaddr_un)) < 0) { @@ -137,7 +137,7 @@ unix_socket_client(char *name) /*-----------------------------------------------------------------------------------*/ static int -unix_socket_server(char *name) +unix_socket_server(const char *name) { int fd, len; struct sockaddr_un unix_addr; @@ -147,9 +147,9 @@ unix_socket_server(char *name) perror("unixif: unix_socket_server: socket"); return(-1); } - + unlink(name); /* in case it already exists */ - + /* fill in socket address structure */ memset(&unix_addr, 0, sizeof(unix_addr)); unix_addr.sun_family = AF_UNIX; @@ -162,7 +162,7 @@ unix_socket_server(char *name) len = sizeof(unix_addr.sun_family) + strlen(unix_addr.sun_path) + 1; #endif /* linux */ - + /* bind the name to the descriptor */ if (bind(fd, (struct sockaddr *) &unix_addr, sizeof(struct sockaddr_un)) < 0) { @@ -189,12 +189,12 @@ unixif_input_handler(void *data) { struct netif *netif; struct unixif *unixif; - char buf[4096], *bufptr; + char buf[1532], *bufptr; int len, plen, rlen; struct pbuf *p, *q; - netif = data; - unixif = netif->state; + netif = (struct netif *)data; + unixif = (struct unixif *)netif->state; len = read(unixif->fd, &plen, sizeof(int)); if (len == -1) { @@ -217,7 +217,7 @@ unixif_input_handler(void *data) } LWIP_DEBUGF(UNIXIF_DEBUG, ("unixif_irq_handler: read %d bytes\n", len)); p = pbuf_alloc(PBUF_LINK, len, PBUF_POOL); - + if (p != NULL) { rlen = len; bufptr = buf; @@ -235,7 +235,7 @@ unixif_input_handler(void *data) } else { LWIP_DEBUGF(UNIXIF_DEBUG, ("unixif_irq_handler: could not allocate pbuf\n")); } - + } } @@ -245,18 +245,18 @@ unixif_thread(void *arg) { struct netif *netif; struct unixif *unixif; - + LWIP_DEBUGF(UNIXIF_DEBUG, ("unixif_thread: started.\n")); - netif = arg; - unixif = netif->state; + netif = (struct netif *)arg; + unixif = (struct unixif *)netif->state; while (1) { sys_sem_wait(&unixif->sem); unixif_input_handler(netif); } - + } /*-----------------------------------------------------------------------------------*/ static void @@ -265,16 +265,16 @@ unixif_thread2(void *arg) struct netif *netif; struct unixif *unixif; fd_set fdset; - + LWIP_DEBUGF(UNIXIF_DEBUG, ("unixif_thread2: started.\n")); - netif = arg; - unixif = netif->state; + netif = (struct netif *)arg; + unixif = (struct unixif *)netif->state; while (1) { FD_ZERO(&fdset); FD_SET(unixif->fd, &fdset); - + if (select(unixif->fd + 1, &fdset, NULL, NULL, NULL) > 0) { sys_sem_signal(&unixif->sem); } @@ -288,9 +288,11 @@ unixif_output(struct netif *netif, struct pbuf *p, ip_addr_t *ipaddr) { struct unixif *unixif; struct unixif_buf *buf; - unixif = netif->state; + LWIP_UNUSED_ARG(ipaddr); - buf = malloc(sizeof(struct unixif_buf)); + unixif = (struct unixif *)netif->state; + + buf = (struct unixif_buf *)malloc(sizeof(struct unixif_buf)); buf->p = p; buf->len = p->len; buf->tot_len = p->tot_len; @@ -303,13 +305,13 @@ unixif_output(struct netif *netif, struct pbuf *p, ip_addr_t *ipaddr) netif); LWIP_DEBUGF(UNIXIF_DEBUG, ("unixif_output: first on list\n")); - + } else { pbuf_ref(p); if (list_push(unixif->q, buf) == 0) { #ifdef UNIXIF_DROP_FIRST struct unixif_buf *buf2; - + buf2 = list_pop(unixif->q); pbuf_free(buf2->p); free(buf2); @@ -317,9 +319,9 @@ unixif_output(struct netif *netif, struct pbuf *p, ip_addr_t *ipaddr) #else free(buf); pbuf_free(p); - + LWIP_DEBUGF(UNIXIF_DEBUG, ("unixif_output: drop\n")); - + #endif /* UNIXIF_DROP_FIRST */ LINK_STATS_INC(link.drop); @@ -333,8 +335,8 @@ unixif_output(struct netif *netif, struct pbuf *p, ip_addr_t *ipaddr) /*-----------------------------------------------------------------------------------*/ static void unixif_output_timeout(void *arg) -{ - struct pbuf *p, *q; +{ + struct pbuf *p, *q; int i, j, len; unsigned short plen, ptot_len; struct unixif_buf *buf; @@ -343,33 +345,33 @@ unixif_output_timeout(void *arg) struct unixif *unixif; char *data; - netif = arg; - unixif = netif->state; + netif = (struct netif *)arg; + unixif = (struct unixif *)netif->state; LWIP_DEBUGF(UNIXIF_DEBUG, ("unixif_output_timeout\n")); /* buf = unixif->q[0]; unixif->q[0] = unixif->q[1]; unixif->q[1] = NULL;*/ - buf = list_pop(unixif->q); - + buf = (struct unixif_buf *)list_pop(unixif->q); + p = buf->p; plen = p->len; ptot_len = p->tot_len; payload = p->payload; - + p->len = buf->len; p->tot_len = buf->tot_len; p->payload = buf->payload; - - + + if (p->tot_len == 0) { LWIP_DEBUGF(UNIXIF_DEBUG, ("p->len!\n")); abort(); } - data = malloc(p->tot_len); + data = (char *)malloc(p->tot_len); i = 0; for(q = p; q != NULL; q = q->next) { @@ -381,13 +383,13 @@ unixif_output_timeout(void *arg) LWIP_DEBUGF(UNIXIF_DEBUG, ("unixif_output: sending %d (%d) bytes\n", p->len, p->tot_len)); - + len = p->tot_len; if (write(unixif->fd, &len, sizeof(int)) == -1) { perror("unixif_output: write"); abort(); } - + if (write(unixif->fd, data, p->tot_len) == -1) { perror("unixif_output: write"); abort(); @@ -402,7 +404,7 @@ unixif_output_timeout(void *arg) p->payload = payload; pbuf_free(p); - + /* if (unixif->q[0] != NULL) { sys_timeout(unixif->q[0]->tot_len * 8000 / UNIXIF_BPS, unixif_output_timeout, netif); @@ -429,10 +431,11 @@ unixif_init_server(struct netif *netif) abort(); } LWIP_DEBUGF(UNIXIF_DEBUG, ("unixif_server: fd %d\n", fd)); - - unixif = malloc(sizeof(struct unixif)); - if (!unixif) - return ERR_MEM; + + unixif = (struct unixif *)malloc(sizeof(struct unixif)); + if (!unixif) { + return ERR_MEM; + } netif->state = unixif; netif->name[0] = 'u'; netif->name[1] = 'n'; @@ -442,11 +445,11 @@ unixif_init_server(struct netif *netif) printf("Now run ./simnode.\n"); len = sizeof(addr); fd2 = accept(fd, (struct sockaddr *)&addr, &len); - + if (fd2 == -1) { perror("unixif_accept"); abort(); - } + } LWIP_DEBUGF(UNIXIF_DEBUG, ("unixif_accept: %d\n", fd2)); @@ -463,14 +466,15 @@ err_t unixif_init_client(struct netif *netif) { struct unixif *unixif; - unixif = malloc(sizeof(struct unixif)); - if (!unixif) - return ERR_MEM; + unixif = (struct unixif *)malloc(sizeof(struct unixif)); + if (!unixif) { + return ERR_MEM; + } netif->state = unixif; netif->name[0] = 'u'; netif->name[1] = 'n'; netif->output = unixif_output; - + unixif->fd = unix_socket_client("/tmp/unixif"); if (unixif->fd == -1) { perror("unixif_init"); diff --git a/ports/unix/proj/unixsim/Makefile b/ports/unix/proj/unixsim/Makefile index 12ddcc6..3f12fbc 100644 --- a/ports/unix/proj/unixsim/Makefile +++ b/ports/unix/proj/unixsim/Makefile @@ -35,7 +35,16 @@ CC=gcc #To compile for linux: make ARCH=linux #To compile for cygwin: make ARCH=cygwin ARCH=unix -CFLAGS=-g -Wall -D$(ARCH) -DIPv4 -DLWIP_DEBUG -pedantic -Werror +CFLAGS=-g -Wall -D$(ARCH) -DIPv4 -DLWIP_DEBUG -pedantic -Werror \ + -Wparentheses -Wsequence-point -Wswitch-default \ + -Wextra -Wundef -Wshadow -Wpointer-arith -Wbad-function-cast \ + -Wc++-compat -Wwrite-strings -Wold-style-definition \ + -Wmissing-prototypes -Wredundant-decls -Wnested-externs +# not used for now but interesting: +# -Wpacked +# -Wunreachable-code +# -ansi +# -std=c89 LDFLAGS=-lpthread -lutil CONTRIBDIR=../../../.. LWIPARCH=$(CONTRIBDIR)/ports/unix diff --git a/ports/unix/proj/unixsim/simhost.c b/ports/unix/proj/unixsim/simhost.c index 2a90b13..4b96ab9 100644 --- a/ports/unix/proj/unixsim/simhost.c +++ b/ports/unix/proj/unixsim/simhost.c @@ -109,10 +109,10 @@ static struct option longopts[] = { static void init_netifs(void); -void usage(void) +static void usage(void) { unsigned char i; - + printf("options:\n"); for (i = 0; i < NUM_OPTS; i++) { printf("-%c --%s\n",longopts[i].val, longopts[i].name); @@ -122,6 +122,7 @@ void usage(void) static void tcp_debug_timeout(void *data) { + LWIP_UNUSED_ARG(data); #if TCP_DEBUG tcp_debug_print_pcbs(); #endif /* TCP_DEBUG */ @@ -132,7 +133,7 @@ static void tcpip_init_done(void *arg) { sys_sem_t *sem; - sem = arg; + sem = (sys_sem_t *)arg; init_netifs(); @@ -260,7 +261,7 @@ ping_send(int s, ip_addr_t *addr) struct icmp_echo_hdr *iecho; struct sockaddr_in to; - if (!(iecho = malloc(sizeof(struct icmp_echo_hdr)))) + if (!(iecho = (struct icmp_echo_hdr *)malloc(sizeof(struct icmp_echo_hdr)))) return; ICMPH_TYPE_SET(iecho,ICMP_ECHO); @@ -285,6 +286,7 @@ ping_recv(int s, ip_addr_t *addr) socklen_t fromlen; int len; struct sockaddr_in from; + LWIP_UNUSED_ARG(addr); len = lwip_recvfrom(s, buf,sizeof(buf),0,(struct sockaddr*)&from,&fromlen); @@ -295,6 +297,7 @@ static void ping_thread(void *arg) { int s; + LWIP_UNUSED_ARG(arg); if ((s = lwip_socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP)) < 0) { return; @@ -379,6 +382,7 @@ main_thread(void *arg) #if PPP_SUPPORT sio_fd_t ppp_sio; #endif + LWIP_UNUSED_ARG(arg); netif_init(); diff --git a/ports/unix/proj/unixsim/simnode.c b/ports/unix/proj/unixsim/simnode.c index 6d9e8cf..538f892 100644 --- a/ports/unix/proj/unixsim/simnode.c +++ b/ports/unix/proj/unixsim/simnode.c @@ -67,6 +67,7 @@ unsigned char debug_flags; static void tcp_timeout(void *data) { + LWIP_UNUSED_ARG(data); #if TCP_DEBUG tcp_debug_print_pcbs(); #endif /* TCP_DEBUG */ @@ -80,7 +81,7 @@ tcpip_init_done(void *arg) { ip_addr_t ipaddr, netmask, gw; sys_sem_t *sem; - sem = arg; + sem = (sys_sem_t *)arg; IP4_ADDR(&gw, 192,168,1,1); IP4_ADDR(&ipaddr, 192,168,1,2); @@ -108,6 +109,7 @@ static void main_thread(void *arg) { sys_sem_t sem; + LWIP_UNUSED_ARG(arg); if(sys_sem_new(&sem, 0) != ERR_OK) { LWIP_ASSERT("Failed to create semaphore", 0); @@ -125,7 +127,7 @@ main_thread(void *arg) } /*-----------------------------------------------------------------------------------*/ int -main(int argc, char **argv) +main(void) { #ifdef PERF perf_init("/tmp/client.perf"); diff --git a/ports/unix/proj/unixsim/simrouter.c b/ports/unix/proj/unixsim/simrouter.c index 4ae58f1..ec9606b 100644 --- a/ports/unix/proj/unixsim/simrouter.c +++ b/ports/unix/proj/unixsim/simrouter.c @@ -69,6 +69,7 @@ unsigned char debug_flags; static void tcp_timeout(void *data) { + LWIP_UNUSED_ARG(data); #if TCP_DEBUG tcp_debug_print_pcbs(); #endif /* TCP_DEBUG */ @@ -84,7 +85,7 @@ tcpip_init_done(void *arg) { ip_addr_t ipaddr, netmask, gw; sys_sem_t *sem; - sem = arg; + sem = (sys_sem_t *)arg; IP4_ADDR(&gw, 192,168,0,1); IP4_ADDR(&ipaddr, 192,168,0,2); @@ -123,6 +124,7 @@ static void main_thread(void *arg) { sys_sem_t sem; + LWIP_UNUSED_ARG(arg); if(sys_sem_new(&sem, 0) != ERR_OK) { LWIP_ASSERT("Failed to create semaphore", 0); @@ -139,7 +141,7 @@ main_thread(void *arg) } /*-----------------------------------------------------------------------------------*/ int -main(int argc, char **argv) +main(void) { #ifdef PERF perf_init("/tmp/client.perf"); diff --git a/ports/unix/sys_arch.c b/ports/unix/sys_arch.c index de4a582..fb16077 100644 --- a/ports/unix/sys_arch.c +++ b/ports/unix/sys_arch.c @@ -110,9 +110,9 @@ static struct sys_thread * introduce_thread(pthread_t id) { struct sys_thread *thread; - - thread = malloc(sizeof(struct sys_thread)); - + + thread = (struct sys_thread *)malloc(sizeof(struct sys_thread)); + if (thread != NULL) { pthread_mutex_lock(&threads_mutex); thread->next = threads; @@ -120,12 +120,12 @@ introduce_thread(pthread_t id) threads = thread; pthread_mutex_unlock(&threads_mutex); } - + return thread; } /*-----------------------------------------------------------------------------------*/ sys_thread_t -sys_thread_new(char *name, void (* function)(void *arg), void *arg, int stacksize, int prio) +sys_thread_new(const char *name, lwip_thread_fn function, void *arg, int stacksize, int prio) { int code; pthread_t tmp; @@ -157,8 +157,8 @@ sys_mbox_new(struct sys_mbox **mb, int size) { struct sys_mbox *mbox; LWIP_UNUSED_ARG(size); - - mbox = malloc(sizeof(struct sys_mbox)); + + mbox = (struct sys_mbox *)malloc(sizeof(struct sys_mbox)); if (mbox == NULL) { return ERR_MEM; } @@ -197,27 +197,27 @@ sys_mbox_trypost(struct sys_mbox **mb, void *msg) struct sys_mbox *mbox; LWIP_ASSERT("invalid mbox", (mb != NULL) && (*mb != NULL)); mbox = *mb; - + sys_arch_sem_wait(&mbox->mutex, 0); - + LWIP_DEBUGF(SYS_DEBUG, ("sys_mbox_trypost: mbox %p msg %p\n", (void *)mbox, (void *)msg)); - + if ((mbox->last + 1) >= (mbox->first + SYS_MBOX_SIZE)) { sys_sem_signal(&mbox->mutex); return ERR_MEM; } mbox->msgs[mbox->last % SYS_MBOX_SIZE] = msg; - + if (mbox->last == mbox->first) { first = 1; } else { first = 0; } - + mbox->last++; - + if (first) { sys_sem_signal(&mbox->not_empty); } @@ -236,9 +236,9 @@ sys_mbox_post(struct sys_mbox **mb, void *msg) mbox = *mb; sys_arch_sem_wait(&mbox->mutex, 0); - + LWIP_DEBUGF(SYS_DEBUG, ("sys_mbox_post: mbox %p msg %p\n", (void *)mbox, (void *)msg)); - + while ((mbox->last + 1) >= (mbox->first + SYS_MBOX_SIZE)) { mbox->wait_send++; sys_sem_signal(&mbox->mutex); @@ -246,17 +246,17 @@ sys_mbox_post(struct sys_mbox **mb, void *msg) sys_arch_sem_wait(&mbox->mutex, 0); mbox->wait_send--; } - + mbox->msgs[mbox->last % SYS_MBOX_SIZE] = msg; - + if (mbox->last == mbox->first) { first = 1; } else { first = 0; } - + mbox->last++; - + if (first) { sys_sem_signal(&mbox->not_empty); } @@ -287,7 +287,7 @@ sys_arch_mbox_tryfetch(struct sys_mbox **mb, void **msg) } mbox->first++; - + if (mbox->wait_send) { sys_sem_signal(&mbox->not_full); } @@ -311,19 +311,19 @@ sys_arch_mbox_fetch(struct sys_mbox **mb, void **msg, u32_t timeout) while (mbox->first == mbox->last) { sys_sem_signal(&mbox->mutex); - + /* We block while waiting for a mail to arrive in the mailbox. We must be prepared to timeout. */ if (timeout != 0) { time_needed = sys_arch_sem_wait(&mbox->not_empty, timeout); - + if (time_needed == SYS_ARCH_TIMEOUT) { return SYS_ARCH_TIMEOUT; } } else { sys_arch_sem_wait(&mbox->not_empty, 0); } - + sys_arch_sem_wait(&mbox->mutex, 0); } @@ -336,7 +336,7 @@ sys_arch_mbox_fetch(struct sys_mbox **mb, void **msg, u32_t timeout) } mbox->first++; - + if (mbox->wait_send) { sys_sem_signal(&mbox->not_full); } @@ -350,8 +350,8 @@ static struct sys_sem * sys_sem_new_internal(u8_t count) { struct sys_sem *sem; - - sem = malloc(sizeof(struct sys_sem)); + + sem = (struct sys_sem *)malloc(sizeof(struct sys_sem)); if (sem != NULL) { sem->c = count; pthread_cond_init(&(sem->cond), NULL); @@ -378,12 +378,11 @@ cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex, u32_t timeout) unsigned long sec, usec; struct timeval rtime1, rtime2; struct timespec ts; - struct timezone tz; int retval; - + if (timeout > 0) { /* Get a timestamp and add the timeout value. */ - gettimeofday(&rtime1, &tz); + gettimeofday(&rtime1, NULL); sec = rtime1.tv_sec; usec = rtime1.tv_usec; usec += timeout % 1000 * 1000; @@ -391,21 +390,21 @@ cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex, u32_t timeout) usec = usec % 1000000; ts.tv_nsec = usec * 1000; ts.tv_sec = sec; - + retval = pthread_cond_timedwait(cond, mutex, &ts); - + if (retval == ETIMEDOUT) { return SYS_ARCH_TIMEOUT; } else { /* Calculate for how long we waited for the cond. */ - gettimeofday(&rtime2, &tz); + gettimeofday(&rtime2, NULL); tdiff = (rtime2.tv_sec - rtime1.tv_sec) * 1000 + (rtime2.tv_usec - rtime1.tv_usec) / 1000; - + if (tdiff <= 0) { return 0; } - + return tdiff; } } else { @@ -426,7 +425,7 @@ sys_arch_sem_wait(struct sys_sem **s, u32_t timeout) while (sem->c <= 0) { if (timeout > 0) { time_needed = cond_wait(&(sem->cond), &(sem->mutex), timeout); - + if (time_needed == SYS_ARCH_TIMEOUT) { pthread_mutex_unlock(&(sem->mutex)); return SYS_ARCH_TIMEOUT; @@ -478,26 +477,24 @@ sys_sem_free(struct sys_sem **sem) } #endif /* !NO_SYS */ /*-----------------------------------------------------------------------------------*/ -u32_t +u32_t sys_now(void) { struct timeval tv; - struct timezone tz; u32_t sec, usec, msec; - gettimeofday(&tv, &tz); - + gettimeofday(&tv, NULL); + sec = (u32_t)(tv.tv_sec - starttime.tv_sec); usec = (u32_t)(tv.tv_usec - starttime.tv_usec); msec = sec * 1000 + usec / 1000; - + return msec; } /*-----------------------------------------------------------------------------------*/ void -sys_init() +sys_init(void) { - struct timezone tz; - gettimeofday(&starttime, &tz); + gettimeofday(&starttime, NULL); } /*-----------------------------------------------------------------------------------*/ #if SYS_LIGHTWEIGHT_PROT