From 953d783a3ef306093eb95cd47dc0e43252979f2e Mon Sep 17 00:00:00 2001 From: fbernon Date: Wed, 23 May 2007 17:46:53 +0000 Subject: [PATCH] api.h, api_lib.c, api_msg.c, sockets.c: group the different NETCONN_UDPxxx code in only one part... --- CHANGELOG | 4 +++ src/api/api_lib.c | 17 +++++-------- src/api/api_msg.c | 58 ++++++++---------------------------------- src/api/sockets.c | 4 +-- src/include/lwip/api.h | 17 +++++++++---- 5 files changed, 34 insertions(+), 66 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 3fece96c..9408a17e 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -19,6 +19,10 @@ HISTORY ++ New features: + 2007-05-23 Frédéric Bernon + * api.h, api_lib.c, api_msg.c, sockets.c: group the different NETCONN_UDPxxx + code in only one part... + 2007-05-18 Simon Goldschmidt * opt.h, memp.h, memp.c: Added option MEMP_OVERFLOW_CHECK to check for memp elements to overflow. This is achieved by adding some bytes before and after diff --git a/src/api/api_lib.c b/src/api/api_lib.c index a1b8ee70..f7633ca1 100644 --- a/src/api/api_lib.c +++ b/src/api/api_lib.c @@ -272,7 +272,6 @@ netconn_delete(struct netconn *conn) sys_mbox_free(conn->recvmbox); conn->recvmbox = SYS_MBOX_NULL; } - /* Drain the acceptmbox. */ if (conn->acceptmbox != SYS_MBOX_NULL) { @@ -304,12 +303,10 @@ netconn_type(struct netconn *conn) err_t netconn_peer(struct netconn *conn, struct ip_addr *addr, u16_t *port) { - switch (conn->type) { + switch (NETCONNTYPE_GROUP(conn->type)) { case NETCONN_RAW: /* return an error as connecting is only a helper for upper layers */ return ERR_CONN; - case NETCONN_UDPLITE: - case NETCONN_UDPNOCHKSUM: case NETCONN_UDP: if (conn->pcb.udp == NULL || ((conn->pcb.udp->flags & UDP_FLAGS_CONNECTED) == 0)) @@ -330,13 +327,11 @@ netconn_peer(struct netconn *conn, struct ip_addr *addr, u16_t *port) err_t netconn_addr(struct netconn *conn, struct ip_addr **addr, u16_t *port) { - switch (conn->type) { + switch (NETCONNTYPE_GROUP(conn->type)) { case NETCONN_RAW: *addr = &(conn->pcb.raw->local_ip); *port = conn->pcb.raw->protocol; break; - case NETCONN_UDPLITE: - case NETCONN_UDPNOCHKSUM: case NETCONN_UDP: *addr = &(conn->pcb.udp->local_ip); *port = conn->pcb.udp->local_port; @@ -390,7 +385,7 @@ netconn_connect(struct netconn *conn, struct ip_addr *addr, u16_t port) } msg.function = do_connect; - msg.msg.conn = conn; + msg.msg.conn = conn; msg.msg.msg.bc.ipaddr = addr; msg.msg.msg.bc.port = port; tcpip_apimsg(&msg); @@ -407,7 +402,7 @@ netconn_disconnect(struct netconn *conn) } msg.function = do_disconnect; - msg.msg.conn = conn; + msg.msg.conn = conn; tcpip_apimsg(&msg); return conn->err; @@ -438,11 +433,11 @@ struct netconn * netconn_accept(struct netconn *conn) { struct netconn *newconn; - + if (conn == NULL) { return NULL; } - + sys_arch_mbox_fetch(conn->acceptmbox, (void *)&newconn, 0); /* Register event with callback */ if (conn->callback) diff --git a/src/api/api_msg.c b/src/api/api_msg.c index e1e08922..bc8c101a 100644 --- a/src/api/api_msg.c +++ b/src/api/api_msg.c @@ -290,7 +290,7 @@ pcb_new(struct api_msg_msg *msg) msg->conn->err = ERR_OK; /* Allocate a PCB for this connection */ - switch(msg->conn->type) { + switch(NETCONNTYPE_GROUP(msg->conn->type)) { #if LWIP_RAW case NETCONN_RAW: msg->conn->pcb.raw = raw_new(msg->msg.n.proto); @@ -302,30 +302,14 @@ pcb_new(struct api_msg_msg *msg) break; #endif /* LWIP_RAW */ #if LWIP_UDP - case NETCONN_UDPLITE: - msg->conn->pcb.udp = udp_new(); - if(msg->conn->pcb.udp == NULL) { - msg->conn->err = ERR_MEM; - break; - } - udp_setflags(msg->conn->pcb.udp, UDP_FLAGS_UDPLITE); - udp_recv(msg->conn->pcb.udp, recv_udp, msg->conn); - break; - case NETCONN_UDPNOCHKSUM: - msg->conn->pcb.udp = udp_new(); - if(msg->conn->pcb.udp == NULL) { - msg->conn->err = ERR_MEM; - break; - } - udp_setflags(msg->conn->pcb.udp, UDP_FLAGS_NOCHKSUM); - udp_recv(msg->conn->pcb.udp, recv_udp, msg->conn); - break; case NETCONN_UDP: msg->conn->pcb.udp = udp_new(); if(msg->conn->pcb.udp == NULL) { msg->conn->err = ERR_MEM; break; } + if (msg->conn->type==NETCONN_UDPLITE) udp_setflags(msg->conn->pcb.udp, UDP_FLAGS_UDPLITE); + if (msg->conn->type==NETCONN_UDPNOCHKSUM) udp_setflags(msg->conn->pcb.udp, UDP_FLAGS_NOCHKSUM); udp_recv(msg->conn->pcb.udp, recv_udp, msg->conn); break; #endif /* LWIP_UDP */ @@ -363,15 +347,13 @@ void do_delconn(struct api_msg_msg *msg) { if (msg->conn->pcb.tcp != NULL) { - switch (msg->conn->type) { + switch (NETCONNTYPE_GROUP(msg->conn->type)) { #if LWIP_RAW case NETCONN_RAW: raw_remove(msg->conn->pcb.raw); break; #endif /* LWIP_RAW */ #if LWIP_UDP - case NETCONN_UDPLITE: - case NETCONN_UDPNOCHKSUM: case NETCONN_UDP: msg->conn->pcb.udp->recv_arg = NULL; udp_remove(msg->conn->pcb.udp); @@ -419,15 +401,13 @@ do_bind(struct api_msg_msg *msg) } if (msg->conn->pcb.tcp != NULL) { - switch (msg->conn->type) { + switch (NETCONNTYPE_GROUP(msg->conn->type)) { #if LWIP_RAW case NETCONN_RAW: msg->conn->err = raw_bind(msg->conn->pcb.raw,msg->msg.bc.ipaddr); break; #endif /* LWIP_RAW */ #if LWIP_UDP - case NETCONN_UDPLITE: - case NETCONN_UDPNOCHKSUM: case NETCONN_UDP: msg->conn->err = udp_bind(msg->conn->pcb.udp, msg->msg.bc.ipaddr, msg->msg.bc.port); break; @@ -479,7 +459,7 @@ do_connect(struct api_msg_msg *msg) } } - switch (msg->conn->type) { + switch (NETCONNTYPE_GROUP(msg->conn->type)) { #if LWIP_RAW case NETCONN_RAW: msg->conn->err = raw_connect(msg->conn->pcb.raw, msg->msg.bc.ipaddr); @@ -487,8 +467,6 @@ do_connect(struct api_msg_msg *msg) break; #endif /* LWIP_RAW */ #if LWIP_UDP - case NETCONN_UDPLITE: - case NETCONN_UDPNOCHKSUM: case NETCONN_UDP: msg->conn->err = udp_connect(msg->conn->pcb.udp, msg->msg.bc.ipaddr, msg->msg.bc.port); sys_mbox_post(msg->conn->mbox, NULL); @@ -510,19 +488,11 @@ do_connect(struct api_msg_msg *msg) void do_disconnect(struct api_msg_msg *msg) { - switch (msg->conn->type) { #if LWIP_UDP - case NETCONN_UDPLITE: - case NETCONN_UDPNOCHKSUM: - case NETCONN_UDP: + if (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_UDP) { udp_disconnect(msg->conn->pcb.udp); - break; -#endif /* LWIP_UDP */ - case NETCONN_TCP: - case NETCONN_RAW: - /* nothing to do */ - break; } +#endif /* LWIP_UDP */ sys_mbox_post(msg->conn->mbox, NULL); } @@ -559,7 +529,7 @@ do_send(struct api_msg_msg *msg) { if (msg->conn->err == ERR_OK) { if (msg->conn->pcb.tcp != NULL) { - switch (msg->conn->type) { + switch (NETCONNTYPE_GROUP(msg->conn->type)) { #if LWIP_RAW case NETCONN_RAW: if (msg->msg.b->addr == NULL) { @@ -570,8 +540,6 @@ do_send(struct api_msg_msg *msg) break; #endif #if LWIP_UDP - case NETCONN_UDPLITE: - case NETCONN_UDPNOCHKSUM: case NETCONN_UDP: if (msg->msg.b->addr == NULL) { msg->conn->err = udp_send(msg->conn->pcb.udp, msg->msg.b->p); @@ -664,20 +632,16 @@ do_join_leave_group(struct api_msg_msg *msg) { if (msg->conn->err == ERR_OK) { if (msg->conn->pcb.tcp != NULL) { - switch (msg->conn->type) { + if (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_UDP) { #if LWIP_UDP - case NETCONN_UDPLITE: - case NETCONN_UDPNOCHKSUM: - case NETCONN_UDP: if (msg->msg.jl.join_or_leave == NETCONN_JOIN) { msg->conn->err = igmp_joingroup ( netif_default, msg->msg.jl.multiaddr); } else { msg->conn->err = igmp_leavegroup( netif_default, msg->msg.jl.multiaddr); } - break; #endif /* LWIP_UDP */ #if (LWIP_TCP || LWIP_RAW) - default: + } else { msg->conn->err = ERR_VAL; #endif /* (LWIP_TCP || LWIP_RAW) */ } diff --git a/src/api/sockets.c b/src/api/sockets.c index 4095c805..6e1aa75c 100644 --- a/src/api/sockets.c +++ b/src/api/sockets.c @@ -1081,7 +1081,7 @@ int lwip_getsockopt(int s, int level, int optname, void *optval, socklen_t *optl break; case SO_TYPE: - switch (sock->conn->type) { + switch (NETCONNTYPE_GROUP(sock->conn->type)) { case NETCONN_RAW: *(int*)optval = SOCK_RAW; break; @@ -1089,8 +1089,6 @@ int lwip_getsockopt(int s, int level, int optname, void *optval, socklen_t *optl *(int*)optval = SOCK_STREAM; break; case NETCONN_UDP: - case NETCONN_UDPLITE: - case NETCONN_UDPNOCHKSUM: *(int*)optval = SOCK_DGRAM; break; default: /* unrecognized socket type */ diff --git a/src/include/lwip/api.h b/src/include/lwip/api.h index 03685027..e8ece5b4 100644 --- a/src/include/lwip/api.h +++ b/src/include/lwip/api.h @@ -55,12 +55,19 @@ extern "C" { #define NETCONN_NOCOPY 0x00 #define NETCONN_COPY 0x01 +/* Helpers to process several netconn_types by the same code */ +#define NETCONNTYPE_GROUP(t) (t&0xF0) +#define NETCONNTYPE_DATAGRAM(t) (t&0xE0) + enum netconn_type { - NETCONN_TCP, - NETCONN_UDP, - NETCONN_UDPLITE, - NETCONN_UDPNOCHKSUM, - NETCONN_RAW + /* NETCONN_TCP Group */ + NETCONN_TCP = 0x10, + /* NETCONN_UDP Group */ + NETCONN_UDP = 0x20, + NETCONN_UDPLITE = 0x21, + NETCONN_UDPNOCHKSUM= 0x22, + /* NETCONN_RAW Group */ + NETCONN_RAW = 0x40 }; enum netconn_state {