sw-collector: Cast chunk length to int when printing as string
[strongswan.git] / src / sw-collector / sw_collector_info.c
1 /*
2 * Copyright (C) 2017 Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #define _GNU_SOURCE
17 #include <stdio.h>
18
19 #include "sw_collector_info.h"
20
21 #include <library.h>
22 #include <utils/lexparser.h>
23
24 typedef struct private_sw_collector_info_t private_sw_collector_info_t;
25
26 /**
27 * Private data of an sw_collector_info_t object.
28 */
29 struct private_sw_collector_info_t {
30
31 /**
32 * Public members of sw_collector_info_state_t
33 */
34 sw_collector_info_t public;
35
36 /**
37 * tagCreator
38 */
39 char *tag_creator;
40
41 /**
42 * OS string 'Name_Version-Arch'
43 */
44 char *os;
45
46 /**
47 * Product string 'Name Version Arch'
48 */
49 char *product;
50
51 /**
52 * OS info about endpoint
53 */
54 imc_os_info_t *os_info;
55
56 };
57
58 /**
59 * Replaces invalid character by a valid one
60 */
61 static void sanitize_uri(char *uri, char a, char b)
62 {
63 char *pos = uri;
64
65 while (TRUE)
66 {
67 pos = strchr(pos, a);
68 if (!pos)
69 {
70 break;
71 }
72 *pos = b;
73 pos++;
74 }
75 }
76
77 METHOD(sw_collector_info_t, get_os_type, os_type_t,
78 private_sw_collector_info_t *this)
79 {
80 return this->os_info->get_type(this->os_info);
81 }
82
83 METHOD(sw_collector_info_t, get_os, char*,
84 private_sw_collector_info_t *this, char **product)
85 {
86 if (product)
87 {
88 *product = this->product;
89 }
90 return this->os;
91 }
92
93 METHOD(sw_collector_info_t, create_sw_id, char*,
94 private_sw_collector_info_t *this, char *package, char *version)
95 {
96 char *sw_id;
97
98 if (asprintf(&sw_id, "%s__%s-%s%s%s", this->tag_creator, this->os,
99 package, strlen(version) ? "-" : "", version) == -1)
100 {
101 return NULL;
102 }
103 sanitize_uri(sw_id, ':', '~');
104 sanitize_uri(sw_id, '+', '~');
105
106 return sw_id;
107 }
108
109 METHOD(sw_collector_info_t, destroy, void,
110 private_sw_collector_info_t *this)
111 {
112 this->os_info->destroy(this->os_info);
113 free(this->os);
114 free(this->product);
115 free(this->tag_creator);
116 free(this);
117 }
118
119 /**
120 * Described in header.
121 */
122 sw_collector_info_t *sw_collector_info_create(char *tag_creator)
123 {
124 private_sw_collector_info_t *this;
125 chunk_t os_name, os_version, os_arch;
126
127 INIT(this,
128 .public = {
129 .get_os_type = _get_os_type,
130 .get_os = _get_os,
131 .create_sw_id = _create_sw_id,
132 .destroy = _destroy,
133 },
134 .os_info = imc_os_info_create(),
135 .tag_creator = strdup(tag_creator),
136 );
137
138 os_name = this->os_info->get_name(this->os_info);
139 os_arch = this->os_info->get_version(this->os_info);
140
141 /* get_version() returns version followed by arch */
142 if (!extract_token(&os_version, ' ', &os_arch))
143 {
144 DBG1(DBG_IMC, "separation of OS version from arch failed");
145 destroy(this);
146 return NULL;
147 }
148
149 /* construct OS string */
150 if (asprintf(&this->os, "%.*s_%.*s-%.*s", (int)os_name.len, os_name.ptr,
151 (int)os_version.len, os_version.ptr, (int)os_arch.len,
152 os_arch.ptr) == -1)
153 {
154 DBG1(DBG_IMC, "constructon of OS string failed");
155 destroy(this);
156 return NULL;
157 }
158
159 /* construct product string */
160 if (asprintf(&this->product, "%.*s %.*s %.*s", (int)os_name.len,
161 os_name.ptr, (int)os_version.len, os_version.ptr,
162 (int)os_arch.len, os_arch.ptr) == -1)
163 {
164 DBG1(DBG_IMC, "constructon of product string failed");
165 destroy(this);
166 return NULL;
167 }
168
169 return &this->public;
170 }