Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 44 additions & 10 deletions client/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,17 @@ static void add_otherInfos(Z_APDU *a)
}
}

static void print_comstack_error(const char *operation, COMSTACK cs)
{
const char *details = 0;
int code = cs_get_error(cs, &details);

fprintf(stderr, "%s: cs=%d msg=%s", operation, code, cs_errmsg(code));
if (details)
fprintf(stderr, " details=%s", details);
fprintf(stderr, "\n");
}

int send_apdu(Z_APDU *a)
{
char *buf;
Expand All @@ -301,7 +312,7 @@ int send_apdu(Z_APDU *a)
do_hex_dump(buf, len);
if (cs_put(conn, buf, len) < 0)
{
fprintf(stderr, "cs_put: %s\n", cs_errmsg(cs_errno(conn)));
print_comstack_error("cs_put", conn);
close_session();
return 0;
}
Expand Down Expand Up @@ -736,7 +747,7 @@ static int session_connect_base(const char *arg, const char **basep)
fflush(stdout);
if (cs_connect(conn, add) < 0)
{
printf("error = %s\n", cs_strerror(conn));
print_comstack_error("connect failed", conn);
cs_close(conn);
conn = 0;
return 0;
Expand Down Expand Up @@ -1322,6 +1333,7 @@ static int send_gdu(Z_GDU *gdu)

if (r >= 0)
return 2;
print_comstack_error("cs_put", conn);
}
return 0;
}
Expand Down Expand Up @@ -2740,30 +2752,40 @@ static WRBUF get_url(const char *uri, WRBUF username, WRBUF password,
"text/xml");
if (!z_GDU(out, &gdu, 0, 0))
{
yaz_log(YLOG_WARN, "Can not encode HTTP request URL:%s", uri);
fprintf(stderr, "Can not encode HTTP request URL:%s\n", uri);
}
else
{
void *add;
int cs_flags = CS_FLAGS_BLOCKING | (check_cert ? CS_FLAGS_CHECK_CERT : 0);
COMSTACK conn = cs_create_host(uri, cs_flags, &add);
if (cs_connect(conn, add) < 0)
yaz_log(YLOG_WARN, "Can not connect to URL:%s", uri);
if (!conn)
fprintf(stderr, "Can not create connection for URL:%s\n", uri);
else if (cs_connect(conn, add) < 0)
{
print_comstack_error("Can not connect", conn);
cs_close(conn);
}
else
Comment thread
Copilot marked this conversation as resolved.
{
int len;
char *buf = odr_getbuf(out, &len, 0);

if (cs_put(conn, buf, len) < 0)
yaz_log(YLOG_WARN, "cs_put failed URL:%s", uri);
print_comstack_error("cs_put failed", conn);
else
{
char *netbuffer = 0;
int netlen = 0;
int res = cs_get(conn, &netbuffer, &netlen);
if (res <= 0)
{
yaz_log(YLOG_WARN, "cs_get failed URL:%s", uri);
if (res < 0)
print_comstack_error("cs_get failed", conn);
else
fprintf(stderr,
"Connection closed while reading URL:%s\n",
uri);
}
else
{
Expand All @@ -2772,7 +2794,7 @@ static WRBUF get_url(const char *uri, WRBUF username, WRBUF password,
if (!z_GDU(in, &gdu, 0, 0)
|| gdu->which != Z_GDU_HTTP_Response)
{
yaz_log(YLOG_WARN, "decode failed URL: %s", uri);
fprintf(stderr, "decode failed URL: %s\n", uri);
}
else
{
Expand Down Expand Up @@ -4554,6 +4576,8 @@ static void wait_and_handle_response(int one_response_only)
res = cs_get(conn, &netbuffer, &netbufferlen);
if (res <= 0)
{
if (res < 0)
print_comstack_error("cs_get", conn);
if (reconnect_ok && protocol == PROTO_HTTP)
{
cs_close(conn);
Expand All @@ -4566,7 +4590,12 @@ static void wait_and_handle_response(int one_response_only)
int len_out;
buf_out = odr_getbuf(out, &len_out, 0);
do_hex_dump(buf_out, len_out);
cs_put(conn, buf_out, len_out);
if (cs_put(conn, buf_out, len_out) < 0)
{
print_comstack_error("cs_put", conn);
close_session();
break;
}
odr_reset(out);
continue;
}
Expand Down Expand Up @@ -4603,7 +4632,12 @@ static void wait_and_handle_response(int one_response_only)
int len_out;
buf_out = odr_getbuf(out, &len_out, 0);
do_hex_dump(buf_out, len_out);
cs_put(conn, buf_out, len_out);
if (cs_put(conn, buf_out, len_out) < 0)
{
print_comstack_error("cs_put", conn);
close_session();
break;
}
odr_reset(out);
continue;
}
Expand Down
13 changes: 13 additions & 0 deletions doc/book.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9880,6 +9880,17 @@ void odr_choice_bias(ODR o, int what);
<synopsis>
const char *cs_strerror(COMSTACK h);
</synopsis>
<para>
Additional information about the current error can be retrieved with
<function>cs_get_error</function>. The returned string is owned by the
COMSTACK and remains valid until the error is changed or the COMSTACK is
closed. If no additional information is available,
<literal>*details</literal> is set to null. The function also returns the
current error code.
</para>
<synopsis>
int cs_get_error(COMSTACK h, const char **details);
</synopsis>
</sect1>
<sect1 id="comstack.summary">
<title>Summary and Synopsis</title>
Expand Down Expand Up @@ -9916,6 +9927,8 @@ void odr_choice_bias(ODR o, int what);

int cs_look(COMSTACK handle);

int cs_get_error(COMSTACK handle, const char **details);

void *cs_straddr(COMSTACK handle, const char *str);

const char *cs_addrstr(COMSTACK h);
Expand Down
2 changes: 1 addition & 1 deletion src/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ cc_library(
"dirent", "mutex", "condvar", "thread_id", "gettimeofday",
"thread_create", "spipe", "url", "backtrace"
])
+ h_dir(".", ["cclp", "iconv-p", "mime", "mutex-p",
+ h_dir(".", ["cclp", "comstack-p", "iconv-p", "mime", "mutex-p",
"odr-priv", "sru-p", "zoom-p", "config", "diag-entry"
])
,
Expand Down
2 changes: 1 addition & 1 deletion src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ libyaz_la_SOURCES= $(GEN_FILES) \
odr_seq.c odr_oct.c ber_oct.c odr_bit.c ber_bit.c odr_oid.c \
ber_oid.c odr_use.c odr_choice.c odr_any.c ber_any.c odr.c odr_mem.c \
dumpber.c odr_enum.c odr-priv.h \
comstack.c tcpip.c unix.c \
comstack.c comstack-p.h tcpip.c unix.c \
prt-ext.c \
proxunit.c \
ill-get.c \
Expand Down
52 changes: 52 additions & 0 deletions src/comstack-p.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* This file is part of the YAZ toolkit.
* Copyright (C) Index Data.
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Index Data nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/**
* \file comstack-p.h
* \brief Private Header for COMSTACK
*/

#ifndef COMSTACK_P_H
#define COMSTACK_P_H

#include <yaz/comstack.h>

YAZ_BEGIN_CDECL

const char *yaz_tcpip_get_error_details(COMSTACK cs);

YAZ_END_CDECL

#endif
/*
* Local variables:
* c-basic-offset: 4
* c-file-style: "Stroustrup"
* indent-tabs-mode: nil
* End:
* vim: shiftwidth=4 tabstop=8 expandtab
*/
10 changes: 8 additions & 2 deletions src/comstack.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

#include <yaz/yaz-iconv.h>
#include <yaz/log.h>
#include <yaz/comstack.h>
#include "comstack-p.h"
#include <yaz/tcpip.h>
#include <yaz/unix.h>
#include <yaz/odr.h>
Expand Down Expand Up @@ -48,6 +48,13 @@ const char *cs_strerror(COMSTACK h)
return cs_errmsg(h->cerrno);
}

int cs_get_error(COMSTACK cs, const char **details)
{
if (details)
*details = yaz_tcpip_get_error_details(cs);
return cs->cerrno;
}

void cs_get_host_args(const char *type_and_host, const char **args)
{
*args = "";
Expand Down Expand Up @@ -470,4 +477,3 @@ void cs_set_max_recv_bytes(COMSTACK cs, int max_recv_bytes)
* End:
* vim: shiftwidth=4 tabstop=8 expandtab
*/

39 changes: 30 additions & 9 deletions src/statserv.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

#ifdef WIN32
#include <process.h>
Expand Down Expand Up @@ -132,6 +133,25 @@ static int log_session = 0; /* one-line logs for session */
static int log_sessiondetail = 0; /* more detailed stuff */
static int log_server = 0;

static void log_comstack_error(int level, COMSTACK cs, const char *fmt, ...)
Comment thread
adamdickmeiss marked this conversation as resolved.
{
char message[512];
const char *details = 0;
int code = cs_get_error(cs, &details);
va_list ap;

va_start(ap, fmt);
yaz_vsnprintf(message, sizeof(message), fmt, ap);
va_end(ap);

if (details)
yaz_log(level, "%s: cs=%d msg=%s details=%s",
message, code, cs_errmsg(code), details);
else
yaz_log(level, "%s: cs=%d msg=%s",
message, code, cs_errmsg(code));
}

/** get_logbits sets global loglevel bits */
static void get_logbits(int force)
{ /* needs to be called after parsing cmd-line args that can set loglevels!*/
Expand Down Expand Up @@ -857,7 +877,8 @@ static void listener(IOCHAN h, int event)

if ((res = cs_listen(line, 0, 0)) < 0)
{
yaz_log(YLOG_FATAL|YLOG_ERRNO, "cs_listen failed");
log_comstack_error(YLOG_FATAL|YLOG_ERRNO, line,
"cs_listen failed");
return;
}
else if (res == 1)
Expand All @@ -866,7 +887,7 @@ static void listener(IOCHAN h, int event)
new_line = cs_accept(line);
if (!new_line)
{
yaz_log(YLOG_FATAL, "Accept failed.");
log_comstack_error(YLOG_FATAL, line, "Accept failed");
return;
}
yaz_log(YLOG_DEBUG, "Accept ok");
Expand Down Expand Up @@ -955,7 +976,8 @@ static void listener(IOCHAN h, int event)
if ((res = cs_listen_check(line, 0, 0, control_block.check_ip,
control_block.daemon_name)) < 0)
{
yaz_log(YLOG_WARN|YLOG_ERRNO, "cs_listen failed");
log_comstack_error(YLOG_WARN|YLOG_ERRNO, line,
"cs_listen failed");
return;
}
else if (res == 1)
Expand All @@ -966,7 +988,7 @@ static void listener(IOCHAN h, int event)
new_line = cs_accept(line);
if (!new_line)
{
yaz_log(YLOG_FATAL, "Accept failed.");
log_comstack_error(YLOG_FATAL, line, "Accept failed");
iochan_setflags(h, EVENT_INPUT | EVENT_EXCEPT); /* reset listener */
return;
}
Expand Down Expand Up @@ -1170,11 +1192,11 @@ static int add_listener(char *where, int listen_id)

if (cs_bind(l, ap, CS_SERVER) < 0)
{
int level = YLOG_FATAL;

if (cs_errno(l) == CSYSERR)
yaz_log(YLOG_FATAL|YLOG_ERRNO, "Failed to bind to %s", where);
else
yaz_log(YLOG_FATAL, "Failed to bind to %s: %s", where,
cs_strerror(l));
level |= YLOG_ERRNO;
log_comstack_error(level, l, "Failed to bind to %s", where);
cs_close(l);
return -1;
}
Expand Down Expand Up @@ -1532,4 +1554,3 @@ int statserv_main(int argc, char **argv,
* End:
* vim: shiftwidth=4 tabstop=8 expandtab
*/

Loading