]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/plr/server/src/gdb_stub/tcp_connection.cc
update
[l4.git] / l4 / pkg / plr / server / src / gdb_stub / tcp_connection.cc
1 /*
2  * gdb_stub/tcp_connection.cc --
3  *
4  *     Connectivity through TCP/IP
5  *
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.
11  */
12
13 #include "../log"
14 #include "../exceptions"
15 #include "connection"
16
17 #include <l4/util/util.h>
18
19 #define MSG() DEBUGf(Romain::Log::Gdb)
20
21 void Romain::TCPConnection::setup_and_wait()
22 {
23         /* XXX make configurable */
24         char const *argv[] =  {"--shm","shm","--bufsize","16384",0};
25         int argc = 4;
26         static int firstTime = 0;
27
28         if (!firstTime) {
29                 /* Initialize lwIP NIC */
30                 l4_lwip_init(&argc,(char **)argv);
31
32                 while(!netif_is_up(&ankh_netif)) {
33                         MSG() << "waiting for DHCP to finish...";
34                         l4_sleep(2000);
35                 }
36
37                 MSG() << "lwIP init done.";
38         }
39
40         /*
41          * Create socket and wait for connection
42          */
43         _inet.sin_len = 0;
44         _inet.sin_family = AF_INET;
45         _inet.sin_port = htons(port());
46         _inet.sin_addr.s_addr = ankh_netif.ip_addr.addr;
47
48         _socket = socket(PF_INET, SOCK_STREAM, 0);
49         MSG() << "socket(): " << _socket;
50         _check(_socket == -1, "socket()");
51
52         int err = bind(_socket, (struct sockaddr*)&_inet, sizeof(_inet));
53         MSG() << "bound: " << err;
54         _check(err < 0, "bind()");
55
56         err = listen(_socket, 10);
57         MSG() << "listen: " << err;
58         _check(err == -1, "listen()");
59
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()");
64 }
65
66
67 void Romain::TCPConnection::disconnect()
68 {
69         //close(_socket_fd);
70         close(_socket);
71         memset(&_inet, 0, sizeof(_inet));
72         memset(&_clnt, 0, sizeof(_clnt));
73 }
74
75
76 int Romain::TCPConnection::wait_for_cmd(char *const packetbuf, unsigned bufsize)
77 {
78         int readBytes = 0;
79
80         memset(packetbuf, 0, bufsize);
81
82         MSG() << "reading from remote";
83         readBytes = read(_socket_fd, packetbuf, bufsize);
84         if (readBytes <= 0) {
85                 printf("read_socket(): %d\n", readBytes);
86                 return -1;
87         }
88
89         return readBytes;
90 }
91
92
93 int Romain::TCPConnection::senddata(char const * const buf, unsigned size)
94 {
95         return ::write(_socket_fd, buf, size);
96 }