Version 1.8.0.0
[socat.git] / hostan.c
blob80ea5694edf7b1de53d54f9909f6e800ae9532ea
1 /* source: hostan.c */
2 /* Copyright Gerhard Rieger and contributors (see file CHANGES) */
3 /* Published under the GNU General Public License V.2, see file COPYING */
5 /* the subroutine hostan makes a "HOST ANalysis". It gathers information
6 about the host environment it is running in without modifying its state
7 (almost).
8 */
10 #include "xiosysincludes.h"
11 #include "mytypes.h"
12 #include "compat.h"
13 #include "error.h"
14 #include "sycls.h"
15 #include "sysutils.h"
16 #include "filan.h"
18 #include "hostan.h"
21 static int iffan(FILE *outfile);
22 static int vsockan(FILE *outfile);
25 int hostan(FILE *outfile) {
26 fprintf(outfile, "\nC TYPE SIZES\n");
27 fprintf(outfile, "sizeof(char) = %u\n", (unsigned int)sizeof(char));
28 fprintf(outfile, "sizeof(short) = %u\n", (unsigned int)sizeof(short));
29 fprintf(outfile, "sizeof(int) = %u\n", (unsigned int)sizeof(int));
30 fprintf(outfile, "sizeof(long) = %u\n", (unsigned int)sizeof(long));
31 #if HAVE_TYPE_LONGLONG
32 fprintf(outfile, "sizeof(long long) = %u\n", (unsigned int)sizeof(long long));
33 #endif
34 fprintf(outfile, "sizeof(size_t) = %u\n", (unsigned int)sizeof(size_t));
36 # if HAVE_BASIC_SIZE_T==2
37 fprintf(outfile, "typedef unsigned short size_t; /* sizeof(size_t) = %u */\n", (unsigned int)sizeof(size_t));
38 #elif HAVE_BASIC_SIZE_T==4
39 fprintf(outfile, "typedef unsigned int size_t; /* sizeof(size_t) = %u */\n", (unsigned int)sizeof(size_t));
40 #elif HAVE_BASIC_SIZE_T==6
41 fprintf(outfile, "typedef unsigned long size_t; /* sizeof(size_t) = %u */\n", (unsigned int)sizeof(size_t));
42 #elif HAVE_BASIC_SIZE_T==8
43 fprintf(outfile, "typedef unsigned long long size_t; /* sizeof(size_t) = %u */\n", (unsigned int)sizeof(size_t));
44 #endif
46 # if HAVE_BASIC_MODE_T==1
47 fprintf(outfile, "typedef short mode_t; /* sizeof(mode_t) = %u */\n", (unsigned int)sizeof(mode_t));
48 #elif HAVE_BASIC_MODE_T==2
49 fprintf(outfile, "typedef unsigned short mode_t; /* sizeof(mode_t) = %u */\n", (unsigned int)sizeof(mode_t));
50 #elif HAVE_BASIC_MODE_T==3
51 fprintf(outfile, "typedef int mode_t; /* sizeof(mode_t) = %u */\n", (unsigned int)sizeof(mode_t));
52 #elif HAVE_BASIC_MODE_T==4
53 fprintf(outfile, "typedef unsigned int mode_t; /* sizeof(mode_t) = %u */\n", (unsigned int)sizeof(mode_t));
54 #elif HAVE_BASIC_MODE_T==5
55 fprintf(outfile, "typedef long mode_t; /* sizeof(mode_t) = %u */\n", (unsigned int)sizeof(mode_t));
56 #elif HAVE_BASIC_MODE_T==6
57 fprintf(outfile, "typedef unsigned long mode_t; /* sizeof(mode_t) = %u */\n", (unsigned int)sizeof(mode_t));
58 #elif HAVE_BASIC_MODE_T==7
59 fprintf(outfile, "typedef long long mode_t; /* sizeof(mode_t) = %u */\n", (unsigned int)sizeof(mode_t));
60 #elif HAVE_BASIC_MODE_T==8
61 fprintf(outfile, "typedef unsigned long long mode_t; /* sizeof(mode_t) = %u */\n", (unsigned int)sizeof(mode_t));
62 #endif
64 # if HAVE_BASIC_PID_T==1
65 fprintf(outfile, "typedef short pid_t; /* sizeof(pid_t) = %u */\n", (unsigned int)sizeof(pid_t));
66 #elif HAVE_BASIC_PID_T==2
67 fprintf(outfile, "typedef unsigned short pid_t; /* sizeof(pid_t) = %u */\n", (unsigned int)sizeof(pid_t));
68 #elif HAVE_BASIC_PID_T==3
69 fprintf(outfile, "typedef int pid_t; /* sizeof(pid_t) = %u */\n", (unsigned int)sizeof(pid_t));
70 #elif HAVE_BASIC_PID_T==4
71 fprintf(outfile, "typedef unsigned int pid_t; /* sizeof(pid_t) = %u */\n", (unsigned int)sizeof(pid_t));
72 #elif HAVE_BASIC_PID_T==5
73 fprintf(outfile, "typedef long pid_t; /* sizeof(pid_t) = %u */\n", (unsigned int)sizeof(pid_t));
74 #elif HAVE_BASIC_PID_T==6
75 fprintf(outfile, "typedef unsigned long pid_t; /* sizeof(pid_t) = %u */\n", (unsigned int)sizeof(pid_t));
76 #elif HAVE_BASIC_PID_T==7
77 fprintf(outfile, "typedef long long pid_t; /* sizeof(pid_t) = %u */\n", (unsigned int)sizeof(pid_t));
78 #elif HAVE_BASIC_PID_T==8
79 fprintf(outfile, "typedef unsigned long long pid_t; /* sizeof(pid_t) = %u */\n", (unsigned int)sizeof(pid_t));
80 #endif
82 # if HAVE_BASIC_UID_T==1
83 fprintf(outfile, "typedef short uid_t; /* sizeof(uid_t) = %u */\n", (unsigned int)sizeof(uid_t));
84 #elif HAVE_BASIC_UID_T==2
85 fprintf(outfile, "typedef unsigned short uid_t; /* sizeof(uid_t) = %u */\n", (unsigned int)sizeof(uid_t));
86 #elif HAVE_BASIC_UID_T==3
87 fprintf(outfile, "typedef int uid_t; /* sizeof(uid_t) = %u */\n", (unsigned int)sizeof(uid_t));
88 #elif HAVE_BASIC_UID_T==4
89 fprintf(outfile, "typedef unsigned int uid_t; /* sizeof(uid_t) = %u */\n", (unsigned int)sizeof(uid_t));
90 #elif HAVE_BASIC_UID_T==5
91 fprintf(outfile, "typedef long uid_t; /* sizeof(uid_t) = %u */\n", (unsigned int)sizeof(uid_t));
92 #elif HAVE_BASIC_UID_T==6
93 fprintf(outfile, "typedef unsigned long uid_t; /* sizeof(uid_t) = %u */\n", (unsigned int)sizeof(uid_t));
94 #elif HAVE_BASIC_UID_T==7
95 fprintf(outfile, "typedef long long uid_t; /* sizeof(uid_t) = %u */\n", (unsigned int)sizeof(uid_t));
96 #elif HAVE_BASIC_UID_T==8
97 fprintf(outfile, "typedef unsigned long long uid_t; /* sizeof(uid_t) = %u */\n", (unsigned int)sizeof(uid_t));
98 #endif
100 # if HAVE_BASIC_GID_T==1
101 fprintf(outfile, "typedef short gid_t; /* sizeof(gid_t) = %u */\n", (unsigned int)sizeof(gid_t));
102 #elif HAVE_BASIC_GID_T==2
103 fprintf(outfile, "typedef unsigned short gid_t; /* sizeof(gid_t) = %u */\n", (unsigned int)sizeof(gid_t));
104 #elif HAVE_BASIC_GID_T==3
105 fprintf(outfile, "typedef int gid_t; /* sizeof(gid_t) = %u */\n", (unsigned int)sizeof(gid_t));
106 #elif HAVE_BASIC_GID_T==4
107 fprintf(outfile, "typedef unsigned int gid_t; /* sizeof(gid_t) = %u */\n", (unsigned int)sizeof(gid_t));
108 #elif HAVE_BASIC_GID_T==5
109 fprintf(outfile, "typedef long gid_t; /* sizeof(gid_t) = %u */\n", (unsigned int)sizeof(gid_t));
110 #elif HAVE_BASIC_GID_T==6
111 fprintf(outfile, "typedef unsigned long gid_t; /* sizeof(gid_t) = %u */\n", (unsigned int)sizeof(gid_t));
112 #elif HAVE_BASIC_GID_T==7
113 fprintf(outfile, "typedef long long gid_t; /* sizeof(gid_t) = %u */\n", (unsigned int)sizeof(gid_t));
114 #elif HAVE_BASIC_GID_T==8
115 fprintf(outfile, "typedef unsigned long long gid_t; /* sizeof(gid_t) = %u */\n", (unsigned int)sizeof(gid_t));
116 #endif
118 # if HAVE_BASIC_TIME_T==1
119 fprintf(outfile, "typedef short time_t; /* sizeof(time_t) = %u */\n", (unsigned int)sizeof(time_t));
120 #elif HAVE_BASIC_TIME_T==2
121 fprintf(outfile, "typedef unsigned short time_t; /* sizeof(time_t) = %u */\n", (unsigned int)sizeof(time_t));
122 #elif HAVE_BASIC_TIME_T==3
123 fprintf(outfile, "typedef int time_t; /* sizeof(time_t) = %u */\n", (unsigned int)sizeof(time_t));
124 #elif HAVE_BASIC_TIME_T==4
125 fprintf(outfile, "typedef unsigned int time_t; /* sizeof(time_t) = %u */\n", (unsigned int)sizeof(time_t));
126 #elif HAVE_BASIC_TIME_T==5
127 fprintf(outfile, "typedef long time_t; /* sizeof(time_t) = %u */\n", (unsigned int)sizeof(time_t));
128 #elif HAVE_BASIC_TIME_T==6
129 fprintf(outfile, "typedef unsigned long time_t; /* sizeof(time_t) = %u */\n", (unsigned int)sizeof(time_t));
130 #elif HAVE_BASIC_TIME_T==7
131 fprintf(outfile, "typedef long long time_t; /* sizeof(time_t) = %u */\n", (unsigned int)sizeof(time_t));
132 #elif HAVE_BASIC_TIME_T==8
133 fprintf(outfile, "typedef unsigned long long time_t; /* sizeof(time_t) = %u */\n", (unsigned int)sizeof(time_t));
134 #endif
136 # if HAVE_BASIC_SOCKLEN_T==1
137 fprintf(outfile, "typedef short socklen_t; /* sizeof(socklen_t) = %u */\n", (unsigned int)sizeof(socklen_t));
138 #elif HAVE_BASIC_SOCKLEN_T==2
139 fprintf(outfile, "typedef unsigned short socklen_t; /* sizeof(socklen_t) = %u */\n", (unsigned int)sizeof(socklen_t));
140 #elif HAVE_BASIC_SOCKLEN_T==3
141 fprintf(outfile, "typedef int socklen_t; /* sizeof(socklen_t) = %u */\n", (unsigned int)sizeof(socklen_t));
142 #elif HAVE_BASIC_SOCKLEN_T==4
143 fprintf(outfile, "typedef unsigned int socklen_t; /* sizeof(socklen_t) = %u */\n", (unsigned int)sizeof(socklen_t));
144 #elif HAVE_BASIC_SOCKLEN_T==5
145 fprintf(outfile, "typedef long socklen_t; /* sizeof(socklen_t) = %u */\n", (unsigned int)sizeof(socklen_t));
146 #elif HAVE_BASIC_SOCKLEN_T==6
147 fprintf(outfile, "typedef unsigned long socklen_t; /* sizeof(socklen_t) = %u */\n", (unsigned int)sizeof(socklen_t));
148 #elif HAVE_BASIC_SOCKLEN_T==7
149 fprintf(outfile, "typedef long long socklen_t; /* sizeof(socklen_t) = %u */\n", (unsigned int)sizeof(socklen_t));
150 #elif HAVE_BASIC_SOCKLEN_T==8
151 fprintf(outfile, "typedef unsigned long long socklen_t; /* sizeof(socklen_t) = %u */\n", (unsigned int)sizeof(socklen_t));
152 #endif
154 # if HAVE_BASIC_OFF_T==1
155 fprintf(outfile, "typedef short off_t; /* sizeof(off_t) = %u */\n", (unsigned int)sizeof(off_t));
156 #elif HAVE_BASIC_OFF_T==2
157 fprintf(outfile, "typedef unsigned short off_t; /* sizeof(off_t) = %u */\n", (unsigned int)sizeof(off_t));
158 #elif HAVE_BASIC_OFF_T==3
159 fprintf(outfile, "typedef int off_t; /* sizeof(off_t) = %u */\n", (unsigned int)sizeof(off_t));
160 #elif HAVE_BASIC_OFF_T==4
161 fprintf(outfile, "typedef unsigned int off_t; /* sizeof(off_t) = %u */\n", (unsigned int)sizeof(off_t));
162 #elif HAVE_BASIC_OFF_T==5
163 fprintf(outfile, "typedef long off_t; /* sizeof(off_t) = %u */\n", (unsigned int)sizeof(off_t));
164 #elif HAVE_BASIC_OFF_T==6
165 fprintf(outfile, "typedef unsigned long off_t; /* sizeof(off_t) = %u */\n", (unsigned int)sizeof(off_t));
166 #elif HAVE_BASIC_OFF_T==7
167 fprintf(outfile, "typedef long long off_t; /* sizeof(off_t) = %u */\n", (unsigned int)sizeof(off_t));
168 #elif HAVE_BASIC_OFF_T==8
169 fprintf(outfile, "typedef unsigned long long off_t; /* sizeof(off_t) = %u */\n", (unsigned int)sizeof(off_t));
170 #endif
172 #if HAVE_TYPE_OFF64 && defined(HAVE_BASIC_OFF64_T) && HAVE_BASIC_OFF64_T
173 # if HAVE_BASIC_OFF64_T==1
174 fprintf(outfile, "typedef short off64_t; /* sizeof(off64_t) = %u */\n", (unsigned int)sizeof(off64_t));
175 #elif HAVE_BASIC_OFF64_T==2
176 fprintf(outfile, "typedef unsigned short off64_t; /* sizeof(off64_t) = %u */\n", (unsigned int)sizeof(off64_t));
177 #elif HAVE_BASIC_OFF64_T==3
178 fprintf(outfile, "typedef int off64_t; /* sizeof(off64_t) = %u */\n", (unsigned int)sizeof(off64_t));
179 #elif HAVE_BASIC_OFF64_T==4
180 fprintf(outfile, "typedef unsigned int off64_t; /* sizeof(off64_t) = %u */\n", (unsigned int)sizeof(off64_t));
181 #elif HAVE_BASIC_OFF64_T==5
182 fprintf(outfile, "typedef long off64_t; /* sizeof(off64_t) = %u */\n", (unsigned int)sizeof(off64_t));
183 #elif HAVE_BASIC_OFF64_T==6
184 fprintf(outfile, "typedef unsigned long off64_t; /* sizeof(off64_t) = %u */\n", (unsigned int)sizeof(off64_t));
185 #elif HAVE_BASIC_OFF64_T==7
186 fprintf(outfile, "typedef long long off64_t; /* sizeof(off64_t) = %u */\n", (unsigned int)sizeof(off64_t));
187 #elif HAVE_BASIC_OFF64_T==8
188 fprintf(outfile, "typedef unsigned long long off64_t; /* sizeof(off64_t) = %u */\n", (unsigned int)sizeof(off64_t));
189 #endif
190 #endif /* defined(HAVE_BASIC_OFF64_T) && HAVE_BASIC_OFF64_T */
192 # if HAVE_BASIC_DEV_T==1
193 fprintf(outfile, "typedef short dev_t; /* sizeof(dev_t) = %u */\n", (unsigned int)sizeof(dev_t));
194 #elif HAVE_BASIC_DEV_T==2
195 fprintf(outfile, "typedef unsigned short dev_t; /* sizeof(dev_t) = %u */\n", (unsigned int)sizeof(dev_t));
196 #elif HAVE_BASIC_DEV_T==3
197 fprintf(outfile, "typedef int dev_t; /* sizeof(dev_t) = %u */\n", (unsigned int)sizeof(dev_t));
198 #elif HAVE_BASIC_DEV_T==4
199 fprintf(outfile, "typedef unsigned int dev_t; /* sizeof(dev_t) = %u */\n", (unsigned int)sizeof(dev_t));
200 #elif HAVE_BASIC_DEV_T==5
201 fprintf(outfile, "typedef long dev_t; /* sizeof(dev_t) = %u */\n", (unsigned int)sizeof(dev_t));
202 #elif HAVE_BASIC_DEV_T==6
203 fprintf(outfile, "typedef unsigned long dev_t; /* sizeof(dev_t) = %u */\n", (unsigned int)sizeof(dev_t));
204 #elif HAVE_BASIC_DEV_T==7
205 fprintf(outfile, "typedef long long dev_t; /* sizeof(dev_t) = %u */\n", (unsigned int)sizeof(dev_t));
206 #elif HAVE_BASIC_DEV_T==8
207 fprintf(outfile, "typedef unsigned long long dev_t; /* sizeof(dev_t) = %u */\n", (unsigned int)sizeof(dev_t));
208 #endif
211 struct stat x;
212 # if HAVE_TYPEOF_ST_INO==1
213 fprintf(outfile, "typedef short ino_t; /* sizeof(ino_t) = %u */\n", (unsigned int)sizeof(x.st_ino));
214 #elif HAVE_TYPEOF_ST_INO==2
215 fprintf(outfile, "typedef unsigned short ino_t; /* sizeof(ino_t) = %u */\n", (unsigned int)sizeof(x.st_ino));
216 #elif HAVE_TYPEOF_ST_INO==3
217 fprintf(outfile, "typedef int ino_t; /* sizeof(ino_t) = %u */\n", (unsigned int)sizeof(x.st_ino));
218 #elif HAVE_TYPEOF_ST_INO==4
219 fprintf(outfile, "typedef unsigned int ino_t; /* sizeof(ino_t) = %u */\n", (unsigned int)sizeof(x.st_ino));
220 #elif HAVE_TYPEOF_ST_INO==5
221 fprintf(outfile, "typedef long ino_t; /* sizeof(ino_t) = %u */\n", (unsigned int)sizeof(x.st_ino));
222 #elif HAVE_TYPEOF_ST_INO==6
223 fprintf(outfile, "typedef unsigned long ino_t; /* sizeof(ino_t) = %u */\n", (unsigned int)sizeof(x.st_ino));
224 #elif HAVE_TYPEOF_ST_INO==7
225 fprintf(outfile, "typedef long long ino_t; /* sizeof(ino_t) = %u */\n", (unsigned int)sizeof(x.st_ino));
226 #elif HAVE_TYPEOF_ST_INO==8
227 fprintf(outfile, "typedef unsigned long long ino_t; /* sizeof(ino_t) = %u */\n", (unsigned int)sizeof(x.st_ino));
228 #endif
231 unsigned short x = 0x100;
232 if (x == ntohs(0x100)) {
233 fprintf(outfile, "#define __BYTE_ORDER __BIG_ENDIAN\t/* Motorola ea.*/\n");
234 } else {
235 fprintf(outfile, "#define __BYTE_ORDER __LITTLE_ENDIAN\t/* Intel ea.*/\n");
239 #include <sys/time.h> /* select(); OpenBSD: struct timespec */
240 fprintf(outfile, "sizeof(struct timespec) = %u\n", (unsigned int)sizeof(struct timespec));
242 fprintf(outfile, "\n");
243 fprintf(outfile, "/* Socat types */\n");
244 fprintf(outfile, "sizeof(struct diag_dgram) = %u\n", (unsigned int)sizeof(struct diag_dgram));
245 fprintf(outfile, "((struct diag_dgram *)0)->op-((struct diag_dgram *)0) = %u\n", (unsigned int)((char *)(&((struct diag_dgram *)0)->op)-(char *)((struct diag_dgram *)0)));
246 fprintf(outfile, "((struct diag_dgram *)0)->now-((struct diag_dgram *)0) = %u\n", (unsigned int)((char *)(&((struct diag_dgram *)0)->now)-(char *)((struct diag_dgram *)0)));
247 fprintf(outfile, "((struct diag_dgram *)0)->exitcode-((struct diag_dgram *)0) = %u\n", (unsigned int)((char *)(&((struct diag_dgram *)0)->exitcode)-(char *)((struct diag_dgram *)0)));
248 fprintf(outfile, "((struct diag_dgram *)0)->text-((struct diag_dgram *)0) = %u\n", (unsigned int)((((struct diag_dgram *)0)->text)-(char *)((struct diag_dgram *)0)));
249 #if _WITH_SOCKET && (_WITH_IP4 || _WITH_IP6)
250 fprintf(outfile, "\nIP INTERFACES\n");
251 iffan(outfile);
252 #endif
253 #if WITH_VSOCK
254 vsockan(outfile);
255 #endif
256 return 0;
259 #if _WITH_SOCKET && (_WITH_IP4 || _WITH_IP6)
260 static int iffan(FILE *outfile) {
261 /* Linux: man 7 netdevice */
262 /* FreeBSD, NetBSD: man 4 networking */
263 /* Solaris: man 7 if_tcp */
265 /* currently we support Linux and a little FreeBSD */
266 #ifdef SIOCGIFCONF /* not Solaris */
268 #define IFBUFSIZ 32*sizeof(struct ifreq) /*1024*/
269 int s;
270 unsigned char buff[IFBUFSIZ];
271 struct ifconf ic;
272 int i;
274 if ((s = Socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0) {
275 Warn1("socket(PF_INET, SOCK_DGRAM, IPPROTO_IP): %s", strerror(errno));
276 return -1;
279 for (i=0; i < IFBUFSIZ; ++i) {
280 buff[i] = 255;
282 ic.ifc_len = sizeof(buff);
283 ic.ifc_ifcu.ifcu_buf = (caddr_t)buff;
284 if (Ioctl(s, SIOCGIFCONF, &ic) < 0) {
285 Warn3("ioctl(%d, SIOCGIFCONF, %p): %s", s, &ic, strerror(errno));
286 return -1;
289 for (i = 0; i < ic.ifc_len; i += sizeof(struct ifreq)) {
290 struct ifreq *ifp = (struct ifreq *)((caddr_t)ic.ifc_req + i);
291 #if 0 || defined(SIOCGIFINDEX) /* not NetBSD, OpenBSD */
292 struct ifreq ifr;
293 #endif
295 #if 0 || defined(SIOCGIFINDEX) /* not NetBSD, OpenBSD */
296 strcpy(ifr.ifr_name, ifp->ifr_name);
297 if (Ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
298 Warn3("ioctl(%d, SIOCGIFINDEX, {\"%s\"}): %s",
299 s, ifr.ifr_name, strerror(errno));
300 return 1;
302 #if HAVE_STRUCT_IFREQ_IFR_INDEX
303 fprintf(outfile, "%2d: %s\n", ifr.ifr_index, ifp->ifr_name);
304 #elif HAVE_STRUCT_IFREQ_IFR_IFINDEX
305 fprintf(outfile, "%2d: %s\n", ifr.ifr_ifindex, ifp->ifr_name);
306 #endif /* HAVE_STRUCT_IFREQ_IFR_INDEX */
307 #else /* !defined(SIOCGIFINDEX) */
308 fprintf(outfile, "%2d: %s\n", i/(int)sizeof(struct ifreq), ifp->ifr_name);
309 #endif /* defined(SIOCGIFINDEX) */
311 Close(s);
312 #endif /* defined(SIOCGIFCONF) */
313 return 0;
315 #endif /* _WITH_SOCKET */
318 #if WITH_VSOCK
319 static int vsockan(FILE *outfile) {
320 unsigned int cid;
321 int vsock;
322 if (Getuid() != 0) {
323 return 1;
325 if ((vsock = Open("/dev/vsock", O_RDONLY, 0)) < 0 ) {
326 Warn1("open(\"/dev/vsock\", ...): %s", strerror(errno));
327 return -1;
328 } else if (Ioctl(vsock, IOCTL_VM_SOCKETS_GET_LOCAL_CID, &cid) < 0) {
329 Warn2("ioctl(%d, IOCTL_VM_SOCKETS_GET_LOCAL_CID, ...): %s",
330 vsock, strerror(errno));
331 return -1;
332 } else {
333 Notice1("VSOCK CID=%u", cid);
334 fprintf(outfile, "\nVSOCK_CID = %u\n", cid);
336 if (vsock >= 0) {
337 Close(vsock);
339 return 0;
341 #endif /* WITH_VSOCK */