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