Code updates which makes things compile as C++. Mostly type casts were
[yaz-moved-to-github.git] / util / yaz-illclient.c
1 /*
2  * Copyright (C) 1995-2006, Index Data ApS
3  * See the file LICENSE for details.
4  *
5  * $Id: yaz-illclient.c,v 1.6 2007-05-06 20:12:20 adam Exp $
6  */
7
8 /* WARNING - This is work in progress - not at all ready */
9
10 /** \file yaz-illclient.c
11  *  \brief client for ILL requests (ISO 10161-1)
12  *
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.
16  *
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 
19  *  stdout.
20  *
21  *  Exit codes  (note, the program exits as soon as it finds a good reason)
22  *     0   ok
23  *     1   errors in arguments
24  *     2   Internal errors in creating objects (comstack, odr...)
25  *         mostly programming errors.
26  *     3   could not connect
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)
31  *
32  *
33  *
34  *
35  */
36
37 #include <stdlib.h>
38 #include <stdio.h>
39
40 #if HAVE_UNISTD_H
41 #include <unistd.h>
42 #endif
43 #if HAVE_SYS_STAT_H
44 #include <sys/stat.h>
45 #endif
46 #if HAVE_SYS_TIME_H
47 #include <sys/time.h>
48 #endif
49
50
51 #include <yaz/yaz-util.h>
52 #include <yaz/proto.h>
53 #include <yaz/comstack.h>
54 #include <yaz/tcpip.h>
55 #include <yaz/unix.h>
56 #include <yaz/odr.h>
57 #include <yaz/log.h>
58 #include <yaz/ill.h>
59 #include <yaz/oclc-ill-req-ext.h>
60
61
62 /* A structure for holding name-value pairs in a linked list */
63 struct nameval {
64     char *name;
65     char *val;
66     struct nameval *next;
67 };
68
69 /* A structure for storing all the arguments */
70 struct prog_args {
71     char *host;
72     char *auth_userid;
73     char *auth_passwd;
74     struct nameval* namevals; /* circular list, points to last */
75 } ;
76
77
78
79 /* Call-back to be called for every field in the ill-request */
80 /* It can set values to any field, perhaps from the prog_args */
81 const char *get_ill_element(void *clientData, const char *element) {
82     struct prog_args *args = (struct prog_args *) clientData;
83     struct nameval *nv=args->namevals;
84     char *ret=0;
85     if (!nv)
86         return "";
87     do  {
88         nv=nv->next;
89         /* printf("comparing '%s' with '%s' \n",element, nv->name ); */
90         if ( strcmp(element, nv->name) == 0 )
91             ret = nv->val;
92     } while ( ( !ret) && ( nv != args->namevals) );
93 #if 0
94     if (!strcmp(element,"foo")) {
95         ret=args->host;
96     } else if (!strcmp(element,"ill,protocol-version-num")) {
97         ret="2";
98     } else if (!strcmp(element,"ill,transaction-id,initial-requester-id,person-or-institution-symbol,institution")) {
99         ret=args->auth_userid; 
100     } else if (!strcmp(element,"ill,requester-id,person-or-institution-symbol,institution")) {
101         ret=args->auth_userid;
102     } else if (!strcmp(element,"ill,responder-id,person-or-institution-symbol,institution")) {
103         ret=args->auth_userid;
104     } else if (!strcmp(element,"ill,ill-service-type")) {
105         ret="1"; /* Loan */
106     } 
107 #endif
108  /*
109     } else if (!strcmp(element,"ill,transaction-id,initial-requester-id,person-or-institution-symbol,institution")) {
110         ret="IndexData";
111  */
112     yaz_log(YLOG_DEBUG,"get_ill_element:'%s'->'%s'", element, ret );
113     return ret;
114 }
115
116
117 /* * * * * * * * * * * * * * * * * */
118
119 /** \brief parse a parameter string */
120 /* string is like 'name=value' */
121 struct nameval *parse_nameval( char *arg ) {
122     struct nameval *nv = (struct nameval *) xmalloc(sizeof(*nv));
123     char *p=arg;
124     int len;
125     if (!p || !*p) 
126         return 0; /* yeah, leaks a bit of memory. so what */
127     while ( *p && ( *p != '=' ) ) 
128         p++;
129     len = p - arg;
130     if (!len)
131         return 0;
132     nv->name = (char *) xmalloc(len+1);
133     strncpy(nv->name, arg, len);
134     nv->name[len]='\0';
135     if (*p == '=' )
136         p++; /* skip the '=' */
137     else
138         return 0; /* oops, no '=' */
139     if (!*p)
140         return 0; /* no value */
141     nv->val=xstrdup(p);
142     nv->next=0;
143     yaz_log(YLOG_DEBUG,"parse_nameval: n='%s' v='%s'", nv->name, nv->val );
144     return nv;
145 }
146
147 /** \brief append nv to the list of namevals */
148 void append_nameval (struct prog_args *args, struct nameval *nv) {
149     if (!nv)
150         return;
151     if (args->namevals) {
152         nv->next=args->namevals->next; /* first */
153         args->namevals->next=nv; 
154         args->namevals=nv;
155     } else {
156         nv->next=nv;
157         args->namevals=nv;
158     }
159 } /* append_nameval */
160
161 /** \brief parse a parameter file */
162 void parse_paramfile(char *arg, struct prog_args *args) {
163     FILE *f=fopen(arg,"r");
164 #define BUFSIZE 4096    
165     char buf[BUFSIZE];
166     int len;
167     struct nameval *nv;
168     if (!f) {
169         yaz_log(YLOG_FATAL,"Could not open param file '%s' ", arg);
170         printf("Could not open file '%s' \n",arg);
171         exit(1);
172     }
173     yaz_log(YLOG_DEBUG,"Opened input file '%s' ",arg );
174     while (fgets(buf,BUFSIZE,f)) {
175         if (buf[0] != '#' ) {
176             len=strlen(buf)-1;
177             if (buf[len] == '\n')
178                 buf[len] = '\0' ;
179             nv=parse_nameval(buf);
180             append_nameval(args, nv);
181         } /* not a comment */
182     }
183     (void) fclose(f);
184
185     if (0) {
186         nv=args->namevals;
187         printf("nv chain: ================ \n");
188         printf("(last:) %p: '%s' = '%s' (%p) \n",nv, nv->name, nv->val, nv->next );
189         do {
190             nv=nv->next;
191             printf("%p: '%s' = '%s' (%p)\n",nv, nv->name, nv->val, nv->next );
192         } while (nv != args->namevals );
193         exit(1);
194     }
195
196 } /* parse_paramfile */
197
198
199 /** \brief  Parse program arguments */
200 void parseargs( int argc, char * argv[],  struct prog_args *args) {
201     int ret;
202     char *arg;
203     char *prog=*argv;
204     char *version="$Id: yaz-illclient.c,v 1.6 2007-05-06 20:12:20 adam Exp $"; /* from cvs */
205     struct nameval *nv;
206
207     /* default values */
208     args->host = 0; /* not known (yet) */
209     args->namevals=0; /* none set up */
210 #if 0    
211     /* Example 1, from TSLAC */
212     args->auth_userid = "100-228-301" ; /* FIXME - get from cmd line */
213     args->auth_passwd = "dxg5magxc" ;   /* FIXME - get from cmd line */
214     /* Example 2, from TSLAC */
215     args->auth_userid = "100070049" ; /* FIXME - get from cmd line */
216     args->auth_passwd = "cowgirl" ;   /* FIXME - get from cmd line */
217 #else
218     /* Example 3 - directly from OCLC, supposed to work on their test server*/
219     args->auth_userid = "100-310-658" ; /* FIXME - get from cmd line */
220     args->auth_passwd = "apii2test" ;   /* FIXME - get from cmd line */
221 #endif
222
223     while ((ret = options("v:D:f:V", argv, argc, &arg)) != -2)
224     {
225         switch (ret)
226         {
227         case 0:
228             if (!args->host)
229             {
230                 args->host = xstrdup (arg);
231             }
232             else
233             {
234                 fprintf(stderr, "%s: Specify at most one server address\n",
235                         prog);
236                 exit(1);
237             }
238             break;
239         case 'v':
240             yaz_log_init(yaz_log_mask_str(arg), "", 0);
241             break;
242         case 'V':
243             printf("%s %s",prog, version );
244             break;
245         case 'D':
246             nv=parse_nameval(arg);
247             append_nameval(args,nv);
248             break;
249         case 'f':
250             parse_paramfile(arg,args);
251             break;
252         default:
253             fprintf (stderr, "Usage: %s "
254                      " [-v loglevel...]"
255                      " [-D name=value ]"
256                      " [-V]"
257                      " <server-addr>\n",
258                      prog);
259             exit (1);
260         }
261     }
262 } /* parseargs */
263
264 /* * * * * * * * * * * */
265 /** \brief  Validate the arguments make sense */
266 void validateargs( struct prog_args *args) {
267     if (!args->host) {
268         fprintf(stderr, "Specify a connection address, "
269                         "as in 'bagel.indexdata.dk:210' \n");
270         exit(1);
271     }
272 } /* validateargs */
273
274
275 /* * * * * * * * * * * * * * * */
276 /** \brief  Connect to the target */
277 COMSTACK connect_to( char *hostaddr ){
278     COMSTACK stack;
279     void *server_address_ip;
280     int status;
281
282     yaz_log(YLOG_DEBUG,"Connecting to '%s'", hostaddr);
283     stack = cs_create_host(hostaddr, 1, &server_address_ip );
284     if (!stack) {
285         yaz_log(YLOG_FATAL,"Error in creating the comstack '%s' ",
286                  hostaddr );
287         exit(2);
288     }
289     
290     yaz_log(YLOG_DEBUG,"Created stack ok ");
291
292     status = cs_connect(stack, server_address_ip);
293     if (status != 0) {
294         yaz_log(YLOG_FATAL|YLOG_ERRNO,"Can not connect '%s' ",
295                  hostaddr );
296         exit(3);
297     }
298     yaz_log(YLOG_DEBUG,"Connected OK to '%s'", hostaddr);
299     return stack;
300 }
301
302
303 /* * * * * * * * * * * * * * * */
304 /* Makes a Z39.50-like prompt package with username and password */
305 Z_PromptObject1 *makeprompt(struct prog_args *args, ODR odr) {
306     Z_PromptObject1 *p = (Z_PromptObject1 *) odr_malloc(odr, sizeof(*p) );
307     Z_ResponseUnit1 *ru = (Z_ResponseUnit1 *) odr_malloc(odr, sizeof(*ru) );
308     
309     p->which=Z_PromptObject1_response;
310     p->u.response = (Z_Response1*) odr_malloc(odr, sizeof(*(p->u.response)) );
311     p->u.response->num=2;
312     p->u.response->elements= (Z_ResponseUnit1 **) odr_malloc(odr, 
313              p->u.response->num*sizeof(*(p->u.response->elements)) );
314     /* user id, aka "oclc authorization number" */
315     p->u.response->elements[0] = ru;
316     ru->promptId = (Z_PromptId *) odr_malloc(odr, sizeof(*(ru->promptId) ));
317     ru->promptId->which = Z_PromptId_enumeratedPrompt;
318     ru->promptId->u.enumeratedPrompt = (Z_PromptIdEnumeratedPrompt *)
319         odr_malloc(odr, sizeof(*(ru->promptId->u.enumeratedPrompt) ));
320     ru->promptId->u.enumeratedPrompt->type = 
321          odr_intdup(odr,Z_PromptIdEnumeratedPrompt_userId);
322     ru->promptId->u.enumeratedPrompt->suggestedString = 0 ;
323     ru->which = Z_ResponseUnit1_string ;
324     ru->u.string = odr_strdup(odr, args->auth_userid);
325     /* password */
326     ru = (Z_ResponseUnit1 *) odr_malloc(odr, sizeof(*ru) );
327     p->u.response->elements[1] = ru;
328     ru->promptId = (Z_PromptId *) odr_malloc(odr, sizeof(*(ru->promptId) ));
329     ru->promptId->which = Z_PromptId_enumeratedPrompt;
330     ru->promptId->u.enumeratedPrompt =  (Z_PromptIdEnumeratedPrompt *)
331         odr_malloc(odr, sizeof(*(ru->promptId->u.enumeratedPrompt) ));
332     ru->promptId->u.enumeratedPrompt->type = 
333          odr_intdup(odr,Z_PromptIdEnumeratedPrompt_password);
334     ru->promptId->u.enumeratedPrompt->suggestedString = 0 ;
335     ru->which = Z_ResponseUnit1_string ;
336     ru->u.string = odr_strdup(odr, args->auth_passwd);
337     return p;
338 } /* makeprompt */
339
340 ILL_Extension *makepromptextension(struct prog_args *args, ODR odr) {
341     ODR odr_ext = odr_createmem(ODR_ENCODE);
342     ODR odr_prt = odr_createmem(ODR_PRINT);
343     ILL_Extension *e = (ILL_Extension *) odr_malloc(odr, sizeof(*e));
344     Z_PromptObject1 *p = makeprompt(args,odr_ext);
345     char * buf;
346     int siz;
347     Z_External *ext = (Z_External *) odr_malloc(odr, sizeof(*ext));
348     ext->direct_reference = odr_getoidbystr(odr,"1.2.840.10003.8.1");
349     ext->indirect_reference=0;
350     ext->descriptor=0;
351     ext->which=Z_External_single;
352     if ( ! z_PromptObject1(odr_ext, &p, 0,0 ) ) {
353         yaz_log(YLOG_FATAL,"Encoding of z_PromptObject1 failed ");
354         exit (6);
355     }
356     
357     printf ("Prompt: \n"); /*!*/
358     z_PromptObject1(odr_prt, &p, 0,0 ); /*!*/
359
360     buf= odr_getbuf(odr_ext,&siz,0);
361     ext->u.single_ASN1_type=(Odr_any *) 
362         odr_malloc(odr,sizeof(*ext->u.single_ASN1_type));
363     ext->u.single_ASN1_type->buf= (unsigned char *) odr_malloc(odr, siz);
364     memcpy(ext->u.single_ASN1_type->buf,buf, siz );
365     ext->u.single_ASN1_type->len = ext->u.single_ASN1_type->size = siz;
366     odr_reset(odr_ext);
367     odr_reset(odr_prt); /*!*/
368
369     e->identifier = odr_intdup(odr,1);
370     e->critical = odr_intdup(odr,0);
371     e->item = (Odr_any *) odr_malloc(odr,sizeof(*e->item));
372     if ( ! z_External(odr_ext, &ext,0,0) ) {
373         yaz_log(YLOG_FATAL,"Encoding of z_External failed ");
374         exit (6);
375     }
376     printf("External: \n");
377     z_External(odr_prt, &ext,0,0);  /*!*/
378     buf= odr_getbuf(odr_ext,&siz,0); 
379     e->item->buf= (unsigned char *) odr_malloc(odr, siz);
380     memcpy(e->item->buf,buf, siz );
381     e->item->len = e->item->size = siz;
382
383     odr_destroy(odr_prt);
384     odr_destroy(odr_ext);
385     return e;
386 } /* makepromptextension */
387
388 ILL_Extension *makeoclcextension(struct prog_args *args, ODR odr) {
389     /* The oclc extension is required, but only contains optional */
390     /* fields. Here we just null them all out */
391     ODR odr_ext = odr_createmem(ODR_ENCODE);
392     ODR odr_prt = odr_createmem(ODR_PRINT);
393     ILL_Extension *e = (ILL_Extension *) odr_malloc(odr, sizeof(*e));
394     ILL_OCLCILLRequestExtension *oc = (ILL_OCLCILLRequestExtension *)
395         odr_malloc(odr_ext, sizeof(*oc));
396     char * buf;
397     int siz;
398     Z_External *ext = (Z_External *) odr_malloc(odr, sizeof(*ext));
399     oc->clientDepartment = 0;
400     oc->paymentMethod = 0;
401     oc->uniformTitle = 0;
402     oc->dissertation = 0;
403     oc->issueNumber = 0;
404     oc->volume = 0;
405     oc->affiliations = 0;
406     oc->source = 0;
407     ext->direct_reference = odr_getoidbystr(odr,"1.0.10161.13.2");
408     ext->indirect_reference=0;
409     ext->descriptor=0;
410     ext->which=Z_External_single;
411     if ( ! ill_OCLCILLRequestExtension(odr_ext, &oc, 0,0 ) ) {
412         yaz_log(YLOG_FATAL,"Encoding of ill_OCLCILLRequestExtension failed ");
413         exit (6);
414     }
415     
416     printf ("OCLC: \n"); /*!*/
417     ill_OCLCILLRequestExtension(odr_prt, &oc, 0,0 ); /*!*/
418
419     buf= odr_getbuf(odr_ext,&siz,0);
420     ext->u.single_ASN1_type = (Odr_any*)
421         odr_malloc(odr,sizeof(*ext->u.single_ASN1_type));
422     ext->u.single_ASN1_type->buf = (unsigned char *) odr_malloc(odr, siz);
423     memcpy(ext->u.single_ASN1_type->buf,buf, siz );
424     ext->u.single_ASN1_type->len = ext->u.single_ASN1_type->size = siz;
425     odr_reset(odr_ext);
426     odr_reset(odr_prt); /*!*/
427
428     e->identifier = odr_intdup(odr,1);
429     e->critical = odr_intdup(odr,0);
430     e->item = (Odr_any *) odr_malloc(odr,sizeof(*e->item));
431     if ( ! z_External(odr_ext, &ext,0,0) ) {
432         yaz_log(YLOG_FATAL,"Encoding of z_External failed ");
433         exit (6);
434     }
435     printf("External: \n");
436     z_External(odr_prt, &ext,0,0);  /*!*/
437     buf= odr_getbuf(odr_ext,&siz,0); 
438     e->item->buf= (unsigned char *) odr_malloc(odr, siz);
439     memcpy(e->item->buf, buf, siz);
440     e->item->len = e->item->size = siz;
441
442     odr_destroy(odr_prt);
443     odr_destroy(odr_ext);
444     return e;
445
446 } /* makeoclcextension */
447
448 ILL_APDU *createrequest( struct prog_args *args, ODR odr) {
449     struct ill_get_ctl ctl;
450     ILL_APDU *apdu;
451     ILL_Request *req;
452
453     ctl.odr = odr;
454     ctl.clientData = args;
455     ctl.f = get_ill_element;
456     apdu = (ILL_APDU *) odr_malloc( odr, sizeof(*apdu) );
457     apdu->which=ILL_APDU_ILL_Request;
458     req = ill_get_ILLRequest(&ctl, "ill", 0);
459     apdu->u.illRequest=req;
460     req->num_iLL_request_extensions=2;
461     req->iLL_request_extensions = (ILL_Extension **)
462         odr_malloc(odr, req->num_iLL_request_extensions*sizeof(*req->iLL_request_extensions));
463     req->iLL_request_extensions[0]=makepromptextension(args,odr);
464     req->iLL_request_extensions[1]=makeoclcextension(args,odr);
465     if (!req) {
466         yaz_log(YLOG_FATAL,"Could not create ill request");
467         exit(2);
468     }
469     return apdu;
470 } /* createrequest */
471
472
473 /* * * * * * * * * * * * * * * */
474 /** \brief Send the request */
475 void sendrequest(ILL_APDU *apdu, ODR odr, COMSTACK stack ) {
476     char *buf_out;
477     int len_out;
478     int res;
479     if (!ill_APDU  (odr, &apdu, 0, 0)) { 
480         yaz_log(YLOG_FATAL,"ill_Apdu failed");
481         exit(2);
482     }
483     buf_out = odr_getbuf(odr, &len_out, 0);
484     if (0) {
485         yaz_log(YLOG_DEBUG,"Request PDU Dump");
486         odr_dumpBER(yaz_log_file(), buf_out, len_out);
487     }
488     if (!buf_out) {
489         yaz_log(YLOG_FATAL,"Encoding failed. Len=%d", len_out);
490         odr_perror(odr, "encoding failed");
491         exit(2);
492     }
493     yaz_log(YLOG_DEBUG,"About to send the request. Len=%d", len_out);
494     res = cs_put(stack, buf_out, len_out);
495     if ( res<0 ) {
496         yaz_log(YLOG_FATAL,"Could not send packet. code %d",res );
497         exit (4);
498     }
499     if (1) {
500         FILE *F = fopen("req.apdu","w");
501         fwrite ( buf_out, 1, len_out, F);
502         fclose(F);
503     }
504     
505 } /* sendrequest */
506
507 /* * * * * * * * * * * * * * * */
508 /** \brief  Get a response */
509 ILL_APDU *getresponse( COMSTACK stack, ODR in_odr ){
510     ILL_APDU *resp;
511     int res;
512     char *buf_in=0;
513     int len_in=0;
514     yaz_log(YLOG_DEBUG,"About to wait for a response");
515     res = cs_get(stack, &buf_in, &len_in);
516     yaz_log(YLOG_DEBUG,"Got a response of %d bytes at %p. res=%d", len_in,buf_in, res);
517     if (res<0) {
518         yaz_log(YLOG_FATAL,"Could not receive packet. code %d",res );
519         yaz_log(YLOG_DEBUG,"%02x %02x %02x %02x %02x %02x %02x %02x ...", 
520                 buf_in[0], buf_in[1], buf_in[2], buf_in[3],
521                 buf_in[4], buf_in[5], buf_in[6], buf_in[7]  );
522         yaz_log(YLOG_DEBUG,"PDU Dump:");
523         odr_dumpBER(yaz_log_file(), buf_in, len_in);
524         exit (5);
525     }
526     odr_setbuf(in_odr, buf_in, res, 0);
527     if (!ill_APDU (in_odr, &resp, 0, 0))
528     {
529         int x;
530         int err = odr_geterrorx(in_odr, &x);
531         char msg[60];
532         const char *element = odr_getelement(in_odr);
533         sprintf(msg, "ODR code %d:%d element=%-20s",
534                 err, x, element ? element : "<unknown>");
535         yaz_log(YLOG_FATAL,"Error decoding incoming packet: %s",msg);
536         yaz_log(YLOG_DEBUG,"%02x %02x %02x %02x %02x %02x %02x %02x ...", 
537                 buf_in[0], buf_in[1], buf_in[2], buf_in[3],
538                 buf_in[4], buf_in[5], buf_in[6], buf_in[7]  );
539         yaz_log(YLOG_DEBUG,"PDU Dump:");
540         odr_dumpBER(yaz_log_file(), buf_in, len_in);
541         yaz_log(YLOG_FATAL,"Error decoding incoming packet: %s",msg);
542         exit(6);
543     }
544     return resp;
545 } /* getresponse */
546
547
548 /** \brief Dump a apdu */
549 void dumpapdu( ILL_APDU *apdu) {
550     ODR print_odr = odr_createmem(ODR_PRINT);
551     ill_APDU (print_odr, &apdu, 0, 0);
552     odr_destroy(print_odr);
553 } /* dumpapdu */
554
555 /** \brief  Check apdu type and extract the status_or_error */
556 ILL_Status_Or_Error_Report *getstaterr( ILL_APDU *resp, ODR in_odr ) {
557     if (resp->which != ILL_APDU_Status_Or_Error_Report ) {
558         const char *element = odr_getelement(in_odr);
559         if (!element) 
560             element="unknown";
561         printf("Server returned wrong packet type: %d\n", resp->which);
562         yaz_log(YLOG_FATAL,"Server returned a (%d) and "
563                  "not a 'Status_Or_Error_Report' (%d) ",
564                  resp->which, ILL_APDU_Status_Or_Error_Report);
565         exit(6);
566     }
567     return resp->u.Status_Or_Error_Report;
568 } /* getstaterr */
569
570 /** \brief  Return a printable string from an ILL_String */
571 char *getillstring( ILL_String *s) {
572     if (s->which == ILL_String_GeneralString ) 
573         return s->u.GeneralString;
574     else if (s->which == ILL_String_EDIFACTString ) 
575         return s->u.EDIFACTString;
576     else {
577         yaz_log(YLOG_FATAL,"Invalid ILL_String ");
578         exit (6);
579     }
580 } /* getillstring */
581
582 /** \brief Check if the status was an error packet */
583 /* The presence of an error_report indicates it was an error */
584 /* Then the problem is to find the right message. We dig around */
585 /* until we find the first message, print that, and exit the program */
586 void checkerr( ILL_Status_Or_Error_Report *staterr ) {
587     yaz_log(YLOG_DEBUG, "err= %p ",staterr->error_report );
588     if (staterr->error_report) {
589         ILL_Error_Report *err= staterr->error_report;
590         if ( err->user_error_report) {
591             ILL_User_Error_Report *uerr= err->user_error_report;
592             switch( uerr->which ) {
593                 case ILL_User_Error_Report_already_forwarded:
594                     printf("Already forwarded: \n");
595                     break;
596                 case ILL_User_Error_Report_intermediary_problem:
597                     printf("Intermediary problem: %d\n", 
598                         *uerr->u.intermediary_problem);
599                     break;
600                 case ILL_User_Error_Report_security_problem:
601                     printf("Security problem: %s\n", 
602                         getillstring(uerr->u.security_problem));
603                     break;
604                 case ILL_User_Error_Report_unable_to_perform:
605                     printf("Unable to perform: %d\n", 
606                           *uerr->u.unable_to_perform);
607                     break;
608                 default:
609                     printf("Unknown problem");
610             }
611             exit(7);
612         }
613         if ( err->provider_error_report) {
614             ILL_Provider_Error_Report *perr= err->provider_error_report;
615             switch( perr->which ) {
616                 case ILL_Provider_Error_Report_general_problem:
617                     printf("General Problem: %d\n", 
618                           *perr->u.general_problem);
619                     break;
620                 case ILL_Provider_Error_Report_transaction_id_problem:
621                     printf("Transaction Id Problem: %d\n", 
622                           *perr->u.general_problem);
623                     break;
624                 case ILL_Provider_Error_Report_state_transition_prohibited:
625                     printf("State Transition prohibited \n");
626                     break;
627             }
628             exit(7);
629         } 
630         /* fallbacks */
631         if ( staterr->note ) 
632             printf("%s", getillstring(staterr->note));
633         else 
634             printf("Unknown error type");
635         exit(7);
636     }
637 } /* checkerr */
638
639
640
641 /* * * * * * * * * * * * * * * */
642
643 /** \brief Main program 
644  *
645  * Parse arguments
646  * Validate arguments
647  * Establish connection
648  * Build a request
649  * Send a request
650  * Get a reply
651  * Parse reply
652  * Produce output
653  */
654
655 int main (int argc, char * argv[]) {
656     struct prog_args args;
657     COMSTACK stack;
658     ODR out_odr = odr_createmem(ODR_ENCODE);
659     ODR in_odr = odr_createmem(ODR_DECODE);
660     ILL_APDU *apdu;
661     ILL_APDU *resp;
662     ILL_Status_Or_Error_Report *staterr;
663
664     parseargs( argc, argv,  &args);
665     validateargs(&args);
666     stack = connect_to(args.host);
667     apdu = createrequest(&args, out_odr);
668     if (1) 
669         dumpapdu(apdu);
670     sendrequest(apdu, out_odr, stack ); 
671     resp = getresponse(stack, in_odr );
672     if (1) 
673         dumpapdu(resp);
674     staterr=getstaterr(resp, in_odr);
675     checkerr(staterr);
676
677
678     printf ("Ok\n"); /* while debugging */
679     exit (0);
680 }
681
682 /*
683  * Local variables:
684  * c-basic-offset: 4
685  * indent-tabs-mode: nil
686  * End:
687  * vim: shiftwidth=4 tabstop=8 expandtab
688  */
689