The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
version.c
Go to the documentation of this file.
1 /*
2  * This library is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU Lesser General Public
4  * License as published by the Free Software Foundation; either
5  * version 2.1 of the License, or (at your option) any later version.
6  *
7  * This library is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10  * Lesser General Public License for more details.
11  *
12  * You should have received a copy of the GNU Lesser General Public
13  * License along with this library; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15  */
16 
17 /** Validate application and library magic numbers
18  *
19  * @file src/lib/util/version.c
20  *
21  * @copyright 2016 The FreeRADIUS server project
22  * @copyright 2016 Arran Cudbard-Bell (a.cudbardb@freeradius.org)
23  */
24 RCSID("$Id: de20dff3eb1b88cc1c270dcdc4b76ab3775e61bf $")
25 
26 #include <freeradius-devel/util/strerror.h>
27 #include <freeradius-devel/util/version.h>
28 
29 static uint64_t libmagic = RADIUSD_MAGIC_NUMBER;
30 
31 /** Check if the application linking to the library has the correct magic number
32  *
33  * @param magic number as defined by RADIUSD_MAGIC_NUMBER
34  * @returns
35  * - 0 on success.
36  * - -1 on prefix mismatch.
37  * - -2 on version mismatch.
38  * - -3 on commit mismatch.
39  */
40 int fr_check_lib_magic(uint64_t magic)
41 {
42  if (MAGIC_PREFIX(magic) != MAGIC_PREFIX(libmagic)) {
43  fr_strerror_printf("Application and libfreeradius-util magic number (prefix) mismatch."
44  " application: %x library: %x",
46  return -1;
47  }
48 
49  if (MAGIC_VERSION(magic) != MAGIC_VERSION(libmagic)) {
50  fr_strerror_printf("Application and libfreeradius-util magic number (version) mismatch."
51  " application: %lx library: %lx",
52  (unsigned long) MAGIC_VERSION(magic), (unsigned long) MAGIC_VERSION(libmagic));
53  return -2;
54  }
55 
56  if (MAGIC_COMMIT(magic) != MAGIC_COMMIT(libmagic)) {
57  fr_strerror_printf("Application and libfreeradius-util magic number (commit) mismatch."
58  " application: %lx library: %lx",
59  (unsigned long) MAGIC_COMMIT(magic), (unsigned long) MAGIC_COMMIT(libmagic));
60  return -3;
61  }
62 
63  return 0;
64 }
#define RCSID(id)
Definition: build.h:444
#define fr_strerror_printf(_fmt,...)
Log to thread local error buffer.
Definition: strerror.h:64
static uint64_t libmagic
Definition: version.c:29
int fr_check_lib_magic(uint64_t magic)
Check if the application linking to the library has the correct magic number.
Definition: version.c:40
#define MAGIC_PREFIX(_x)
Definition: version.h:82
#define MAGIC_VERSION(_x)
Definition: version.h:83
#define MAGIC_COMMIT(_x)
Definition: version.h:84
#define RADIUSD_MAGIC_NUMBER
Definition: version.h:81