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 | |
---|
15 | struct og_dbi *og_dbi_open(struct og_dbi_config *config) |
---|
16 | { |
---|
17 | struct og_dbi *dbi; |
---|
18 | |
---|
19 | dbi = (struct og_dbi *)malloc(sizeof(struct og_dbi)); |
---|
20 | if (!dbi) |
---|
21 | return NULL; |
---|
22 | |
---|
23 | dbi_initialize_r(NULL, &dbi->inst); |
---|
24 | dbi->conn = dbi_conn_new_r("mysql", dbi->inst); |
---|
25 | if (!dbi->conn) { |
---|
26 | free(dbi); |
---|
27 | return NULL; |
---|
28 | } |
---|
29 | |
---|
30 | dbi_conn_set_option(dbi->conn, "host", config->host); |
---|
31 | dbi_conn_set_option(dbi->conn, "port", config->port); |
---|
32 | dbi_conn_set_option(dbi->conn, "username", config->user); |
---|
33 | dbi_conn_set_option(dbi->conn, "password", config->passwd); |
---|
34 | dbi_conn_set_option(dbi->conn, "dbname", config->database); |
---|
35 | dbi_conn_set_option(dbi->conn, "encoding", "UTF-8"); |
---|
36 | |
---|
37 | if (dbi_conn_connect(dbi->conn) < 0) { |
---|
38 | free(dbi); |
---|
39 | return NULL; |
---|
40 | } |
---|
41 | |
---|
42 | return dbi; |
---|
43 | } |
---|
44 | |
---|
45 | void og_dbi_close(struct og_dbi *dbi) |
---|
46 | { |
---|
47 | dbi_conn_close(dbi->conn); |
---|
48 | dbi_shutdown_r(dbi->inst); |
---|
49 | free(dbi); |
---|
50 | } |
---|
51 | |
---|
52 | int og_dbi_get_computer_info(struct og_dbi *dbi, struct og_computer *computer, |
---|
53 | struct in_addr addr) |
---|
54 | { |
---|
55 | const char *msglog; |
---|
56 | dbi_result result; |
---|
57 | |
---|
58 | result = dbi_conn_queryf(dbi->conn, |
---|
59 | "SELECT ordenadores.idordenador," |
---|
60 | " ordenadores.nombreordenador," |
---|
61 | " ordenadores.idaula," |
---|
62 | " ordenadores.idproautoexec," |
---|
63 | " centros.idcentro FROM ordenadores " |
---|
64 | "INNER JOIN aulas ON aulas.idaula=ordenadores.idaula " |
---|
65 | "INNER JOIN centros ON centros.idcentro=aulas.idcentro " |
---|
66 | "WHERE ordenadores.ip='%s'", inet_ntoa(addr)); |
---|
67 | if (!result) { |
---|
68 | dbi_conn_error(dbi->conn, &msglog); |
---|
69 | syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", |
---|
70 | __func__, __LINE__, msglog); |
---|
71 | return -1; |
---|
72 | } |
---|
73 | if (!dbi_result_next_row(result)) { |
---|
74 | syslog(LOG_ERR, "client does not exist in database (%s:%d)\n", |
---|
75 | __func__, __LINE__); |
---|
76 | dbi_result_free(result); |
---|
77 | return -1; |
---|
78 | } |
---|
79 | |
---|
80 | computer->id = dbi_result_get_uint(result, "idordenador"); |
---|
81 | computer->center = dbi_result_get_uint(result, "idcentro"); |
---|
82 | computer->room = dbi_result_get_uint(result, "idaula"); |
---|
83 | computer->procedure_id = dbi_result_get_uint(result, "idproautoexec"); |
---|
84 | strncpy(computer->name, |
---|
85 | dbi_result_get_string(result, "nombreordenador"), |
---|
86 | OG_DB_COMPUTER_NAME_MAXLEN); |
---|
87 | |
---|
88 | dbi_result_free(result); |
---|
89 | |
---|
90 | return 0; |
---|
91 | } |
---|