mirror of
https://git.savannah.nongnu.org/git/lwip.git
synced 2026-05-20 23:26:56 +08:00
Changes from Patch #1871 (more Raw IP functionality)
This commit is contained in:
@@ -194,7 +194,8 @@ netbuf_fromport(struct netbuf *buf)
|
||||
}
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
struct
|
||||
netconn *netconn_new(enum netconn_type t)
|
||||
netconn *netconn_new_with_proto_and_callback(enum netconn_type t, u16_t proto,
|
||||
void (*callback)(struct netconn *, enum netconn_evt, u16_t len))
|
||||
{
|
||||
struct netconn *conn;
|
||||
struct api_msg *msg;
|
||||
@@ -217,7 +218,7 @@ netconn *netconn_new(enum netconn_type t)
|
||||
conn->sem = SYS_SEM_NULL;
|
||||
conn->state = NETCONN_NONE;
|
||||
conn->socket = 0;
|
||||
conn->callback = 0;
|
||||
conn->callback = callback;
|
||||
conn->recv_avail = 0;
|
||||
|
||||
if((msg = memp_malloc(MEMP_API_MSG)) == NULL) {
|
||||
@@ -226,6 +227,7 @@ netconn *netconn_new(enum netconn_type t)
|
||||
}
|
||||
|
||||
msg->type = API_MSG_NEWCONN;
|
||||
msg->msg.msg.bc.port = proto; /* misusing the port field */
|
||||
msg->msg.conn = conn;
|
||||
api_msg_post(msg);
|
||||
sys_mbox_fetch(conn->mbox, NULL);
|
||||
@@ -238,18 +240,19 @@ netconn *netconn_new(enum netconn_type t)
|
||||
|
||||
return conn;
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
struct
|
||||
netconn *netconn_new(enum netconn_type t)
|
||||
{
|
||||
return netconn_new_with_proto_and_callback(t,0,NULL);
|
||||
}
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
struct
|
||||
netconn *netconn_new_with_callback(enum netconn_type t,
|
||||
void (*callback)(struct netconn *, enum netconn_evt, u16_t len))
|
||||
{
|
||||
struct netconn *conn;
|
||||
|
||||
/* get a netconn and then initialize callback pointer and socket */
|
||||
conn = netconn_new(t);
|
||||
if (conn)
|
||||
conn->callback = callback;
|
||||
return conn;
|
||||
return netconn_new_with_proto_and_callback(t,0,callback);
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------------------------------*/
|
||||
@@ -318,6 +321,9 @@ netconn_peer(struct netconn *conn, struct ip_addr *addr,
|
||||
u16_t *port)
|
||||
{
|
||||
switch (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:
|
||||
@@ -342,6 +348,10 @@ netconn_addr(struct netconn *conn, struct ip_addr **addr,
|
||||
u16_t *port)
|
||||
{
|
||||
switch (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:
|
||||
|
||||
@@ -37,6 +37,37 @@
|
||||
#include "lwip/sys.h"
|
||||
#include "lwip/tcpip.h"
|
||||
|
||||
#if LWIP_RAW
|
||||
static int
|
||||
recv_raw(void *arg, struct raw_pcb *pcb, struct pbuf *p,
|
||||
struct ip_addr *addr)
|
||||
{
|
||||
struct netbuf *buf;
|
||||
struct netconn *conn;
|
||||
|
||||
conn = arg;
|
||||
if (!conn) return 0;
|
||||
|
||||
if (conn->recvmbox != SYS_MBOX_NULL) {
|
||||
if (!(buf = memp_malloc(MEMP_NETBUF))) {
|
||||
return 0;
|
||||
}
|
||||
pbuf_ref(p);
|
||||
buf->p = p;
|
||||
buf->ptr = p;
|
||||
buf->fromaddr = addr;
|
||||
buf->fromport = pcb->protocol;
|
||||
|
||||
conn->recv_avail += p->tot_len;
|
||||
/* Register event with callback */
|
||||
if (conn->callback)
|
||||
(*conn->callback)(conn, NETCONN_EVT_RCVPLUS, p->tot_len);
|
||||
sys_mbox_post(conn->recvmbox, buf);
|
||||
}
|
||||
|
||||
return 0; /* do not eat the packet */
|
||||
}
|
||||
#endif
|
||||
#if LWIP_UDP
|
||||
static void
|
||||
recv_udp(void *arg, struct udp_pcb *pcb, struct pbuf *p,
|
||||
@@ -250,6 +281,12 @@ do_newconn(struct api_msg_msg *msg)
|
||||
|
||||
/* Allocate a PCB for this connection */
|
||||
switch(msg->conn->type) {
|
||||
#if LWIP_RAW
|
||||
case NETCONN_RAW:
|
||||
msg->conn->pcb.raw = raw_new(msg->msg.bc.port); /* misusing the port field */
|
||||
raw_recv(msg->conn->pcb.raw, recv_raw, msg->conn);
|
||||
break;
|
||||
#endif
|
||||
#if LWIP_UDP
|
||||
case NETCONN_UDPLITE:
|
||||
msg->conn->pcb.udp = udp_new();
|
||||
@@ -300,6 +337,11 @@ do_delconn(struct api_msg_msg *msg)
|
||||
{
|
||||
if (msg->conn->pcb.tcp != NULL) {
|
||||
switch (msg->conn->type) {
|
||||
#if LWIP_RAW
|
||||
case NETCONN_RAW:
|
||||
raw_remove(msg->conn->pcb.raw);
|
||||
break;
|
||||
#endif
|
||||
#if LWIP_UDP
|
||||
case NETCONN_UDPLITE:
|
||||
/* FALLTHROUGH */
|
||||
@@ -348,6 +390,12 @@ do_bind(struct api_msg_msg *msg)
|
||||
{
|
||||
if (msg->conn->pcb.tcp == NULL) {
|
||||
switch (msg->conn->type) {
|
||||
#if LWIP_RAW
|
||||
case NETCONN_RAW:
|
||||
msg->conn->pcb.raw = raw_new(msg->msg.bc.port); /* misusing the port field as protocol */
|
||||
raw_recv(msg->conn->pcb.raw, recv_raw, msg->conn);
|
||||
break;
|
||||
#endif
|
||||
#if LWIP_UDP
|
||||
case NETCONN_UDPLITE:
|
||||
msg->conn->pcb.udp = udp_new();
|
||||
@@ -374,6 +422,11 @@ do_bind(struct api_msg_msg *msg)
|
||||
}
|
||||
}
|
||||
switch (msg->conn->type) {
|
||||
#if LWIP_RAW
|
||||
case NETCONN_RAW:
|
||||
msg->conn->err = raw_bind(msg->conn->pcb.raw,msg->msg.bc.ipaddr);
|
||||
break;
|
||||
#endif
|
||||
#if LWIP_UDP
|
||||
case NETCONN_UDPLITE:
|
||||
/* FALLTHROUGH */
|
||||
@@ -420,6 +473,12 @@ do_connect(struct api_msg_msg *msg)
|
||||
{
|
||||
if (msg->conn->pcb.tcp == NULL) {
|
||||
switch (msg->conn->type) {
|
||||
#if LWIP_RAW
|
||||
case NETCONN_RAW:
|
||||
msg->conn->pcb.raw = raw_new(msg->msg.bc.port); /* misusing the port field as protocol */
|
||||
raw_recv(msg->conn->pcb.raw, recv_raw, msg->conn);
|
||||
break;
|
||||
#endif
|
||||
#if LWIP_UDP
|
||||
case NETCONN_UDPLITE:
|
||||
msg->conn->pcb.udp = udp_new();
|
||||
@@ -465,6 +524,12 @@ do_connect(struct api_msg_msg *msg)
|
||||
}
|
||||
}
|
||||
switch (msg->conn->type) {
|
||||
#if LWIP_RAW
|
||||
case NETCONN_RAW:
|
||||
raw_connect(msg->conn->pcb.raw, msg->msg.bc.ipaddr);
|
||||
sys_mbox_post(msg->conn->mbox, NULL);
|
||||
break;
|
||||
#endif
|
||||
#if LWIP_UDP
|
||||
case NETCONN_UDPLITE:
|
||||
/* FALLTHROUGH */
|
||||
@@ -483,6 +548,7 @@ do_connect(struct api_msg_msg *msg)
|
||||
do_connected);
|
||||
/*tcp_output(msg->conn->pcb.tcp);*/
|
||||
#endif
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@@ -493,6 +559,11 @@ do_disconnect(struct api_msg_msg *msg)
|
||||
{
|
||||
|
||||
switch (msg->conn->type) {
|
||||
#if LWIP_RAW
|
||||
case NETCONN_RAW:
|
||||
/* Do nothing as connecting is only a helper for upper lwip layers */
|
||||
break;
|
||||
#endif
|
||||
#if LWIP_UDP
|
||||
case NETCONN_UDPLITE:
|
||||
/* FALLTHROUGH */
|
||||
@@ -514,6 +585,11 @@ do_listen(struct api_msg_msg *msg)
|
||||
{
|
||||
if (msg->conn->pcb.tcp != NULL) {
|
||||
switch (msg->conn->type) {
|
||||
#if LWIP_RAW
|
||||
case NETCONN_RAW:
|
||||
LWIP_DEBUGF(API_MSG_DEBUG, ("api_msg: listen RAW: cannot listen for RAW.\n"));
|
||||
break;
|
||||
#endif
|
||||
#if LWIP_UDP
|
||||
case NETCONN_UDPLITE:
|
||||
/* FALLTHROUGH */
|
||||
@@ -552,6 +628,11 @@ do_accept(struct api_msg_msg *msg)
|
||||
{
|
||||
if (msg->conn->pcb.tcp != NULL) {
|
||||
switch (msg->conn->type) {
|
||||
#if LWIP_RAW
|
||||
case NETCONN_RAW:
|
||||
LWIP_DEBUGF(API_MSG_DEBUG, ("api_msg: accept RAW: cannot accept for RAW.\n"));
|
||||
break;
|
||||
#endif
|
||||
#if LWIP_UDP
|
||||
case NETCONN_UDPLITE:
|
||||
/* FALLTHROUGH */
|
||||
@@ -572,6 +653,11 @@ do_send(struct api_msg_msg *msg)
|
||||
{
|
||||
if (msg->conn->pcb.tcp != NULL) {
|
||||
switch (msg->conn->type) {
|
||||
#if LWIP_RAW
|
||||
case NETCONN_RAW:
|
||||
raw_send(msg->conn->pcb.raw, msg->msg.p);
|
||||
break;
|
||||
#endif
|
||||
#if LWIP_UDP
|
||||
case NETCONN_UDPLITE:
|
||||
/* FALLTHROUGH */
|
||||
@@ -609,6 +695,11 @@ do_write(struct api_msg_msg *msg)
|
||||
#endif
|
||||
if (msg->conn->pcb.tcp != NULL) {
|
||||
switch (msg->conn->type) {
|
||||
#if LWIP_RAW
|
||||
case NETCONN_RAW:
|
||||
msg->conn->err = ERR_VAL;
|
||||
break;
|
||||
#endif
|
||||
#if LWIP_UDP
|
||||
case NETCONN_UDPLITE:
|
||||
/* FALLTHROUGH */
|
||||
@@ -653,6 +744,10 @@ do_close(struct api_msg_msg *msg)
|
||||
|
||||
if (msg->conn->pcb.tcp != NULL) {
|
||||
switch (msg->conn->type) {
|
||||
#if LWIP_RAW
|
||||
case NETCONN_RAW:
|
||||
break;
|
||||
#endif
|
||||
#if LWIP_UDP
|
||||
case NETCONN_UDPLITE:
|
||||
/* FALLTHROUGH */
|
||||
|
||||
@@ -479,6 +479,7 @@ lwip_send(int s, void *data, int size, unsigned int flags)
|
||||
}
|
||||
|
||||
switch (netconn_type(sock->conn)) {
|
||||
case NETCONN_RAW:
|
||||
case NETCONN_UDP:
|
||||
case NETCONN_UDPLITE:
|
||||
case NETCONN_UDPNOCHKSUM:
|
||||
@@ -567,6 +568,10 @@ lwip_socket(int domain, int type, int protocol)
|
||||
|
||||
/* create a netconn */
|
||||
switch (type) {
|
||||
case SOCK_RAW:
|
||||
conn = netconn_new_with_proto_and_callback(NETCONN_RAW, protocol, event_callback);
|
||||
LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_socket(%s, SOCK_RAW, %d) = ", domain == PF_INET ? "PF_INET" : "UNKNOWN", protocol));
|
||||
break;
|
||||
case SOCK_DGRAM:
|
||||
conn = netconn_new_with_callback(NETCONN_UDP, event_callback);
|
||||
LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_socket(%s, SOCK_DGRAM, %d) = ", domain == PF_INET ? "PF_INET" : "UNKNOWN", protocol));
|
||||
@@ -1095,6 +1100,9 @@ int lwip_getsockopt (int s, int level, int optname, void *optval, socklen_t *opt
|
||||
|
||||
case SO_TYPE:
|
||||
switch (sock->conn->type) {
|
||||
case NETCONN_RAW:
|
||||
*(int*)optval = SOCK_RAW;
|
||||
break;
|
||||
case NETCONN_TCP:
|
||||
*(int*)optval = SOCK_STREAM;
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user