This is Webservice SDK for mbed. LPCXpresso1769/LPC1768/FRDM-K64F/LPC4088

Dependents:   MbedFileServer_1768MiniDK2 RedWireBridge IssueDebug_gcc MiMicRemoteMCU-for-Mbed ... more

libMiMic(MiMic library for mbed)は、WebService機能を提供するSDKです。 mbedでWebAPIに対応したネットワークデバイスを簡単に作ることが出来ます。

libMiMicはMiMic projectで開発しています。MiMic projectについてはこちらをご覧ください。 http://nyatla.jp/mimic/wp/

構成

libMiMicはmbedRTOS上で動作し、ユーザアプリケーションにAPIを提供します。コアAPIはC言語で記述されていますが、使用頻度の高いものについてはmbed向けのC++APIが準備されています。

/media/uploads/nyatla/libmimic-sdk.png

※libMiMicはmbedの標準イーサネットドライバをしようしていません。

標準イーサネットドライバと同時に使用することはできません。

  • MiMicIP - IPv4スタックです。レテンシとメモリ消費量を抑えたuipベースのライブラリです。
  • ARP/ICMP/UDP/TCP - 基礎的なソケットAPIを提供します。APIは独自です。
  • HTTP/1.1 Server - HTTP/1.1に対応したサーバです。マルチセッション・Chunked・持続性接続に対応しています。
  • HTTP Modules - HTTP/1.1の機能モジュールです。以下のモジュールがあります。
    • ROM file provider - ROMに格納したファイルイメージを公開します。
    • File system provider - mbedファイルシステムを公開します。
    • Onchip configuration - プログラムフラッシュを利用して設定を保存します。
    • MiMicVM processor - RPCリクエスト(MiMicVM)を処理します。
    • FileUpload - ファイルアップロードを受け取ります。
    • URL decoder - HTTPリクエストを解析します。
    • UPnP handler -UPnPメッセージを処理します。
    • WebSocket - Websocketサーバです。
  • mDNS - マルチキャストDNSサービスです。
  • UPnP - UPnP/1.0の機能を提供します。UPnP handlerと協調して動作します。(現在はデバイス探索(SSDP)・デスクリプション(Description)のみ実装してあります。)
  • DHCP/APIPA - ゼロコンフィギュレーション用のモジュールです。
  • HTTP/1.1 Client
  • mbed C++ class library - mbed向けのC++CPIです。C言語のものより簡単です。

対応機種

  • mbed(mbed LPC1768)
  • LPCXpresso1769

プログラム

Import programMiMicRemoteMCU-for-Mbed

MiMic RemoteMCU for mbed. This program provides MCU control API over REST API. It can control MCU from Javascript,PHP or any HTTP rest client directly. And, The application has self development environment.

Import programMbedFileServer

The program publishes files at local directory and SD filesystem. It is a full-fledged webServer somewhat.

サンプル

Import programMiMicSimpleHttpd

This is a simplest HTTP server made ​​of libMiMic. It will echo back a request path.

Import programUPnPBasicDevice

Simplest UPnP basic device example. This program to run UPnP basic device on the mbed.

Import programWebSocketSample

MiMicSDK Websocket module sample program.

Import programHttpClientSamlpe

A http client sample program.

Import programTcpSocketClientSamlpe

MiMicSDK Tcp client socket sample program.

Import programUdpSocketSamlpe

Udp socket sample program. This program will send back the received packet.

チュートリアル

English

libMiMic(MiMic library for mbed) is SDK which provides Webservice functions. It can be created networking device easily using mbed.

See more MiMic information, See MiMic project website. http://nyatla.jp/mimic/wp/

Structure

libMiMic run on mbed RTOS and provides networking API to user application. This library has C++ class API for the mbed, and low-level C language API.

/media/uploads/nyatla/libmimic-sdk.png

For WebService general, it can be written in a simple C + + API.

