/** \brief COMSTACK synopsis from manual, doc/comstack.xml */ static int comstack_example(const char *server_address_str) { COMSTACK stack; char *buf = 0; int size = 0, length_incoming; void *server_address_ip; int status; char *protocol_package = "GET / HTTP/1.0\r\n\r\n"; int protocol_package_length = strlen(protocol_package); stack = cs_create(tcpip_type, 1, PROTO_HTTP); if (!stack) { perror("cs_create"); /* use perror() here since we have no stack yet */ return -1; } server_address_ip = cs_straddr(stack, server_address_str); if (!server_address_ip) { fprintf(stderr, "cs_straddr: address could not be resolved\n"); return -1; } status = cs_connect(stack, server_address_ip); if (status) { fprintf(stderr, "cs_connect: %s\n", cs_strerror(stack)); return -1; } status = cs_rcvconnect(stack); if (status) { fprintf(stderr, "cs_rcvconnect: %s\n", cs_strerror(stack)); return -1; } status = cs_put(stack, protocol_package, protocol_package_length); if (status) { fprintf(stderr, "cs_put: %s\n", cs_strerror(stack)); return -1; } /* Now get a response */ length_incoming = cs_get(stack, &buf, &size); if (!length_incoming) { fprintf(stderr, "Connection closed\n"); return -1; } else if (length_incoming < 0) { fprintf(stderr, "cs_get: %s\n", cs_strerror(stack)); return -1; } /* Print result */ fwrite(buf, length_incoming, 1, stdout); /* clean up */ cs_close(stack); if (buf) xfree(buf); return 0; }
COMSTACK cs_create_host_proxy(const char *vhost, int blocking, void **vp, const char *proxy_host) { enum oid_proto proto = PROTO_Z3950; const char *host = 0; COMSTACK cs; CS_TYPE t; char *connect_host = 0; if (!cs_parse_host(vhost, &host, &t, &proto, &connect_host)) return 0; if (proxy_host) { enum oid_proto proto1; xfree(connect_host); if (!cs_parse_host(proxy_host, &host, &t, &proto1, &connect_host)) return 0; } if (t == tcpip_type) { const char *bind_host = strchr(vhost, ' '); if (bind_host && bind_host[1]) bind_host++; else bind_host = 0; cs = yaz_tcpip_create2(-1, blocking, proto, connect_host ? host : 0, bind_host); } else { cs = cs_create(t, blocking, proto); } if (cs) { if (!(*vp = cs_straddr(cs, connect_host ? connect_host : host))) { cs_close (cs); cs = 0; } } xfree(connect_host); return cs; }