summaryrefslogtreecommitdiffstats
path: root/src/repo.c
blob: d2b55cbde9c3d9dcf65eb999abbffd63daa0b8e8 (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
/*
 * Copyright (C) 2020-2021 Soleta Networks <info@soleta.eu>
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License as published by the
 * Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 */

#include <stdint.h>
#include <syslog.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "cfg.h"
#include "list.h"
#include "repo.h"
#include "dbi.h"

static struct og_repo *og_repo_alloc(uint32_t id, const char *name,
				     struct in_addr *addr)
{
	struct og_repo *repo;

	repo = calloc(1, sizeof(struct og_repo));
	if (!repo)
		return NULL;

	repo->name = strdup(name);
	repo->id = id;
	repo->addr[0] = *addr;
	repo->num_ips++;

	return repo;
}

static int og_repo_add_ip(struct og_repo *repo, struct in_addr *addr)
{
	if (repo->num_ips > OG_ADDR_REPO_MAX)
		return -1;

	repo->addr[repo->num_ips++] = *addr;

	return 0;
}

static struct og_repo *og_repo_find(uint32_t id, struct list_head *repo_list)
{
	struct og_repo *repo;

	list_for_each_entry(repo, repo_list, list) {
		if (repo->id == id)
			return repo;
	}

	return NULL;
}

int og_repo_list(struct list_head *repo_list)
{
	const char *name, *ip;
	struct og_repo *repo;
	struct in_addr addr;
	const char *msglog;
	struct og_dbi *dbi;
	uint32_t id, alias;
	dbi_result result;

	dbi = og_dbi_open(&ogconfig.db);
	if (!dbi) {
		syslog(LOG_ERR, "cannot open connection database (%s:%d)\n",
		       __func__, __LINE__);
		return -1;
	}

	result = dbi_conn_queryf(dbi->conn,
				 "SELECT idrepositorio, ip, nombrerepositorio, alias "
				 "FROM repositorios");
	if (!result) {
		dbi_conn_error(dbi->conn, &msglog);
		syslog(LOG_ERR, "failed to query database (%s:%d) %s\n",
		       __func__, __LINE__, msglog);
		og_dbi_close(dbi);
		return -1;
	}

	while (dbi_result_next_row(result)) {
		id = dbi_result_get_ulonglong(result, "idrepositorio");
		ip = dbi_result_get_string(result, "ip");
		name = dbi_result_get_string(result, "nombrerepositorio");
		alias = dbi_result_get_ulonglong(result, "alias");

		if (inet_aton(ip, &addr) < 0) {
			syslog(LOG_ERR, "malformed IP in repo %s\n", name);
			goto err_repo_list;
		}

		if (alias)
			id = alias;

		repo = og_repo_find(id, repo_list);
		if (repo) {
			if (og_repo_add_ip(repo, &addr) < 0) {
				syslog(LOG_ERR, "too many IPs in repo %s\n",
				       repo->name);
				goto err_repo_list;
			}
			continue;
		}

		repo = og_repo_alloc(id, name, &addr);
		if (!repo)
			goto err_repo_list;

		list_add_tail(&repo->list, repo_list);
	}

	dbi_result_free(result);
	og_dbi_close(dbi);

	return 0;

err_repo_list:
	og_repo_free_list(repo_list);
	dbi_result_free(result);
	og_dbi_close(dbi);

	return -1;
}

void og_repo_free_list(struct list_head *repo_list)
{
	struct og_repo *repo, *next;

	list_for_each_entry_safe(repo, next, repo_list, list) {
		list_del(&repo->list);
		free((void *)repo->name);
		free(repo);
	}
}