©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/adi.c

Go to the documentation of this file.
00001 /**
00002     getaddrinfo test program
00003     AUP2, Sec. 8.02.6
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 /*[]*/
00031 static void a1(void)
00032 {
00033     struct addrinfo *infop = NULL;
00034     int r;
00035 
00036     r = getaddrinfo("www.basepath.com", NULL, NULL, &infop);
00037     if (r != 0) {
00038         printf("Got error number %d\n", r);
00039         EC_FAIL
00040     }
00041     printf("It worked!\n");
00042     return;
00043 
00044 EC_CLEANUP_BGN
00045     exit(EXIT_FAILURE);
00046 EC_CLEANUP_END
00047 }
00048 #define main main1
00049 /*[main1]*/
00050 int main(void)
00051 {
00052     struct addrinfo *infop = NULL, hint;
00053 
00054     memset(&hint, 0, sizeof(hint));
00055     hint.ai_family = AF_INET;
00056     hint.ai_socktype = SOCK_STREAM;
00057     ec_ai( getaddrinfo("www.yahoo.com", "80", &hint, &infop) )
00058     for ( ; infop != NULL; infop = infop->ai_next) {
00059         struct sockaddr_in *sa = (struct sockaddr_in *)infop->ai_addr;
00060 
00061         printf("%s port: %d protocol: %d\n", inet_ntoa(sa->sin_addr),
00062           ntohs(sa->sin_port), infop->ai_protocol);
00063     }
00064     exit(EXIT_SUCCESS);
00065 
00066 EC_CLEANUP_BGN
00067     exit(EXIT_FAILURE);
00068 EC_CLEANUP_END
00069 }
00070 /*[]*/
00071 #undef main
00072 static void a3(void)
00073 {
00074     struct addrinfo *infop = NULL, *a, hint;
00075     int r;
00076 
00077     memset(&hint, 0, sizeof(hint));
00078     hint.ai_family = AF_INET6;
00079     hint.ai_socktype = SOCK_STREAM;
00080     //r = getaddrinfo("2001:0388::", "32", &hint, &infop);
00081     r = getaddrinfo("ipv6.research.microsoft.com", NULL, &hint, &infop);
00082     if (r != 0) {
00083         printf("Got error number %d\n", r);
00084         printf("%s\n", gai_strerror(r));
00085         EC_FAIL
00086     }
00087     printf("It worked!\n");
00088     for (a = infop; a != NULL; a = a->ai_next) {
00089         unsigned long ip = (long)((struct sockaddr_in *)a->ai_addr)->sin_addr.s_addr;
00090 
00091         if (a->ai_family == AF_INET6)
00092             printf("Bingo!\n");
00093         printf("port = 0x%x (%d) {%d,%d,%d} ip=0x%lx\n",
00094           ((struct sockaddr_in *)a->ai_addr)->sin_port,
00095           ((struct sockaddr_in *)a->ai_addr)->sin_port,
00096           a->ai_family, a->ai_socktype, a->ai_protocol, ip);
00097         printf("\t\t%lu.%lu.%lu.%lu\n", ip & 0xFF, (ip >> 8) & 0xFF, (ip >> 16) & 0xFF, ip >> 24);
00098     }
00099     return;
00100 
00101 EC_CLEANUP_BGN
00102     exit(EXIT_FAILURE);
00103 EC_CLEANUP_END
00104 }
00105 //#define main main2
00106 /*[main2]*/
00107 #define REQUEST "GET / HTTP/1.0\r\n\r\n"
00108 
00109 int main(void)
00110 {
00111     struct addrinfo *infop = NULL, hint;
00112     int fd_skt;
00113     char buf[1000];
00114     ssize_t nread;
00115 
00116 printf("%s\n", strerror(EAI_ADDRFAMILY));
00117 printf("%s\n", strerror(EAI_AGAIN));
00118 printf("%s\n", strerror(EAI_BADFLAGS));
00119 printf("%s\n", strerror(EAI_FAIL));
00120 exit(EXIT_SUCCESS);
00121 
00122     memset(&hint, 0, sizeof(hint));
00123     hint.ai_family = AF_INET;
00124     hint.ai_socktype = SOCK_STREAM;
00125     ec_ai( getaddrinfo("www.yahoo.com", "80", &hint, &infop) )
00126     ec_neg1( fd_skt = socket(infop->ai_family, infop->ai_socktype,
00127       infop->ai_protocol) )
00128     ec_neg1( connect(fd_skt, (struct sockaddr *)infop->ai_addr,
00129       infop->ai_addrlen) )
00130     ec_neg1( write(fd_skt, REQUEST, strlen(REQUEST) ) )
00131     ec_neg1( nread = read(fd_skt, buf, sizeof(buf)) )
00132     (void)write(STDOUT_FILENO, buf, nread);
00133     ec_neg1( close(fd_skt) )
00134     exit(EXIT_SUCCESS);
00135 
00136 EC_CLEANUP_BGN
00137     exit(EXIT_FAILURE);
00138 EC_CLEANUP_END
00139 }
00140 /*[]*/
00141 #if 0
00142 int main(void)
00143 {
00144     a3();
00145     exit(EXIT_SUCCESS);
00146 }
00147 #endif
00148 /*[]*/

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