1 #define _XOPEN_SOURCE 500
4 /*#include <locale.h>*/
16 /*char username[] = "jrfh7i";*/
17 /*char password[] = "Ab123456";*/
18 /*char password[] = "Ac123456";*/
20 char username
[] = "5s59sd";
21 /*char password[] = "Ab123456";*/
22 char password
[] = "Ac123456";
25 void print_DbState(const long int state
) {
27 case DBSTATE_ACCESSIBLE
: printf("ACCESSIBLE\n"); break;
28 case DBSTATE_TEMP_UNACCESSIBLE
: printf("TEMP_UNACCESSIBLE\n"); break;
29 case DBSTATE_NOT_YET_ACCESSIBLE
: printf("NOT_YET_ACCESSIBLE\n"); break;
30 case DBSTATE_PERM_UNACCESSIBLE
: printf("PERM_UNACCESSIBLE\n"); break;
31 case DBSTATE_REMOVED
: printf("REMOVED\n"); break;
32 default: printf("<unknown state %ld>\n", state
);
36 void print_DbType(const long int *type
) {
37 if (!type
) printf("NULL\n");
40 case DBTYPE_FO
: printf("FO\n"); break;
41 case DBTYPE_PFO
: printf("PFO\n"); break;
42 case DBTYPE_PFO_ADVOK
: printf("PFO_ADVOK\n"); break;
43 case DBTYPE_PFO_DANPOR
: printf("PFO_DAPOR\n"); break;
44 case DBTYPE_PFO_INSSPR
: printf("PFO_INSSPR\n"); break;
45 case DBTYPE_PO
: printf("PO\n"); break;
46 case DBTYPE_PO_ZAK
: printf("PO_ZAK\n"); break;
47 case DBTYPE_PO_REQ
: printf("PO_REQ\n"); break;
48 case DBTYPE_OVM
: printf("OVM\n"); break;
49 case DBTYPE_OVM_NOTAR
: printf("OVM_NOTAR\n"); break;
50 case DBTYPE_OVM_EXEKUT
: printf("OVM_EXEKUT\n"); break;
51 case DBTYPE_OVM_REQ
: printf("OVM_REQ\n"); break;
52 default: printf("<unknown type %ld>\n", *type
);
57 void print_UserType(const long int *type
) {
58 if (!type
) printf("NULL\n");
61 case USERTYPE_PRIMARY
: printf("PRIMARY\n"); break;
62 case USERTYPE_ENTRUSTED
: printf("ENTRUSTED\n"); break;
63 case USERTYPE_ADMINISTRATOR
: printf("ADMINISTRATOR\n"); break;
64 case USERTYPE_OFFICIAL
: printf("OFFICIAL\n"); break;
65 default: printf("<unknown type %ld>\n", *type
);
70 void print_UserPrivils(const long int *privils
) {
72 const char *priviledges
[] = {
82 const int priviledges_count
= sizeof(priviledges
)/sizeof(priviledges
[0]);
84 if (!privils
) printf("NULL\n");
86 printf("%ld (", *privils
);
88 for (int i
= 0; i
< priviledges_count
; i
++) {
89 if (*privils
& (1<<i
)) {
91 ((i
+ 1) == priviledges_count
) ? "%s" : "%s|",
101 void print_hash(const struct isds_hash
*hash
) {
107 switch(hash
->algorithm
) {
108 case HASH_ALGORITHM_MD5
: printf("MD5 "); break;
109 case HASH_ALGORITHM_SHA_1
: printf("SHA-1 "); break;
110 case HASH_ALGORITHM_SHA_256
: printf("SHA-256 "); break;
111 case HASH_ALGORITHM_SHA_512
: printf("SHA-512 "); break;
112 default: printf("<Unknown hash algorithm %d> ", hash
->algorithm
);
116 if (!hash
->value
) printf("<NULL>");
118 for (int i
= 0; i
< hash
->length
; i
++) {
119 if (i
> 0) printf(":");
120 printf("%02x", ((uint8_t *)(hash
->value
))[i
]);
127 void print_raw_type(const isds_raw_type type
) {
129 case RAWTYPE_INCOMING_MESSAGE
:
130 printf("INCOMING_MESSAGE\n"); break;
131 case RAWTYPE_PLAIN_SIGNED_INCOMING_MESSAGE
:
132 printf("PLAIN_SIGNED_INCOMING_MESSAGE\n"); break;
133 case RAWTYPE_CMS_SIGNED_INCOMING_MESSAGE
:
134 printf("CMS_SIGNED_INCOMING_MESSAGE\n"); break;
135 case RAWTYPE_PLAIN_SIGNED_OUTGOING_MESSAGE
:
136 printf("PLAIN_SIGNED_OUTGOING_MESSAGE\n"); break;
137 case RAWTYPE_CMS_SIGNED_OUTGOING_MESSAGE
:
138 printf("CMS_SIGNED_OUTGOING_MESSAGE\n"); break;
139 case RAWTYPE_DELIVERYINFO
:
140 printf("DELIVERYINFO\n"); break;
141 case RAWTYPE_PLAIN_SIGNED_DELIVERYINFO
:
142 printf("PLAIN_SIGNED_DELIVERYINFO\n"); break;
143 case RAWTYPE_CMS_SIGNED_DELIVERYINFO
:
144 printf("CMS_SIGNED_DELIVERYINFO\n"); break;
146 printf("<Unknown raw type %d> ", type
);
152 void print_bool(const _Bool
*boolean
) {
153 printf("%s\n", (!boolean
) ? "NULL" : ((*boolean
)? "true" : "false") );
157 void print_longint(const long int *number
) {
158 if (!number
) printf("NULL\n");
159 else printf("%ld\n", *number
);
163 void print_PersonName(const struct isds_PersonName
*personName
) {
164 printf("\tpersonName = ");
165 if (!personName
) printf("NULL\n");
168 printf("\t\tpnFirstName = %s\n", personName
->pnFirstName
);
169 printf("\t\tpnMiddleName = %s\n", personName
->pnMiddleName
);
170 printf("\t\tpnLastName = %s\n", personName
->pnLastName
);
171 printf("\t\tpnLastNameAtBirth = %s\n", personName
->pnLastNameAtBirth
);
177 void print_Address(const struct isds_Address
*address
) {
178 printf("\taddress = ");
179 if (!address
) printf("NULL\n");
182 printf("\t\tadCity = %s\n", address
->adCity
);
183 printf("\t\tadStreet = %s\n", address
->adStreet
);
184 printf("\t\tadNumberInStreet = %s\n", address
->adNumberInStreet
);
185 printf("\t\tadNumberInMunicipality = %s\n",
186 address
->adNumberInMunicipality
);
187 printf("\t\tadZipCode = %s\n", address
->adZipCode
);
188 printf("\t\tadState = %s\n", address
->adState
);
194 void print_date(const struct tm
*date
) {
195 if (!date
) printf("NULL\n");
196 else printf("%s", asctime(date
));
200 void print_DbOwnerInfo(const struct isds_DbOwnerInfo
*info
) {
201 printf("dbOwnerInfo = ");
209 printf("\tdbID = %s\n", info
->dbID
);
211 printf("\tdbType = ");
212 print_DbType((long int *) (info
->dbType
));
213 printf("\tic = %s\n", info
->ic
);
215 print_PersonName(info
->personName
);
217 printf("\tfirmName = %s\n", info
->firmName
);
219 printf("\tbirthInfo = ");
220 if (!info
->birthInfo
) printf("NULL\n");
224 printf("\t\tbiDate = ");
225 print_date(info
->birthInfo
->biDate
);
227 printf("\t\tbiCity = %s\n", info
->birthInfo
->biCity
);
228 printf("\t\tbiCounty = %s\n", info
->birthInfo
->biCounty
);
229 printf("\t\tbiState = %s\n", info
->birthInfo
->biState
);
233 print_Address(info
->address
);
235 printf("\tnationality = %s\n", info
->nationality
);
236 printf("\temail = %s\n", info
->email
);
237 printf("\ttelNumber = %s\n", info
->telNumber
);
238 printf("\tidentifier = %s\n", info
->identifier
);
239 printf("\tregistryCode = %s\n", info
->registryCode
);
241 printf("\tdbState = ");
242 if (!info
->dbState
) printf("NULL\n");
243 else print_DbState(*(info
->dbState
));
245 printf("\tdbEffectiveOVM = ");
246 print_bool(info
->dbEffectiveOVM
);
248 printf("\tdbOpenAddressing = ");
249 print_bool(info
->dbOpenAddressing
);
256 void print_DbUserInfo(const struct isds_DbUserInfo
*info
) {
257 printf("dbUserInfo = ");
265 printf("\tuserID = %s\n", info
->userID
);
267 printf("\tuserType = ");
268 print_UserType((long int *) (info
->userType
));
270 printf("\tuserPrivils = ");
271 print_UserPrivils(info
->userPrivils
);
273 print_PersonName(info
->personName
);
274 print_Address(info
->address
);
276 printf("\tbiDate = ");
277 print_date(info
->biDate
);
279 printf("\tic = %s\n", info
->ic
);
280 printf("\tfirmName = %s\n", info
->firmName
);
282 printf("\tcaStreet = %s\n", info
->caStreet
);
283 printf("\tcaCity = %s\n", info
->caCity
);
284 printf("\tcaZipCode = %s\n", info
->caZipCode
);
290 void print_timeval(const struct timeval
*time
) {
299 if (!localtime_r(&(time
->tv_sec
), &broken
)) goto error
;
300 if (!strftime(buffer
, sizeof(buffer
)/sizeof(char), "%c", &broken
))
302 printf("%s, %ld us\n", buffer
, time
->tv_usec
);
306 printf("<Error while formating>\n>");
311 void print_event_type(const isds_event_type
*type
) {
317 case EVENT_UKNOWN
: printf("UNKNOWN\n"); break;
318 case EVENT_ACCEPTED_BY_RECIPIENT
:
319 printf("ACCEPTED_BY_RECIPIENT\n"); break;
320 case EVENT_ACCEPTED_BY_FICTION
:
321 printf("DELIVERED_BY_FICTION\n"); break;
322 case EVENT_UNDELIVERABLE
:
323 printf("UNDELIVERABLE\n"); break;
324 default: printf("<unknown type %d>\n", *type
);
329 void print_events(const struct isds_list
*events
) {
330 const struct isds_list
*item
;
331 const struct isds_event
*event
;
340 for (item
= events
; item
; item
= item
->next
) {
341 event
= (struct isds_event
*) item
->data
;
342 printf("\t\t\tevent = ");
343 if (!event
) printf("NULL");
347 printf("\t\t\t\ttype = ");
348 print_event_type(event
->type
);
350 printf("\t\t\t\tdescription = %s\n", event
->description
);
352 printf("\t\t\t\ttime = ");
353 print_timeval(event
->time
);
363 void print_envelope(const struct isds_envelope
*envelope
) {
364 printf("\tenvelope = ");
372 printf("\t\tdmID = %s\n", envelope
->dmID
);
373 printf("\t\tdbIDSender = %s\n", envelope
->dbIDSender
);
374 printf("\t\tdmSender = %s\n", envelope
->dmSender
);
375 printf("\t\tdmSenderAddress = %s\n", envelope
->dmSenderAddress
);
376 printf("\t\tdmSenderType = ");
377 print_DbType(envelope
->dmSenderType
);
378 printf("\t\tdmRecipient = %s\n", envelope
->dmRecipient
);
379 printf("\t\tdmRecipientAddress = %s\n", envelope
->dmRecipientAddress
);
380 printf("\t\tdmAmbiguousRecipient = ");
381 print_bool(envelope
->dmAmbiguousRecipient
);
382 printf("\t\tdmType = %s\n", envelope
->dmType
);
384 printf("\t\tdmSenderOrgUnit = %s\n", envelope
->dmSenderOrgUnit
);
385 printf("\t\tdmSenderOrgUnitNum = ");
386 print_longint(envelope
->dmSenderOrgUnitNum
);
387 printf("\t\tdbIDRecipient = %s\n", envelope
->dbIDRecipient
);
388 printf("\t\tdmRecipientOrgUnit = %s\n", envelope
->dmRecipientOrgUnit
);
389 printf("\t\tdmRecipientOrgUnitNum = ");
390 print_longint(envelope
->dmRecipientOrgUnitNum
);
391 printf("\t\tdmToHands = %s\n", envelope
->dmToHands
);
392 printf("\t\tdmAnnotation = %s\n", envelope
->dmAnnotation
);
393 printf("\t\tdmRecipientRefNumber = %s\n", envelope
->dmRecipientRefNumber
);
394 printf("\t\tdmSenderRefNumber = %s\n", envelope
->dmSenderRefNumber
);
395 printf("\t\tdmRecipientIdent = %s\n", envelope
->dmRecipientIdent
);
396 printf("\t\tdmSenderIdent = %s\n", envelope
->dmSenderIdent
);
398 printf("\t\tdmLegalTitleLaw = ");
399 print_longint(envelope
->dmLegalTitleLaw
);
400 printf("\t\tdmLegalTitleYear = ");
401 print_longint(envelope
->dmLegalTitleYear
);
402 printf("\t\tdmLegalTitleSect = %s\n", envelope
->dmLegalTitleSect
);
403 printf("\t\tdmLegalTitlePar = %s\n", envelope
->dmLegalTitlePar
);
404 printf("\t\tdmLegalTitlePoint = %s\n", envelope
->dmLegalTitlePoint
);
406 printf("\t\tdmPersonalDelivery = ");
407 print_bool(envelope
->dmPersonalDelivery
);
408 printf("\t\tdmAllowSubstDelivery = ");
409 print_bool(envelope
->dmAllowSubstDelivery
);
410 printf("\t\tdmOVM = ");
411 print_bool(envelope
->dmOVM
);
413 printf("\t\tdmOrdinal = ");
414 if (!envelope
->dmOrdinal
) printf("NULL\n");
415 else printf("%lu\n", *(envelope
->dmOrdinal
));
417 printf("\t\tdmMessageStatus = ");
418 if (!envelope
->dmMessageStatus
) printf("NULL\n");
420 switch(*(envelope
->dmMessageStatus
)) {
421 case MESSAGESTATE_SENT
: printf("SENT\n"); break;
422 case MESSAGESTATE_STAMPED
: printf("STAMPED\n"); break;
423 case MESSAGESTATE_INFECTED
: printf("INFECTED\n"); break;
424 case MESSAGESTATE_DELIVERED
: printf("DELIVERED\n"); break;
425 case MESSAGESTATE_SUBSTITUTED
: printf("SUBSTITUTED\n"); break;
426 case MESSAGESTATE_RECEIVED
: printf("RECEIVED\n"); break;
427 case MESSAGESTATE_READ
: printf("READ\n"); break;
428 case MESSAGESTATE_UNDELIVERABLE
: printf("UNDELIVERABLE\n"); break;
429 case MESSAGESTATE_REMOVED
: printf("REMOVED\n"); break;
430 case MESSAGESTATE_IN_SAFE
: printf("IN_SAFE\n"); break;
431 default: printf("<unknown type %d>\n",
432 *(envelope
->dmMessageStatus
));
435 printf("\t\tdmAttachmentSize = ");
436 if (!envelope
->dmAttachmentSize
) printf("NULL\n");
437 else printf("%lu kB\n", *(envelope
->dmAttachmentSize
));
439 printf("\t\tdmDeliveryTime = ");
440 print_timeval(envelope
->dmDeliveryTime
);
442 printf("\t\tdmAcceptanceTime = ");
443 print_timeval(envelope
->dmAcceptanceTime
);
445 printf("\t\thash = ");
446 print_hash(envelope
->hash
);
448 printf("\t\ttimestamp = %p\n", envelope
->timestamp
);
449 printf("\t\ttimestamp_length = %zu\n", envelope
->timestamp_length
);
451 printf("\t\tevents = ");
452 print_events(envelope
->events
);
458 void print_document(const struct isds_document
*document
) {
459 printf("\t\tdocument = ");
467 printf("\t\t\tdata = %p\n", document
->data
);
468 printf("\t\t\tdata_length = %zu\n", document
->data_length
);
469 printf("\t\t\tdmMimeType = %s\n", document
->dmMimeType
);
471 printf("\t\t\tdmFileMetaType = ");
472 switch(document
->dmFileMetaType
) {
473 case FILEMETATYPE_MAIN
: printf("MAIN\n"); break;
474 case FILEMETATYPE_ENCLOSURE
: printf("ENCLOSURE\n"); break;
475 case FILEMETATYPE_SIGNATURE
: printf("SIGNATURE\n"); break;
476 case FILEMETATYPE_META
: printf("META\n"); break;
477 default: printf("<unknown type %d>\n", document
->dmFileMetaType
);
480 printf("\t\t\tdmFileGuid = %s\n", document
->dmFileGuid
);
481 printf("\t\t\tdmUpFileGuid = %s\n", document
->dmUpFileGuid
);
482 printf("\t\t\tdmFileDescr = %s\n", document
->dmFileDescr
);
483 printf("\t\t\tdmFormat = %s\n", document
->dmFormat
);
488 void print_documents(const struct isds_list
*documents
) {
489 const struct isds_list
*item
;
491 printf("\tdocuments = ");
499 for (item
= documents
; item
; item
= item
->next
) {
500 print_document((struct isds_document
*) (item
->data
));
507 void print_message(const struct isds_message
*message
) {
508 printf("message = ");
517 printf("\traw = %p\n", message
->raw
);
518 printf("\traw_length = %zu\n", message
->raw_length
);
519 printf("\traw_type = ");
520 print_raw_type(message
->raw_type
);
521 print_envelope(message
->envelope
);
522 print_documents(message
->documents
);
527 void print_copies(const struct isds_list
*copies
) {
528 const struct isds_list
*item
;
529 struct isds_message_copy
*copy
;
538 for (item
= copies
; item
; item
= item
->next
) {
539 copy
= (struct isds_message_copy
*) item
->data
;
546 printf("\t\tdbIDRecipient = %s\n", copy
->dbIDRecipient
);
547 printf("\t\tdmRecipientOrgUnit = %s\n", copy
->dmRecipientOrgUnit
);
549 printf("\t\tdmRecipientOrgUnitNum = ");
550 if (copy
->dmRecipientOrgUnitNum
)
551 printf("%ld\n", *copy
->dmRecipientOrgUnitNum
);
554 printf("\t\tdmToHands = %s\n", copy
->dmToHands
);
556 printf("\t\terror = %s\n", isds_strerror(copy
->error
));
557 printf("\t\tdmStatus = %s\n", copy
->dmStatus
);
558 printf("\t\tdmID = %s\n", copy
->dmID
);
566 void compare_hashes(const struct isds_hash
*hash1
,
567 const struct isds_hash
*hash2
) {
570 printf("Comparing hashes... ");
571 err
= isds_hash_cmp(hash1
, hash2
);
572 if (err
== IE_SUCCESS
)
573 printf("Hashes equal\n");
575 (err
== IE_NOTEQUAL
) printf("Hashes differ\n");
577 printf("isds_hash_cmp() failed: %s\n", isds_strerror(err
));
581 int progressbar(double upload_total
, double upload_current
,
582 double download_total
, double download_current
,
585 printf("Progress: upload %0f/%0f, download %0f/%0f, data=%p\n",
586 upload_current
, upload_total
, download_current
, download_total
,
592 int mmap_file(const char *file
, int *fd
, void **buffer
, size_t *length
) {
593 struct stat file_info
;
595 if (!file
|| !fd
|| !buffer
|| !length
) return -1;
598 *fd
= open(file
, O_RDONLY
);
600 fprintf(stderr
, "%s: Could not open file: %s\n", file
, strerror(errno
));
604 if (-1 == fstat(*fd
, &file_info
)) {
605 fprintf(stderr
, "%s: Could not get file size: %s\n", file
,
610 if (file_info
.st_size
< 0) {
611 fprintf(stderr
, "File `%s' has negative size: %jd\n", file
,
612 (intmax_t) file_info
.st_size
);
616 *length
= file_info
.st_size
;
618 *buffer
= mmap(NULL
, *length
, PROT_READ
, MAP_PRIVATE
, *fd
, 0);
619 if (*buffer
== MAP_FAILED
) {
620 fprintf(stderr
, "%s: Could not map file to memory: %s\n", file
,
630 int munmap_file(int fd
, void *buffer
, size_t length
) {
632 long int page_size
= sysconf(_SC_PAGE_SIZE
);
633 size_t pages
= (length
% page_size
) ?
634 ((length
/ page_size
) + 1) * page_size
:
637 err
= munmap(buffer
, pages
);
639 fprintf(stderr
, "Could not unmap memory at %p and length %zu: %s\n",
640 buffer
, pages
, strerror(errno
));
645 fprintf(stderr
, "Could close file descriptor %d: %s\n", fd
,
653 static int save_data_to_file(const char *file
, const void *data
,
654 const size_t length
) {
656 ssize_t written
, left
= length
;
658 if (!file
) return -1;
659 if (length
> 0 && !data
) return -1;
661 fd
= open(file
, O_WRONLY
|O_TRUNC
|O_CREAT
, 0666);
663 fprintf(stderr
, "%s: Could not open file for writing: %s\n",
664 file
, strerror(errno
));
668 printf("Writing %zu bytes to file `%s'...\n", length
, file
);
670 written
= write(fd
, data
+ length
- left
, left
);
672 fprintf(stderr
, "%s: Could not save file: %s\n",
673 file
, strerror(errno
));
680 if (-1 == close(fd
)) {
681 fprintf(stderr
, "%s: Closing file failed: %s\n",
682 file
, strerror(errno
));
691 int save_data(const char *message
, const void *data
, const size_t length
) {
693 printf("%s\n", message
);
694 return save_data_to_file("output", data
, length
);