source: ogServer-Git/src/dbi.c @ d89d6c6

Last change on this file since d89d6c6 was cbd9421, checked in by OpenGnSys Support Team <soporte-og@…>, 4 years ago

#941 Extend og_dbi_get_computer_info(...)

For the strings in og_computer we do not need to know the max size in
advance but instead we need to free up memmory using
og_dbi_free_computer_info(...) function.

  • Property mode set to 100644
File size: 4.0 KB
Line 
1/*
2 * Copyright (C) 2020 Soleta Networks <info@soleta.eu>
3 *
4 * This program is free software: you can redistribute it and/or modify it under
5 * the terms of the GNU Affero General Public License as published by the
6 * Free Software Foundation, version 3.
7 */
8
9#include <syslog.h>
10#include <netinet/in.h>
11#include <arpa/inet.h>
12#include <string.h>
13#include "dbi.h"
14#include <syslog.h>
15#include <string.h>
16
17struct og_dbi *og_dbi_open(struct og_dbi_config *config)
18{
19        struct og_dbi *dbi;
20
21        dbi = (struct og_dbi *)malloc(sizeof(struct og_dbi));
22        if (!dbi)
23                return NULL;
24
25        dbi_initialize_r(NULL, &dbi->inst);
26        dbi->conn = dbi_conn_new_r("mysql", dbi->inst);
27        if (!dbi->conn) {
28                free(dbi);
29                return NULL;
30        }
31
32        dbi_conn_set_option(dbi->conn, "host", config->host);
33        dbi_conn_set_option(dbi->conn, "port", config->port);
34        dbi_conn_set_option(dbi->conn, "username", config->user);
35        dbi_conn_set_option(dbi->conn, "password", config->passwd);
36        dbi_conn_set_option(dbi->conn, "dbname", config->database);
37        dbi_conn_set_option(dbi->conn, "encoding", "UTF-8");
38
39        if (dbi_conn_connect(dbi->conn) < 0) {
40                free(dbi);
41                return NULL;
42        }
43
44        return dbi;
45}
46
47void og_dbi_close(struct og_dbi *dbi)
48{
49        dbi_conn_close(dbi->conn);
50        dbi_shutdown_r(dbi->inst);
51        free(dbi);
52}
53
54int og_dbi_get_computer_info(struct og_dbi *dbi, struct og_computer *computer,
55                             struct in_addr addr)
56{
57        const char *msglog;
58        dbi_result result;
59
60        result = dbi_conn_queryf(dbi->conn,
61                                 "SELECT ordenadores.idordenador,"
62                                 "       ordenadores.nombreordenador,"
63                                 "       ordenadores.numserie,"
64                                 "       ordenadores.ip,"
65                                 "       ordenadores.mac,"
66                                 "       ordenadores.idaula,"
67                                 "       ordenadores.idperfilhard,"
68                                 "       ordenadores.idrepositorio,"
69                                 "       ordenadores.mascara,"
70                                 "       ordenadores.arranque,"
71                                 "       ordenadores.netiface,"
72                                 "       ordenadores.netdriver,"
73                                 "       ordenadores.idproautoexec,"
74                                 "       ordenadores.oglivedir,"
75                                 "       ordenadores.inremotepc,"
76                                 "       ordenadores.maintenance,"
77                                 "       centros.idcentro "
78                                 "FROM ordenadores "
79                                 "INNER JOIN aulas ON aulas.idaula=ordenadores.idaula "
80                                 "INNER JOIN centros ON centros.idcentro=aulas.idcentro "
81                                 "WHERE ordenadores.ip='%s'", inet_ntoa(addr));
82        if (!result) {
83                dbi_conn_error(dbi->conn, &msglog);
84                syslog(LOG_ERR, "failed to query database (%s:%d) %s\n",
85                       __func__, __LINE__, msglog);
86                return -1;
87        }
88        if (!dbi_result_next_row(result)) {
89                syslog(LOG_ERR, "client does not exist in database (%s:%d)\n",
90                       __func__, __LINE__);
91                dbi_result_free(result);
92                return -1;
93        }
94
95        computer->id = dbi_result_get_uint(result, "idordenador");
96        computer->name = strdup(dbi_result_get_string(result, "nombreordenador"));
97        computer->serial_number = strdup(dbi_result_get_string(result, "numserie"));
98        computer->ip = strdup(dbi_result_get_string(result, "ip"));
99        computer->mac = strdup(dbi_result_get_string(result, "mac"));
100        computer->room = dbi_result_get_uint(result, "idaula");
101        computer->hardware_id = dbi_result_get_uint(result, "idperfilhard");
102        computer->repo_id = dbi_result_get_uint(result, "idrepositorio");
103        computer->netmask = strdup(dbi_result_get_string(result, "mascara"));
104        computer->boot = strdup(dbi_result_get_string(result, "arranque"));
105        computer->netiface = strdup(dbi_result_get_string(result, "netiface"));
106        computer->netdriver = strdup(dbi_result_get_string(result, "netdriver"));
107        computer->procedure_id = dbi_result_get_uint(result, "idproautoexec");
108        computer->livedir = strdup(dbi_result_get_string(result, "oglivedir"));
109        computer->remote = dbi_result_get_uint(result, "inremotepc") != 0;
110        computer->maintenance = dbi_result_get_uint(result, "maintenance") != 0;
111
112        dbi_result_free(result);
113
114        return 0;
115}
116
117void og_dbi_free_computer_info(struct og_computer *computer)
118{
119        free(computer->serial_number);
120        free(computer->netdriver);
121        free(computer->netiface);
122        free(computer->netmask);
123        free(computer->livedir);
124        free(computer->name);
125        free(computer->boot);
126        free(computer->mac);
127        free(computer->ip);
128}
Note: See TracBrowser for help on using the repository browser.