©2004 by Marc J. Rochkind. All rights reserved. Portions marked "Open Source" may be copied under license.

 

Main Page   Modules   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

c8/ndb.c

Go to the documentation of this file.
00001 /*
00002     Network database examples
00003     AUP2, Sec. 8.08
00004 
00005     Copyright 2003 by Marc J. Rochkind. All rights reserved.
00006     May be copied only for purposes and under conditions described
00007     on the Web page www.basepath.com/aup/copyright.htm.
00008 
00009     The Example Files are provided "as is," without any warranty;
00010     without even the implied warranty of merchantability or fitness
00011     for a particular purpose. The author and his publisher are not
00012     responsible for any damages, direct or incidental, resulting
00013     from the use or non-use of these Example Files.
00014 
00015     The Example Files may contain defects, and some contain deliberate
00016     coding mistakes that were included for educational reasons.
00017     You are responsible for determining if and how the Example Files
00018     are to be used.
00019 
00020 */
00021 #include "defs.h"
00022 #ifdef SOLARIS
00023 #define __EXTENSIONS__
00024 #endif
00025 #include <sys/socket.h>
00026 #include <netdb.h>
00027 #undef __EXTENSIONS__
00028 #include <netinet/in.h>
00029 #include <arpa/inet.h>
00030 #include <net/if.h>
00031 
00032 /*[ifdb]*/
00033 static void ifdb(void)
00034 {
00035     struct if_nameindex *ni;
00036     int i;
00037 
00038     ec_null( ni = if_nameindex() )
00039     for (i = 0; ni[i].if_index != 0 || ni[i].if_name != NULL; i++)
00040         printf("index: %d; name: %s\n", ni[i].if_index, ni[i].if_name);
00041     if_freenameindex(ni);
00042     return;
00043 
00044 EC_CLEANUP_BGN
00045     EC_FLUSH("ifdb")
00046 EC_CLEANUP_END
00047 }
00048 /*[display_hostent]*/
00049 static void display_hostent(struct hostent *h)
00050 {
00051     int i;
00052 
00053     printf("name: %s; type: %d; len: %d\n", h->h_name, h->h_addrtype,
00054       h->h_length);
00055     for (i = 0; h->h_aliases[i] != NULL; i++)
00056         printf("\t%s\n", h->h_aliases[i]);
00057     if (h->h_addrtype == AF_INET) {
00058         for (i = 0; h->h_addr_list[i] != NULL; i++)
00059             printf("\t%s\n",
00060               inet_ntoa(*((struct in_addr *)h->h_addr_list[i])));
00061     }
00062 }
00063 /*[hostdb]*/
00064 static void hostdb(void)
00065 {
00066     struct hostent *h;
00067 
00068     sethostent(true);
00069     while ((h = gethostent()) != NULL)
00070         display_hostent(h);
00071     endhostent();
00072 }
00073 /*[gethostbyname_ex]*/
00074 static void gethostbyname_ex(void)
00075 {
00076     struct hostent *h;
00077 
00078     if ((h = gethostbyname("www.yahoo.com")) == NULL) {
00079         if (h_errno == HOST_NOT_FOUND)
00080             printf("host not found\n");
00081         else
00082             printf("h_errno = %d\n", h_errno);
00083     }
00084     else
00085         display_hostent(h);
00086 }
00087 /*[gethostbyaddr_ex]*/
00088 static void gethostbyaddr_ex(void)
00089 {
00090     struct hostent *h;
00091     in_addr_t a;
00092 
00093     ec_neg1( a = inet_addr("66.218.71.94") )
00094     /*
00095         Solaris wants a "const char *" as first arg of gethostbyaddr,
00096         but SUS says "const void *" so we will put in a cast.
00097     */
00098     if ((h = gethostbyaddr((const char *)&a, sizeof(a), AF_INET))
00099       == NULL) {
00100         if (h_errno == HOST_NOT_FOUND)
00101             printf("address not found\n");
00102         else
00103             printf("h_errno = %d\n", h_errno);
00104     }
00105     else
00106         display_hostent(h);
00107     return;
00108 
00109 EC_CLEANUP_BGN
00110     EC_FLUSH("gethostbyaddr_ex")
00111 EC_CLEANUP_END
00112 }
00113 /*[getnameinfo_ex]*/
00114 static void getnameinfo_ex(void)
00115 {
00116     struct sockaddr_in sa;
00117     char nodename[200], servname[200];
00118 
00119     sa.sin_family = AF_INET;
00120     sa.sin_port = htons(80);
00121     sa.sin_addr.s_addr =  inet_addr("216.109.125.70");
00122     ec_ai( getnameinfo((struct sockaddr *)&sa, sizeof(sa), nodename,
00123       sizeof(nodename), servname, sizeof(servname), 0) )
00124     printf("node: %s; service: %s\n", nodename, servname);
00125     return;
00126 
00127 EC_CLEANUP_BGN
00128     EC_FLUSH("getnameinfo_ex")
00129 EC_CLEANUP_END
00130 }
00131 /*[display_netent]*/
00132 static void display_netent(struct netent *n)
00133 {
00134     int i;
00135 
00136     printf("name: %s; type: %d; number: %lu\n", n->n_name, n->n_addrtype,
00137       (unsigned long)n->n_net);
00138     for (i = 0; n->n_aliases[i] != NULL; i++)
00139         printf("\t%s\n", n->n_aliases[i]);
00140 }
00141 /*[netdb]*/
00142 static void netdb(void)
00143 {
00144     struct netent *n;
00145 
00146     setnetent(true);
00147     while ((n = getnetent()) != NULL)
00148         display_netent(n);
00149     endnetent();
00150 }
00151 /*[display_protoent]*/
00152 static void display_protoent(struct protoent *p)
00153 {
00154     int i;
00155 
00156     printf("name: %s; number: %d\n", p->p_name, p->p_proto);
00157     for (i = 0; p->p_aliases[i] != NULL; i++)
00158         printf("\t%s\n", p->p_aliases[i]);
00159 }
00160 /*[protodb]*/
00161 static void protodb(void)
00162 {
00163     struct protoent *p;
00164 
00165     setprotoent(true);
00166     while ((p = getprotoent()) != NULL)
00167         display_protoent(p);
00168     endprotoent();
00169 }
00170 /*[display_servent]*/
00171 static void display_servent(struct servent *s)
00172 {
00173     int i;
00174 
00175     printf("name: %s; port: %d; protocol: %s\n", s->s_name, s->s_port,
00176       s->s_proto);
00177     for (i = 0; s->s_aliases[i] != NULL; i++)
00178         printf("\t%s\n", s->s_aliases[i]);
00179 }
00180 /*[servdb]*/
00181 static void servdb(void)
00182 {
00183     struct servent *s;
00184 
00185     setservent(true);
00186     while ((s = getservent()) != NULL)
00187         display_servent(s);
00188     endservent();
00189 }
00190 /*[cvt]*/
00191 static void cvt(void)
00192 {
00193     char ipv6[16], ipv6str[INET6_ADDRSTRLEN], ipv4str[INET_ADDRSTRLEN];
00194     uint32_t ipv4;
00195     int r;
00196 
00197     ec_neg1( r = inet_pton(AF_INET, "66.218.71.94", &ipv4) )
00198     if (r == 0)
00199         printf("Can't convert\n");
00200     else {
00201         ec_null( inet_ntop(AF_INET, &ipv4, ipv4str, sizeof(ipv4str)) )
00202         printf("%s\n", ipv4str);
00203     }
00204     ec_neg1( r = inet_pton(AF_INET6,
00205       "FEDC:BA98:7654:3210:FEDC:BA98:7654:3210", &ipv6) )
00206     if (r == 0)
00207         printf("Can't convert\n");
00208     else {
00209         ec_null( inet_ntop(AF_INET6, &ipv6, ipv6str, sizeof(ipv6str)) )
00210         printf("%s\n", ipv6str);
00211     }
00212     return;
00213 
00214 EC_CLEANUP_BGN
00215     EC_FLUSH("cvt")
00216 EC_CLEANUP_END
00217 }
00218 /*[]*/
00219 int main(void)
00220 {
00221 #if 0
00222     struct in_addr in;
00223 
00224     in.s_addr = (in_addr_t)gethostid();
00225     printf("hostid: %ld %lx %s\n", gethostid(), gethostid(),
00226       inet_ntoa(in));
00227     hostdb();
00228     gethostbyaddr_ex();
00229     gethostbyname_ex();
00230     getnameinfo_ex();
00231     netdb();
00232     protodb();
00233     servdb();
00234 #endif
00235     ifdb();
00236     cvt();
00237     exit(EXIT_SUCCESS);
00238 }

Generated on Fri Apr 23 10:57:01 2004 for AUP2 Example Source by doxygen 1.3.1