Darran Shepherd
/
AutoIpNetStack
Net stack with AutoIP enabled
if/lwip/lwipNetUdpSocket.cpp@0:ac21159e27f4, 2010-07-02 (annotated)
- Committer:
- darran
- Date:
- Fri Jul 02 17:21:58 2010 +0000
- Revision:
- 0:ac21159e27f4
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
darran | 0:ac21159e27f4 | 1 | |
darran | 0:ac21159e27f4 | 2 | /* |
darran | 0:ac21159e27f4 | 3 | Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
darran | 0:ac21159e27f4 | 4 | |
darran | 0:ac21159e27f4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
darran | 0:ac21159e27f4 | 6 | of this software and associated documentation files (the "Software"), to deal |
darran | 0:ac21159e27f4 | 7 | in the Software without restriction, including without limitation the rights |
darran | 0:ac21159e27f4 | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
darran | 0:ac21159e27f4 | 9 | copies of the Software, and to permit persons to whom the Software is |
darran | 0:ac21159e27f4 | 10 | furnished to do so, subject to the following conditions: |
darran | 0:ac21159e27f4 | 11 | |
darran | 0:ac21159e27f4 | 12 | The above copyright notice and this permission notice shall be included in |
darran | 0:ac21159e27f4 | 13 | all copies or substantial portions of the Software. |
darran | 0:ac21159e27f4 | 14 | |
darran | 0:ac21159e27f4 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
darran | 0:ac21159e27f4 | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
darran | 0:ac21159e27f4 | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
darran | 0:ac21159e27f4 | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
darran | 0:ac21159e27f4 | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
darran | 0:ac21159e27f4 | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
darran | 0:ac21159e27f4 | 21 | THE SOFTWARE. |
darran | 0:ac21159e27f4 | 22 | */ |
darran | 0:ac21159e27f4 | 23 | |
darran | 0:ac21159e27f4 | 24 | #include "lwipNetUdpSocket.h" |
darran | 0:ac21159e27f4 | 25 | #include "lwip/udp.h" |
darran | 0:ac21159e27f4 | 26 | |
darran | 0:ac21159e27f4 | 27 | //#define __DEBUG |
darran | 0:ac21159e27f4 | 28 | #include "dbg/dbg.h" |
darran | 0:ac21159e27f4 | 29 | |
darran | 0:ac21159e27f4 | 30 | #include "netCfg.h" |
darran | 0:ac21159e27f4 | 31 | #if NET_LWIP_STACK |
darran | 0:ac21159e27f4 | 32 | |
darran | 0:ac21159e27f4 | 33 | LwipNetUdpSocket::LwipNetUdpSocket(udp_pcb* pPcb /*= NULL*/) : NetUdpSocket(), m_pPcb(pPcb), m_lInPkt() //Passes a pcb if already created (by an accept req for instance), in that case transfers ownership |
darran | 0:ac21159e27f4 | 34 | { |
darran | 0:ac21159e27f4 | 35 | DBG("\r\nNew LwipNetUdpSocket %p (pPCb=%p)\r\n", (void*)this, (void*) pPcb); |
darran | 0:ac21159e27f4 | 36 | if(!m_pPcb) |
darran | 0:ac21159e27f4 | 37 | m_pPcb = udp_new(); |
darran | 0:ac21159e27f4 | 38 | if(m_pPcb) |
darran | 0:ac21159e27f4 | 39 | { |
darran | 0:ac21159e27f4 | 40 | //Setup callback |
darran | 0:ac21159e27f4 | 41 | udp_recv( (udp_pcb*) m_pPcb, LwipNetUdpSocket::sRecvCb, (void*) this ); |
darran | 0:ac21159e27f4 | 42 | } |
darran | 0:ac21159e27f4 | 43 | } |
darran | 0:ac21159e27f4 | 44 | |
darran | 0:ac21159e27f4 | 45 | LwipNetUdpSocket::~LwipNetUdpSocket() |
darran | 0:ac21159e27f4 | 46 | { |
darran | 0:ac21159e27f4 | 47 | close(); |
darran | 0:ac21159e27f4 | 48 | } |
darran | 0:ac21159e27f4 | 49 | |
darran | 0:ac21159e27f4 | 50 | NetUdpSocketErr LwipNetUdpSocket::bind(const Host& me) |
darran | 0:ac21159e27f4 | 51 | { |
darran | 0:ac21159e27f4 | 52 | if(!m_pPcb) |
darran | 0:ac21159e27f4 | 53 | return NETUDPSOCKET_MEM; //NetUdpSocket was not properly initialised, should destroy it & retry |
darran | 0:ac21159e27f4 | 54 | |
darran | 0:ac21159e27f4 | 55 | err_t err = udp_bind( (udp_pcb*) m_pPcb, IP_ADDR_ANY, me.getPort()); //IP_ADDR_ANY : Bind the connection to all local addresses |
darran | 0:ac21159e27f4 | 56 | if(err) |
darran | 0:ac21159e27f4 | 57 | return NETUDPSOCKET_INUSE; |
darran | 0:ac21159e27f4 | 58 | |
darran | 0:ac21159e27f4 | 59 | //Setup callback |
darran | 0:ac21159e27f4 | 60 | udp_recv( (udp_pcb*) m_pPcb, LwipNetUdpSocket::sRecvCb, (void*) this ); |
darran | 0:ac21159e27f4 | 61 | |
darran | 0:ac21159e27f4 | 62 | return NETUDPSOCKET_OK; |
darran | 0:ac21159e27f4 | 63 | } |
darran | 0:ac21159e27f4 | 64 | |
darran | 0:ac21159e27f4 | 65 | #define MAX(a,b) ((a>b)?a:b) |
darran | 0:ac21159e27f4 | 66 | #define MIN(a,b) ((a<b)?a:b) |
darran | 0:ac21159e27f4 | 67 | |
darran | 0:ac21159e27f4 | 68 | int /*if < 0 : NetUdpSocketErr*/ LwipNetUdpSocket::sendto(const char* buf, int len, Host* pHost) |
darran | 0:ac21159e27f4 | 69 | { |
darran | 0:ac21159e27f4 | 70 | if( !m_pPcb ) //Pcb doesn't exist (anymore) |
darran | 0:ac21159e27f4 | 71 | return NETUDPSOCKET_MEM; |
darran | 0:ac21159e27f4 | 72 | pbuf* p = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_POOL); |
darran | 0:ac21159e27f4 | 73 | if( !p ) |
darran | 0:ac21159e27f4 | 74 | return NETUDPSOCKET_MEM; |
darran | 0:ac21159e27f4 | 75 | char* pBuf = (char*) buf; |
darran | 0:ac21159e27f4 | 76 | pbuf* q = p; |
darran | 0:ac21159e27f4 | 77 | do |
darran | 0:ac21159e27f4 | 78 | { |
darran | 0:ac21159e27f4 | 79 | memcpy (q->payload, (void*)pBuf, q->len); |
darran | 0:ac21159e27f4 | 80 | pBuf += q->len; |
darran | 0:ac21159e27f4 | 81 | q = q->next; |
darran | 0:ac21159e27f4 | 82 | } while(q != NULL); |
darran | 0:ac21159e27f4 | 83 | |
darran | 0:ac21159e27f4 | 84 | err_t err = udp_sendto( (udp_pcb*) m_pPcb, p, &(pHost->getIp().getStruct()), pHost->getPort() ); |
darran | 0:ac21159e27f4 | 85 | pbuf_free( p ); |
darran | 0:ac21159e27f4 | 86 | if(err) |
darran | 0:ac21159e27f4 | 87 | return NETUDPSOCKET_SETUP; //Connection problem |
darran | 0:ac21159e27f4 | 88 | DBG("\r\n%d bytes sent in UDP Socket.\r\n", len); |
darran | 0:ac21159e27f4 | 89 | return len; |
darran | 0:ac21159e27f4 | 90 | } |
darran | 0:ac21159e27f4 | 91 | |
darran | 0:ac21159e27f4 | 92 | int /*if < 0 : NetUdpSocketErr*/ LwipNetUdpSocket::recvfrom(char* buf, int len, Host* pHost) |
darran | 0:ac21159e27f4 | 93 | { |
darran | 0:ac21159e27f4 | 94 | if( !m_pPcb ) //Pcb doesn't exist (anymore) |
darran | 0:ac21159e27f4 | 95 | return NETUDPSOCKET_MEM; |
darran | 0:ac21159e27f4 | 96 | int inLen = 0; |
darran | 0:ac21159e27f4 | 97 | int cpyLen = 0; |
darran | 0:ac21159e27f4 | 98 | |
darran | 0:ac21159e27f4 | 99 | static int rmgLen = 0; |
darran | 0:ac21159e27f4 | 100 | //Contains the remaining len in this pbuf |
darran | 0:ac21159e27f4 | 101 | |
darran | 0:ac21159e27f4 | 102 | if( m_lInPkt.empty() ) |
darran | 0:ac21159e27f4 | 103 | return 0; |
darran | 0:ac21159e27f4 | 104 | |
darran | 0:ac21159e27f4 | 105 | pbuf* pBuf = (pbuf*) m_lInPkt.front().pBuf; |
darran | 0:ac21159e27f4 | 106 | |
darran | 0:ac21159e27f4 | 107 | if(pHost) |
darran | 0:ac21159e27f4 | 108 | *pHost = Host( IpAddr(&m_lInPkt.front().addr), m_lInPkt.front().port ); |
darran | 0:ac21159e27f4 | 109 | |
darran | 0:ac21159e27f4 | 110 | if( !pBuf ) |
darran | 0:ac21159e27f4 | 111 | { |
darran | 0:ac21159e27f4 | 112 | rmgLen = 0; |
darran | 0:ac21159e27f4 | 113 | return 0; |
darran | 0:ac21159e27f4 | 114 | } |
darran | 0:ac21159e27f4 | 115 | |
darran | 0:ac21159e27f4 | 116 | if ( !rmgLen ) //We did not know m_pReadPbuf->len last time we called this fn |
darran | 0:ac21159e27f4 | 117 | { |
darran | 0:ac21159e27f4 | 118 | rmgLen = pBuf->len; |
darran | 0:ac21159e27f4 | 119 | } |
darran | 0:ac21159e27f4 | 120 | |
darran | 0:ac21159e27f4 | 121 | while ( inLen < len ) |
darran | 0:ac21159e27f4 | 122 | { |
darran | 0:ac21159e27f4 | 123 | cpyLen = MIN( (len - inLen), rmgLen ); //Remaining len to copy, remaining len in THIS pbuf |
darran | 0:ac21159e27f4 | 124 | memcpy((void*)buf, (void*)((char*)(pBuf->payload) + (pBuf->len - rmgLen)), cpyLen); |
darran | 0:ac21159e27f4 | 125 | inLen += cpyLen; |
darran | 0:ac21159e27f4 | 126 | buf += cpyLen; |
darran | 0:ac21159e27f4 | 127 | |
darran | 0:ac21159e27f4 | 128 | rmgLen = rmgLen - cpyLen; //Update rmgLen |
darran | 0:ac21159e27f4 | 129 | |
darran | 0:ac21159e27f4 | 130 | if( rmgLen > 0 ) |
darran | 0:ac21159e27f4 | 131 | { |
darran | 0:ac21159e27f4 | 132 | //We did not read this pbuf completely, so let's save it's pos & return |
darran | 0:ac21159e27f4 | 133 | break; |
darran | 0:ac21159e27f4 | 134 | } |
darran | 0:ac21159e27f4 | 135 | |
darran | 0:ac21159e27f4 | 136 | if(pBuf->next) |
darran | 0:ac21159e27f4 | 137 | { |
darran | 0:ac21159e27f4 | 138 | pbuf* pNextPBuf = pBuf->next; |
darran | 0:ac21159e27f4 | 139 | pBuf->next = NULL; //So that it is not freed as well |
darran | 0:ac21159e27f4 | 140 | //We get the reference to pNextPBuf from m_pReadPbuf |
darran | 0:ac21159e27f4 | 141 | pbuf_free((pbuf*)pBuf); |
darran | 0:ac21159e27f4 | 142 | pBuf = pNextPBuf; |
darran | 0:ac21159e27f4 | 143 | rmgLen = pBuf->len; |
darran | 0:ac21159e27f4 | 144 | } |
darran | 0:ac21159e27f4 | 145 | else |
darran | 0:ac21159e27f4 | 146 | { |
darran | 0:ac21159e27f4 | 147 | pbuf_free((pbuf*)pBuf); |
darran | 0:ac21159e27f4 | 148 | pBuf = NULL; |
darran | 0:ac21159e27f4 | 149 | rmgLen = 0; |
darran | 0:ac21159e27f4 | 150 | m_lInPkt.pop_front(); |
darran | 0:ac21159e27f4 | 151 | break; //No more data to read |
darran | 0:ac21159e27f4 | 152 | } |
darran | 0:ac21159e27f4 | 153 | } |
darran | 0:ac21159e27f4 | 154 | |
darran | 0:ac21159e27f4 | 155 | return inLen; |
darran | 0:ac21159e27f4 | 156 | } |
darran | 0:ac21159e27f4 | 157 | |
darran | 0:ac21159e27f4 | 158 | NetUdpSocketErr LwipNetUdpSocket::close() |
darran | 0:ac21159e27f4 | 159 | { |
darran | 0:ac21159e27f4 | 160 | DBG("\r\nLwipNetUdpSocket::close() : Closing...\r\n"); |
darran | 0:ac21159e27f4 | 161 | |
darran | 0:ac21159e27f4 | 162 | if(m_closed) |
darran | 0:ac21159e27f4 | 163 | return NETUDPSOCKET_OK; //Already being closed |
darran | 0:ac21159e27f4 | 164 | m_closed = true; |
darran | 0:ac21159e27f4 | 165 | |
darran | 0:ac21159e27f4 | 166 | if( !m_pPcb ) //Pcb doesn't exist (anymore) |
darran | 0:ac21159e27f4 | 167 | return NETUDPSOCKET_MEM; |
darran | 0:ac21159e27f4 | 168 | |
darran | 0:ac21159e27f4 | 169 | DBG("\r\nLwipNetUdpSocket::close() : Cleanup...\r\n"); |
darran | 0:ac21159e27f4 | 170 | |
darran | 0:ac21159e27f4 | 171 | //Cleanup incoming data |
darran | 0:ac21159e27f4 | 172 | cleanUp(); |
darran | 0:ac21159e27f4 | 173 | |
darran | 0:ac21159e27f4 | 174 | DBG("\r\nLwipNetUdpSocket::close() : removing m_pPcb...\r\n"); |
darran | 0:ac21159e27f4 | 175 | udp_remove( (udp_pcb*) m_pPcb); |
darran | 0:ac21159e27f4 | 176 | |
darran | 0:ac21159e27f4 | 177 | m_pPcb = NULL; |
darran | 0:ac21159e27f4 | 178 | return NETUDPSOCKET_OK; |
darran | 0:ac21159e27f4 | 179 | } |
darran | 0:ac21159e27f4 | 180 | |
darran | 0:ac21159e27f4 | 181 | NetUdpSocketErr LwipNetUdpSocket::poll() |
darran | 0:ac21159e27f4 | 182 | { |
darran | 0:ac21159e27f4 | 183 | NetUdpSocket::flushEvents(); |
darran | 0:ac21159e27f4 | 184 | return NETUDPSOCKET_OK; |
darran | 0:ac21159e27f4 | 185 | } |
darran | 0:ac21159e27f4 | 186 | |
darran | 0:ac21159e27f4 | 187 | // Callbacks events |
darran | 0:ac21159e27f4 | 188 | |
darran | 0:ac21159e27f4 | 189 | void LwipNetUdpSocket::recvCb(udp_pcb* pcb, struct pbuf* p, ip_addr_t* addr, u16_t port) |
darran | 0:ac21159e27f4 | 190 | { |
darran | 0:ac21159e27f4 | 191 | DBG("\r\n Packet of length %d arrived in UDP Socket.\r\n", p->tot_len); |
darran | 0:ac21159e27f4 | 192 | list<InPacket>::iterator it; |
darran | 0:ac21159e27f4 | 193 | for ( it = m_lInPkt.begin(); it != m_lInPkt.end(); it++ ) |
darran | 0:ac21159e27f4 | 194 | { |
darran | 0:ac21159e27f4 | 195 | if( ip_addr_cmp((&((*it).addr)), addr) && ((*it).port == port) ) |
darran | 0:ac21159e27f4 | 196 | { |
darran | 0:ac21159e27f4 | 197 | //Let's tail this packet to the previous one |
darran | 0:ac21159e27f4 | 198 | pbuf_cat((pbuf*)((*it).pBuf), p); |
darran | 0:ac21159e27f4 | 199 | //No need to queue an event in that case since the read buf has not been processed yet |
darran | 0:ac21159e27f4 | 200 | return; |
darran | 0:ac21159e27f4 | 201 | } |
darran | 0:ac21159e27f4 | 202 | } |
darran | 0:ac21159e27f4 | 203 | |
darran | 0:ac21159e27f4 | 204 | //New host, add a packet to the queue |
darran | 0:ac21159e27f4 | 205 | InPacket pkt; |
darran | 0:ac21159e27f4 | 206 | pkt.pBuf = p; |
darran | 0:ac21159e27f4 | 207 | pkt.addr = *addr; |
darran | 0:ac21159e27f4 | 208 | pkt.port = port; |
darran | 0:ac21159e27f4 | 209 | m_lInPkt.push_back(pkt); |
darran | 0:ac21159e27f4 | 210 | |
darran | 0:ac21159e27f4 | 211 | queueEvent(NETUDPSOCKET_READABLE); |
darran | 0:ac21159e27f4 | 212 | } |
darran | 0:ac21159e27f4 | 213 | |
darran | 0:ac21159e27f4 | 214 | void LwipNetUdpSocket::cleanUp() //Flush input buffer |
darran | 0:ac21159e27f4 | 215 | { |
darran | 0:ac21159e27f4 | 216 | //Ensure that further error won't be followed to this inst (which can be destroyed) |
darran | 0:ac21159e27f4 | 217 | if( m_pPcb ) |
darran | 0:ac21159e27f4 | 218 | { |
darran | 0:ac21159e27f4 | 219 | udp_recv( (udp_pcb*) m_pPcb, NULL, (void*) NULL ); |
darran | 0:ac21159e27f4 | 220 | } |
darran | 0:ac21159e27f4 | 221 | |
darran | 0:ac21159e27f4 | 222 | list<InPacket>::iterator it; |
darran | 0:ac21159e27f4 | 223 | for ( it = m_lInPkt.begin(); it != m_lInPkt.end(); it++ ) |
darran | 0:ac21159e27f4 | 224 | { |
darran | 0:ac21159e27f4 | 225 | //Free buf |
darran | 0:ac21159e27f4 | 226 | pbuf_free((pbuf*)((*it).pBuf)); |
darran | 0:ac21159e27f4 | 227 | } |
darran | 0:ac21159e27f4 | 228 | m_lInPkt.clear(); |
darran | 0:ac21159e27f4 | 229 | } |
darran | 0:ac21159e27f4 | 230 | |
darran | 0:ac21159e27f4 | 231 | // Static callback from LwIp |
darran | 0:ac21159e27f4 | 232 | |
darran | 0:ac21159e27f4 | 233 | void LwipNetUdpSocket::sRecvCb(void *arg, struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *addr, u16_t port) |
darran | 0:ac21159e27f4 | 234 | { |
darran | 0:ac21159e27f4 | 235 | LwipNetUdpSocket* pMe = (LwipNetUdpSocket*) arg; |
darran | 0:ac21159e27f4 | 236 | return pMe->recvCb( pcb, p, addr, port ); |
darran | 0:ac21159e27f4 | 237 | } |
darran | 0:ac21159e27f4 | 238 | |
darran | 0:ac21159e27f4 | 239 | #endif |