moved checksum_builder/libchechsum to top srcdir to respect build order
[strongswan.git] / src / checksum / checksum_builder.c
1 /*
2 * Copyright (C) 2009 Martin Willi
3 * Hochschule fuer Technik Rapperswil, Switzerland
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 #include <stdlib.h>
17 #include <stdio.h>
18 #include <dlfcn.h>
19
20 #include <library.h>
21
22 /* we need to fake some charon symbols to dlopen() its plugins */
23 void *eap_type_names, *auth_class_names, *charon, *protocol_id_names,
24 *action_names, *ipsec_mode_names, *ike_sa_state_names, *child_sa_state_names,
25 *proposal_create_default, *peer_cfg_create, *ike_cfg_create, *child_cfg_create,
26 *traffic_selector_create_dynamic, *callback_job_create, *delete_ike_sa_job_create,
27 *eap_type_from_string, *auth_cfg_create, *proposal_create_from_string,
28 *traffic_selector_create_from_subnet, *traffic_selector_create_from_bytes,
29 *eap_payload_create_data, *ike_sa_create, *child_sa_create, *policy_dir_names,
30 *controller_cb_empty, *proposal_create, *ipcomp_transform_names,
31 *traffic_selector_create_from_string, *update_sa_job_create, *ike_sa_id_create,
32 *rekey_child_sa_job_create, *roam_job_create, *migrate_job_create,
33 *acquire_job_create, *delete_child_sa_job_create, *debug_names;
34
35 int main(int argc, char* argv[])
36 {
37 int i;
38 integrity_checker_t *integrity;
39
40 /* avoid confusing leak reports in build process */
41 setenv("LEAK_DETECTIVE_DISABLE", "1", 0);
42 library_init(NULL);
43 atexit(library_deinit);
44
45 integrity = integrity_checker_create(NULL);
46
47 printf("/**\n");
48 printf(" * checksums of files and loaded code segments.\n");
49 printf(" * created by %s\n", argv[0]);
50 printf(" */\n");
51 printf("\n");
52 printf("#include <library.h>\n");
53 printf("\n");
54 printf("integrity_checksum_t checksums[] = {\n");
55 for (i = 1; i < argc; i++)
56 {
57 char *name, *path, *sname;
58 void *handle, *symbol;
59 u_int32_t fsum, ssum;
60
61 path = argv[i];
62
63 if ((name = strstr(path, "libstrongswan-")))
64 {
65 name = strdup(name + strlen("libstrongswan-"));
66 name[strlen(name) - 3] = '"';
67 name[strlen(name) - 2] = ',';
68 name[strlen(name) - 1] = '\0';
69 sname = "plugin_create";
70 }
71 else if (strstr(path, "libstrongswan.so"))
72 {
73 name = strdup("libstrongswan\",");
74 sname = "library_init";
75 }
76 else
77 {
78 fprintf(stderr, "don't know how to handle '%s', ignored", path);
79 continue;
80 }
81
82 fsum = integrity->build_file(integrity, path);
83 ssum = 0;
84 handle = dlopen(path, RTLD_LAZY);
85 if (handle)
86 {
87 symbol = dlsym(handle, sname);
88 if (symbol)
89 {
90 ssum = integrity->build_segment(integrity, symbol);
91 }
92 else
93 {
94 fprintf(stderr, "symbol lookup failed: %s\n", dlerror());
95 }
96 dlclose(handle);
97 }
98 else
99 {
100 fprintf(stderr, "dlopen failed: %s\n", dlerror());
101 }
102
103 printf("\t{\"%-20s0x%08x, 0x%08x},\n", name, fsum, ssum);
104 free(name);
105 }
106 printf("};\n");
107 printf("\n");
108 printf("int checksum_count = countof(checksums);\n");
109 printf("\n");
110 integrity->destroy(integrity);
111
112 exit(0);
113 }
114