| 1 | |
|---|
| 2 | /* ciupdsocx.c - included from ciupd.c |
|---|
| 3 | */ |
|---|
| 4 | |
|---|
| 5 | /* |
|---|
| 6 | Client program that makes a connection for a byte stream socket in the Internet namespace. |
|---|
| 7 | Once it has connected to the server, it sends a text string to the server and gets the response. |
|---|
| 8 | Returns the response size or a negative error value. |
|---|
| 9 | */ |
|---|
| 10 | /* |
|---|
| 11 | 16.9.6 Byte Stream Socket Example |
|---|
| 12 | Here is an example client program that makes a connection for a byte stream socket in the Internet namespace. It doesn't do anything particularly interesting once it has connected to the server; it just sends a text string to the server and exits. |
|---|
| 13 | This program uses init_sockaddr to set up the socket address; see Inet Example. |
|---|
| 14 | */ |
|---|
| 15 | |
|---|
| 16 | #ifndef CISIS_H |
|---|
| 17 | #define GEN_MAIN 1 |
|---|
| 18 | #endif |
|---|
| 19 | |
|---|
| 20 | #if GEN_MAIN |
|---|
| 21 | #include <stdio.h> |
|---|
| 22 | #include <errno.h> |
|---|
| 23 | #include <stdlib.h> |
|---|
| 24 | #include <unistd.h> |
|---|
| 25 | #include <sys/types.h> |
|---|
| 26 | #include <stdint.h> |
|---|
| 27 | #include <netinet/in.h> |
|---|
| 28 | #include <netdb.h> |
|---|
| 29 | #include <time.h> |
|---|
| 30 | #endif /* GEN_MAIN */ |
|---|
| 31 | |
|---|
| 32 | #if GEN_MAIN |
|---|
| 33 | #define PORT 80 |
|---|
| 34 | #define MESSAGE "HEAD / HTTP/1.0\r\n\r\n" |
|---|
| 35 | #define SERVERHOST "www.bireme.br" |
|---|
| 36 | #define PROTOCOL "HTTP" |
|---|
| 37 | #define PGCC 1 |
|---|
| 38 | #endif /* GEN_MAIN */ |
|---|
| 39 | |
|---|
| 40 | #define BUFFERSIZE 51200 |
|---|
| 41 | |
|---|
| 42 | #if 0 |
|---|
| 43 | "HEAD /'message' HTTP/1.0\r\n\r\n" |
|---|
| 44 | "GET / HTTP/1.0\nUser-Agent: Mozilla/3.0 (compatible; Opera/3.0; Windows 95/NT4)\nAccept: */*\nHost: birk105.studby.uio.no:81\n\n" |
|---|
| 45 | #endif |
|---|
| 46 | |
|---|
| 47 | #if PGCC |
|---|
| 48 | int init_sockaddr (struct sockaddr_in *name, const char *hostname, uint16_t port); |
|---|
| 49 | #endif |
|---|
| 50 | int mainclient_connect_to_server (int *sock, struct sockaddr_in* addr); |
|---|
| 51 | int mainclient_read_from_server (int cmd, int filedes, char *buffer, int buffersize, int maxrds); |
|---|
| 52 | int mainclient_write_to_server (int cmd, int filedes, char *message, int messagesize); |
|---|
| 53 | //int mainclient (int cmd, char *protocol, char *serverhost, uint16_t port, char *message, char *buffer, int buffersize, int maxrds); /* char *referrer, char *useragent, char *from */ |
|---|
| 54 | |
|---|
| 55 | int sock_debug = 0; |
|---|
| 56 | |
|---|
| 57 | /* |
|---|
| 58 | */ |
|---|
| 59 | #if PGCC |
|---|
| 60 | int |
|---|
| 61 | init_sockaddr (struct sockaddr_in *name, |
|---|
| 62 | const char *hostname, |
|---|
| 63 | uint16_t port) |
|---|
| 64 | { |
|---|
| 65 | struct hostent *hostinfo; |
|---|
| 66 | |
|---|
| 67 | if (sock_debug) { |
|---|
| 68 | fprintf(stderr, "***DEBUG*** init_sockaddr: host=%s port=%d\n", hostname, (int)port); |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | name->sin_family = AF_INET; |
|---|
| 72 | name->sin_port = htons (port); |
|---|
| 73 | hostinfo = gethostbyname (hostname); |
|---|
| 74 | if (hostinfo == NULL) |
|---|
| 75 | { |
|---|
| 76 | /*perror("unknown host");*/ |
|---|
| 77 | return -1; |
|---|
| 78 | } |
|---|
| 79 | name->sin_addr = *(struct in_addr *) hostinfo->h_addr; |
|---|
| 80 | |
|---|
| 81 | return 0; |
|---|
| 82 | } |
|---|
| 83 | #endif |
|---|
| 84 | |
|---|
| 85 | #define MAX_CONNECT_TIMES 10 |
|---|
| 86 | int |
|---|
| 87 | mainclient_connect_to_server (int *sock, struct sockaddr_in* addr) |
|---|
| 88 | { |
|---|
| 89 | int times; |
|---|
| 90 | int rc = -1; |
|---|
| 91 | |
|---|
| 92 | /* Connect to the server. */ |
|---|
| 93 | for (times = 1; times <= MAX_CONNECT_TIMES; times++) { |
|---|
| 94 | rc = connect (*sock, (struct sockaddr *) addr, sizeof (*addr)); |
|---|
| 95 | if (rc >= 0) { |
|---|
| 96 | break; |
|---|
| 97 | } |
|---|
| 98 | close(*sock); |
|---|
| 99 | *sock = socket (PF_INET, SOCK_STREAM, 0); |
|---|
| 100 | if (*sock < 0) { |
|---|
| 101 | fprintf (stderr, "***ERROR*** mainclient_connect_to_server: family=%d type=%d protocol=%d ret=%d errno=%d\n", |
|---|
| 102 | PF_INET, SOCK_STREAM, 0, *sock, errno); |
|---|
| 103 | break; |
|---|
| 104 | } |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | if (rc < 0) { |
|---|
| 108 | fprintf (stderr, "***ERROR*** mainclient_connect_to_server: socket=%d addr=%p addrlen=%d ret=%d errno=%d try_times=%d\n", |
|---|
| 109 | *sock, (struct sockaddr *) addr, sizeof (*addr), rc, errno, times); |
|---|
| 110 | /*} else if (times > 1) {*/ |
|---|
| 111 | } else if (times > 0) { |
|---|
| 112 | if (sock_debug) { |
|---|
| 113 | fprintf (stderr, "***DEBUG*** mainclient_connect_to_server: times=%d rc=%d\n", times, rc); |
|---|
| 114 | } |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | return rc; |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | int |
|---|
| 121 | mainclient_write_to_server (int cmd, int filedes, char *message, int messagesize) |
|---|
| 122 | { |
|---|
| 123 | int nbytes; |
|---|
| 124 | |
|---|
| 125 | if (sock_debug) { |
|---|
| 126 | fprintf (stderr, "***DEBUG*** mainclient_write_to_server: send message(%d): [%s]\n", messagesize, message); |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | if (cmd == 3) fprintf (stderr, "Client: send message(%d): '%s'\n", messagesize, message); |
|---|
| 130 | if (cmd == 2) fprintf (stderr, "Client: send message(%d) \n", messagesize); |
|---|
| 131 | |
|---|
| 132 | nbytes = write (filedes, message, messagesize); |
|---|
| 133 | if (nbytes < messagesize) |
|---|
| 134 | { |
|---|
| 135 | /* Write error */ |
|---|
| 136 | /*perror ("write");*/ |
|---|
| 137 | fprintf (stderr, "***ERROR*** mainclient_write_to_server/write: socket=%d buf=[%s] buflen=%d ret=%d errno=%d\n", |
|---|
| 138 | filedes, message, messagesize, nbytes, errno); |
|---|
| 139 | return -1; |
|---|
| 140 | } |
|---|
| 141 | else |
|---|
| 142 | return nbytes; |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | int |
|---|
| 146 | mainclient_read_from_server (int cmd, int filedes, char *buffer, int buffersize, int maxrds) |
|---|
| 147 | { |
|---|
| 148 | int nbytes; |
|---|
| 149 | int more; |
|---|
| 150 | |
|---|
| 151 | buffer[0]='\0'; |
|---|
| 152 | nbytes = read (filedes, buffer, buffersize-1); |
|---|
| 153 | if (nbytes < 0) |
|---|
| 154 | { |
|---|
| 155 | /* Read error */ |
|---|
| 156 | /*perror ("read");*/ |
|---|
| 157 | fprintf (stderr, "***ERROR*** mainclient_read_from_server/read1: socket=%d buff=[%s] bufflen=%d ret=%d errno=%d\n", |
|---|
| 158 | filedes, buffer, buffersize-1, nbytes, errno); |
|---|
| 159 | return -1; |
|---|
| 160 | } |
|---|
| 161 | else |
|---|
| 162 | { |
|---|
| 163 | /* Data read. */ |
|---|
| 164 | buffer[nbytes]='\0'; |
|---|
| 165 | if (cmd == 3) fprintf (stderr, "Client: got message(%d): '%s' \n", nbytes, buffer); |
|---|
| 166 | if (cmd == 2) fprintf (stderr, "Client: got message(%d) \n", nbytes); |
|---|
| 167 | |
|---|
| 168 | if (sock_debug) { |
|---|
| 169 | fprintf (stderr, "***DEBUG*** mainclient_read_from_server/1: got +message(%d) \n", more); |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | while (--maxrds) |
|---|
| 173 | if ((more = read (filedes, buffer+nbytes, buffersize-1-nbytes)) > 0) |
|---|
| 174 | { |
|---|
| 175 | if (sock_debug) { |
|---|
| 176 | fprintf (stderr, "***DEBUG*** mainclient_read_from_server/2: got +message(%d) \n", more); |
|---|
| 177 | } |
|---|
| 178 | buffer[nbytes+more]='\0'; |
|---|
| 179 | if (cmd == 3) fprintf (stderr, "Client: got +message(%d): '%s' \n", more, buffer+nbytes); |
|---|
| 180 | if (cmd == 2) fprintf (stderr, "Client: got +message(%d) \n", more); |
|---|
| 181 | nbytes+=more; |
|---|
| 182 | } |
|---|
| 183 | else { |
|---|
| 184 | break; |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | if (more < 0) { |
|---|
| 188 | fprintf (stderr, "***ERROR*** mainclient_read_from_server/read2: socket=%d buff=[%s] bufflen=%d ret=%d errno=%d\n", |
|---|
| 189 | filedes, buffer+nbytes, buffersize-1-nbytes, more, errno); |
|---|
| 190 | return -2; |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | return nbytes; |
|---|
| 194 | } |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | int |
|---|
| 199 | mainclient (int cmd, |
|---|
| 200 | char *protocol, /* HTTP */ /* X1417 */ |
|---|
| 201 | char *serverhost, /* www.bireme.br */ /* serverofi.bireme.br */ |
|---|
| 202 | uint16_t port, /* 80 */ /* 1417 */ |
|---|
| 203 | char *message, /* HEAD|GET /docpath HTTP/1.0\r\n\r\n */ /* wtrig2 c=dff maxrel=3 minsim=0.87 text=diabetes mellitus */ |
|---|
| 204 | char *buffer, /* results */ |
|---|
| 205 | int buffersize, /* max size */ |
|---|
| 206 | int maxrds /* max reads */ /* 1 */ |
|---|
| 207 | ) |
|---|
| 208 | { |
|---|
| 209 | int sock; |
|---|
| 210 | #if PGCC |
|---|
| 211 | struct sockaddr_in servername; |
|---|
| 212 | #endif |
|---|
| 213 | |
|---|
| 214 | int nbytes; |
|---|
| 215 | int rc; |
|---|
| 216 | char *p,*q; |
|---|
| 217 | |
|---|
| 218 | /* Trap null msg */ |
|---|
| 219 | if (!message) /*return 0;*/ message="\\n"; //\n |
|---|
| 220 | if (!*message) /*return 0;*/ message="\\n"; |
|---|
| 221 | |
|---|
| 222 | if (cmd >= 1) fprintf(stdout, "<mainclient>\n"); |
|---|
| 223 | if (cmd >= 1) fprintf(stdout, "<message>%s</message><serverhost>%s</serverhost><port>%d</port>\n",message,serverhost,port); |
|---|
| 224 | |
|---|
| 225 | /* Convert calling \n to LF */ |
|---|
| 226 | for (p=buffer, q=message; *q; p++, q++) |
|---|
| 227 | if (*q=='\\' && *(q+1)=='n') { |
|---|
| 228 | //*p = '\n'; q++; HB 20081010 |
|---|
| 229 | *p++ = '\r';*p = '\n'; q++; |
|---|
| 230 | } |
|---|
| 231 | else *p = *q; |
|---|
| 232 | *p='\0'; |
|---|
| 233 | |
|---|
| 234 | #if !PGCC |
|---|
| 235 | #if CIWTF |
|---|
| 236 | /* Fake output. */ |
|---|
| 237 | if (*buffer) { |
|---|
| 238 | char *msgp=strdup(buffer); |
|---|
| 239 | if (strcmp(protocol,"X1417") == 0) { |
|---|
| 240 | //loadcoll |
|---|
| 241 | rc = loadcoll(cmd, ciawtfp, /*message*/ msgp, buffer); |
|---|
| 242 | if (rc < 0) { |
|---|
| 243 | ///*perror ("read/loadcoll (client)");*/ |
|---|
| 244 | nbytes=(-5); *buffer='\0'; |
|---|
| 245 | } |
|---|
| 246 | else nbytes=rc; |
|---|
| 247 | } |
|---|
| 248 | else |
|---|
| 249 | if (strcmp(protocol,"HTTP") == 0) { |
|---|
| 250 | sprintf(buffer,"%s","HTTP/1.1 200 OK\n...\nContent-Length: 14082\nConnection: close\nContent-Type: text/html\n\n"); |
|---|
| 251 | nbytes=strlen(buffer); |
|---|
| 252 | } |
|---|
| 253 | #if CICPP |
|---|
| 254 | delete[] msgp; |
|---|
| 255 | #else |
|---|
| 256 | FREE(msgp); |
|---|
| 257 | #endif |
|---|
| 258 | } |
|---|
| 259 | #endif //CIWTF |
|---|
| 260 | #else //!PGCC |
|---|
| 261 | |
|---|
| 262 | /* Create the socket. */ |
|---|
| 263 | sock = socket (PF_INET, SOCK_STREAM, 0); |
|---|
| 264 | if (sock < 0) |
|---|
| 265 | { |
|---|
| 266 | /*perror ("socket (client)");*/ |
|---|
| 267 | fprintf (stderr, "***ERROR*** mainclient/socket: family=%d type=%d protocol=%d ret=%d errno=%d\n", |
|---|
| 268 | PF_INET, SOCK_STREAM, 0, sock, errno); |
|---|
| 269 | return -1; |
|---|
| 270 | } |
|---|
| 271 | |
|---|
| 272 | /* Init (init_sockaddr). */ |
|---|
| 273 | rc = init_sockaddr (&servername, serverhost, port); |
|---|
| 274 | if (rc < 0) |
|---|
| 275 | { |
|---|
| 276 | /*fprintf (stderr, "Unknown host %s.\n", hostname);*/ |
|---|
| 277 | fprintf (stderr, "***ERROR*** mainclient/init_sockaddr: name=%p hostname=%s port=%u ret=%d errno=%d\n", |
|---|
| 278 | &servername, serverhost, port, rc, errno); |
|---|
| 279 | close (sock); |
|---|
| 280 | return -2; |
|---|
| 281 | } |
|---|
| 282 | |
|---|
| 283 | /* Connect to the server. */ |
|---|
| 284 | /* |
|---|
| 285 | rc = connect (sock, (struct sockaddr *) &servername, sizeof (servername)); |
|---|
| 286 | if (rc < 0) |
|---|
| 287 | { |
|---|
| 288 | fprintf (stderr, "***ERROR*** mainclient/sock: socket=%d addr=%p addrlen=%d ret=%d errno=%d\n", |
|---|
| 289 | sock, (struct sockaddr *) &servername, sizeof (servername), rc, errno); |
|---|
| 290 | close (sock); |
|---|
| 291 | return -3; |
|---|
| 292 | } |
|---|
| 293 | */ |
|---|
| 294 | rc = mainclient_connect_to_server (&sock, &servername); |
|---|
| 295 | if (rc < 0) |
|---|
| 296 | { |
|---|
| 297 | close (sock); |
|---|
| 298 | return -3; |
|---|
| 299 | } |
|---|
| 300 | |
|---|
| 301 | /* Send data to the server. */ |
|---|
| 302 | nbytes = mainclient_write_to_server (cmd, sock, /*message*/buffer, strlen(/*message*/buffer)); |
|---|
| 303 | if (nbytes < 0) |
|---|
| 304 | { |
|---|
| 305 | /*perror ("write (client)");*/ |
|---|
| 306 | close (sock); |
|---|
| 307 | return -4; |
|---|
| 308 | } |
|---|
| 309 | |
|---|
| 310 | /* Get data from the server. */ |
|---|
| 311 | nbytes = mainclient_read_from_server (cmd, sock, buffer, buffersize, maxrds); |
|---|
| 312 | if (nbytes < 0) |
|---|
| 313 | { |
|---|
| 314 | /*perror ("read (client)");*/ |
|---|
| 315 | close (sock); |
|---|
| 316 | return -5; |
|---|
| 317 | } |
|---|
| 318 | |
|---|
| 319 | /* Release the socket. */ |
|---|
| 320 | rc = close (sock); |
|---|
| 321 | if (rc < 0) { |
|---|
| 322 | fprintf (stderr, "***ERROR*** mainclient/close: ret=%d errno=%d\n", rc, errno); |
|---|
| 323 | return -6; |
|---|
| 324 | } |
|---|
| 325 | if (sock_debug) { |
|---|
| 326 | fprintf (stderr, "***DEBUG*** mainclient/close\n"); |
|---|
| 327 | } |
|---|
| 328 | |
|---|
| 329 | #endif //!PGCC |
|---|
| 330 | |
|---|
| 331 | /* parse 1st line |
|---|
| 332 | HTTP/1.1 200 OK |
|---|
| 333 | HTTP/1.1 3 Found |
|---|
| 334 | HTTP/1.1 403 Forbidden |
|---|
| 335 | HTTP/1.1 404 Not Found |
|---|
| 336 | HTTP/1.1 501 Method Not Implemented |
|---|
| 337 | */ |
|---|
| 338 | |
|---|
| 339 | if (cmd >= 1) { |
|---|
| 340 | fprintf(stdout, "<content>%s</content>\n",buffer); |
|---|
| 341 | if (strncmp(buffer,"HTTP/",5) == 0) { |
|---|
| 342 | for (p=buffer; *p; p++) if (*p==' ') break; *p++='\0'; rc=atoi(p); |
|---|
| 343 | for ( ; *p; p++) if (*p==' ') break; *p++='\0'; q=p; |
|---|
| 344 | for ( ; *p; p++) if (*p=='\r' || *p=='\n') break; *p='\0'; |
|---|
| 345 | fprintf(stdout, "<protocol>%s</protocol><code>%d</code><msg>%s</msg>\n",buffer,rc,q); |
|---|
| 346 | } |
|---|
| 347 | fprintf(stdout, "</mainclient>\n"); |
|---|
| 348 | } |
|---|
| 349 | |
|---|
| 350 | return nbytes; |
|---|
| 351 | } |
|---|
| 352 | |
|---|
| 353 | |
|---|
| 354 | /* Main |
|---|
| 355 | |
|---|
| 356 | ./client4 0 "HEAD / HTTP/1.0\n\n" |
|---|
| 357 | ./client4 1 "HEAD / HTTP/1.0\n\n" |
|---|
| 358 | ./client4 2 "HEAD / HTTP/1.0\n\n" |
|---|
| 359 | |
|---|
| 360 | ./client4 1 "HEAD http://lis.bvs.br/xml2html/xmlListT.php?xml%5B%5D=http://lis.bvs.br/lis-Regional/P/define.xml&xsl=http://lis.bvs.br/lis-Regional/home.xsl HTTP/1.0\n\n" lis.bvs.br 80 |
|---|
| 361 | ./client4 0 "GET http://lis.bvs.br/xml2html/xmlListT.php?xml%5B%5D=http://lis.bvs.br/lis-Regional/P/define.xml&xsl=http://lis.bvs.br/lis-Regional/home.xsl HTTP/1.0\n\n" lis.bvs.br 80 |
|---|
| 362 | |
|---|
| 363 | */ |
|---|
| 364 | |
|---|
| 365 | |
|---|
| 366 | #if GEN_MAIN |
|---|
| 367 | int |
|---|
| 368 | main(int argc, char *argv[]) |
|---|
| 369 | { |
|---|
| 370 | /* calling parameters */ |
|---|
| 371 | int cmd; |
|---|
| 372 | char *protocol=PROTOCOL; |
|---|
| 373 | char *message=MESSAGE; |
|---|
| 374 | char *serverhost=SERVERHOST; |
|---|
| 375 | uint16_t port=PORT; |
|---|
| 376 | int maxrds=1; |
|---|
| 377 | int rc; |
|---|
| 378 | |
|---|
| 379 | char buffer[BUFFERSIZE]; |
|---|
| 380 | |
|---|
| 381 | if (argc == 1) { |
|---|
| 382 | fprintf(stderr, "client4 0=plain|1=xml|2=tell|3=trace \"HEAD|GET docpath\\n\\n\" | \"wtrig2 c=col text=x\" [serverhost [port [maxreads [HTTP|X1417]] ] ] \n"); |
|---|
| 383 | exit (EXIT_FAILURE); |
|---|
| 384 | } |
|---|
| 385 | |
|---|
| 386 | cmd=atoi(argv[1]); |
|---|
| 387 | |
|---|
| 388 | if (argc > 2) message=argv[2]; |
|---|
| 389 | if (argc > 3) serverhost=argv[3]; |
|---|
| 390 | if (argc > 4) port=(uint16_t)atoi(argv[4]); |
|---|
| 391 | if (argc > 5) maxrds=atoi(argv[5]); |
|---|
| 392 | if (argc > 6) protocol=argv[6]; |
|---|
| 393 | |
|---|
| 394 | if (cmd >= 3) fprintf(stderr, "client4: \"%s\" %s %d \n",message,serverhost,port); |
|---|
| 395 | |
|---|
| 396 | rc=mainclient(cmd, protocol, serverhost, (uint16_t)port, message, buffer, BUFFERSIZE, maxrds); |
|---|
| 397 | |
|---|
| 398 | if (rc < 0) { |
|---|
| 399 | if (cmd >= 3) fprintf(stderr, "client4: exit_failure %d \n", rc); |
|---|
| 400 | exit (EXIT_FAILURE); |
|---|
| 401 | } |
|---|
| 402 | |
|---|
| 403 | /* Output results */ |
|---|
| 404 | fprintf(stdout, "%s",buffer); |
|---|
| 405 | |
|---|
| 406 | if (cmd >= 3) fprintf(stderr, "client4: exit_success \n"); |
|---|
| 407 | exit (EXIT_SUCCESS); |
|---|
| 408 | } |
|---|
| 409 | #endif /* GEN_MAIN */ |
|---|
| 410 | |
|---|
| 411 | #undef GEN_MAIN |
|---|