libMiMic does not have the standard Ethernet driver of mbed. It is not possible that will be used with the standard Ethernet driver.

  • MiMicIP - IPv4 protocol stack. This is based uip which is reduced memory and latency.
  • ARP / ICMP / UDP / TCP - Those are provide basic IP protocols.
  • HTTP/1.1 Server - The Http server compatible HTTP/1.1. It supports multi-session, chunked transport, persistent connection.
  • HTTP Modules - There are addon-module for HTTP server. The following modules.
    • ROM file module - Publish the file images in ROM.
    • File system module - Publish thefiles in mbed file system.
    • Onchip configuration module - To save the network settings to the program flash via REST.
    • MiMicVM module - To handle the (MiMicVM) RPC request.
    • FileUpload module - Accept a file via HTTP POST.
    • URL dedoce module - A versatility URL decoder.
    • UPnP handle module - To handle UPnP messages.
    • UPnP - This provides UPnP/1.0 device functions. It works together with UPnP handler.
    • Websocket - websocket (version13) server
  • mDNS Service - DNS-SD protocol server.
  • UPnP - This provides UPnP/1.0 device functions which works with UPnP handler. (You have been implemented (SSDP) ? description only (Description) device search now.) It is a module zero configuration for - DHCP / APIPA. mbed C + + class library - C of mbed for + + is the CPI. It is simple than that of the C language.
  • DHCP/APIPA - It support zero-cpnfigulation.
  • mbed C++ class library. Almost APIs for Web applications are available.
  • HTTP/1.1 Client

Supported target

  • mbed(mbed LPC1768)
  • LPCXpresso1769

Application

Import programMiMicRemoteMCU-for-Mbed

MiMic RemoteMCU for mbed. This program provides MCU control API over REST API. It can control MCU from Javascript,PHP or any HTTP rest client directly. And, The application has self development environment.

Import programMbedFileServer

The program publishes files at local directory and SD filesystem. It is a full-fledged webServer somewhat.

Sample

Import programMiMicSimpleHttpd

This is a simplest HTTP server made ​​of libMiMic. It will echo back a request path.

Import programUPnPBasicDevice

Simplest UPnP basic device example. This program to run UPnP basic device on the mbed.

Import programWebSocketSample

MiMicSDK Websocket module sample program.

Import programHttpClientSamlpe

A http client sample program.

Import programTcpSocketClientSamlpe

MiMicSDK Tcp client socket sample program.

Import programUdpSocketSamlpe

Udp socket sample program. This program will send back the received packet.

Tutorial

