2 * Copyright (C) 1995-2006, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: yaz-illclient.c,v 1.7 2007-05-30 13:59:04 heikki Exp $
8 /* WARNING - This is work in progress - not at all ready */
10 /** \file yaz-illclient.c
11 * \brief client for ILL requests (ISO 10161-1)
13 * This is a test client for handling ISO 10161-1 ILL requests.
14 * Those are not directly Z39.50, but the protocol is quite similar
15 * and yaz already provides the APDUS for it.
17 * This is not an interactive client like yaz-client, but driven by command-
18 * line arguments. Its output is a return code, and possibly some text on
21 * Exit codes (note, the program exits as soon as it finds a good reason)
23 * 1 errors in arguments
24 * 2 Internal errors in creating objects (comstack, odr...)
25 * mostly programming errors.
27 * 4 could not send request
28 * 5 No reponse received
29 * 6 Error decoding response packet
30 * 7 Server returned an error (see log or stdout)
51 #include <yaz/yaz-util.h>
52 #include <yaz/proto.h>
53 #include <yaz/comstack.h>
54 #include <yaz/tcpip.h>
59 #include <yaz/oclc-ill-req-ext.h>
62 /* A structure for holding name-value pairs in a linked list */
69 /* A structure for storing all the arguments */
74 char *oclc_recno; /* record number in oclc-mode */
75 int oclc_auth; /* 1=use oclc-type auth */
76 struct nameval* namevals; /* circular list, points to last */
81 /* Call-back to be called for every field in the ill-request */
82 /* It can set values to any field, perhaps from the prog_args */
83 const char *get_ill_element(void *clientData, const char *element) {
84 struct prog_args *args = (struct prog_args *) clientData;
85 struct nameval *nv=args->namevals;
91 /* printf("comparing '%s' with '%s' \n",element, nv->name ); */
92 if ( strcmp(element, nv->name) == 0 )
94 } while ( ( !ret) && ( nv != args->namevals) );
95 yaz_log(YLOG_DEBUG,"get_ill_element:'%s'->'%s'", element, ret );
100 /* * * * * * * * * * * * * * * * * */
102 /** \brief parse a parameter string */
103 /* string is like 'name=value' */
104 struct nameval *parse_nameval( char *arg ) {
105 struct nameval *nv = (struct nameval *) xmalloc(sizeof(*nv));
109 return 0; /* yeah, leaks a bit of memory. so what */
110 while ( *p && ( *p != '=' ) )
115 nv->name = (char *) xmalloc(len+1);
116 strncpy(nv->name, arg, len);
119 p++; /* skip the '=' */
121 return 0; /* oops, no '=' */
123 return 0; /* no value */
126 yaz_log(YLOG_DEBUG,"parse_nameval: n='%s' v='%s'", nv->name, nv->val );
130 /** \brief append nv to the list of namevals */
131 void append_nameval (struct prog_args *args, struct nameval *nv) {
134 if (args->namevals) {
135 nv->next=args->namevals->next; /* first */
136 args->namevals->next=nv;
142 } /* append_nameval */
144 /** \brief parse a parameter file */
145 void parse_paramfile(char *arg, struct prog_args *args) {
146 FILE *f=fopen(arg,"r");
152 yaz_log(YLOG_FATAL,"Could not open param file '%s' ", arg);
153 printf("Could not open file '%s' \n",arg);
156 yaz_log(YLOG_DEBUG,"Opened input file '%s' ",arg );
157 while (fgets(buf,BUFSIZE,f)) {
158 if (buf[0] != '#' ) {
160 if (buf[len] == '\n')
162 nv=parse_nameval(buf);
163 append_nameval(args, nv);
164 } /* not a comment */
170 printf("nv chain: ================ \n");
171 printf("(last:) %p: '%s' = '%s' (%p) \n",nv, nv->name, nv->val, nv->next );
174 printf("%p: '%s' = '%s' (%p)\n",nv, nv->name, nv->val, nv->next );
175 } while (nv != args->namevals );
179 } /* parse_paramfile */
182 /** \brief Parse program arguments */
183 void parseargs( int argc, char * argv[], struct prog_args *args) {
187 char *version="$Id: yaz-illclient.c,v 1.7 2007-05-30 13:59:04 heikki Exp $"; /* from cvs */
191 args->host = 0; /* not known (yet) */
192 args->namevals=0; /* none set up */
195 args->auth_userid = 0;
196 args->auth_passwd = 0;
198 /* Example 3 - directly from OCLC, supposed to work on their test server*/
199 args->auth_userid = "100-310-658" ; /* FIXME - get from cmd line */
200 args->auth_passwd = "apii2test" ; /* FIXME - get from cmd line */
203 while ((ret = options("Vov:p:u:D:f:r:l:", argv, argc, &arg)) != -2)
205 yaz_log(YLOG_DEBUG,"parsing option '%c' '%s'",ret, arg);
211 args->host = xstrdup (arg);
215 fprintf(stderr, "%s: Specify at most one server address\n",
221 yaz_log_init(yaz_log_mask_str(arg), "", 0);
224 yaz_log_init_file(arg);
226 printf("%s %s",prog, version );
229 nv=parse_nameval(arg);
230 append_nameval(args,nv);
233 parse_paramfile(arg,args);
239 args->auth_userid=xstrdup(arg);
242 args->auth_passwd=xstrdup(arg);
245 args->oclc_recno=xstrdup(arg);
248 fprintf (stderr, "Usage: %s "
252 " [-o -u user -p passwd]"
261 /* * * * * * * * * * * */
262 /** \brief Validate the arguments make sense */
263 void validateargs( struct prog_args *args) {
265 fprintf(stderr, "Specify a connection address, "
266 "as in 'bagel.indexdata.dk:210' \n");
269 if (args->oclc_auth && ((!args->auth_userid) || (!args->auth_passwd))){
270 fprintf(stderr, "-o option requires -u <user> and -p <pwd>\n");
276 /* * * * * * * * * * * * * * * */
277 /** \brief Connect to the target */
278 COMSTACK connect_to( char *hostaddr ){
280 void *server_address_ip;
283 yaz_log(YLOG_DEBUG,"Connecting to '%s'", hostaddr);
284 stack = cs_create_host(hostaddr, 1, &server_address_ip );
286 yaz_log(YLOG_FATAL,"Error in creating the comstack '%s' ",
291 yaz_log(YLOG_DEBUG,"Created stack ok ");
293 status = cs_connect(stack, server_address_ip);
295 yaz_log(YLOG_FATAL|YLOG_ERRNO,"Can not connect '%s' ",
299 yaz_log(YLOG_DEBUG,"Connected OK to '%s'", hostaddr);
304 /* * * * * * * * * * * * * * * */
305 /* Makes a Z39.50-like prompt package with username and password */
306 Z_PromptObject1 *makeprompt(struct prog_args *args, ODR odr) {
307 Z_PromptObject1 *p = (Z_PromptObject1 *) odr_malloc(odr, sizeof(*p) );
308 Z_ResponseUnit1 *ru = (Z_ResponseUnit1 *) odr_malloc(odr, sizeof(*ru) );
310 p->which=Z_PromptObject1_response;
311 p->u.response = (Z_Response1*) odr_malloc(odr, sizeof(*(p->u.response)) );
312 p->u.response->num=2;
313 p->u.response->elements= (Z_ResponseUnit1 **) odr_malloc(odr,
314 p->u.response->num*sizeof(*(p->u.response->elements)) );
315 /* user id, aka "oclc authorization number" */
316 p->u.response->elements[0] = ru;
317 ru->promptId = (Z_PromptId *) odr_malloc(odr, sizeof(*(ru->promptId) ));
318 ru->promptId->which = Z_PromptId_enumeratedPrompt;
319 ru->promptId->u.enumeratedPrompt = (Z_PromptIdEnumeratedPrompt *)
320 odr_malloc(odr, sizeof(*(ru->promptId->u.enumeratedPrompt) ));
321 ru->promptId->u.enumeratedPrompt->type =
322 odr_intdup(odr,Z_PromptIdEnumeratedPrompt_userId);
323 ru->promptId->u.enumeratedPrompt->suggestedString = 0 ;
324 ru->which = Z_ResponseUnit1_string ;
325 ru->u.string = odr_strdup(odr, args->auth_userid);
327 ru = (Z_ResponseUnit1 *) odr_malloc(odr, sizeof(*ru) );
328 p->u.response->elements[1] = ru;
329 ru->promptId = (Z_PromptId *) odr_malloc(odr, sizeof(*(ru->promptId) ));
330 ru->promptId->which = Z_PromptId_enumeratedPrompt;
331 ru->promptId->u.enumeratedPrompt = (Z_PromptIdEnumeratedPrompt *)
332 odr_malloc(odr, sizeof(*(ru->promptId->u.enumeratedPrompt) ));
333 ru->promptId->u.enumeratedPrompt->type =
334 odr_intdup(odr,Z_PromptIdEnumeratedPrompt_password);
335 ru->promptId->u.enumeratedPrompt->suggestedString = 0 ;
336 ru->which = Z_ResponseUnit1_string ;
337 ru->u.string = odr_strdup(odr, args->auth_passwd);
341 ILL_Extension *makepromptextension(struct prog_args *args, ODR odr) {
342 ODR odr_ext = odr_createmem(ODR_ENCODE);
343 ODR odr_prt = odr_createmem(ODR_PRINT);
344 ILL_Extension *e = (ILL_Extension *) odr_malloc(odr, sizeof(*e));
345 Z_PromptObject1 *p = makeprompt(args,odr_ext);
348 Z_External *ext = (Z_External *) odr_malloc(odr, sizeof(*ext));
349 ext->direct_reference = odr_getoidbystr(odr,"1.2.840.10003.8.1");
350 ext->indirect_reference=0;
352 ext->which=Z_External_single;
353 if ( ! z_PromptObject1(odr_ext, &p, 0,0 ) ) {
354 yaz_log(YLOG_FATAL,"Encoding of z_PromptObject1 failed ");
358 printf ("Prompt: \n"); /*!*/
359 z_PromptObject1(odr_prt, &p, 0,0 ); /*!*/
361 buf= odr_getbuf(odr_ext,&siz,0);
362 ext->u.single_ASN1_type=(Odr_any *)
363 odr_malloc(odr,sizeof(*ext->u.single_ASN1_type));
364 ext->u.single_ASN1_type->buf= (unsigned char *) odr_malloc(odr, siz);
365 memcpy(ext->u.single_ASN1_type->buf,buf, siz );
366 ext->u.single_ASN1_type->len = ext->u.single_ASN1_type->size = siz;
368 odr_reset(odr_prt); /*!*/
370 e->identifier = odr_intdup(odr,1);
371 e->critical = odr_intdup(odr,0);
372 e->item = (Odr_any *) odr_malloc(odr,sizeof(*e->item));
373 if ( ! z_External(odr_ext, &ext,0,0) ) {
374 yaz_log(YLOG_FATAL,"Encoding of z_External failed ");
377 printf("External: \n");
378 z_External(odr_prt, &ext,0,0); /*!*/
379 buf= odr_getbuf(odr_ext,&siz,0);
380 e->item->buf= (unsigned char *) odr_malloc(odr, siz);
381 memcpy(e->item->buf,buf, siz );
382 e->item->len = e->item->size = siz;
384 odr_destroy(odr_prt);
385 odr_destroy(odr_ext);
387 } /* makepromptextension */
389 ILL_Extension *makeoclcextension(struct prog_args *args, ODR odr) {
390 /* The oclc extension is required, but only contains optional */
391 /* fields. Here we just null them all out */
392 ODR odr_ext = odr_createmem(ODR_ENCODE);
393 ODR odr_prt = odr_createmem(ODR_PRINT);
394 ILL_Extension *e = (ILL_Extension *) odr_malloc(odr, sizeof(*e));
395 ILL_OCLCILLRequestExtension *oc = (ILL_OCLCILLRequestExtension *)
396 odr_malloc(odr_ext, sizeof(*oc));
399 Z_External *ext = (Z_External *) odr_malloc(odr, sizeof(*ext));
400 oc->clientDepartment = 0;
401 oc->paymentMethod = 0;
402 oc->uniformTitle = 0;
403 oc->dissertation = 0;
406 oc->affiliations = 0;
408 ext->direct_reference = odr_getoidbystr(odr,"1.0.10161.13.2");
409 ext->indirect_reference=0;
411 ext->which=Z_External_single;
412 if ( ! ill_OCLCILLRequestExtension(odr_ext, &oc, 0,0 ) ) {
413 yaz_log(YLOG_FATAL,"Encoding of ill_OCLCILLRequestExtension failed ");
417 printf ("OCLC: \n"); /*!*/
418 ill_OCLCILLRequestExtension(odr_prt, &oc, 0,0 ); /*!*/
420 buf= odr_getbuf(odr_ext,&siz,0);
421 ext->u.single_ASN1_type = (Odr_any*)
422 odr_malloc(odr,sizeof(*ext->u.single_ASN1_type));
423 ext->u.single_ASN1_type->buf = (unsigned char *) odr_malloc(odr, siz);
424 memcpy(ext->u.single_ASN1_type->buf,buf, siz );
425 ext->u.single_ASN1_type->len = ext->u.single_ASN1_type->size = siz;
427 odr_reset(odr_prt); /*!*/
429 e->identifier = odr_intdup(odr,1);
430 e->critical = odr_intdup(odr,0);
431 e->item = (Odr_any *) odr_malloc(odr,sizeof(*e->item));
432 if ( ! z_External(odr_ext, &ext,0,0) ) {
433 yaz_log(YLOG_FATAL,"Encoding of z_External failed ");
436 printf("External: \n");
437 z_External(odr_prt, &ext,0,0); /*!*/
438 buf= odr_getbuf(odr_ext,&siz,0);
439 e->item->buf= (unsigned char *) odr_malloc(odr, siz);
440 memcpy(e->item->buf, buf, siz);
441 e->item->len = e->item->size = siz;
443 odr_destroy(odr_prt);
444 odr_destroy(odr_ext);
447 } /* makeoclcextension */
449 ILL_APDU *createrequest( struct prog_args *args, ODR odr) {
450 struct ill_get_ctl ctl;
455 ctl.clientData = args;
456 ctl.f = get_ill_element;
457 apdu = (ILL_APDU *) odr_malloc( odr, sizeof(*apdu) );
458 apdu->which=ILL_APDU_ILL_Request;
459 req = ill_get_ILLRequest(&ctl, "ill", 0);
460 apdu->u.illRequest=req;
461 if (args->oclc_auth) {
462 req->num_iLL_request_extensions=2;
463 req->iLL_request_extensions=
464 odr_malloc(odr, req->num_iLL_request_extensions*
465 sizeof(*req->iLL_request_extensions));
466 req->iLL_request_extensions[0]=makepromptextension(args,odr);
467 req->iLL_request_extensions[1]=makeoclcextension(args,odr);
470 yaz_log(YLOG_FATAL,"Could not create ill request");
474 } /* createrequest */
477 /* * * * * * * * * * * * * * * */
478 /** \brief Send the request */
479 void sendrequest(ILL_APDU *apdu, ODR odr, COMSTACK stack ) {
483 if (!ill_APDU (odr, &apdu, 0, 0)) {
484 yaz_log(YLOG_FATAL,"ill_Apdu failed");
487 buf_out = odr_getbuf(odr, &len_out, 0);
489 yaz_log(YLOG_DEBUG,"Request PDU Dump");
490 odr_dumpBER(yaz_log_file(), buf_out, len_out);
493 yaz_log(YLOG_FATAL,"Encoding failed. Len=%d", len_out);
494 odr_perror(odr, "encoding failed");
497 yaz_log(YLOG_DEBUG,"About to send the request. Len=%d", len_out);
498 res = cs_put(stack, buf_out, len_out);
500 yaz_log(YLOG_FATAL,"Could not send packet. code %d",res );
504 FILE *F = fopen("req.apdu","w");
505 fwrite ( buf_out, 1, len_out, F);
511 /* * * * * * * * * * * * * * * */
512 /** \brief Get a response */
513 ILL_APDU *getresponse( COMSTACK stack, ODR in_odr ){
518 yaz_log(YLOG_DEBUG,"About to wait for a response");
519 res = cs_get(stack, &buf_in, &len_in);
520 yaz_log(YLOG_DEBUG,"Got a response of %d bytes at %p. res=%d", len_in,buf_in, res);
522 yaz_log(YLOG_FATAL,"Could not receive packet. code %d",res );
523 yaz_log(YLOG_DEBUG,"%02x %02x %02x %02x %02x %02x %02x %02x ...",
524 buf_in[0], buf_in[1], buf_in[2], buf_in[3],
525 buf_in[4], buf_in[5], buf_in[6], buf_in[7] );
526 yaz_log(YLOG_DEBUG,"PDU Dump:");
527 odr_dumpBER(yaz_log_file(), buf_in, len_in);
530 odr_setbuf(in_odr, buf_in, res, 0);
531 if (!ill_APDU (in_odr, &resp, 0, 0))
534 int err = odr_geterrorx(in_odr, &x);
536 const char *element = odr_getelement(in_odr);
537 sprintf(msg, "ODR code %d:%d element=%-20s",
538 err, x, element ? element : "<unknown>");
539 yaz_log(YLOG_FATAL,"Error decoding incoming packet: %s",msg);
540 yaz_log(YLOG_DEBUG,"%02x %02x %02x %02x %02x %02x %02x %02x ...",
541 buf_in[0], buf_in[1], buf_in[2], buf_in[3],
542 buf_in[4], buf_in[5], buf_in[6], buf_in[7] );
543 yaz_log(YLOG_DEBUG,"PDU Dump:");
544 odr_dumpBER(yaz_log_file(), buf_in, len_in);
545 yaz_log(YLOG_FATAL,"Error decoding incoming packet: %s",msg);
552 /** \brief Dump a apdu */
553 void dumpapdu( ILL_APDU *apdu) {
554 ODR print_odr = odr_createmem(ODR_PRINT);
555 ill_APDU (print_odr, &apdu, 0, 0);
556 odr_destroy(print_odr);
559 /** \brief Check apdu type and extract the status_or_error */
560 ILL_Status_Or_Error_Report *getstaterr( ILL_APDU *resp, ODR in_odr ) {
561 if (resp->which != ILL_APDU_Status_Or_Error_Report ) {
562 const char *element = odr_getelement(in_odr);
565 printf("Server returned wrong packet type: %d\n", resp->which);
566 yaz_log(YLOG_FATAL,"Server returned a (%d) and "
567 "not a 'Status_Or_Error_Report' (%d) ",
568 resp->which, ILL_APDU_Status_Or_Error_Report);
571 return resp->u.Status_Or_Error_Report;
574 /** \brief Return a printable string from an ILL_String */
575 char *getillstring( ILL_String *s) {
576 if (s->which == ILL_String_GeneralString )
577 return s->u.GeneralString;
578 else if (s->which == ILL_String_EDIFACTString )
579 return s->u.EDIFACTString;
581 yaz_log(YLOG_FATAL,"Invalid ILL_String ");
586 /** \brief Check if the status was an error packet */
587 /* The presence of an error_report indicates it was an error */
588 /* Then the problem is to find the right message. We dig around */
589 /* until we find the first message, print that, and exit the program */
590 void checkerr( ILL_Status_Or_Error_Report *staterr ) {
591 yaz_log(YLOG_DEBUG, "err= %p ",staterr->error_report );
592 if (staterr->error_report) {
593 ILL_Error_Report *err= staterr->error_report;
594 if ( err->user_error_report) {
595 ILL_User_Error_Report *uerr= err->user_error_report;
596 switch( uerr->which ) {
597 case ILL_User_Error_Report_already_forwarded:
598 printf("Already forwarded: \n");
600 case ILL_User_Error_Report_intermediary_problem:
601 printf("Intermediary problem: %d\n",
602 *uerr->u.intermediary_problem);
604 case ILL_User_Error_Report_security_problem:
605 printf("Security problem: %s\n",
606 getillstring(uerr->u.security_problem));
608 case ILL_User_Error_Report_unable_to_perform:
609 printf("Unable to perform: %d\n",
610 *uerr->u.unable_to_perform);
613 printf("Unknown problem");
617 if ( err->provider_error_report) {
618 ILL_Provider_Error_Report *perr= err->provider_error_report;
619 switch( perr->which ) {
620 case ILL_Provider_Error_Report_general_problem:
621 printf("General Problem: %d:",
622 *perr->u.general_problem);
624 case ILL_Provider_Error_Report_transaction_id_problem:
625 printf("Transaction Id Problem: %d:",
626 *perr->u.general_problem);
628 case ILL_Provider_Error_Report_state_transition_prohibited:
629 printf("State Transition prohibited:");
636 printf("%s", getillstring(staterr->note));
638 printf("Unknown error type");
646 /* * * * * * * * * * * * * * * */
648 /** \brief Main program
652 * Establish connection
660 int main (int argc, char * argv[]) {
661 struct prog_args args;
663 ODR out_odr = odr_createmem(ODR_ENCODE);
664 ODR in_odr = odr_createmem(ODR_DECODE);
667 ILL_Status_Or_Error_Report *staterr;
669 parseargs( argc, argv, &args);
671 stack = connect_to(args.host);
672 apdu = createrequest(&args, out_odr);
675 sendrequest(apdu, out_odr, stack );
676 resp = getresponse(stack, in_odr );
679 staterr=getstaterr(resp, in_odr);
683 printf ("Ok\n"); /* while debugging */
690 * indent-tabs-mode: nil
692 * vim: shiftwidth=4 tabstop=8 expandtab