summaryrefslogtreecommitdiff
path: root/src/debug/jdwp/tcp.c
blob: fc926b3fc7d8a86edede2da2e916dbdb0245f619 (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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368

/* Chrysalide - Outil d'analyse de fichiers binaires
 * tcp.c - gestion des connexions TCP aux serveurs JDWP.
 *
 * Copyright (C) 2010-2012 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 <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/types.h>


#include <i18n.h>


#include "packet.h"
#include "misc/header.h"
#include "sets/list.h"
#include "../stream-int.h"
#include "../../common/net.h"
#include "../../gui/panels/log.h"



/* Flux de communication TCP avec un serveur JDWP (instance) */
struct _GJdwpTcpClient
{
    GDebugStream parent;                    /* A laisser en premier        */

    char *server;                           /* Serveur à contacter         */
    char *port;                             /* Port de connexion           */
    int fd;                                 /* Flux ouvert en L./E.        */

};


/* Flux de communication TCP avec un serveur JDWP (classe) */
struct _GJdwpTcpClientClass
{
    GDebugStreamClass parent;               /* A laisser en premier        */

};


/* Initialise la classe des flux de communication JDWP over TCP. */
static void g_jdwp_tcp_client_class_init(GJdwpTcpClientClass *);

/* Initialise une instance de flux de communication avec JDWP. */
static void g_jdwp_tcp_client_init(GJdwpTcpClient *);

/* Etablit de façon effective une connexion à la cible. */
static bool g_jdwp_tcp_client_connect(GJdwpTcpClient *);

/* Attend le signalement de données à traiter. */
static bool g_jdwp_tcp_client_poll(GJdwpTcpClient *);

/* Envoie un paquet de données à un serveur de débogage. */
static bool g_jdwp_tcp_client_send_packet(GJdwpTcpClient *, const GJdwpPacket *);

/* Réceptionne un paquet de données d'un serveur de débogage. */
static bool g_jdwp_tcp_client_recv_packet(GJdwpTcpClient *, GJdwpPacket *);

/* Libère le contenu alloué d'un paquet de débogage. */
static void g_jdwp_tcp_client_free_packet(GJdwpTcpClient *, GJdwpPacket *);



/* Indique le type défini pour un flux de communication TCP avec un serveur JDWP. */
G_DEFINE_TYPE(GJdwpTcpClient, g_jdwp_tcp_client, G_TYPE_DEBUG_STREAM);


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

static void g_jdwp_tcp_client_class_init(GJdwpTcpClientClass *klass)
{

}


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

static void g_jdwp_tcp_client_init(GJdwpTcpClient *client)
{
    GDebugStream *stream;                   /* Version parente             */

    stream = G_DEBUG_STREAM(client);

    stream->connect = (debug_connect_fc)g_jdwp_tcp_client_connect;

    stream->poll = (debug_poll_fc)g_jdwp_tcp_client_poll;
    stream->send_packet = (debug_pkt_op_fc)g_jdwp_tcp_client_send_packet;
    stream->recv_packet = (debug_pkt_op_fc)g_jdwp_tcp_client_recv_packet;
    stream->free_packet = (debug_free_pkt_fc)g_jdwp_tcp_client_free_packet;

    stream->pkt_type = G_TYPE_JDWP_PACKET;

}


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

GDebugStream *g_jdwp_tcp_client_new(const char *server, const char *port)
{
    GJdwpTcpClient *result;                 /* Structure à retourner       */

    result = g_object_new(G_TYPE_JDWP_TCP_CLIENT, NULL);

    result->server = strdup(server);
    result->port = strdup(port);
    result->fd = -1;

    return G_DEBUG_STREAM(result);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : client = paramètres de connexion au serveur JDWP.            *
*                                                                             *
*  Description : Etablit de façon effective une connexion à la cible.         *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool g_jdwp_tcp_client_connect(GJdwpTcpClient *client)
{
    struct sockaddr_in addr;                /* Infos de connexion distante */
    int sock;                               /* Flux ouvert à construire    */
    char handshake[15];                     /* Poignée de main chaleureuse */

    sock = connect_via_tcp(client->server, client->port, &addr);
    if (sock == -1)
    {
        log_variadic_message(LMT_ERROR, _("Error while connecting to the JDWP server at %s:%s."),
                             //printf("Echec de connexion au serveur JDWP sur %s:%s\n",
                             client->server, client->port);
        return false;
    }

    log_variadic_message(LMT_PROCESS, _("Connected to %s:%hd."),
                         client->server, ntohs(addr.sin_port));

    if (send(sock, "JDWP-Handshake", 14, 0) != 14)
        goto gjtcc_error;

    if (recv(sock, handshake, 14, 0) != 14)
        goto gjtcc_error;

    if (strncmp(handshake, "JDWP-Handshake", 14) != 0)
        goto gjtcc_error;

    client->fd = sock;

    return true;

 gjtcc_error:

    log_simple_message(LMT_ERROR, _("Failure in the first JDWP handshake."));

    close(sock);

    return false;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : client = paramètres de connexion au serveur JDWP.            *
*                                                                             *
*  Description : Attend le signalement de données à traiter.                  *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool g_jdwp_tcp_client_poll(GJdwpTcpClient *client)
{
    bool result;                            /* Statut à faire remonter     */
    fd_set rfds;                            /* Liste des flux à surveiller */
    int ret;                                /* Bilan d'un appel            */

    result = false;

    FD_ZERO(&rfds);
    FD_SET(client->fd, &rfds);

    ret = select(client->fd + 1, &rfds, NULL, NULL, NULL);

    switch (ret)
    {
            case -1:
                perror("select()");
                break;

            case 0:
                /* ?! */
                break;

            default:
                result = true;
                break;

    }

    return true;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : client = flux ouvert en écriture à utiliser.                 *
*                packet = zone mémoire à parcourir.                           *
*                                                                             *
*  Description : Envoie un paquet de données à un serveur de débogage.        *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool g_jdwp_tcp_client_send_packet(GJdwpTcpClient *client, const GJdwpPacket *packet)
{
    struct iovec iov[UIO_MAXIOV];           /* Table de vecteurs à écrire  */
    int iovcnt;                             /* Quantité de champs valides  */
    int i;                                  /* Boucle de parcours          */

#if 0
    jdwp_header *header;                    /* En-tête à reconstituer      */

    header = g_jdwp_packet_get_header(packet);
    printf(" <JDWP> send %p :: %u / %hhu.%hhu (%u)\n", packet, header->id, header->set,
           header->command, header->length);
#endif

    g_debug_packet_vectorize(G_DEBUG_PACKET(packet), iov, &iovcnt);

    for (i = 0; i < iovcnt; i++)
        if (send(client->fd, iov[i].iov_base, iov[i].iov_len, 0) != iov[i].iov_len)
            return false;

    return true;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : client = flux ouvert en lecture à utiliser.                  *
*                packet = zone mémoire à remplir. [OUT]                       *
*                                                                             *
*  Description : Réceptionne un paquet de données d'un serveur de débogage.   *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool g_jdwp_tcp_client_recv_packet(GJdwpTcpClient *client, GJdwpPacket *packet)
{
    bin_t *hblob;                           /* Contenu encodé en B.E.      */
    jdwp_header *header;                    /* En-tête à reconstituer      */
    uint32_t length;                        /* Taille de la charge utile   */
    bin_t *pblob;                           /* Contenu encodé en B.E.      */

    hblob = g_jdwp_packet_get_hblob(packet);

    if (recv(client->fd, hblob, sizeof(jdwp_header), 0) != sizeof(jdwp_header))
        return false;

    if (!g_jdwp_packet_parse_header(packet))
        return false;

    header = g_jdwp_packet_get_header(packet);
    length = header->length - sizeof(jdwp_header);

    //printf(" <JDWP> recv %p :: %u / %hu (%u)\n", packet, header->id, header->error, header->length);

    pblob = g_jdwp_packet_get_pblob(packet);

    if (recv(client->fd, pblob, length, 0) != length)
        return false;

    return true;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : client = flux ouvert inutile.                                *
*                packet = zone mémoire à libérer.                             *
*                                                                             *
*  Description : Libère le contenu alloué d'un paquet de débogage.            *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_jdwp_tcp_client_free_packet(GJdwpTcpClient *client, GJdwpPacket *packet)
{
    g_jdwp_packet_free_payload(packet);

}