oliverschmidt | b025e43 | 2007-09-05 16:11:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2007, Swedish Institute of Computer Science. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in the |
| 14 | * documentation and/or other materials provided with the distribution. |
| 15 | * |
| 16 | * 3. Neither the name of the Institute nor the names of its contributors |
| 17 | * may be used to endorse or promote products derived from this software |
| 18 | * without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND |
| 21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE |
| 24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 30 | * SUCH DAMAGE. |
| 31 | * |
| 32 | * This file is part of the uIP TCP/IP stack. |
| 33 | * |
| 34 | * Author: Oliver Schmidt <ol.sc@web.de> |
| 35 | * |
| 36 | * $Id: wpcap-service.c,v 1.1 2007/09/05 16:11:08 oliverschmidt Exp $ |
| 37 | */ |
| 38 | |
| 39 | #include <memory.h> |
| 40 | |
| 41 | #include "packet-service.h" |
| 42 | #include "uip_arp.h" |
| 43 | #include "wpcap.h" |
| 44 | |
| 45 | static void output(u8_t *hdr, u16_t hdrlen, u8_t *data, u16_t datalen); |
| 46 | |
| 47 | static struct packet_service_state |
| 48 | state = {PACKET_SERVICE_VERSION, output}; |
| 49 | |
| 50 | EK_EVENTHANDLER(eventhandler, ev, data); |
| 51 | EK_POLLHANDLER(pollhandler); |
| 52 | EK_PROCESS(proc, PACKET_SERVICE_NAME, EK_PRIO_NORMAL, |
| 53 | eventhandler, pollhandler, &state); |
| 54 | |
| 55 | /*---------------------------------------------------------------------------*/ |
| 56 | static void |
| 57 | output(u8_t *hdr, u16_t hdrlen, u8_t *data, u16_t datalen) |
| 58 | { |
| 59 | /* Just copy all data into uip_buf. */ |
| 60 | memcpy(hdr + hdrlen, data, datalen); |
| 61 | uip_arp_out(); |
| 62 | wpcap_send(); |
| 63 | } |
| 64 | /*---------------------------------------------------------------------------*/ |
| 65 | EK_PROCESS_INIT(packet_service_init, arg) |
| 66 | { |
| 67 | ek_service_start(PACKET_SERVICE_NAME, &proc); |
| 68 | } |
| 69 | /*---------------------------------------------------------------------------*/ |
| 70 | EK_EVENTHANDLER(eventhandler, ev, data) |
| 71 | { |
| 72 | switch(ev) { |
| 73 | case EK_EVENT_INIT: |
| 74 | case EK_EVENT_REPLACE: |
| 75 | wpcap_init(); |
| 76 | break; |
| 77 | case EK_EVENT_REQUEST_REPLACE: |
| 78 | ek_replace(data, NULL); |
| 79 | LOADER_UNLOAD(); |
| 80 | break; |
| 81 | case EK_EVENT_REQUEST_EXIT: |
| 82 | wpcap_exit(); |
| 83 | ek_exit(); |
| 84 | LOADER_UNLOAD(); |
| 85 | break; |
| 86 | default: |
| 87 | break; |
| 88 | } |
| 89 | } |
| 90 | /*---------------------------------------------------------------------------*/ |
| 91 | EK_POLLHANDLER(pollhandler) |
| 92 | { |
| 93 | #define BUF ((struct uip_eth_hdr *)&uip_buf[0]) |
| 94 | |
| 95 | /* Poll Ethernet device to see if there is a frame avaliable. */ |
| 96 | uip_len = wpcap_poll(); |
| 97 | if(uip_len > 0) { |
| 98 | /* A frame was avaliable (and is now read into the uip_buf), so |
| 99 | we process it. */ |
| 100 | if(BUF->type == HTONS(UIP_ETHTYPE_IP)) { |
| 101 | uip_arp_ipin(); |
| 102 | uip_len -= sizeof(struct uip_eth_hdr); |
| 103 | tcpip_input(); |
| 104 | } else if(BUF->type == HTONS(UIP_ETHTYPE_ARP)) { |
| 105 | uip_arp_arpin(); |
| 106 | /* If the above function invocation resulted in data that |
| 107 | should be sent out on the network, the global variable |
| 108 | uip_len is set to a value > 0. */ |
| 109 | if(uip_len > 0) { |
| 110 | wpcap_send(); |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | /*---------------------------------------------------------------------------*/ |