version 1.7.3.0
[socat.git] / hostan.c
blob341249fdd0857da6c61a4b73e6c21c3edd74e7b1
1 /* source: hostan.c */
2 /* Copyright Gerhard Rieger */
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"
20 #include "error.h"
22 static int iffan(FILE *outfile);
24 int hostan(FILE *outfile) {
25 fprintf(outfile, "\nC TYPE SIZES\n");
26 fprintf(outfile, "sizeof(char) = %u\n", (unsigned int)sizeof(char));
27 fprintf(outfile, "sizeof(short) = %u\n", (unsigned int)sizeof(short));
28 fprintf(outfile, "sizeof(int) = %u\n", (unsigned int)sizeof(int));
29 fprintf(outfile, "sizeof(long) = %u\n", (unsigned int)sizeof(long));
30 #if HAVE_TYPE_LONGLONG
31 fprintf(outfile, "sizeof(long long) = %u\n", (unsigned int)sizeof(long long));
32 #endif
33 fprintf(outfile, "sizeof(size_t) = %u\n", (unsigned int)sizeof(size_t));
34 #include <sys/time.h> /* select(); OpenBSD: struct timespec */
35 fprintf(outfile, "sizeof(struct timespec) = %u\n", (unsigned int)sizeof(struct timespec));
36 fprintf(outfile, "sizeof(struct diag_dgram) = %u\n", (unsigned int)sizeof(struct diag_dgram));
37 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)));
38 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)));
39 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)));
40 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)));
41 #if _WITH_SOCKET && (_WITH_IP4 || _WITH_IP6)
42 fprintf(outfile, "\nIP INTERFACES\n");
43 iffan(outfile);
44 #endif
45 return 0;
48 #if _WITH_SOCKET && (_WITH_IP4 || _WITH_IP6)
49 static int iffan(FILE *outfile) {
50 /* Linux: man 7 netdevice */
51 /* FreeBSD, NetBSD: man 4 networking */
52 /* Solaris: man 7 if_tcp */
54 /* currently we support Linux and a little FreeBSD */
55 #ifdef SIOCGIFCONF /* not Solaris */
57 #define IFBUFSIZ 32*sizeof(struct ifreq) /*1024*/
58 int s;
59 unsigned char buff[IFBUFSIZ];
60 struct ifconf ic;
61 int i;
63 if ((s = Socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0) {
64 Error1("socket(PF_INET, SOCK_DGRAM, IPPROTO_IP): %s", strerror(errno));
65 return -1;
68 for (i=0; i < IFBUFSIZ; ++i) {
69 buff[i] = 255;
71 ic.ifc_len = sizeof(buff);
72 ic.ifc_ifcu.ifcu_buf = (caddr_t)buff;
73 if (Ioctl(s, SIOCGIFCONF, &ic) < 0) {
74 Error3("ioctl(%d, SIOCGIFCONF, %p): %s", s, &ic, strerror(errno));
75 return -1;
78 for (i = 0; i < ic.ifc_len; i += sizeof(struct ifreq)) {
79 struct ifreq *ifp = (struct ifreq *)((caddr_t)ic.ifc_req + i);
80 #if 0 || defined(SIOCGIFINDEX) /* not NetBSD, OpenBSD */
81 struct ifreq ifr;
82 #endif
84 #if 0 || defined(SIOCGIFINDEX) /* not NetBSD, OpenBSD */
85 strcpy(ifr.ifr_name, ifp->ifr_name);
86 if (Ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
87 Error3("ioctl(%d, SIOCGIFINDEX, {\"%s\"}): %s",
88 s, ifr.ifr_name, strerror(errno));
89 return 1;
91 #if HAVE_STRUCT_IFREQ_IFR_INDEX
92 fprintf(outfile, "%2d: %s\n", ifr.ifr_index, ifp->ifr_name);
93 #elif HAVE_STRUCT_IFREQ_IFR_IFINDEX
94 fprintf(outfile, "%2d: %s\n", ifr.ifr_ifindex, ifp->ifr_name);
95 #endif /* HAVE_STRUCT_IFREQ_IFR_INDEX */
96 #else /* !defined(SIOCGIFINDEX) */
97 fprintf(outfile, "%2d: %s\n", i/(int)sizeof(struct ifreq), ifp->ifr_name);
98 #endif /* defined(SIOCGIFINDEX) */
100 Close(s);
101 #endif /* defined(SIOCGIFCONF) */
102 return 0;
104 #endif /* _WITH_SOCKET */