diff --git a/ports/win32/msvc8/lwIP pktif.vcproj b/ports/win32/msvc8/lwIP pktif.vcproj index 624e342..63f2881 100644 --- a/ports/win32/msvc8/lwIP pktif.vcproj +++ b/ports/win32/msvc8/lwIP pktif.vcproj @@ -218,6 +218,18 @@ + + + + + + diff --git a/ports/win32/pktdrv.c b/ports/win32/pktdrv.c index 0c76989..41d7932 100644 --- a/ports/win32/pktdrv.c +++ b/ports/win32/pktdrv.c @@ -62,6 +62,8 @@ * */ +#include "pktdrv.h" + #define WIN32_LEAN_AND_MEAN /* get the windows definitions of the following 4 functions out of the way */ #include @@ -78,24 +80,26 @@ #define PACKET_ADAPTER_BUFSIZE 512000 #define PACKET_INPUT_BUFSIZE 256000 -extern void process_input(void); - -/* global variables for only one adapter */ -LPADAPTER lpAdapter; -LPPACKET lpPacket; -char buffer[PACKET_INPUT_BUFSIZE]; /* buffer to hold the data coming from the driver */ -unsigned char *cur_packet; -int cur_length; +struct packet_adapter { + input_fn input; + void *input_fn_arg; + LPADAPTER lpAdapter; + LPPACKET lpPacket; + /* buffer to hold the data coming from the driver */ + char buffer[PACKET_INPUT_BUFSIZE]; +}; /** * Open a network adapter and set it up for packet input * * @param adapter_num the index of the adapter to use * @param mac_addr the MAC address of the adapter is stored here (if != NULL) - * @return 0 on success, -1 on failure + * @param input a function to call to receive a packet + * @param arg argument to pass to input + * @return an adapter handle on success, NULL on failure */ -int -init_adapter(int adapter_num, char* mac_addr) +void* +init_adapter(int adapter_num, char *mac_addr, input_fn input, void *arg) { void *AdapterList[MAX_NUM_ADAPTERS]; int i; @@ -104,8 +108,13 @@ init_adapter(int adapter_num, char* mac_addr) int AdapterNum =0; ULONG AdapterLength; PPACKET_OID_DATA ppacket_oid_data; + struct packet_adapter *pa = malloc(sizeof(struct packet_adapter)); unsigned char ethaddr[ETHARP_HWADDR_LEN]; + memset(pa, 0, sizeof(struct packet_adapter)); + pa->input = input; + pa->input_fn_arg = arg; + memset(AdapterList, 0, sizeof(AdapterList)); memset(AdapterName, 0, sizeof(AdapterName)); @@ -114,7 +123,8 @@ init_adapter(int adapter_num, char* mac_addr) AdapterLength = ADAPTER_NAME_LEN; if (PacketGetAdapterNames((char*)AdapterName, &AdapterLength)==FALSE){ printf("Unable to retrieve the list of the adapters!\n"); - return -1; + free(pa); + return NULL; } /* get the start of each adapter name in the list and put it into @@ -137,86 +147,96 @@ init_adapter(int adapter_num, char* mac_addr) /* print all adapter names */ AdapterNum = i; if (AdapterNum <= 0) { - return -1; /* no adapters found */ + free(pa); + return NULL; /* no adapters found */ } for (i = 0; i < AdapterNum; i++) { printf("%2i: %s\n", i, AdapterList[i]); } /* invalid adapter index -> check this after printing the adapters */ if (adapter_num < 0) { - return -1; + free(pa); + return NULL; } /* adapter index out of range */ if (adapter_num >= AdapterNum) { - return -1; + free(pa); + return NULL; } /* set up the selected adapter */ ppacket_oid_data = malloc(sizeof(PACKET_OID_DATA) + ETHARP_HWADDR_LEN); - lpAdapter = PacketOpenAdapter(AdapterList[adapter_num]); - if (!lpAdapter || (lpAdapter->hFile == INVALID_HANDLE_VALUE)) { - lpAdapter = NULL; - return -1; + pa->lpAdapter = PacketOpenAdapter(AdapterList[adapter_num]); + if (!pa->lpAdapter || (pa->lpAdapter->hFile == INVALID_HANDLE_VALUE)) { + free(pa); + return NULL; } /* get the MAC address of the selected adapter */ ppacket_oid_data->Oid = OID_802_3_PERMANENT_ADDRESS; ppacket_oid_data->Length = ETHARP_HWADDR_LEN; - if (!PacketRequest(lpAdapter, FALSE, ppacket_oid_data)) { - lpAdapter = NULL; - return -1; + if (!PacketRequest(pa->lpAdapter, FALSE, ppacket_oid_data)) { + free(pa); + return NULL; } /* copy the MAC address */ memcpy(ðaddr, ppacket_oid_data->Data, ETHARP_HWADDR_LEN); free(ppacket_oid_data); if (mac_addr != NULL) { /* copy the MAC address to the user supplied buffer, also */ - memcpy(mac_addr, ethaddr, ETHARP_HWADDR_LEN); + memcpy(mac_addr, ðaddr, ETHARP_HWADDR_LEN); } - printf("MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", ethaddr[0], ethaddr[1], ethaddr[2], ethaddr[3], ethaddr[4], ethaddr[5]); + printf("MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", ethaddr[0], ethaddr[1], ethaddr[2], + ethaddr[3], ethaddr[4], ethaddr[5]); /* some more adapter settings */ - PacketSetBuff(lpAdapter, PACKET_ADAPTER_BUFSIZE); - PacketSetReadTimeout(lpAdapter, 1); - PacketSetHwFilter(lpAdapter, NDIS_PACKET_TYPE_ALL_LOCAL | NDIS_PACKET_TYPE_PROMISCUOUS); + PacketSetBuff(pa->lpAdapter, PACKET_ADAPTER_BUFSIZE); + PacketSetReadTimeout(pa->lpAdapter, 1); + PacketSetHwFilter(pa->lpAdapter, NDIS_PACKET_TYPE_ALL_LOCAL | NDIS_PACKET_TYPE_PROMISCUOUS); /* set up packet descriptor (the application input buffer) */ - if ((lpPacket = PacketAllocatePacket()) == NULL) { - lpAdapter = NULL; - return -1; + if ((pa->lpPacket = PacketAllocatePacket()) == NULL) { + free(pa); + return NULL; } - PacketInitPacket(lpPacket,(char*)buffer, sizeof(buffer)); + PacketInitPacket(pa->lpPacket,(char*)pa->buffer, sizeof(pa->buffer)); - return 0; + return pa; } /** - * Close the adapter (no more packets can be sent or received + * Close the adapter (no more packets can be sent or received) + * + * @param adapter adapter handle received by a call to init_adapter, invalid on return */ void -shutdown_adapter(void) +shutdown_adapter(void *adapter) { - if (lpAdapter != NULL) { - PacketFreePacket(lpPacket); - PacketCloseAdapter(lpAdapter); + struct packet_adapter *pa = (struct packet_adapter*)adapter; + if (pa != NULL) { + PacketFreePacket(pa->lpPacket); + PacketCloseAdapter(pa->lpAdapter); + free(pa); } } /** * Send a packet * + * @param adapter adapter handle received by a call to init_adapter * @param buffer complete packet to send (including ETH header; without CRC) * @param len length of the packet (including ETH header; without CRC) */ int -packet_send(void *buffer, int len) +packet_send(void *adapter, void *buffer, int len) { + struct packet_adapter *pa = (struct packet_adapter*)adapter; LPPACKET lpPacket; - if (lpAdapter == NULL) { + if (pa == NULL) { return -1; } if ((lpPacket = PacketAllocatePacket()) == NULL) { return -1; } PacketInitPacket(lpPacket, buffer, len); - if (!PacketSendPacket(lpAdapter, lpPacket, TRUE)) { + if (!PacketSendPacket(pa->lpAdapter, lpPacket, TRUE)) { return -1; } PacketFreePacket(lpPacket); @@ -228,16 +248,25 @@ packet_send(void *buffer, int len) * Process a packet buffer (which can hold multiple packets) and feed * every packet to process_input(). * - * @param lpPacket the packet buffer to process */ + * @param adapter adapter handle received by a call to init_adapter + * @param lpPacket the packet buffer to process + */ static void -ProcessPackets(LPPACKET lpPacket) +ProcessPackets(void *adapter, LPPACKET lpPacket) { + struct packet_adapter *pa = (struct packet_adapter*)adapter; ULONG ulLines, ulBytesReceived; char *base; char *buf; u_int off = 0; u_int tlen, tlen1; struct bpf_hdr *hdr; + void *cur_packet; + int cur_length; + + if (pa == NULL) { + return; + } ulBytesReceived = lpPacket->ulBytesReceived; @@ -261,18 +290,22 @@ ProcessPackets(LPPACKET lpPacket) cur_packet = base; off = Packet_WORDALIGN(off + tlen); - process_input(); + pa->input(pa->input_fn_arg, cur_packet, cur_length); } } /** * Check for newly received packets. Called in the main loop: 'interrupt' mode is not * really supported :( + * + * @param adapter adapter handle received by a call to init_adapter */ void -update_adapter(void) +update_adapter(void *adapter) { - if ((lpAdapter != NULL) && (PacketReceivePacket(lpAdapter, lpPacket, TRUE))) { - ProcessPackets(lpPacket); + struct packet_adapter *pa = (struct packet_adapter*)adapter; + + if ((pa != NULL) && (PacketReceivePacket(pa->lpAdapter, pa->lpPacket, TRUE))) { + ProcessPackets(adapter, pa->lpPacket); } } diff --git a/ports/win32/pktdrv.h b/ports/win32/pktdrv.h new file mode 100644 index 0000000..6bd54a4 --- /dev/null +++ b/ports/win32/pktdrv.h @@ -0,0 +1,19 @@ +#ifndef __PKTDRV_H__ +#define __PKTDRV_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +typedef void (*input_fn)(void *arg, void *packet, int len); + +void *init_adapter(int adapter_num, char *mac_addr, input_fn input, void *arg); +void shutdown_adapter(void *adapter); +int packet_send(void *adapter, void *buffer, int len); +void update_adapter(void *adapter); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/ports/win32/pktif.c b/ports/win32/pktif.c index 5cef559..5c6108f 100644 --- a/ports/win32/pktif.c +++ b/ports/win32/pktif.c @@ -62,6 +62,8 @@ * */ +#include "pktif.h" + /* get the windows definitions of the following 4 functions out of the way */ #include #include @@ -78,6 +80,7 @@ #include "lwip/snmp.h" #include "netif/etharp.h" +#include "pktdrv.h" /* include the port-dependent configuration */ #include "lwipcfg_msvc.h" @@ -94,19 +97,13 @@ #define PACKET_LIB_ADAPTER_NR 0 #endif -static struct eth_addr broadcastaddr = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; +const static struct eth_addr broadcastaddr = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; /* Forward declarations. */ -static void ethernetif_input(struct netif *netif); +void ethernetif_process_input(void *arg, void *packet, int len); static struct netif *pktif_netif; -extern unsigned char ethaddr[ETHARP_HWADDR_LEN]; -extern unsigned char *cur_packet; -extern int cur_length; -extern int init_adapter(int adapter_num, char* mac_addr); -extern int packet_send(void *buffer, int len); - /*-----------------------------------------------------------------------------------*/ static void low_level_init(struct netif *netif) @@ -116,7 +113,8 @@ low_level_init(struct netif *netif) LWIP_DEBUGF(NETIF_DEBUG, ("pktif: eth_addr %02X%02X%02X%02X%02X%02X\n",netif->hwaddr[0],netif->hwaddr[1],netif->hwaddr[2],netif->hwaddr[3],netif->hwaddr[4],netif->hwaddr[5])); /* Do whatever else is needed to initialize interface. */ - if (init_adapter(PACKET_LIB_ADAPTER_NR, mac_addr) != 0) { + if ((netif->state = init_adapter(PACKET_LIB_ADAPTER_NR, mac_addr, + ethernetif_process_input, netif)) == NULL) { printf("ERROR initializing network adapter %d!\n", PACKET_LIB_ADAPTER_NR); return; } @@ -139,7 +137,7 @@ low_level_init(struct netif *netif) /*-----------------------------------------------------------------------------------*/ static err_t -low_level_output(struct netif *ethernetif, struct pbuf *p) +low_level_output(struct netif *netif, struct pbuf *p) { struct pbuf *q; unsigned char buffer[1600]; @@ -161,7 +159,7 @@ low_level_output(struct netif *ethernetif, struct pbuf *p) } /* signal that packet should be sent(); */ - if (packet_send(buffer, p->tot_len) < 0) { + if (packet_send(netif->state, buffer, p->tot_len) < 0) { return ERR_BUF; } @@ -178,26 +176,24 @@ low_level_output(struct netif *ethernetif, struct pbuf *p) */ /*-----------------------------------------------------------------------------------*/ static struct pbuf * -low_level_input(struct netif *netif) +low_level_input(struct netif *netif, void *packet, int packet_len) { struct pbuf *p, *q; int start, length; struct eth_hdr *ethhdr; /* Obtain the size of the packet and put it into the "len" variable. */ - length = cur_length; - if (length<=0) { + length = packet_len; + if (length <= 0) { return NULL; } - ethhdr = (struct eth_hdr*)cur_packet; + ethhdr = (struct eth_hdr*)packet; /* MAC filter: only let my MAC or non-unicast through */ if (((memcmp(ðhdr->dest, &netif->hwaddr, ETHARP_HWADDR_LEN)) && ((ethhdr->dest.addr[0] & 0x01) == 0)) || /* and don't let feedback packets through (limitation in winpcap?) */ !memcmp(ðhdr->src, netif->hwaddr, ETHARP_HWADDR_LEN)) { - /* acknowledge that packet has been read(); */ - cur_length=0; return NULL; } @@ -215,19 +211,16 @@ low_level_input(struct netif *netif) variable. */ /* read data into(q->payload, q->len); */ LWIP_DEBUGF(NETIF_DEBUG, ("netif: recv start %i length %i q->payload %p q->len %i q->next %p\n", start, length, q->payload, (int)q->len, q->next)); - memcpy(q->payload, &cur_packet[start], q->len); + memcpy(q->payload, &((char*)packet)[start], q->len); start += q->len; length -= q->len; if (length<=0) { break; } } - /* acknowledge that packet has been read(); */ - cur_length = 0; LINK_STATS_INC(link.recv); } else { /* drop packet(); */ - cur_length = 0; LINK_STATS_INC(link.memerr); LINK_STATS_INC(link.drop); } @@ -247,16 +240,13 @@ low_level_input(struct netif *netif) */ /*-----------------------------------------------------------------------------------*/ static void -ethernetif_input(struct netif *netif) +ethernetif_input(struct netif *netif, void *packet, int packet_len) { - struct ethernetif *ethernetif; struct eth_hdr *ethhdr; struct pbuf *p; - ethernetif = netif->state; - /* move received packet into a new pbuf */ - p = low_level_input(netif); + p = low_level_input(netif, packet, packet_len); /* no packet could be read, silently ignore this */ if (p == NULL) { return; @@ -315,6 +305,19 @@ ethernetif_init(struct netif *netif) return ERR_OK; } + +void +ethernetif_shutdown(struct netif *netif) +{ + shutdown_adapter(netif->state); +} + +void +ethernetif_poll(struct netif *netif) +{ + update_adapter(netif->state); +} + /*-----------------------------------------------------------------------------------*/ /* * pktif_update(): @@ -323,7 +326,9 @@ ethernetif_init(struct netif *netif) * be done inside a thread. */ /*-----------------------------------------------------------------------------------*/ -void process_input(void) +void +ethernetif_process_input(void *arg, void *packet, int packet_len) { - ethernetif_input(pktif_netif); + struct netif *netif = (struct netif*)arg; + ethernetif_input(netif, packet, packet_len); } diff --git a/ports/win32/pktif.h b/ports/win32/pktif.h new file mode 100644 index 0000000..7193caa --- /dev/null +++ b/ports/win32/pktif.h @@ -0,0 +1,10 @@ +#ifndef __PKTIF_H__ +#define __PKTIF_H__ + +#include "lwip/err.h" + +err_t ethernetif_init(struct netif *netif); +void ethernetif_shutdown(struct netif *netif); +void ethernetif_poll(struct netif *netif); + +#endif diff --git a/ports/win32/test.c b/ports/win32/test.c index a69ac3c..2d3d655 100644 --- a/ports/win32/test.c +++ b/ports/win32/test.c @@ -49,6 +49,7 @@ #include "lwip/tcp.h" #include "lwip/udp.h" #include "lwip/dns.h" +#include "lwip/dhcp.h" /* lwIP netif includes */ #include "netif/loopif.h" @@ -69,13 +70,11 @@ #include "lwip/autoip.h" #endif /* NO_SYS */ +#include "pktif.h" + /* include the port-dependent configuration */ #include "lwipcfg_msvc.h" -/* some forward function definitions... */ -err_t ethernetif_init(struct netif *netif); -void shutdown_adapter(void); -void update_adapter(void); #if NO_SYS /* port-defined functions used for timer execution */ @@ -169,25 +168,7 @@ msvc_netif_init() struct ip_addr ipaddr, netmask, gw; #if LWIP_HAVE_LOOPIF struct ip_addr loop_ipaddr, loop_netmask, loop_gw; -#endif /* LWIP_HAVE_LOOPIF */ - LWIP_PORT_INIT_GW(&gw); - LWIP_PORT_INIT_IPADDR(&ipaddr); - LWIP_PORT_INIT_NETMASK(&netmask); - printf("Starting lwIP, local interface IP is %s\n", inet_ntoa(*(struct in_addr*)&ipaddr)); - -#if NO_SYS -#if LWIP_ARP - netif_set_default(netif_add(&netif, &ipaddr, &netmask, &gw, NULL, ethernetif_init, ethernet_input)); -#else /* LWIP_ARP */ - netif_set_default(netif_add(&netif, &ipaddr, &netmask, &gw, NULL, ethernetif_init, ip_input)); -#endif /* LWIP_ARP */ -#else /* NO_SYS */ - netif_set_default(netif_add(&netif, &ipaddr, &netmask, &gw, NULL, ethernetif_init, tcpip_input)); -#endif /* NO_SYS */ - netif_set_up(&netif); - -#if LWIP_HAVE_LOOPIF IP4_ADDR(&loop_gw, 127,0,0,1); IP4_ADDR(&loop_ipaddr, 127,0,0,1); IP4_ADDR(&loop_netmask, 255,0,0,0); @@ -200,10 +181,38 @@ msvc_netif_init() #endif /* NO_SYS */ netif_set_up(&loop_netif); #endif /* LWIP_HAVE_LOOPIF */ + +#if LWIP_DHCP + gw.addr = 0; + ipaddr.addr = 0; + netmask.addr = 0; + printf("Starting lwIP, local interface IP is dhcp-enabled\n"); +#else /* LWIP_DHCP */ + LWIP_PORT_INIT_GW(&gw); + LWIP_PORT_INIT_IPADDR(&ipaddr); + LWIP_PORT_INIT_NETMASK(&netmask); + printf("Starting lwIP, local interface IP is %s\n", inet_ntoa(*(struct in_addr*)&ipaddr)); +#endif /* LWIP_DHCP */ + +#if NO_SYS +#if LWIP_ARP + netif_set_default(netif_add(&netif, &ipaddr, &netmask, &gw, NULL, ethernetif_init, ethernet_input)); +#else /* LWIP_ARP */ + netif_set_default(netif_add(&netif, &ipaddr, &netmask, &gw, NULL, ethernetif_init, ip_input)); +#endif /* LWIP_ARP */ +#else /* NO_SYS */ + netif_set_default(netif_add(&netif, &ipaddr, &netmask, &gw, NULL, ethernetif_init, tcpip_input)); +#endif /* NO_SYS */ +#if LWIP_DHCP + dhcp_start(&netif); +#else /* LWIP_DHCP */ + netif_set_up(&netif); +#endif /* LWIP_DHCP */ } void dns_found(const char *name, struct ip_addr *addr, void *arg) -{ printf("%s: %s\n", name, addr?inet_ntoa(*(struct in_addr*)addr):""); +{ + printf("%s: %s\n", name, addr?inet_ntoa(*(struct in_addr*)addr):""); } /* This function initializes applications */ @@ -218,7 +227,7 @@ apps_init() } #endif /* LWIP_DNS */ -#if LWIP_RAW +#if LWIP_RAW && LWIP_ICMP ping_init(); #endif /* LWIP_RAW */ @@ -292,11 +301,11 @@ void main_loop() #endif /* NO_SYS */ /* check for packets */ - update_adapter(); + ethernetif_poll(&netif); } /* release the pcap library... */ - shutdown_adapter(); + ethernetif_shutdown(&netif); } int main(void)