/* * \brief Ping-server * \author Josef Soentgen * \date 2013-01-24 * */ /* * Copyright (C) 2013 Genode Labs GmbH * * This file is part of the Genode OS framework, which is distributed * under the terms of the GNU General Public License version 2. */ /* Genode includes */ #include #include #include #include /* libc includes */ #include #include #include #include #include #include #include "../pingpong.h" unsigned int verbose; int announce(const char *addr) { int s; struct sockaddr_in in_addr; PLOG("Create new socket..."); s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (s == -1) { PERR("Could not create socket!"); return -1; } PLOG("Bind socket to %d", Sport); in_addr.sin_port = htons(Sport); in_addr.sin_family = AF_INET; in_addr.sin_addr.s_addr = inet_addr(addr); if ( bind(s, (struct sockaddr *)&in_addr, sizeof (in_addr)) == -1) { PERR("Could not bind!"); close(s); return -1; } return s; } int recvping(const char *addr) { int s, c; struct sockaddr caddr; socklen_t lcaddr = sizeof (caddr); s = announce(addr); if (s == -1) return -1; PLOG("Listen on %s:%d...", addr, Sport); if (listen(s, 5) == -1) { PERR("Could not listen!"); close(s); return -1; } while (1) { Packet p; int act; size_t packets; ssize_t n; PINF("wait..."); c = accept(s, &caddr, &lcaddr); if (c == -1) { PERR("Invalid socket from accept()!"); continue; } PLOG("client %d connected...", c); p.d = (char *)malloc(Databuf); if (p.d == NULL) { PERR("Out of memeory!"); close(c); break; } /* receive packets from client */ act = 1; packets = 0; while (act) { n = recvpacket(c, &p, p.d, Databuf); switch (n) { case -1: /* error */ PERR("recvpacket() == -1"); case 0: /* disconnect */ PERR("disconnect"); close(c); act = 0; break; default: /* check if packet is vaid */ if (checkpacket(n, &p)) { act = 0; } break; } if (verbose) PINF("%u %d", p.h.id, n); } PINF("received packets: %u", packets); free(p.d); } close(s); return 0; } int main(int argc, char *argv[]) { char listenip[16]; /* DHCP */ if (lwip_nic_init(0, 0, 0)) { PERR("We got no IP address!"); return 1; } verbose = 0; Genode::Xml_node argv_node = Genode::config()->xml_node().sub_node("argv"); try { argv_node.attribute("listenip" ).value(listenip, sizeof(listenip)); argv_node.attribute("verbose").value( &verbose ); } catch(...) { PERR("listenip was not specified!"); return 1; } recvping(listenip); return 0; }