1/** 2 * \file 3 * \brief Echo server 4 */ 5 6/* 7 * Copyright (c) 2007, 2008, 2009, ETH Zurich. 8 * All rights reserved. 9 * 10 * This file is distributed under the terms in the attached LICENSE file. 11 * If you do not find this file, copies can be found by writing to: 12 * ETH Zurich D-INFK, Haldeneggsteig 4, CH-8092 Zurich. Attn: Systems Group. 13 */ 14 15#include <barrelfish/barrelfish.h> 16#include <stdio.h> 17#include <assert.h> 18#include <lwip/netif.h> 19#include <lwip/dhcp.h> 20#include <netif/etharp.h> 21#include <lwip/init.h> 22#include <lwip/udp.h> 23#include <netif/bfeth.h> 24#include "echoserver.h" 25 26extern void idc_print_statistics(void); 27extern void idc_print_cardinfo(void); 28 29extern uint64_t minbase, maxbase; 30 31//called whenever a new datagram for that pcb arrived 32static void echo_recv_handler(void *arg, struct udp_pcb *pcb, struct pbuf *pbuf, 33 struct ip_addr *addr, u16_t port) 34{ 35 if ((pbuf->tot_len > 2) && (pbuf->tot_len < 200)) { 36 if (strncmp(pbuf->payload, "stat", 4) == 0) { 37 idc_print_statistics(); 38 } 39 if (strncmp(pbuf->payload, "cardinfo", 8) == 0) { 40 idc_print_cardinfo(); 41 } 42 } 43 //send the echo 44 struct ip_addr destaddr = *addr; 45 udp_sendto(pcb, pbuf, &destaddr, port); 46 pbuf_free(pbuf); 47} 48 49int udp_echo_server_init(void) 50{ 51 int r; 52 53 uint16_t bind_port = 7; //don't use htons() (don't know why...) 54 55 56 //create a new UDP PCB 57 struct udp_pcb *pcb = udp_new(); //UDP connection data 58 if (pcb == NULL) { 59 return ERR_MEM; 60 } 61 62 //bind it to every IP of every interface and define a specific port to 63 //bind to 64 r = udp_bind(pcb, IP_ADDR_ANY, bind_port); 65 if(r != ERR_OK) { 66 udp_remove(pcb); 67 return(r); 68 } 69 70 printf("udp_echo_server_init(): bound.\n"); 71 //install a callback for received datagrams 72 udp_recv(pcb, echo_recv_handler, 0 /*client data, arg in callback*/); 73 printf("installed receive callback.\n"); 74 75 struct pbuf * test_pbuf = pbuf_alloc(PBUF_RAW, 256, PBUF_RAM); 76 memcpy(test_pbuf->payload, "DDDDDDDDDUUUUUUUUU", 16); 77 78 79 netif_default->linkoutput(netif_default, test_pbuf); 80 return (0); 81} 82