| 1 |
606 |
jeremybenn |
/*
|
| 2 |
|
|
* Copyright (c) 2001-2004 Swedish Institute of Computer Science.
|
| 3 |
|
|
* All rights reserved.
|
| 4 |
|
|
*
|
| 5 |
|
|
* Redistribution and use in source and binary forms, with or without modification,
|
| 6 |
|
|
* are permitted provided that the following conditions are met:
|
| 7 |
|
|
*
|
| 8 |
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
| 9 |
|
|
* this list of conditions and the following disclaimer.
|
| 10 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
| 11 |
|
|
* this list of conditions and the following disclaimer in the documentation
|
| 12 |
|
|
* and/or other materials provided with the distribution.
|
| 13 |
|
|
* 3. The name of the author may not be used to endorse or promote products
|
| 14 |
|
|
* derived from this software without specific prior written permission.
|
| 15 |
|
|
*
|
| 16 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
| 17 |
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
| 18 |
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
| 19 |
|
|
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
| 20 |
|
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
| 21 |
|
|
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
| 22 |
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
| 23 |
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
| 24 |
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
| 25 |
|
|
* OF SUCH DAMAGE.
|
| 26 |
|
|
*
|
| 27 |
|
|
* This file is part of the lwIP TCP/IP stack.
|
| 28 |
|
|
*
|
| 29 |
|
|
* Author: Adam Dunkels <adam@sics.se>
|
| 30 |
|
|
*
|
| 31 |
|
|
*/
|
| 32 |
|
|
#ifndef __LWIP_API_H__
|
| 33 |
|
|
#define __LWIP_API_H__
|
| 34 |
|
|
|
| 35 |
|
|
#include "lwip/opt.h"
|
| 36 |
|
|
|
| 37 |
|
|
#if LWIP_NETCONN /* don't build if not configured for use in lwipopts.h */
|
| 38 |
|
|
|
| 39 |
|
|
#include "lwip/netbuf.h"
|
| 40 |
|
|
#include "lwip/sys.h"
|
| 41 |
|
|
#include "lwip/ip_addr.h"
|
| 42 |
|
|
#include "lwip/err.h"
|
| 43 |
|
|
|
| 44 |
|
|
#ifdef __cplusplus
|
| 45 |
|
|
extern "C" {
|
| 46 |
|
|
#endif
|
| 47 |
|
|
|
| 48 |
|
|
/* Throughout this file, IP addresses and port numbers are expected to be in
|
| 49 |
|
|
* the same byte order as in the corresponding pcb.
|
| 50 |
|
|
*/
|
| 51 |
|
|
|
| 52 |
|
|
/* Flags for netconn_write */
|
| 53 |
|
|
#define NETCONN_NOFLAG 0x00
|
| 54 |
|
|
#define NETCONN_NOCOPY 0x00 /* Only for source code compatibility */
|
| 55 |
|
|
#define NETCONN_COPY 0x01
|
| 56 |
|
|
#define NETCONN_MORE 0x02
|
| 57 |
|
|
|
| 58 |
|
|
/* Helpers to process several netconn_types by the same code */
|
| 59 |
|
|
#define NETCONNTYPE_GROUP(t) (t&0xF0)
|
| 60 |
|
|
#define NETCONNTYPE_DATAGRAM(t) (t&0xE0)
|
| 61 |
|
|
|
| 62 |
|
|
enum netconn_type {
|
| 63 |
|
|
NETCONN_INVALID = 0,
|
| 64 |
|
|
/* NETCONN_TCP Group */
|
| 65 |
|
|
NETCONN_TCP = 0x10,
|
| 66 |
|
|
/* NETCONN_UDP Group */
|
| 67 |
|
|
NETCONN_UDP = 0x20,
|
| 68 |
|
|
NETCONN_UDPLITE = 0x21,
|
| 69 |
|
|
NETCONN_UDPNOCHKSUM= 0x22,
|
| 70 |
|
|
/* NETCONN_RAW Group */
|
| 71 |
|
|
NETCONN_RAW = 0x40
|
| 72 |
|
|
};
|
| 73 |
|
|
|
| 74 |
|
|
enum netconn_state {
|
| 75 |
|
|
NETCONN_NONE,
|
| 76 |
|
|
NETCONN_WRITE,
|
| 77 |
|
|
NETCONN_LISTEN,
|
| 78 |
|
|
NETCONN_CONNECT,
|
| 79 |
|
|
NETCONN_CLOSE
|
| 80 |
|
|
};
|
| 81 |
|
|
|
| 82 |
|
|
enum netconn_evt {
|
| 83 |
|
|
NETCONN_EVT_RCVPLUS,
|
| 84 |
|
|
NETCONN_EVT_RCVMINUS,
|
| 85 |
|
|
NETCONN_EVT_SENDPLUS,
|
| 86 |
|
|
NETCONN_EVT_SENDMINUS
|
| 87 |
|
|
};
|
| 88 |
|
|
|
| 89 |
|
|
#if LWIP_IGMP
|
| 90 |
|
|
enum netconn_igmp {
|
| 91 |
|
|
NETCONN_JOIN,
|
| 92 |
|
|
NETCONN_LEAVE
|
| 93 |
|
|
};
|
| 94 |
|
|
#endif /* LWIP_IGMP */
|
| 95 |
|
|
|
| 96 |
|
|
/* forward-declare some structs to avoid to include their headers */
|
| 97 |
|
|
struct ip_pcb;
|
| 98 |
|
|
struct tcp_pcb;
|
| 99 |
|
|
struct udp_pcb;
|
| 100 |
|
|
struct raw_pcb;
|
| 101 |
|
|
struct netconn;
|
| 102 |
|
|
|
| 103 |
|
|
/** A callback prototype to inform about events for a netconn */
|
| 104 |
|
|
typedef void (* netconn_callback)(struct netconn *, enum netconn_evt, u16_t len);
|
| 105 |
|
|
|
| 106 |
|
|
/** A netconn descriptor */
|
| 107 |
|
|
struct netconn {
|
| 108 |
|
|
/** type of the netconn (TCP, UDP or RAW) */
|
| 109 |
|
|
enum netconn_type type;
|
| 110 |
|
|
/** current state of the netconn */
|
| 111 |
|
|
enum netconn_state state;
|
| 112 |
|
|
/** the lwIP internal protocol control block */
|
| 113 |
|
|
union {
|
| 114 |
|
|
struct ip_pcb *ip;
|
| 115 |
|
|
struct tcp_pcb *tcp;
|
| 116 |
|
|
struct udp_pcb *udp;
|
| 117 |
|
|
struct raw_pcb *raw;
|
| 118 |
|
|
} pcb;
|
| 119 |
|
|
/** the last error this netconn had */
|
| 120 |
|
|
err_t err;
|
| 121 |
|
|
/** sem that is used to synchroneously execute functions in the core context */
|
| 122 |
|
|
sys_sem_t op_completed;
|
| 123 |
|
|
/** mbox where received packets are stored until they are fetched
|
| 124 |
|
|
by the netconn application thread (can grow quite big) */
|
| 125 |
|
|
sys_mbox_t recvmbox;
|
| 126 |
|
|
/** mbox where new connections are stored until processed
|
| 127 |
|
|
by the application thread */
|
| 128 |
|
|
sys_mbox_t acceptmbox;
|
| 129 |
|
|
/** only used for socket layer */
|
| 130 |
|
|
int socket;
|
| 131 |
|
|
#if LWIP_SO_RCVTIMEO
|
| 132 |
|
|
/** timeout to wait for new data to be received
|
| 133 |
|
|
(or connections to arrive for listening netconns) */
|
| 134 |
|
|
int recv_timeout;
|
| 135 |
|
|
#endif /* LWIP_SO_RCVTIMEO */
|
| 136 |
|
|
#if LWIP_SO_RCVBUF
|
| 137 |
|
|
/** maximum amount of bytes queued in recvmbox */
|
| 138 |
|
|
int recv_bufsize;
|
| 139 |
|
|
#endif /* LWIP_SO_RCVBUF */
|
| 140 |
|
|
u16_t recv_avail;
|
| 141 |
|
|
/** TCP: when data passed to netconn_write doesn't fit into the send buffer,
|
| 142 |
|
|
this temporarily stores the message. */
|
| 143 |
|
|
struct api_msg_msg *write_msg;
|
| 144 |
|
|
/** TCP: when data passed to netconn_write doesn't fit into the send buffer,
|
| 145 |
|
|
this temporarily stores how much is already sent. */
|
| 146 |
|
|
int write_offset;
|
| 147 |
|
|
#if LWIP_TCPIP_CORE_LOCKING
|
| 148 |
|
|
/** TCP: when data passed to netconn_write doesn't fit into the send buffer,
|
| 149 |
|
|
this temporarily stores whether to wake up the original application task
|
| 150 |
|
|
if data couldn't be sent in the first try. */
|
| 151 |
|
|
u8_t write_delayed;
|
| 152 |
|
|
#endif /* LWIP_TCPIP_CORE_LOCKING */
|
| 153 |
|
|
/** A callback function that is informed about events for this netconn */
|
| 154 |
|
|
netconn_callback callback;
|
| 155 |
|
|
};
|
| 156 |
|
|
|
| 157 |
|
|
/* Register an Network connection event */
|
| 158 |
|
|
#define API_EVENT(c,e,l) if (c->callback) { \
|
| 159 |
|
|
(*c->callback)(c, e, l); \
|
| 160 |
|
|
}
|
| 161 |
|
|
|
| 162 |
|
|
/* Network connection functions: */
|
| 163 |
|
|
#define netconn_new(t) netconn_new_with_proto_and_callback(t, 0, NULL)
|
| 164 |
|
|
#define netconn_new_with_callback(t, c) netconn_new_with_proto_and_callback(t, 0, c)
|
| 165 |
|
|
struct
|
| 166 |
|
|
netconn *netconn_new_with_proto_and_callback(enum netconn_type t, u8_t proto,
|
| 167 |
|
|
netconn_callback callback);
|
| 168 |
|
|
err_t netconn_delete (struct netconn *conn);
|
| 169 |
|
|
enum netconn_type netconn_type (struct netconn *conn);
|
| 170 |
|
|
|
| 171 |
|
|
err_t netconn_getaddr (struct netconn *conn,
|
| 172 |
|
|
struct ip_addr *addr,
|
| 173 |
|
|
u16_t *port,
|
| 174 |
|
|
u8_t local);
|
| 175 |
|
|
#define netconn_peer(c,i,p) netconn_getaddr(c,i,p,0)
|
| 176 |
|
|
#define netconn_addr(c,i,p) netconn_getaddr(c,i,p,1)
|
| 177 |
|
|
|
| 178 |
|
|
err_t netconn_bind (struct netconn *conn,
|
| 179 |
|
|
struct ip_addr *addr,
|
| 180 |
|
|
u16_t port);
|
| 181 |
|
|
err_t netconn_connect (struct netconn *conn,
|
| 182 |
|
|
struct ip_addr *addr,
|
| 183 |
|
|
u16_t port);
|
| 184 |
|
|
err_t netconn_disconnect (struct netconn *conn);
|
| 185 |
|
|
err_t netconn_listen_with_backlog(struct netconn *conn, u8_t backlog);
|
| 186 |
|
|
#define netconn_listen(conn) netconn_listen_with_backlog(conn, TCP_DEFAULT_LISTEN_BACKLOG)
|
| 187 |
|
|
struct netconn * netconn_accept (struct netconn *conn);
|
| 188 |
|
|
struct netbuf * netconn_recv (struct netconn *conn);
|
| 189 |
|
|
err_t netconn_sendto (struct netconn *conn,
|
| 190 |
|
|
struct netbuf *buf, struct ip_addr *addr, u16_t port);
|
| 191 |
|
|
err_t netconn_send (struct netconn *conn,
|
| 192 |
|
|
struct netbuf *buf);
|
| 193 |
|
|
err_t netconn_write (struct netconn *conn,
|
| 194 |
|
|
const void *dataptr, int size,
|
| 195 |
|
|
u8_t apiflags);
|
| 196 |
|
|
err_t netconn_close (struct netconn *conn);
|
| 197 |
|
|
|
| 198 |
|
|
#if LWIP_IGMP
|
| 199 |
|
|
err_t netconn_join_leave_group (struct netconn *conn,
|
| 200 |
|
|
struct ip_addr *multiaddr,
|
| 201 |
|
|
struct ip_addr *interface,
|
| 202 |
|
|
enum netconn_igmp join_or_leave);
|
| 203 |
|
|
#endif /* LWIP_IGMP */
|
| 204 |
|
|
#if LWIP_DNS
|
| 205 |
|
|
err_t netconn_gethostbyname(const char *name, struct ip_addr *addr);
|
| 206 |
|
|
#endif /* LWIP_DNS */
|
| 207 |
|
|
|
| 208 |
|
|
#define netconn_err(conn) ((conn)->err)
|
| 209 |
|
|
#define netconn_recv_bufsize(conn) ((conn)->recv_bufsize)
|
| 210 |
|
|
|
| 211 |
|
|
#ifdef __cplusplus
|
| 212 |
|
|
}
|
| 213 |
|
|
#endif
|
| 214 |
|
|
|
| 215 |
|
|
#endif /* LWIP_NETCONN */
|
| 216 |
|
|
|
| 217 |
|
|
#endif /* __LWIP_API_H__ */
|