Committer:
nyatla
Date:
Wed Nov 19 14:45:17 2014 +0000
Revision:
109:18f12ac01097
new MiMicIP API;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nyatla 109:18f12ac01097 1 /*********************************************************************************
nyatla 109:18f12ac01097 2 * PROJECT: MiMic
nyatla 109:18f12ac01097 3 * --------------------------------------------------------------------------------
nyatla 109:18f12ac01097 4 *
nyatla 109:18f12ac01097 5 * This file is part of MiMic
nyatla 109:18f12ac01097 6 * Copyright (C)2011 Ryo Iizuka
nyatla 109:18f12ac01097 7 *
nyatla 109:18f12ac01097 8 * MiMic is free software: you can redistribute it and/or modify
nyatla 109:18f12ac01097 9 * it under the terms of the GNU Lesser General Public License as published
nyatla 109:18f12ac01097 10 * by the Free Software Foundation, either version 3 of the License, or
nyatla 109:18f12ac01097 11 * (at your option) any later version.
nyatla 109:18f12ac01097 12 *
nyatla 109:18f12ac01097 13 * This program is distributed in the hope that it will be useful,
nyatla 109:18f12ac01097 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nyatla 109:18f12ac01097 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
nyatla 109:18f12ac01097 16 * GNU General Public License for more details.
nyatla 109:18f12ac01097 17 *
nyatla 109:18f12ac01097 18 * You should have received a copy of the GNU Lesser General Public License
nyatla 109:18f12ac01097 19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
nyatla 109:18f12ac01097 20 *
nyatla 109:18f12ac01097 21 * For further information please contact.
nyatla 109:18f12ac01097 22 * http://nyatla.jp/
nyatla 109:18f12ac01097 23 * <airmail(at)ebony.plala.or.jp> or <nyatla(at)nyatla.jp>
nyatla 109:18f12ac01097 24 *
nyatla 109:18f12ac01097 25 *
nyatla 109:18f12ac01097 26 * Parts of this file were leveraged from uIP:
nyatla 109:18f12ac01097 27 *
nyatla 109:18f12ac01097 28 * Copyright (c) 2001-2003, Adam Dunkels.
nyatla 109:18f12ac01097 29 * All rights reserved.
nyatla 109:18f12ac01097 30 *
nyatla 109:18f12ac01097 31 * Redistribution and use in source and binary forms, with or without
nyatla 109:18f12ac01097 32 * modification, are permitted provided that the following conditions
nyatla 109:18f12ac01097 33 * are met:
nyatla 109:18f12ac01097 34 * 1. Redistributions of source code must retain the above copyright
nyatla 109:18f12ac01097 35 * notice, this list of conditions and the following disclaimer.
nyatla 109:18f12ac01097 36 * 2. Redistributions in binary form must reproduce the above copyright
nyatla 109:18f12ac01097 37 * notice, this list of conditions and the following disclaimer in the
nyatla 109:18f12ac01097 38 * documentation and/or other materials provided with the distribution.
nyatla 109:18f12ac01097 39 * 3. The name of the author may not be used to endorse or promote
nyatla 109:18f12ac01097 40 * products derived from this software without specific prior
nyatla 109:18f12ac01097 41 * written permission.
nyatla 109:18f12ac01097 42 *
nyatla 109:18f12ac01097 43 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
nyatla 109:18f12ac01097 44 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
nyatla 109:18f12ac01097 45 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
nyatla 109:18f12ac01097 46 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
nyatla 109:18f12ac01097 47 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
nyatla 109:18f12ac01097 48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
nyatla 109:18f12ac01097 49 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
nyatla 109:18f12ac01097 50 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
nyatla 109:18f12ac01097 51 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
nyatla 109:18f12ac01097 52 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
nyatla 109:18f12ac01097 53 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
nyatla 109:18f12ac01097 54 */
nyatla 109:18f12ac01097 55 #include "NyLPC_cIPv4Arp.h"
nyatla 109:18f12ac01097 56 #include "../NyLPC_NetIf_ip_types.h"
nyatla 109:18f12ac01097 57 #include "NyLPC_cMiMicIpNetIf_protected.h"
nyatla 109:18f12ac01097 58 #include <string.h>
nyatla 109:18f12ac01097 59
nyatla 109:18f12ac01097 60
nyatla 109:18f12ac01097 61 /**
nyatla 109:18f12ac01097 62 * The maxium age of ARP table entries measured in 10ths of seconds.
nyatla 109:18f12ac01097 63 *
nyatla 109:18f12ac01097 64 * An UIP_ARP_MAXAGE of 120 corresponds to 20 minutes (BSD
nyatla 109:18f12ac01097 65 * default).
nyatla 109:18f12ac01097 66 */
nyatla 109:18f12ac01097 67 #define UIP_ARP_MAXAGE 120
nyatla 109:18f12ac01097 68
nyatla 109:18f12ac01097 69
nyatla 109:18f12ac01097 70 //static const struct NyLPC_TEthAddr broadcast_ethaddr = { { 0xff, 0xff, 0xff,0xff, 0xff, 0xff } };
nyatla 109:18f12ac01097 71 //static const struct NyLPC_TIPv4Addr broadcast_ipaddr = { 0xfffffff };
nyatla 109:18f12ac01097 72
nyatla 109:18f12ac01097 73
nyatla 109:18f12ac01097 74
nyatla 109:18f12ac01097 75
nyatla 109:18f12ac01097 76 static void uip_arp_update(NyLPC_TcIPv4Arp_t* i_inst,const struct NyLPC_TIPv4Addr* ipaddr,const struct NyLPC_TEthAddr *ethaddr);
nyatla 109:18f12ac01097 77 /*-----------------------------------------------------------------------------------*/
nyatla 109:18f12ac01097 78 /**
nyatla 109:18f12ac01097 79 * Initialize the ARP module.
nyatla 109:18f12ac01097 80 *
nyatla 109:18f12ac01097 81 */
nyatla 109:18f12ac01097 82 /*-----------------------------------------------------------------------------------*/
nyatla 109:18f12ac01097 83 void NyLPC_cIPv4Arp_initialize(NyLPC_TcIPv4Arp_t* i_inst,const NyLPC_TcIPv4Config_t* i_ref_config)
nyatla 109:18f12ac01097 84 {
nyatla 109:18f12ac01097 85 int i;
nyatla 109:18f12ac01097 86 struct NyLPC_TArpTableItem* tbl=i_inst->arp_table;
nyatla 109:18f12ac01097 87 i_inst->_cfg = i_ref_config;
nyatla 109:18f12ac01097 88 i_inst->arptime = 0;
nyatla 109:18f12ac01097 89 i_inst->tmpage = 0;
nyatla 109:18f12ac01097 90 for (i = 0; i < NyLPC_TcIPv4Arp_ARPTAB_SIZE; ++i) {
nyatla 109:18f12ac01097 91 memset(&(tbl[i].ipaddr), 0, sizeof(struct NyLPC_TIPv4Addr));
nyatla 109:18f12ac01097 92 }
nyatla 109:18f12ac01097 93 }
nyatla 109:18f12ac01097 94 /*-----------------------------------------------------------------------------------*/
nyatla 109:18f12ac01097 95 /**
nyatla 109:18f12ac01097 96 * Periodic ARP processing function.
nyatla 109:18f12ac01097 97 *
nyatla 109:18f12ac01097 98 * This function performs periodic timer processing in the ARP module
nyatla 109:18f12ac01097 99 * and should be called at regular intervals. The recommended interval
nyatla 109:18f12ac01097 100 * is 10 seconds between the calls.
nyatla 109:18f12ac01097 101 *
nyatla 109:18f12ac01097 102 */
nyatla 109:18f12ac01097 103 /*-----------------------------------------------------------------------------------*/
nyatla 109:18f12ac01097 104 void NyLPC_cIPv4Arp_periodic(NyLPC_TcIPv4Arp_t* i_inst)
nyatla 109:18f12ac01097 105 {
nyatla 109:18f12ac01097 106 struct NyLPC_TArpTableItem* tbl=i_inst->arp_table;
nyatla 109:18f12ac01097 107 struct NyLPC_TArpTableItem* tabptr;
nyatla 109:18f12ac01097 108 int i;
nyatla 109:18f12ac01097 109 i_inst->arptime++;
nyatla 109:18f12ac01097 110 for (i = 0; i < NyLPC_TcIPv4Arp_ARPTAB_SIZE; ++i) {
nyatla 109:18f12ac01097 111 tabptr = &tbl[i];
nyatla 109:18f12ac01097 112 if (tabptr->ipaddr.v != 0 && i_inst->arptime - tabptr->time >= UIP_ARP_MAXAGE)
nyatla 109:18f12ac01097 113 {
nyatla 109:18f12ac01097 114 tabptr->ipaddr.v = 0;
nyatla 109:18f12ac01097 115 }
nyatla 109:18f12ac01097 116 }
nyatla 109:18f12ac01097 117
nyatla 109:18f12ac01097 118 }
nyatla 109:18f12ac01097 119 /*-----------------------------------------------------------------------------------*/
nyatla 109:18f12ac01097 120 /**
nyatla 109:18f12ac01097 121 * ARP processing for incoming IP packets
nyatla 109:18f12ac01097 122 *
nyatla 109:18f12ac01097 123 * This function should be called by the device driver when an IP
nyatla 109:18f12ac01097 124 * packet has been received. The function will check if the address is
nyatla 109:18f12ac01097 125 * in the ARP cache, and if so the ARP cache entry will be
nyatla 109:18f12ac01097 126 * refreshed. If no ARP cache entry was found, a new one is created.
nyatla 109:18f12ac01097 127 *
nyatla 109:18f12ac01097 128 * This function expects an IP packet with a prepended Ethernet header
nyatla 109:18f12ac01097 129 * in the uip_buf[] buffer, and the length of the packet in the global
nyatla 109:18f12ac01097 130 * variable uip_len.
nyatla 109:18f12ac01097 131 */
nyatla 109:18f12ac01097 132 /*-----------------------------------------------------------------------------------*/
nyatla 109:18f12ac01097 133 void NyLPC_cIPv4Arp_incomingIp(NyLPC_TcIPv4Arp_t* i_inst,const struct NyLPC_TEthernetIIHeader* i_eth,struct NyLPC_TIPv4Addr i_ip_src)
nyatla 109:18f12ac01097 134 {
nyatla 109:18f12ac01097 135 //EtherとIPv4の値を読みだす。
nyatla 109:18f12ac01097 136 /* Only insert/update an entry if the source IP address of the
nyatla 109:18f12ac01097 137 incoming IP packet comes from a host on the local network. */
nyatla 109:18f12ac01097 138 if ((i_ip_src.v & i_inst->_cfg->netmask.v) != (i_inst->_cfg->ip_addr.v & i_inst->_cfg->netmask.v)) {
nyatla 109:18f12ac01097 139 return;
nyatla 109:18f12ac01097 140 }
nyatla 109:18f12ac01097 141 uip_arp_update(i_inst,&(i_ip_src), &(i_eth->src));
nyatla 109:18f12ac01097 142 return;
nyatla 109:18f12ac01097 143 }
nyatla 109:18f12ac01097 144 /**
nyatla 109:18f12ac01097 145 * ARP processing for incoming ARP packets.
nyatla 109:18f12ac01097 146 *
nyatla 109:18f12ac01097 147 * This function should be called by the device driver when an ARP
nyatla 109:18f12ac01097 148 * packet has been received. The function will act differently
nyatla 109:18f12ac01097 149 * depending on the ARP packet type: if it is a reply for a request
nyatla 109:18f12ac01097 150 * that we previously sent out, the ARP cache will be filled in with
nyatla 109:18f12ac01097 151 * the values from the ARP reply. If the incoming ARP packet is an ARP
nyatla 109:18f12ac01097 152 * request for our IP address, an ARP reply packet is created and put
nyatla 109:18f12ac01097 153 * into the uip_buf[] buffer.
nyatla 109:18f12ac01097 154 *
nyatla 109:18f12ac01097 155 * When the function returns, the value of the global variable uip_len
nyatla 109:18f12ac01097 156 * indicates whether the device driver should send out a packet or
nyatla 109:18f12ac01097 157 * not. If uip_len is zero, no packet should be sent. If uip_len is
nyatla 109:18f12ac01097 158 * non-zero, it contains the length of the outbound packet that is
nyatla 109:18f12ac01097 159 * present in the uip_buf[] buffer.
nyatla 109:18f12ac01097 160 *
nyatla 109:18f12ac01097 161 * This function expects an ARP packet with a prepended Ethernet
nyatla 109:18f12ac01097 162 * header in the uip_buf[] buffer, and the length of the packet in the
nyatla 109:18f12ac01097 163 * global variable uip_len.
nyatla 109:18f12ac01097 164 */
nyatla 109:18f12ac01097 165
nyatla 109:18f12ac01097 166
nyatla 109:18f12ac01097 167 /**
nyatla 109:18f12ac01097 168 * ARPパケットの読出し用構造体
nyatla 109:18f12ac01097 169 */
nyatla 109:18f12ac01097 170 struct TArpPacketPtr
nyatla 109:18f12ac01097 171 {
nyatla 109:18f12ac01097 172 struct NyLPC_TEthernetIIHeader header;
nyatla 109:18f12ac01097 173 struct NyLPC_TArpHeader arp;
nyatla 109:18f12ac01097 174 }PACK_STRUCT_END;
nyatla 109:18f12ac01097 175
nyatla 109:18f12ac01097 176 /**
nyatla 109:18f12ac01097 177 * arpパケットを処理します。
nyatla 109:18f12ac01097 178 */
nyatla 109:18f12ac01097 179 void* NyLPC_cIPv4Arp_rx(NyLPC_TcIPv4Arp_t* i_inst,const struct NyLPC_TArpHeader* i_arp, NyLPC_TUInt16 i_len, NyLPC_TUInt16* o_tx_len)
nyatla 109:18f12ac01097 180 {
nyatla 109:18f12ac01097 181 struct NyLPC_TArpHeader* arp_tx;
nyatla 109:18f12ac01097 182 if (i_len < sizeof(struct NyLPC_TArpHeader)) {
nyatla 109:18f12ac01097 183 return NULL;
nyatla 109:18f12ac01097 184 }
nyatla 109:18f12ac01097 185 const NyLPC_TcIPv4Config_t* cfg=i_inst->_cfg;
nyatla 109:18f12ac01097 186 switch (i_arp->opcode) {
nyatla 109:18f12ac01097 187 case NyLPC_HTONS(ARP_REQUEST):
nyatla 109:18f12ac01097 188 /* ARP request. If it asked for our address, we send out a reply. */
nyatla 109:18f12ac01097 189 if (NyLPC_TIPv4Addr_isEqual(&(i_arp->dipaddr), &(cfg->ip_addr))) {
nyatla 109:18f12ac01097 190 /* First, we register the one who made the request in our ARP
nyatla 109:18f12ac01097 191 table, since it is likely that we will do more communication
nyatla 109:18f12ac01097 192 with this host in the future. */
nyatla 109:18f12ac01097 193 uip_arp_update(i_inst,&(i_arp->sipaddr), &i_arp->shwaddr);
nyatla 109:18f12ac01097 194 //イーサネットヘッダもいじくるから
nyatla 109:18f12ac01097 195 arp_tx=(struct NyLPC_TArpHeader*)NyLPC_cMiMicIpNetIf_allocSysTxBuf();
nyatla 109:18f12ac01097 196
nyatla 109:18f12ac01097 197 /* The reply opcode is 2. */
nyatla 109:18f12ac01097 198 arp_tx->hwtype =i_arp->hwtype;
nyatla 109:18f12ac01097 199 arp_tx->protocol =i_arp->protocol;
nyatla 109:18f12ac01097 200 arp_tx->hwlen =i_arp->hwlen;
nyatla 109:18f12ac01097 201 arp_tx->protolen =i_arp->protolen;
nyatla 109:18f12ac01097 202 arp_tx->opcode = NyLPC_HTONS(2);
nyatla 109:18f12ac01097 203 memcpy(arp_tx->dhwaddr.addr, i_arp->shwaddr.addr, 6);
nyatla 109:18f12ac01097 204 memcpy(arp_tx->shwaddr.addr, cfg->eth_mac.addr, 6);
nyatla 109:18f12ac01097 205 arp_tx->dipaddr = i_arp->sipaddr;
nyatla 109:18f12ac01097 206 arp_tx->sipaddr = cfg->ip_addr;
nyatla 109:18f12ac01097 207 *o_tx_len=NyLPC_TEthernetIIHeader_setArpTx((((struct NyLPC_TEthernetIIHeader*)arp_tx)-1),&(i_inst->_cfg->eth_mac));
nyatla 109:18f12ac01097 208
nyatla 109:18f12ac01097 209 // /* The reply opcode is 2. */
nyatla 109:18f12ac01097 210 // i_arp->opcode = NyLPC_HTONS(2);
nyatla 109:18f12ac01097 211 //
nyatla 109:18f12ac01097 212 // memcpy(i_arp->dhwaddr.addr, i_arp->shwaddr.addr, 6);
nyatla 109:18f12ac01097 213 // memcpy(i_arp->shwaddr.addr, cfg->eth_mac.addr, 6);
nyatla 109:18f12ac01097 214 //
nyatla 109:18f12ac01097 215 // i_arp->dipaddr = i_arp->sipaddr;
nyatla 109:18f12ac01097 216 // i_arp->sipaddr = cfg->ip_addr;
nyatla 109:18f12ac01097 217 return arp_tx;
nyatla 109:18f12ac01097 218 }
nyatla 109:18f12ac01097 219 break;
nyatla 109:18f12ac01097 220 case NyLPC_HTONS(ARP_REPLY):
nyatla 109:18f12ac01097 221 // ARP reply. We insert or update the ARP table if it was meant for us.
nyatla 109:18f12ac01097 222 if (NyLPC_TIPv4Addr_isEqual(&(i_arp->dipaddr),&(cfg->ip_addr))) {
nyatla 109:18f12ac01097 223 uip_arp_update(i_inst,&(i_arp->sipaddr), &i_arp->shwaddr);
nyatla 109:18f12ac01097 224 }
nyatla 109:18f12ac01097 225 break;
nyatla 109:18f12ac01097 226 }
nyatla 109:18f12ac01097 227 return NULL;
nyatla 109:18f12ac01097 228 }
nyatla 109:18f12ac01097 229 /**
nyatla 109:18f12ac01097 230 * Prepend Ethernet header to an outbound IP packet and see if we need
nyatla 109:18f12ac01097 231 * to send out an ARP request.
nyatla 109:18f12ac01097 232 *
nyatla 109:18f12ac01097 233 * This function should be called before sending out an IP packet. The
nyatla 109:18f12ac01097 234 * function checks the destination IP address of the IP packet to see
nyatla 109:18f12ac01097 235 * what Ethernet MAC address that should be used as a destination MAC
nyatla 109:18f12ac01097 236 * address on the Ethernet.
nyatla 109:18f12ac01097 237 *
nyatla 109:18f12ac01097 238 * If the destination IP address is in the local network (determined
nyatla 109:18f12ac01097 239 * by logical ANDing of netmask and our IP address), the function
nyatla 109:18f12ac01097 240 * checks the ARP cache to see if an entry for the destination IP
nyatla 109:18f12ac01097 241 * address is found. If so, an Ethernet header is prepended and the
nyatla 109:18f12ac01097 242 * function returns. If no ARP cache entry is found for the
nyatla 109:18f12ac01097 243 * destination IP address, the packet in the uip_buf[] is replaced by
nyatla 109:18f12ac01097 244 * an ARP request packet for the IP address. The IP packet is dropped
nyatla 109:18f12ac01097 245 * and it is assumed that they higher level protocols (e.g., TCP)
nyatla 109:18f12ac01097 246 * eventually will retransmit the dropped packet.
nyatla 109:18f12ac01097 247 *
nyatla 109:18f12ac01097 248 * If the destination IP address is not on the local network, the IP
nyatla 109:18f12ac01097 249 * address of the default router is used instead.
nyatla 109:18f12ac01097 250 *
nyatla 109:18f12ac01097 251 * When the function returns, a packet is present in the uip_buf[]
nyatla 109:18f12ac01097 252 * buffer, and the length of the packet is in the global variable
nyatla 109:18f12ac01097 253 * uip_len.
nyatla 109:18f12ac01097 254 */
nyatla 109:18f12ac01097 255
nyatla 109:18f12ac01097 256 /**
nyatla 109:18f12ac01097 257 * IPアドレス-MACアドレス交換
nyatla 109:18f12ac01097 258 */
nyatla 109:18f12ac01097 259 const struct NyLPC_TEthAddr* NyLPC_cIPv4Arp_IPv4toEthAddr(NyLPC_TcIPv4Arp_t* i_inst,const struct NyLPC_TIPv4Addr i_ip_addr)
nyatla 109:18f12ac01097 260 {
nyatla 109:18f12ac01097 261 int i;
nyatla 109:18f12ac01097 262 struct NyLPC_TArpTableItem *tabptr;
nyatla 109:18f12ac01097 263 //ARPテーブルから検索
nyatla 109:18f12ac01097 264 for (i = NyLPC_TcIPv4Arp_ARPTAB_SIZE - 1; i >= 0; i--) {
nyatla 109:18f12ac01097 265 tabptr = &i_inst->arp_table[i];
nyatla 109:18f12ac01097 266 if (NyLPC_TIPv4Addr_isEqual(&i_ip_addr,&(tabptr->ipaddr))) {
nyatla 109:18f12ac01097 267 return &tabptr->ethaddr;
nyatla 109:18f12ac01097 268 }
nyatla 109:18f12ac01097 269 }
nyatla 109:18f12ac01097 270 return NULL;
nyatla 109:18f12ac01097 271 }
nyatla 109:18f12ac01097 272
nyatla 109:18f12ac01097 273
nyatla 109:18f12ac01097 274
nyatla 109:18f12ac01097 275
nyatla 109:18f12ac01097 276
nyatla 109:18f12ac01097 277
nyatla 109:18f12ac01097 278
nyatla 109:18f12ac01097 279 static void uip_arp_update(NyLPC_TcIPv4Arp_t* i_inst,const struct NyLPC_TIPv4Addr* ipaddr,const struct NyLPC_TEthAddr *ethaddr)
nyatla 109:18f12ac01097 280 {
nyatla 109:18f12ac01097 281 register struct NyLPC_TArpTableItem *tabptr;
nyatla 109:18f12ac01097 282 int i,c;
nyatla 109:18f12ac01097 283 /* Walk through the ARP mapping table and try to find an entry to
nyatla 109:18f12ac01097 284 update. If none is found, the IP -> MAC address mapping is
nyatla 109:18f12ac01097 285 inserted in the ARP table. */
nyatla 109:18f12ac01097 286 for (i = 0; i < NyLPC_TcIPv4Arp_ARPTAB_SIZE; ++i) {
nyatla 109:18f12ac01097 287 tabptr = &i_inst->arp_table[i];
nyatla 109:18f12ac01097 288 /* Only check those entries that are actually in use. */
nyatla 109:18f12ac01097 289 if (tabptr->ipaddr.v != 0) {
nyatla 109:18f12ac01097 290 /* Check if the source IP address of the incoming packet matches
nyatla 109:18f12ac01097 291 the IP address in this ARP table entry. */
nyatla 109:18f12ac01097 292 if (ipaddr->v == tabptr->ipaddr.v) {
nyatla 109:18f12ac01097 293 /* An old entry found, update this and return. */
nyatla 109:18f12ac01097 294 memcpy(tabptr->ethaddr.addr, ethaddr->addr, 6);
nyatla 109:18f12ac01097 295 tabptr->time = i_inst->arptime;
nyatla 109:18f12ac01097 296
nyatla 109:18f12ac01097 297 return;
nyatla 109:18f12ac01097 298 }
nyatla 109:18f12ac01097 299 }
nyatla 109:18f12ac01097 300 }
nyatla 109:18f12ac01097 301
nyatla 109:18f12ac01097 302 /* If we get here, no existing ARP table entry was found, so we
nyatla 109:18f12ac01097 303 create one. */
nyatla 109:18f12ac01097 304 /* First, we try to find an unused entry in the ARP table. */
nyatla 109:18f12ac01097 305 for (i = 0; i < NyLPC_TcIPv4Arp_ARPTAB_SIZE; ++i) {
nyatla 109:18f12ac01097 306 tabptr = &i_inst->arp_table[i];
nyatla 109:18f12ac01097 307 if (tabptr->ipaddr.v == 0) {
nyatla 109:18f12ac01097 308 break;
nyatla 109:18f12ac01097 309 }
nyatla 109:18f12ac01097 310 }
nyatla 109:18f12ac01097 311
nyatla 109:18f12ac01097 312 /* If no unused entry is found, we try to find the oldest entry and
nyatla 109:18f12ac01097 313 throw it away. */
nyatla 109:18f12ac01097 314 if (i == NyLPC_TcIPv4Arp_ARPTAB_SIZE) {
nyatla 109:18f12ac01097 315 i_inst->tmpage = 0;
nyatla 109:18f12ac01097 316 c = 0;
nyatla 109:18f12ac01097 317 for (i = 0; i < NyLPC_TcIPv4Arp_ARPTAB_SIZE; ++i) {
nyatla 109:18f12ac01097 318 tabptr = &i_inst->arp_table[i];
nyatla 109:18f12ac01097 319 if (i_inst->arptime - tabptr->time > i_inst->tmpage) {
nyatla 109:18f12ac01097 320 i_inst->tmpage = i_inst->arptime - tabptr->time;
nyatla 109:18f12ac01097 321 c = i;
nyatla 109:18f12ac01097 322 }
nyatla 109:18f12ac01097 323 }
nyatla 109:18f12ac01097 324 i = c;
nyatla 109:18f12ac01097 325 tabptr = &i_inst->arp_table[i];
nyatla 109:18f12ac01097 326 }
nyatla 109:18f12ac01097 327
nyatla 109:18f12ac01097 328 /* Now, i is the ARP table entry which we will fill with the new information. */
nyatla 109:18f12ac01097 329 tabptr->ipaddr = *ipaddr;
nyatla 109:18f12ac01097 330 memcpy(tabptr->ethaddr.addr, ethaddr->addr, 6);
nyatla 109:18f12ac01097 331 tabptr->time = i_inst->arptime;
nyatla 109:18f12ac01097 332 }
nyatla 109:18f12ac01097 333
nyatla 109:18f12ac01097 334
nyatla 109:18f12ac01097 335