summaryrefslogtreecommitdiff
path: root/src/debug/remgdb/tcp.c
blob: aa10c54a0a1d28a309a9ba08dfda43b97a3a60e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263

/* Chrysalide - Outil d'analyse de fichiers binaires
 * tcp.c - gestion des connexions TCP aux serveurs GDB.
 *
 * Copyright (C) 2009-2017 Cyrille Bagard
 *
 *  This file is part of Chrysalide.
 *
 *  Chrysalide is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Chrysalide is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
 */


#include "tcp.h"


#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>


#include "stream-int.h"


#include "../../common/net.h"


/* Flux de communication TCP avec un serveur GDB (instance) */
struct _GGdbTcpClient
{
    GGdbStream parent;                      /* A laisser en premier        */

};


/* Flux de communication TCP avec un serveur GDB (classe) */
struct _GGdbTcpClientClass
{
    GGdbStreamClass parent;                 /* A laisser en premier        */

};


/* Initialise la classe des flux de communication TCP avec GDB. */
static void g_gdb_tcp_client_class_init(GGdbTcpClientClass *);

/* Initialise une instance de flux de communication avec GDB. */
static void g_gdb_tcp_client_init(GGdbTcpClient *);

/* Ouvre une connexion TCP à un serveur GDB. */
//static int connect_via_tcp(const char *, const char *);

/* Envoie des données à un serveur GDB. */
static bool g_gdb_tcp_client_send_data(GGdbTcpClient *, const char *, size_t);

/* Réceptionne un octet de donnée d'un serveur GDB. */
static bool g_gdb_tcp_client_recv_byte(GGdbTcpClient *, char *);



/* Indique le type défini pour un flux de communication TCP avec un serveur GDB. */
G_DEFINE_TYPE(GGdbTcpClient, g_gdb_tcp_client, G_TYPE_GDB_STREAM);


/******************************************************************************
*                                                                             *
*  Paramètres  : klass = classe à initialiser.                                *
*                                                                             *
*  Description : Initialise la classe des flux de communication TCP avec GDB. *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_gdb_tcp_client_class_init(GGdbTcpClientClass *klass)
{

}


/******************************************************************************
*                                                                             *
*  Paramètres  : client = instance à initialiser.                             *
*                                                                             *
*  Description : Initialise une instance de flux de communication avec GDB.   *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_gdb_tcp_client_init(GGdbTcpClient *client)
{
    GGdbStream *stream;                     /* Version parente             */

    stream = G_GDB_STREAM(client);

    stream->send_data = (send_gdb_data_fc)g_gdb_tcp_client_send_data;
    stream->recv_byte = (recv_gdb_byte_fc)g_gdb_tcp_client_recv_byte;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : server = nom ou adresse du serveur à contacter.              *
*                port   = port de connexion.                                  *
*                                                                             *
*  Description : Ouvre une connexion TCP à un serveur GDB.                    *
*                                                                             *
*  Retour      : Flux ouvert en lecture/écriture ou -1 en cas d'échec.        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/
#if 0
static int connect_via_tcp(const char *server, const char *port)
{
    int result;                             /* Bilan à retourner           */
    struct addrinfo hints;                  /* Type de connexion souhaitée */
    struct addrinfo *infos;                 /* Informations disponibles    */
    int ret;                                /* Bilan d'un appel            */
    struct addrinfo *iter;                  /* Boucle de parcours          */
    struct sockaddr_in addr;                /* Infos de connexion distante */          

    memset(&hints, 0, sizeof(struct addrinfo));

    hints.ai_family = AF_UNSPEC;        /* IPv4 ou IPv6 */
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = 0;
    hints.ai_protocol = 0;              /* N'importe quel protocole */

    ret = getaddrinfo(server, port, &hints, &infos);
    if (ret != 0)
    {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret));
        return -1;
    }

    for (iter = infos; iter != NULL; iter = iter->ai_next)
    {
        result = socket(iter->ai_family, iter->ai_socktype, iter->ai_protocol);
        if (result == -1) continue;

        ret = connect(result, iter->ai_addr, iter->ai_addrlen);
        if (ret == 0) break;

        perror("connect");
        close(result);

    }

    freeaddrinfo(infos);

    if (iter == NULL) return -1;

    ret = getpeername(result, (struct sockaddr *)&addr, (socklen_t []){ sizeof(struct sockaddr_in) });
    if (ret == -1)
    {
        perror("getpeername");
        close(result);
        return -1;
    }

    printf("Connecté à %s:%hd\n", server, ntohs(addr.sin_port));

    return result;

}
#endif

/******************************************************************************
*                                                                             *
*  Paramètres  : server = nom ou adresse du serveur à contacter.              *
*                port   = port de connexion.                                  *
*                                                                             *
*  Description : Crée une nouvelle connexion TCP à un serveur GDB.            *
*                                                                             *
*  Retour      : Adresse de la structure mise en place.                       *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

GGdbStream *g_gdb_tcp_client_new(const char *server, const char *port)
{
    GGdbTcpClient *result;                  /* Structure à retourner       */
    int sock;                               /* Flux ouvert à construire    */

    sock = connect_via_tcp(server, port, NULL);
    if (sock == -1) return NULL;

    result = g_object_new(G_TYPE_GDB_TCP_CLIENT, NULL);

    G_GDB_STREAM(result)->fd = sock;

    if (!g_gdb_stream_listen(G_GDB_STREAM(result)))
        goto ggtcn_error;

    return G_GDB_STREAM(result);

 ggtcn_error:

    return NULL;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : client = flux ouvert en écriture à utiliser.                 *
*                data   = données à envoyer.                                  *
*                len    = quantité de ces données.                            *
*                                                                             *
*  Description : Envoie des données à un serveur GDB.                         *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool g_gdb_tcp_client_send_data(GGdbTcpClient *client, const char *data, size_t len)
{
    return (send(G_GDB_STREAM(client)->fd, data, len, 0) == len);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : client = flux ouvert en lecture à utiliser.                  *
*                data   = donnée à recevoir.                                  *
*                                                                             *
*  Description : Réceptionne un octet de donnée d'un serveur GDB.             *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool g_gdb_tcp_client_recv_byte(GGdbTcpClient *client, char *data)
{
    return (recv(G_GDB_STREAM(client)->fd, data, 1, 0) == 1);

}