File: check-sizes.c

package info (click to toggle)
ike-scan 1.7-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 4,928 kB
  • ctags: 542
  • sloc: ansic: 5,931; sh: 3,589; makefile: 72
file content (186 lines) | stat: -rw-r--r-- 6,029 bytes parent folder | download | duplicates (2)
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 * The IKE Scanner (ike-scan) is Copyright (C) 2003-2005 Roy Hills,
 * NTA Monitor Ltd.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * If this license is unacceptable to you, I may be willing to negotiate
 * alternative licenses (contact ike-scan@nta-monitor.com).
 *
 * You are encouraged to send comments, improvements or suggestions to
 * me at ike-scan@nta-monitor.com.
 *
 * $Id: check-sizes.c,v 1.6 2005/01/01 16:49:53 rsh Exp $
 *
 * check-sizes -- Check sizes of structures and types
 *
 * Author:	Roy Hills
 * Date:	30 December 2003
 *
 *      Check that the sizes of the various structs are what we expect them
 *      to be.  If they are not, then we return with a failure status.
 *
 *      There are several places in the ike-scan code where we copy structs
 *      to character arrays and vice versa.  E.g. send_packet().
 *
 *      Although this is not condoned in C, it is OK in practice
 *      providing that the sizes of the fields is correct and there is no
 *      padding between fields (e.g. for alignment purposes).  This function
 *      checks for both of these problems.
 */

#include "ike-scan.h"

#define EXPECTED_ISAKMP_HDR 28
#define EXPECTED_ISAKMP_SA 12
#define EXPECTED_ISAKMP_PROPOSAL 8
#define EXPECTED_ISAKMP_TRANSFORM 8
#define EXPECTED_ISAKMP_VID 4
#define EXPECTED_ISAKMP_NOTIFICATION 12
#define EXPECTED_ISAKMP_KX 4
#define EXPECTED_ISAKMP_NONCE 4
#define EXPECTED_ISAKMP_ID 8

#define EXPECTED_UINT8_T 1
#define EXPECTED_UINT16_T 2
#define EXPECTED_UINT32_T 4

int
main() {
   unsigned octets_per_char;	/* Almost always 1 */
   int error=0;

   if (CHAR_BIT % 8)
      err_msg("CHAR_BIT is not a multiple of 8");

   octets_per_char = CHAR_BIT/8;

   printf("Structure\t\tExpect\tObserved\n\n");

   printf("isakmp_hdr\t\t%u\t%lu\t", EXPECTED_ISAKMP_HDR,
          (unsigned long) (octets_per_char * sizeof(struct isakmp_hdr)));
   if (octets_per_char * sizeof(struct isakmp_hdr) != EXPECTED_ISAKMP_HDR) {
      error++;
      printf("ERROR\n");
   } else {
      printf("ok\n");
   }

   printf("isakmp_sa\t\t%u\t%lu\t", EXPECTED_ISAKMP_SA,
          (unsigned long) (octets_per_char * sizeof(struct isakmp_sa)));
   if (octets_per_char * sizeof(struct isakmp_sa) != EXPECTED_ISAKMP_SA) {
      error++;
      printf("ERROR\n");
   } else {
      printf("ok\n");
   }

   printf("isakmp_proposal\t\t%u\t%lu\t", EXPECTED_ISAKMP_PROPOSAL,
          (unsigned long) (octets_per_char * sizeof(struct isakmp_proposal)));
   if (octets_per_char * sizeof(struct isakmp_proposal) != EXPECTED_ISAKMP_PROPOSAL) {
      error++;
      printf("ERROR\n");
   } else {
      printf("ok\n");
   }

   printf("isakmp_transform\t%u\t%lu\t", EXPECTED_ISAKMP_TRANSFORM,
          (unsigned long) (octets_per_char * sizeof(struct isakmp_transform)));
   if (octets_per_char * sizeof(struct isakmp_transform) != EXPECTED_ISAKMP_TRANSFORM) {
      error++;
      printf("ERROR\n");
   } else {
      printf("ok\n");
   }

   printf("isakmp_vid\t\t%u\t%lu\t", EXPECTED_ISAKMP_VID,
          (unsigned long) (octets_per_char * sizeof(struct isakmp_vid)));
   if (octets_per_char * sizeof(struct isakmp_vid) != EXPECTED_ISAKMP_VID) {
      error++;
      printf("ERROR\n");
   } else {
      printf("ok\n");
   }

   printf("isakmp_notification\t%u\t%lu\t", EXPECTED_ISAKMP_NOTIFICATION,
          (unsigned long) (octets_per_char * sizeof(struct isakmp_notification)));
   if (octets_per_char * sizeof(struct isakmp_notification) != EXPECTED_ISAKMP_NOTIFICATION) {
      error++;
      printf("ERROR\n");
   } else {
      printf("ok\n");
   }

   printf("isakmp_kx\t\t%u\t%lu\t", EXPECTED_ISAKMP_KX,
          (unsigned long) (octets_per_char * sizeof(struct isakmp_kx)));
   if (octets_per_char * sizeof(struct isakmp_kx) != EXPECTED_ISAKMP_KX) {
      error++;
      printf("ERROR\n");
   } else {
      printf("ok\n");
   }

   printf("isakmp_nonce\t\t%u\t%lu\t", EXPECTED_ISAKMP_NONCE,
          (unsigned long) (octets_per_char * sizeof(struct isakmp_nonce)));
   if (octets_per_char * sizeof(struct isakmp_nonce) != EXPECTED_ISAKMP_NONCE) {
      error++;
      printf("ERROR\n");
   } else {
      printf("ok\n");
   }

   printf("isakmp_id\t\t%u\t%lu\t", EXPECTED_ISAKMP_ID,
          (unsigned long) (octets_per_char * sizeof(struct isakmp_id)));
   if (octets_per_char * sizeof(struct isakmp_id) != EXPECTED_ISAKMP_ID) {
      error++;
      printf("ERROR\n");
   } else {
      printf("ok\n");
   }

   printf("\nType\t\t\tExpect\tObserved\n\n");

   printf("uint8_t\t\t\t%u\t%lu\t", EXPECTED_UINT8_T,
          (unsigned long) (octets_per_char * sizeof(uint8_t)));
   if (octets_per_char * sizeof(uint8_t) != EXPECTED_UINT8_T) {
      error++;
      printf("ERROR\n");
   } else {
      printf("ok\n");
   }

   printf("uint16_t\t\t%u\t%lu\t", EXPECTED_UINT16_T,
          (unsigned long) (octets_per_char * sizeof(uint16_t)));
   if (octets_per_char * sizeof(uint16_t) != EXPECTED_UINT16_T) {
      error++;
      printf("ERROR\n");
   } else {
      printf("ok\n");
   }

   printf("uint32_t\t\t%u\t%lu\t", EXPECTED_UINT32_T,
          (unsigned long) (octets_per_char * sizeof(uint32_t)));
   if (octets_per_char * sizeof(uint32_t) != EXPECTED_UINT32_T) {
      error++;
      printf("ERROR\n");
   } else {
      printf("ok\n");
   }

   if (error)
      return EXIT_FAILURE;
   else
      return EXIT_SUCCESS;
}