2 * gdb_stub/tcp_connection.cc --
4 * Connectivity through TCP/IP
6 * (c) 2011-2013 Björn Döbel <doebel@os.inf.tu-dresden.de>,
7 * economic rights: Technische Universität Dresden (Germany)
8 * This file is part of TUD:OS and distributed under the terms of the
9 * GNU General Public License 2.
10 * Please see the COPYING-GPL-2 file for details.
14 #include "../exceptions"
17 #include <l4/util/util.h>
19 #define MSG() DEBUGf(Romain::Log::Gdb)
21 void Romain::TCPConnection::setup_and_wait()
23 /* XXX make configurable */
24 char const *argv[] = {"--shm","shm","--bufsize","16384",0};
26 static int firstTime = 0;
29 /* Initialize lwIP NIC */
30 l4_lwip_init(&argc,(char **)argv);
32 while(!netif_is_up(&ankh_netif)) {
33 MSG() << "waiting for DHCP to finish...";
37 MSG() << "lwIP init done.";
41 * Create socket and wait for connection
44 _inet.sin_family = AF_INET;
45 _inet.sin_port = htons(port());
46 _inet.sin_addr.s_addr = ankh_netif.ip_addr.addr;
48 _socket = socket(PF_INET, SOCK_STREAM, 0);
49 MSG() << "socket(): " << _socket;
50 _check(_socket == -1, "socket()");
52 int err = bind(_socket, (struct sockaddr*)&_inet, sizeof(_inet));
53 MSG() << "bound: " << err;
54 _check(err < 0, "bind()");
56 err = listen(_socket, 10);
57 MSG() << "listen: " << err;
58 _check(err == -1, "listen()");
60 socklen_t size = sizeof(_clnt);
61 _socket_fd = accept(_socket, (struct sockaddr*)&_clnt, &size);
62 MSG() << "accept: " << _socket_fd;
63 _check(_socket_fd == -1, "accept()");
67 void Romain::TCPConnection::disconnect()
71 memset(&_inet, 0, sizeof(_inet));
72 memset(&_clnt, 0, sizeof(_clnt));
76 int Romain::TCPConnection::wait_for_cmd(char *const packetbuf, unsigned bufsize)
80 memset(packetbuf, 0, bufsize);
82 MSG() << "reading from remote";
83 readBytes = read(_socket_fd, packetbuf, bufsize);
85 printf("read_socket(): %d\n", readBytes);
93 int Romain::TCPConnection::senddata(char const * const buf, unsigned size)
95 return ::write(_socket_fd, buf, size);