summaryrefslogtreecommitdiff
path: root/src/analysis/db/client.c
blob: 946f202aa895ba12f09aeaaa9b7c7281ecee29e4 (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

/* Chrysalide - Outil d'analyse de fichiers binaires
 * client.c - connexion à un serveur Chrysalide
 *
 * Copyright (C) 2014 Cyrille Bagard
 *
 *  This file is part of Chrysalide.
 *
 *  OpenIDA 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.
 *
 *  OpenIDA 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 "client.h"


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


#include "protocol.h"



/* Description de client à l'écoute (instance) */
struct _GDbClient
{
    GObject parent;                         /* A laisser en premier        */

    int fd;
    struct sockaddr_in addr;                /* Adresse d'écoute            */

    GThread *update;                        /* Procédure de traitement     */

};

/* Description de client à l'écoute (classe) */
struct _GDbClientClass
{
    GObjectClass parent;                    /* A laisser en premier        */

};


/* Initialise la classe des descriptions de fichier binaire. */
static void g_db_client_class_init(GDbClientClass *);

/* Initialise une description de fichier binaire. */
static void g_db_client_init(GDbClient *);

/* Procède à la libération totale de la mémoire. */
static void g_db_client_finalize(GDbClient *);

/* Assure l'accueil des nouvelles mises à jour. */
static void *g_db_client_update(GDbClient *);



/* Indique le type défini pour une description de client à l'écoute. */
G_DEFINE_TYPE(GDbClient, g_db_client, G_TYPE_OBJECT);


/******************************************************************************
*                                                                             *
*  Paramètres  : klass = classe à initialiser.                                *
*                                                                             *
*  Description : Initialise la classe des descriptions de fichier binaire.    *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_db_client_class_init(GDbClientClass *klass)
{
    GObjectClass *object;                   /* Autre version de la classe  */

    object = G_OBJECT_CLASS(klass);

    object->finalize = (GObjectFinalizeFunc)g_db_client_finalize;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : client = instance à initialiser.                             *
*                                                                             *
*  Description : Initialise une description de fichier binaire.               *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_db_client_init(GDbClient *client)
{
    client->fd = -1;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : binary = instance d'objet GLib à traiter.                    *
*                                                                             *
*  Description : Procède à la libération totale de la mémoire.                *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_db_client_finalize(GDbClient *binary)
{
    //free(binary->filename);

    G_OBJECT_CLASS(g_db_client_parent_class)->finalize(G_OBJECT(binary));

}


/******************************************************************************
*                                                                             *
*  Paramètres  : host = hôte à représenter pour le service.                   *
*                port = port de connexion pour les clients.                   *
*                                                                             *
*  Description : Prépare un client pour une connexion à une BD.               *
*                                                                             *
*  Retour      : Structure mise en plae ou NULL en cas d'échec.               *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

GDbClient *g_db_client_new(const char *host, short port)
{
    GDbClient *result;                      /* Adresse à retourner         */
    struct hostent *hp;                     /* Informations sur l'hôte     */

    result = g_object_new(G_TYPE_DB_CLIENT, NULL);

    hp = gethostbyname(host);
    if (hp == NULL) goto gdsn_error;

    result->addr.sin_family = hp->h_addrtype;
    memcpy(&result->addr.sin_addr, hp->h_addr_list[0], sizeof(struct in_addr));

    result->addr.sin_port = htons(port);

    return result;

 gdsn_error:

    g_object_unref(G_OBJECT(result));

    return NULL;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : client = client pour les accès distants à manipuler.         *
*                                                                             *
*  Description : Assure l'accueil des nouvelles mises à jour.                 *
*                                                                             *
*  Retour      : NULL.                                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void *g_db_client_update(GDbClient *client)
{
    struct pollfd fds;                      /* Surveillance des flux       */
    int ret;                                /* Bilan d'un appel            */

    fds.fd = client->fd;
    fds.events = POLLIN | POLLPRI;

    while (1)
    {
        ret = poll(&fds, 1, -1);
        if (ret != 1) continue;

        printf("fds.revents :: %x\n", fds.revents);

        /* Le canal est fermé, une sortie doit être demandée... */
        if (fds.revents & POLLNVAL)
            break;

        if (fds.revents & (POLLIN | POLLPRI))
        {

            /* TODO */
            pause();


        }

    }

    g_db_client_stop(client);

    return NULL;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : client = client pour les accès distants à manipuler.         *
*                                                                             *
*  Description : Démarre la connexion à la base de données.                   *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool g_db_client_start(GDbClient *client)
{
    int ret;                                /* Bilan d'un appel            */

    client->fd = socket(AF_INET, SOCK_STREAM, 0);
    if (client->fd == -1)
    {
        perror("socket");
        return false;
    }

    ret = connect(client->fd, (struct sockaddr *)&client->addr, sizeof(struct sockaddr_in));
    if (ret == -1)
    {
        perror("connect");
        close(client->fd);
        client->fd = -1;
        return false;
    }

    //client->update = g_thread_new("cdb_listener", (GThreadFunc)g_db_client_update, client);

    send(client->fd, "A", 1, 0);

    return true;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : client = client pour les accès distants à manipuler.         *
*                                                                             *
*  Description : Arrête la connexion à la base de données.                    *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void g_db_client_stop(GDbClient *client)
{
    if (client->fd != -1)
        return;

    close(client->fd);
    client->fd = -1;

    g_thread_join(client->update);

}