# Copyright (C) 1994, Index Data I/S
# All rights reserved.
# Sebastian Hammer, Adam Dickmeiss
-# $Id: Makefile,v 1.23 1995-09-27 15:02:33 quinn Exp $
+# $Id: Makefile,v 1.24 1995-09-29 17:01:34 quinn Exp $
# Uncomment the lines below to enable mOSI communcation.
-ODEFS=-DUSE_XTIMOSI
-RFC1006=rfc1006
-LIBMOSI=../../xtimosi/src/libmosi.a ../lib/librfc.a
-XMOSI=xmosi.o
+#ODEFS=-DUSE_XTIMOSI
+#RFC1006=rfc1006
+#LIBMOSI=../../xtimosi/src/libmosi.a ../lib/librfc.a
+#XMOSI=xmosi.o
CDEFS=$(ODEFS)
#CC=
* Sebastian Hammer, Adam Dickmeiss
*
* $Log: client.c,v $
- * Revision 1.20 1995-08-29 14:24:13 quinn
+ * Revision 1.21 1995-09-29 17:01:47 quinn
+ * More Windows work
+ *
+ * Revision 1.20 1995/08/29 14:24:13 quinn
* Added second half of close-handshake
*
* Revision 1.19 1995/08/29 11:17:28 quinn
#include <stdio.h>
#include <stdlib.h>
+#ifdef WINDOWS
+#include <time.h>
+#else
#include <sys/time.h>
+#endif
#include <assert.h>
#ifdef _AIX
#include <sys/select.h>
if (!z_APDU(out, &a, 0))
{
- odr_perror(out, "Encoding APDU");
- exit(1);
+ odr_perror(out, "Encoding APDU");
+ exit(1);
}
buf = odr_getbuf(out, &len, 0);
odr_reset(out); /* release the APDU */
if (cs_put(conn, buf, len) < 0)
{
- fprintf(stderr, "cs_put: %s", cs_errlist[cs_errno(conn)]);
- exit(1);
+ fprintf(stderr, "cs_put: %s", cs_errmsg(cs_errno(conn)));
+ exit(1);
}
}
session = res;
if (!*res->result)
- printf("Connection rejected by target.\n");
+ printf("Connection rejected by target.\n");
else
- printf("Connection accepted by target.\n");
+ printf("Connection accepted by target.\n");
if (res->implementationId)
- printf("ID : %s\n", res->implementationId);
+ printf("ID : %s\n", res->implementationId);
if (res->implementationName)
- printf("Name : %s\n", res->implementationName);
+ printf("Name : %s\n", res->implementationName);
if (res->implementationVersion)
- printf("Version: %s\n", res->implementationVersion);
+ printf("Version: %s\n", res->implementationVersion);
if (res->userInformationField)
{
- printf("UserInformationfield:\n");
- if (!z_External(print, (Z_External**)&res-> userInformationField,
- 0))
- {
- odr_perror(print, "Printing userinfo\n");
- odr_reset(print);
- }
- if (res->userInformationField->which == Z_External_octet)
- {
- printf("Guessing visiblestring:\n");
- printf("'%s'\n", res->userInformationField->u. octet_aligned->buf);
- }
+ printf("UserInformationfield:\n");
+ if (!z_External(print, (Z_External**)&res-> userInformationField,
+ 0))
+ {
+ odr_perror(print, "Printing userinfo\n");
+ odr_reset(print);
+ }
+ if (res->userInformationField->which == Z_External_octet)
+ {
+ printf("Guessing visiblestring:\n");
+ printf("'%s'\n", res->userInformationField->u. octet_aligned->buf);
+ }
}
return 0;
}
if (conn)
{
- printf("Already connected.\n");
- return 0;
+ printf("Already connected.\n");
+ return 0;
}
if (!*arg || sscanf(arg, "%[^:]:%s", type, addr) < 2)
{
- fprintf(stderr, "Usage: open (osi|tcp) ':' [tsel '/']host[':'port]\n");
- return 0;
+ fprintf(stderr, "Usage: open (osi|tcp) ':' [tsel '/']host[':'port]\n");
+ return 0;
}
#ifdef USE_XTIMOSI
if (!strcmp(type, "osi"))
{
- if (!(add = mosi_strtoaddr(addr)))
- {
- perror(arg);
- return 0;
- }
- t = mosi_type;
- protocol = PROTO_SR;
+ if (!(add = mosi_strtoaddr(addr)))
+ {
+ perror(arg);
+ return 0;
+ }
+ t = mosi_type;
+ protocol = PROTO_SR;
}
else
#endif
if (!strcmp(type, "tcp"))
{
- if (!(add = tcpip_strtoaddr(addr)))
- {
- perror(arg);
- return 0;
- }
- t = tcpip_type;
- protocol = PROTO_Z3950;
+ if (!(add = tcpip_strtoaddr(addr)))
+ {
+ perror(arg);
+ return 0;
+ }
+ t = tcpip_type;
+ protocol = PROTO_Z3950;
}
else
{
- fprintf(stderr, "Bad type: %s\n", type);
- return 0;
+ fprintf(stderr, "Bad type: %s\n", type);
+ return 0;
}
if (!(conn = cs_create(t, 1, protocol)))
{
- perror("cs_create");
- return 0;
+ perror("cs_create");
+ return 0;
}
printf("Connecting...");
fflush(stdout);
if (cs_connect(conn, add) < 0)
{
- perror("connect");
- cs_close(conn);
- conn = 0;
- return 0;
+ perror("connect");
+ cs_close(conn);
+ conn = 0;
+ return 0;
}
printf("Ok.\n");
send_initRequest();
if (!*arg)
{
- printf("Auth field set to null\n");
- auth = 0;
- return 1;
+ printf("Auth field set to null\n");
+ auth = 0;
+ return 1;
}
auth = &au;
au.which = Z_IdAuthentication_open;
int i;
if (!r)
- return;
+ return;
for (i = 0; i < r->num_elements; i++)
{
- Z_TaggedElement *t;
-
- printf("%*s", level * 4, "");
- t = r->elements[i];
- printf("(");
- if (t->tagType)
- printf("%d,", *t->tagType);
- else
- printf("?,");
- if (t->tagValue->which == Z_StringOrNumeric_numeric)
- printf("%d) ", *t->tagValue->u.numeric);
- else
- printf("%s) ", t->tagValue->u.string);
- if (t->content->which == Z_ElementData_subtree)
- {
- printf("\n");
- display_grs1(t->content->u.subtree, level+1);
- }
- else if (t->content->which == Z_ElementData_string)
- printf("%s\n", t->content->u.string);
- else
- printf("??????\n");
+ Z_TaggedElement *t;
+
+ printf("%*s", level * 4, "");
+ t = r->elements[i];
+ printf("(");
+ if (t->tagType)
+ printf("%d,", *t->tagType);
+ else
+ printf("?,");
+ if (t->tagValue->which == Z_StringOrNumeric_numeric)
+ printf("%d) ", *t->tagValue->u.numeric);
+ else
+ printf("%s) ", t->tagValue->u.string);
+ if (t->content->which == Z_ElementData_subtree)
+ {
+ printf("\n");
+ display_grs1(t->content->u.subtree, level+1);
+ }
+ else if (t->content->which == Z_ElementData_string)
+ printf("%s\n", t->content->u.string);
+ else
+ printf("??????\n");
}
}
if (r->direct_reference)
{
- printf("Record type: ");
- if (ent)
- printf("%s\n", ent->desc);
- else if (!odr_oid(print, &r->direct_reference, 0))
- {
- odr_perror(print, "print oid");
- odr_reset(print);
- }
+ printf("Record type: ");
+ if (ent)
+ printf("%s\n", ent->desc);
+ else if (!odr_oid(print, &r->direct_reference, 0))
+ {
+ odr_perror(print, "print oid");
+ odr_reset(print);
+ }
}
if (r->which == Z_External_octet && p->u.octet_aligned->len)
- marc_display ((char*)p->u.octet_aligned->buf, stdout);
+ marc_display ((char*)p->u.octet_aligned->buf, stdout);
else if (ent->value == VAL_SUTRS)
{
- if (r->which != Z_External_sutrs)
- {
- printf("Expecting single SUTRS type for SUTRS.\n");
- return;
- }
- printf("%.*s", r->u.sutrs->len, r->u.sutrs->buf);
+ if (r->which != Z_External_sutrs)
+ {
+ printf("Expecting single SUTRS type for SUTRS.\n");
+ return;
+ }
+ printf("%.*s", r->u.sutrs->len, r->u.sutrs->buf);
}
else if (ent->value == VAL_GRS1)
{
- if (r->which != Z_External_grs1)
- {
- printf("Expecting single GRS type for GRS.\n");
- return;
- }
- display_grs1(r->u.grs1, 0);
+ if (r->which != Z_External_grs1)
+ {
+ printf("Expecting single GRS type for GRS.\n");
+ return;
+ }
+ display_grs1(r->u.grs1, 0);
}
else
{
- printf("Unknown record representation.\n");
- if (!z_External(print, &r, 0))
- {
- odr_perror(print, "Printing external");
- odr_reset(print);
- }
+ printf("Unknown record representation.\n");
+ if (!z_External(print, &r, 0))
+ {
+ odr_perror(print, "Printing external");
+ odr_reset(print);
+ }
}
}
#ifdef Z_95
if (p->which != Z_DiagRec_defaultFormat)
{
- printf("Diagnostic record not in default format.\n");
- return;
+ printf("Diagnostic record not in default format.\n");
+ return;
}
else
- r = p->u.defaultFormat;
+ r = p->u.defaultFormat;
#endif
if (!(ent = oid_getentbyoid(r->diagnosticSetId)) ||
- ent->class != CLASS_DIAGSET || ent->value != VAL_BIB1)
- printf("Missing or unknown diagset\n");
+ ent->class != CLASS_DIAGSET || ent->value != VAL_BIB1)
+ printf("Missing or unknown diagset\n");
printf(" [%d] %s", *r->condition, diagbib1_str(*r->condition));
if (r->addinfo && *r->addinfo)
- printf(" -- %s\n", r->addinfo);
+ printf(" -- %s\n", r->addinfo);
else
- printf("\n");
+ printf("\n");
}
static void display_nameplusrecord(Z_NamePlusRecord *p)
{
if (p->databaseName)
- printf("[%s]", p->databaseName);
+ printf("[%s]", p->databaseName);
if (p->which == Z_NamePlusRecord_surrogateDiagnostic)
- display_diagrec(p->u.surrogateDiagnostic);
+ display_diagrec(p->u.surrogateDiagnostic);
else
- display_record(p->u.databaseRecord);
+ display_record(p->u.databaseRecord);
}
static void display_records(Z_Records *p)
int i;
if (p->which == Z_Records_NSD)
- display_diagrec(p->u.nonSurrogateDiagnostic);
+ display_diagrec(p->u.nonSurrogateDiagnostic);
else
{
- printf("Records: %d\n", p->u.databaseOrSurDiagnostics->num_records);
- for (i = 0; i < p->u.databaseOrSurDiagnostics->num_records; i++)
- display_nameplusrecord(p->u.databaseOrSurDiagnostics->records[i]);
+ printf("Records: %d\n", p->u.databaseOrSurDiagnostics->num_records);
+ for (i = 0; i < p->u.databaseOrSurDiagnostics->num_records; i++)
+ display_nameplusrecord(p->u.databaseOrSurDiagnostics->records[i]);
}
}
rpn = ccl_find_str(bibset, arg, &error, &pos);
if (error)
{
- printf("CCL ERROR: %s\n", ccl_err_msg(error));
- return 0;
+ printf("CCL ERROR: %s\n", ccl_err_msg(error));
+ return 0;
}
#endif
#endif
if (!strcmp(arg, "@big")) /* strictly for troublemaking */
{
- static unsigned char big[2100];
- static Odr_oct bigo;
-
- /* send a very big referenceid to test transport stack etc. */
- memset(big, 'A', 2100);
- bigo.len = bigo.size = 2100;
- bigo.buf = big;
- req->referenceId = &bigo;
+ static unsigned char big[2100];
+ static Odr_oct bigo;
+
+ /* send a very big referenceid to test transport stack etc. */
+ memset(big, 'A', 2100);
+ bigo.len = bigo.size = 2100;
+ bigo.buf = big;
+ req->referenceId = &bigo;
}
if (setnumber >= 0)
{
- sprintf(setstring, "%d", ++setnumber);
- req->resultSetName = setstring;
+ sprintf(setstring, "%d", ++setnumber);
+ req->resultSetName = setstring;
}
*req->smallSetUpperBound = smallSetUpperBound;
*req->largeSetLowerBound = largeSetLowerBound;
*req->mediumSetPresentNumber = mediumSetPresentNumber;
if (smallSetUpperBound > 0 || (largeSetLowerBound > 1 &&
- mediumSetPresentNumber > 0))
+ mediumSetPresentNumber > 0))
{
- oident prefsyn;
-
- prefsyn.proto = protocol;
- prefsyn.class = CLASS_RECSYN;
- prefsyn.value = recordsyntax;
- req->preferredRecordSyntax = odr_oiddup(out, oid_getoidbyent(&prefsyn));
- req->smallSetElementSetNames =
- req->mediumSetElementSetNames = elementSetNames;
+ oident prefsyn;
+
+ prefsyn.proto = protocol;
+ prefsyn.class = CLASS_RECSYN;
+ prefsyn.value = recordsyntax;
+ req->preferredRecordSyntax = odr_oiddup(out, oid_getoidbyent(&prefsyn));
+ req->smallSetElementSetNames =
+ req->mediumSetElementSetNames = elementSetNames;
}
req->num_databaseNames = 1;
req->databaseNames = &databaseNames;
static int process_searchResponse(Z_SearchResponse *res)
{
if (*res->searchStatus)
- printf("Search was a success.\n");
+ printf("Search was a success.\n");
else
- printf("Search was a bloomin' failure.\n");
+ printf("Search was a bloomin' failure.\n");
printf("Number of hits: %d, setno %d\n",
- *res->resultCount, setnumber);
+ *res->resultCount, setnumber);
printf("records returned: %d\n",
- *res->numberOfRecordsReturned);
+ *res->numberOfRecordsReturned);
setno += *res->numberOfRecordsReturned;
if (res->records)
- display_records(res->records);
+ display_records(res->records);
return 0;
}
{
if (!*arg)
{
- printf("Find what?\n");
- return 0;
+ printf("Find what?\n");
+ return 0;
}
if (!conn)
{
- printf("Not connected yet\n");
- return 0;
+ printf("Not connected yet\n");
+ return 0;
}
if (!send_searchRequest(arg))
- return 0;
+ return 0;
return 2;
}
static int cmd_ssub(char *arg)
{
if (!(smallSetUpperBound = atoi(arg)))
- return 0;
+ return 0;
return 1;
}
static int cmd_lslb(char *arg)
{
if (!(largeSetLowerBound = atoi(arg)))
- return 0;
+ return 0;
return 1;
}
static int cmd_mspn(char *arg)
{
if (!(mediumSetPresentNumber = atoi(arg)))
- return 0;
+ return 0;
return 1;
}
{
if (!*arg)
{
- printf("Usage: base <database>\n");
- return 0;
+ printf("Usage: base <database>\n");
+ return 0;
}
strcpy(database, arg);
return 1;
{
if (setnumber < 0)
{
- printf("Set numbering enabled.\n");
- setnumber = 0;
+ printf("Set numbering enabled.\n");
+ setnumber = 0;
}
else
{
- printf("Set numbering disabled.\n");
- setnumber = -1;
+ printf("Set numbering disabled.\n");
+ setnumber = -1;
}
return 1;
}
if ((p = strchr(arg, '+')))
{
- nos = atoi(p + 1);
- *p = 0;
+ nos = atoi(p + 1);
+ *p = 0;
}
if (*arg)
- setno = atoi(arg);
+ setno = atoi(arg);
if (setnumber >= 0)
{
- sprintf(setstring, "%d", setnumber);
- req->resultSetId = setstring;
+ sprintf(setstring, "%d", setnumber);
+ req->resultSetId = setstring;
}
req->resultSetStartPoint = &setno;
req->numberOfRecordsRequested = &nos;
req->preferredRecordSyntax = oid_getoidbyent(&prefsyn);
if (elementSetNames)
{
- req->recordComposition = &compo;
- compo.which = Z_RecordComp_simple;
- compo.u.simple = elementSetNames;
+ req->recordComposition = &compo;
+ compo.which = Z_RecordComp_simple;
+ compo.u.simple = elementSetNames;
}
send_apdu(apdu);
printf("Sent presentRequest (%d+%d).\n", setno, nos);
static char *reasons[] =
{
- "finished",
- "shutdown",
- "systemProblem",
- "costLimit",
- "resources",
- "securityViolation",
- "protocolError",
- "lackOfActivity",
- "peerAbort",
- "unspecified"
+ "finished",
+ "shutdown",
+ "systemProblem",
+ "costLimit",
+ "resources",
+ "securityViolation",
+ "protocolError",
+ "lackOfActivity",
+ "peerAbort",
+ "unspecified"
};
printf("Reason: %s, message: %s\n", reasons[*req->closeReason],
- req->diagnosticInformation ? req->diagnosticInformation : "NULL");
+ req->diagnosticInformation ? req->diagnosticInformation : "NULL");
if (sent_close)
{
- printf("Goodbye.\n");
- exit(0);
+ printf("Goodbye.\n");
+ exit(0);
}
*res->closeReason = Z_Close_finished;
send_apdu(apdu);
static int cmd_show(char *arg)
{
if (!send_presentRequest(arg))
- return 0;
+ return 0;
return 2;
}
{
Z_APDU *apdu = zget_APDU(out, Z_APDU_triggerResourceControlRequest);
Z_TriggerResourceControlRequest *req =
- apdu->u.triggerResourceControlRequest;
+ apdu->u.triggerResourceControlRequest;
bool_t false = 0;
if (!session)
{
- printf("Session not initialized yet\n");
- return 0;
+ printf("Session not initialized yet\n");
+ return 0;
}
if (!ODR_MASK_GET(session->options, Z_Options_triggerResourceCtrl))
{
- printf("Target doesn't support cancel (trigger resource ctrl)\n");
- return 0;
+ printf("Target doesn't support cancel (trigger resource ctrl)\n");
+ return 0;
}
*req->requestedAction = Z_TriggerResourceCtrl_cancel;
req->resultSetWanted = &false;
{
if (t->term->which == Z_Term_general)
{
- printf("%.*s (%d)\n", t->term->u.general->len, t->term->u.general->buf,
- t->globalOccurrences ? *t->globalOccurrences : -1);
- sprintf(last_scan, "%.*s", t->term->u.general->len,
- t->term->u.general->buf);
+ printf("%.*s (%d)\n", t->term->u.general->len, t->term->u.general->buf,
+ t->globalOccurrences ? *t->globalOccurrences : -1);
+ sprintf(last_scan, "%.*s", t->term->u.general->len,
+ t->term->u.general->buf);
}
else
- printf("Term type not general.\n");
+ printf("Term type not general.\n");
}
void process_scanResponse(Z_ScanResponse *res)
int i;
printf("SCAN: %d entries, position=%d\n", *res->numberOfEntriesReturned,
- *res->positionOfTerm);
+ *res->positionOfTerm);
if (*res->scanStatus != Z_Scan_success)
- printf("Scan returned code %d\n", *res->scanStatus);
+ printf("Scan returned code %d\n", *res->scanStatus);
if (!res->entries)
- return;
+ return;
if (res->entries->which == Z_ListEntries_entries)
{
- Z_Entries *ent = res->entries->u.entries;
-
- for (i = 0; i < ent->num_entries; i++)
- if (ent->entries[i]->which == Z_Entry_termInfo)
- {
- printf("%c ", i + 1 == *res->positionOfTerm ? '*' : ' ');
- display_term(ent->entries[i]->u.termInfo);
- }
- else
- display_diagrec(ent->entries[i]->u.surrogateDiagnostic);
+ Z_Entries *ent = res->entries->u.entries;
+
+ for (i = 0; i < ent->num_entries; i++)
+ if (ent->entries[i]->which == Z_Entry_termInfo)
+ {
+ printf("%c ", i + 1 == *res->positionOfTerm ? '*' : ' ');
+ display_term(ent->entries[i]->u.termInfo);
+ }
+ else
+ display_diagrec(ent->entries[i]->u.surrogateDiagnostic);
}
else
- display_diagrec(res->entries->u.nonSurrogateDiagnostics->diagRecs[0]);
+ display_diagrec(res->entries->u.nonSurrogateDiagnostics->diagRecs[0]);
}
int cmd_scan(char *arg)
{
if (!session)
{
- printf("Session not initialized yet\n");
- return 0;
+ printf("Session not initialized yet\n");
+ return 0;
}
if (!ODR_MASK_GET(session->options, Z_Options_scan))
{
- printf("Target doesn't support scan\n");
- return 0;
+ printf("Target doesn't support scan\n");
+ return 0;
}
if (*arg)
{
- if (send_scanrequest(arg, 5, 20) < 0)
- return 0;
+ if (send_scanrequest(arg, 5, 20) < 0)
+ return 0;
}
else
- if (send_scanrequest(last_scan, 1, 20) < 0)
- return 0;
+ if (send_scanrequest(last_scan, 1, 20) < 0)
+ return 0;
return 2;
}
{
if (!arg || !*arg)
{
- printf("Usage: format <recordsyntax>\n");
- return 0;
+ printf("Usage: format <recordsyntax>\n");
+ return 0;
}
if (!strcmp(arg, "sutrs"))
{
- printf("Preferred format is SUTRS.\n");
- recordsyntax = VAL_SUTRS;
- return 1;
+ printf("Preferred format is SUTRS.\n");
+ recordsyntax = VAL_SUTRS;
+ return 1;
}
else if (!strcmp(arg, "usmarc"))
{
- printf("Preferred format is USMARC\n");
- recordsyntax = VAL_USMARC;
- return 1;
+ printf("Preferred format is USMARC\n");
+ recordsyntax = VAL_USMARC;
+ return 1;
}
else if (!strcmp(arg, "danmarc"))
{
- printf("Preferred format is DANMARC\n");
- recordsyntax = VAL_DANMARC;
- return 1;
+ printf("Preferred format is DANMARC\n");
+ recordsyntax = VAL_DANMARC;
+ return 1;
}
else if (!strcmp(arg, "grs1"))
{
- printf("Preferred format is GRS1\n");
- recordsyntax = VAL_GRS1;
- return 1;
+ printf("Preferred format is GRS1\n");
+ recordsyntax = VAL_GRS1;
+ return 1;
}
else
{
- printf("Specify one of {sutrs,usmarc,danmarc,grs1}.\n");
- return 0;
+ printf("Specify one of {sutrs,usmarc,danmarc,grs1}.\n");
+ return 0;
}
}
if (!arg || !*arg)
{
- printf("Usage: elements <esn>\n");
- return 0;
+ printf("Usage: elements <esn>\n");
+ return 0;
}
strcpy(what, arg);
esn.which = Z_ElementSetNames_generic;
#endif
if (!(out = odr_createmem(ODR_ENCODE)) ||
- !(in = odr_createmem(ODR_DECODE)) ||
- !(print = odr_createmem(ODR_PRINT)))
+ !(in = odr_createmem(ODR_DECODE)) ||
+ !(print = odr_createmem(ODR_PRINT)))
{
- fprintf(stderr, "failed to allocate ODR streams\n");
- exit(1);
+ fprintf(stderr, "failed to allocate ODR streams\n");
+ exit(1);
}
setvbuf(stdout, 0, _IONBF, 0);
inf = fopen ("default.bib", "r");
if (inf)
{
- ccl_qual_file (bibset, inf);
- fclose (inf);
+ ccl_qual_file (bibset, inf);
+ fclose (inf);
}
#endif
#endif
static int client(void)
{
static struct {
- char *cmd;
- int (*fun)(char *arg);
- char *ad;
+ char *cmd;
+ int (*fun)(char *arg);
+ char *ad;
} cmd[] = {
- {"open", cmd_open, "('tcp'|'osi')':'[<TSEL>'/']<HOST>[':'<PORT>]"},
- {"quit", cmd_quit, ""},
- {"find", cmd_find, "<CCL-QUERY>"},
- {"base", cmd_base, "<BASE-NAME>"},
- {"show", cmd_show, "<REC#>['+'<#RECS>]"},
- {"scan", cmd_scan, "<TERM>"},
- {"authentication", cmd_authentication, "<ACCTSTRING>"},
- {"lslb", cmd_lslb, "<largeSetLowerBound>"},
- {"ssub", cmd_ssub, "<smallSetUpperBound>"},
- {"mspn", cmd_mspn, "<mediumSetPresentNumber>"},
- {"status", cmd_status, ""},
- {"setnames", cmd_setnames, ""},
- {"cancel", cmd_cancel, ""},
- {"format", cmd_format, "<recordsyntax>"},
- {"elements", cmd_elements, "<elementSetName>"},
- {"close", cmd_close, ""},
- {0,0}
+ {"open", cmd_open, "('tcp'|'osi')':'[<TSEL>'/']<HOST>[':'<PORT>]"},
+ {"quit", cmd_quit, ""},
+ {"find", cmd_find, "<CCL-QUERY>"},
+ {"base", cmd_base, "<BASE-NAME>"},
+ {"show", cmd_show, "<REC#>['+'<#RECS>]"},
+ {"scan", cmd_scan, "<TERM>"},
+ {"authentication", cmd_authentication, "<ACCTSTRING>"},
+ {"lslb", cmd_lslb, "<largeSetLowerBound>"},
+ {"ssub", cmd_ssub, "<smallSetUpperBound>"},
+ {"mspn", cmd_mspn, "<mediumSetPresentNumber>"},
+ {"status", cmd_status, ""},
+ {"setnames", cmd_setnames, ""},
+ {"cancel", cmd_cancel, ""},
+ {"format", cmd_format, "<recordsyntax>"},
+ {"elements", cmd_elements, "<elementSetName>"},
+ {"close", cmd_close, ""},
+ {0,0}
};
char *netbuffer= 0;
int netbufferlen = 0;
while (1)
{
- int res;
- fd_set input;
- char line[1024], word[1024], arg[1024];
-
- FD_ZERO(&input);
- FD_SET(0, &input);
- if (conn)
- FD_SET(cs_fileno(conn), &input);
- if ((res = select(20, &input, 0, 0, 0)) < 0)
- {
- perror("select");
- exit(1);
- }
- if (!res)
- continue;
- if (FD_ISSET(0, &input))
- {
- /* quick & dirty way to get a command line. */
- if (!gets(line))
- break;
- if ((res = sscanf(line, "%s %[^;]", word, arg)) <= 0)
- {
- strcpy(word, last_cmd);
- *arg = '\0';
- }
- else if (res == 1)
- *arg = 0;
- strcpy(last_cmd, word);
- for (i = 0; cmd[i].cmd; i++)
- if (!strncmp(cmd[i].cmd, word, strlen(word)))
- {
- res = (*cmd[i].fun)(arg);
- break;
- }
- if (!cmd[i].cmd) /* dump our help-screen */
- {
- printf("Unknown command: %s.\n", word);
- printf("Currently recognized commands:\n");
- for (i = 0; cmd[i].cmd; i++)
- printf(" %s %s\n", cmd[i].cmd, cmd[i].ad);
- res = 1;
- }
- if (res < 2)
- printf(C_PROMPT);
- }
- if (conn && FD_ISSET(cs_fileno(conn), &input))
- {
- do
- {
- if ((res = cs_get(conn, &netbuffer, &netbufferlen)) < 0)
- {
- perror("cs_get");
- exit(1);
- }
- if (!res)
- {
- printf("Target closed connection.\n");
- exit(1);
- }
- odr_reset(in); /* release APDU from last round */
- odr_setbuf(in, netbuffer, res, 0);
- if (!z_APDU(in, &apdu, 0))
- {
- odr_perror(in, "Decoding incoming APDU");
- fprintf(stderr, "Packet dump:\n---------\n");
- odr_dumpBER(stderr, netbuffer, res);
- fprintf(stderr, "---------\n");
- exit(1);
- }
+ int res;
+ fd_set input;
+ char line[1024], word[1024], arg[1024];
+
+ FD_ZERO(&input);
+ FD_SET(0, &input);
+ if (conn)
+ FD_SET(cs_fileno(conn), &input);
+ if ((res = select(20, &input, 0, 0, 0)) < 0)
+ {
+ perror("select");
+ exit(1);
+ }
+ if (!res)
+ continue;
+ if (FD_ISSET(0, &input))
+ {
+ /* quick & dirty way to get a command line. */
+ if (!gets(line))
+ break;
+ if ((res = sscanf(line, "%s %[^;]", word, arg)) <= 0)
+ {
+ strcpy(word, last_cmd);
+ *arg = '\0';
+ }
+ else if (res == 1)
+ *arg = 0;
+ strcpy(last_cmd, word);
+ for (i = 0; cmd[i].cmd; i++)
+ if (!strncmp(cmd[i].cmd, word, strlen(word)))
+ {
+ res = (*cmd[i].fun)(arg);
+ break;
+ }
+ if (!cmd[i].cmd) /* dump our help-screen */
+ {
+ printf("Unknown command: %s.\n", word);
+ printf("Currently recognized commands:\n");
+ for (i = 0; cmd[i].cmd; i++)
+ printf(" %s %s\n", cmd[i].cmd, cmd[i].ad);
+ res = 1;
+ }
+ if (res < 2)
+ printf(C_PROMPT);
+ }
+ if (conn && FD_ISSET(cs_fileno(conn), &input))
+ {
+ do
+ {
+ if ((res = cs_get(conn, &netbuffer, &netbufferlen)) < 0)
+ {
+ perror("cs_get");
+ exit(1);
+ }
+ if (!res)
+ {
+ printf("Target closed connection.\n");
+ exit(1);
+ }
+ odr_reset(in); /* release APDU from last round */
+ odr_setbuf(in, netbuffer, res, 0);
+ if (!z_APDU(in, &apdu, 0))
+ {
+ odr_perror(in, "Decoding incoming APDU");
+ fprintf(stderr, "Packet dump:\n---------\n");
+ odr_dumpBER(stderr, netbuffer, res);
+ fprintf(stderr, "---------\n");
+ exit(1);
+ }
#if 0
- if (!z_APDU(print, &apdu, 0))
- {
- odr_perror(print, "Failed to print incoming APDU");
- odr_reset(print);
- continue;
- }
+ if (!z_APDU(print, &apdu, 0))
+ {
+ odr_perror(print, "Failed to print incoming APDU");
+ odr_reset(print);
+ continue;
+ }
#endif
- switch(apdu->which)
- {
- case Z_APDU_initResponse:
- process_initResponse(apdu->u.initResponse);
- break;
- case Z_APDU_searchResponse:
- process_searchResponse(apdu->u.searchResponse);
- break;
- case Z_APDU_scanResponse:
- process_scanResponse(apdu->u.scanResponse);
- break;
- case Z_APDU_presentResponse:
- printf("Received presentResponse.\n");
- setno +=
- *apdu->u.presentResponse->numberOfRecordsReturned;
- if (apdu->u.presentResponse->records)
- display_records(apdu->u.presentResponse->records);
- else
- printf("No records.\n");
- break;
- case Z_APDU_close:
- printf("Target has closed the association.\n");
- process_close(apdu->u.close);
- break;
- default:
- printf("Received unknown APDU type (%d).\n",
- apdu->which);
- exit(1);
- }
- printf("Z> ");
- fflush(stdout);
- }
- while (cs_more(conn));
- }
+ switch(apdu->which)
+ {
+ case Z_APDU_initResponse:
+ process_initResponse(apdu->u.initResponse);
+ break;
+ case Z_APDU_searchResponse:
+ process_searchResponse(apdu->u.searchResponse);
+ break;
+ case Z_APDU_scanResponse:
+ process_scanResponse(apdu->u.scanResponse);
+ break;
+ case Z_APDU_presentResponse:
+ printf("Received presentResponse.\n");
+ setno +=
+ *apdu->u.presentResponse->numberOfRecordsReturned;
+ if (apdu->u.presentResponse->records)
+ display_records(apdu->u.presentResponse->records);
+ else
+ printf("No records.\n");
+ break;
+ case Z_APDU_close:
+ printf("Target has closed the association.\n");
+ process_close(apdu->u.close);
+ break;
+ default:
+ printf("Received unknown APDU type (%d).\n",
+ apdu->which);
+ exit(1);
+ }
+ printf("Z> ");
+ fflush(stdout);
+ }
+ while (cs_more(conn));
+ }
}
return 0;
}
{
initialize();
if (argc > 1)
- cmd_open(argv[1]);
+ cmd_open(argv[1]);
else
- printf(C_PROMPT);
+ printf(C_PROMPT);
return client();
}
* Sebastian Hammer, Adam Dickmeiss
*
* $Log: comstack.c,v $
- * Revision 1.1 1995-06-14 09:58:20 quinn
+ * Revision 1.2 1995-09-29 17:01:48 quinn
+ * More Windows work
+ *
+ * Revision 1.1 1995/06/14 09:58:20 quinn
* Renamed yazlib to comstack.
*
* Revision 1.2 1995/05/16 08:51:15 quinn
"No data (operation would block)",
"New data while half of old buffer is on the line (flow control)"
};
+
+const char *cs_errmsg(int n)
+{
+ return cs_errlist[n];
+}
-/*
- * Copyright (c) 1995, Index Data
- * See the file LICENSE for details.
- * Sebastian Hammer, Adam Dickmeiss
- *
+/*\r
+ * Copyright (c) 1995, Index Data\r
+ * See the file LICENSE for details.\r
+ * Sebastian Hammer, Adam Dickmeiss\r
+ *\r
* $Log: tcpip.c,v $
- * Revision 1.4 1995-09-28 10:12:26 quinn
- * Windows-support changes
- *
- * Revision 1.3 1995/09/27 15:02:45 quinn
- * Modified function heads & prototypes.
- *
- * Revision 1.2 1995/06/15 12:30:06 quinn
- * Added @ as hostname alias for INADDR ANY.
- *
- * Revision 1.1 1995/06/14 09:58:20 quinn
- * Renamed yazlib to comstack.
- *
- * Revision 1.20 1995/05/16 08:51:16 quinn
- * License, documentation, and memory fixes
- *
- * Revision 1.19 1995/04/10 10:24:08 quinn
- * Some bug-fixes.
- *
- * Revision 1.18 1995/03/30 13:29:27 quinn
- * Added REUSEADDR in tcpip_bind
- *
- * Revision 1.17 1995/03/27 08:36:10 quinn
- * Some work on nonblocking operation in xmosi.c and rfct.c.
- * Added protocol parameter to cs_create()
- *
- * Revision 1.16 1995/03/21 15:53:41 quinn
- * Added rcvconnect
- *
- * Revision 1.15 1995/03/21 12:31:27 quinn
- * Added check for EINPROGRESS on connect.
- *
- * Revision 1.14 1995/03/20 09:47:21 quinn
- * Added server-side support to xmosi.c
- * Fixed possible problems in rfct
- * Other little mods
- *
- * Revision 1.13 1995/03/15 16:15:13 adam
- * Removed p_write.
- *
- * Revision 1.12 1995/03/15 15:36:27 quinn
- * Mods to support nonblocking I/O
- *
- * Revision 1.11 1995/03/15 08:37:57 quinn
- * Now we're pretty much set for nonblocking I/O.
- *
- * Revision 1.10 1995/03/14 17:00:07 quinn
- * Bug-fixes - added tracing info to tcpip.c
- *
- * Revision 1.9 1995/03/14 10:28:42 quinn
- * Adding server-side support to tcpip.c and fixing bugs in nonblocking I/O
- *
- * Revision 1.8 1995/03/10 14:22:50 quinn
- * Removed debug output.
- *
- * Revision 1.7 1995/03/10 11:44:59 quinn
- * Fixes and debugging
- *
- * Revision 1.6 1995/03/07 10:26:55 quinn
- * Initialized type field in the comstacks.
- *
- * Revision 1.5 1995/02/14 20:40:07 quinn
- * Various stuff.
- *
- * Revision 1.4 1995/02/14 11:54:49 quinn
- * Beginning to add full CCL.
- *
- * Revision 1.3 1995/02/10 18:58:10 quinn
- * Fixed tcpip_get (formerly tcpip_read).
- * Turned tst (cli) into a proper, event-driven thingy.
- *
- * Revision 1.2 1995/02/10 15:55:47 quinn
- * Small things.
- *
- * Revision 1.1 1995/02/09 15:51:52 quinn
- * Works better now.
- *
- */
-
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <errno.h>
-#include <fcntl.h>
-
-#include <comstack.h>
-#include <tcpip.h>
-
-#ifndef WINDOWS
-#include <sys/time.h>
-#endif
-
-int tcpip_close(COMSTACK h);
-int tcpip_put(COMSTACK h, char *buf, int size);
-int tcpip_get(COMSTACK h, char **buf, int *bufsize);
-int tcpip_connect(COMSTACK h, void *address);
-int tcpip_more(COMSTACK h);
-int tcpip_rcvconnect(COMSTACK h);
-int tcpip_bind(COMSTACK h, void *address, int mode);
-int tcpip_listen(COMSTACK h, char *addrp, int *addrlen);
-COMSTACK tcpip_accept(COMSTACK h);
-
-int completeBER(unsigned char *buf, int len);
-
-#ifdef TRACE_TCPIP
-#define TRC(x) x
-#else
-#define TRC(X)
-#endif
-
-static int initialized = 0;
-
-typedef struct tcpip_state
-{
- char *altbuf; /* alternate buffer for surplus data */
- int altsize; /* size as malloced */
- int altlen; /* length of data or 0 if none */
-
- int written; /* -1 if we aren't writing */
- int towrite; /* to verify against user input */
-} tcpip_state;
-
-COMSTACK MDF tcpip_type(int blocking, int protocol)
-{
- COMSTACK p;
- struct protoent *proto;
- tcpip_state *state;
- int s, tru = 1;
-
- if (!initialized)
- {
-#ifdef WINDOWS
- WORD requested;
- WSADATA wd;
-
- requested = MAKEWORD(1, 1);
- if (WSAStartup(requested, &wd))
- return 0;
-#endif
- initialized = 1;
- }
-
-#ifndef WINDOWS
- if (!(proto = getprotobyname("tcp")))
- return 0;
- if ((s = socket(AF_INET, SOCK_STREAM, proto->p_proto)) < 0)
-#else
- if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
-#endif
- return 0;
- if (!(p = malloc(sizeof(struct comstack))))
- return 0;
- if (!(state = p->private = malloc(sizeof(tcpip_state))))
- return 0;
-#ifdef WINDOWS
- if (!(p->blocking = blocking) && ioctlsocket(s, FIONBIO, &tru) < 0)
-#else
- if (!(p->blocking = blocking) && fcntl(s, F_SETFL, O_NONBLOCK) < 0)
-#endif
- return 0;
-
- p->iofile = s;
- p->type = tcpip_type;
- p->protocol = protocol;
-
- p->f_connect = tcpip_connect;
- p->f_rcvconnect = tcpip_rcvconnect;
- p->f_get = tcpip_get;
- p->f_put = tcpip_put;
- p->f_close = tcpip_close;
- p->f_more = tcpip_more;
- p->f_bind = tcpip_bind;
- p->f_listen = tcpip_listen;
- p->f_accept = tcpip_accept;
-
- p->state = CS_UNBND;
- p->event = CS_NONE;
- p->cerrno = 0;
- p->stackerr = 0;
-
- state->altbuf = 0;
- state->altsize = state->altlen = 0;
- state->towrite = state->written = -1;
-
- p->timeout = COMSTACK_DEFAULT_TIMEOUT;
- TRC(fprintf(stderr, "Created new TCPIP comstack\n"));
-
- return p;
-}
-
-struct sockaddr_in MDF *tcpip_strtoaddr(const char *str)
-{
- static struct sockaddr_in add;
- struct hostent *hp;
- char *p, buf[512];
- short int port = 210;
- unsigned tmpadd;
-
- TRC(fprintf(stderr, "tcpip_strtoaddress: %s\n", str ? str : "NULL"));
- add.sin_family = AF_INET;
- strcpy(buf, str);
- if ((p = strchr(buf, ':')))
- {
- *p = 0;
- port = atoi(p + 1);
- }
- add.sin_port = htons(port);
- if (!strcmp("@", buf))
- add.sin_addr.s_addr = INADDR_ANY;
- else if ((hp = gethostbyname(buf)))
- memcpy(&add.sin_addr.s_addr, *hp->h_addr_list, sizeof(struct in_addr));
- else if ((tmpadd = (unsigned) inet_addr(buf)) != 0)
- memcpy(&add.sin_addr.s_addr, &tmpadd, sizeof(struct in_addr));
- else
- return 0;
- return &add;
-}
-
-int tcpip_more(COMSTACK h)
-{
- tcpip_state *sp = h->private;
-
- return sp->altlen && completeBER((unsigned char *) sp->altbuf, sp->altlen);
-}
-
-/*
- * connect(2) will block (sometimes) - nothing we can do short of doing
- * weird things like spawning subprocesses or threading or some weird junk
- * like that.
- */
-int tcpip_connect(COMSTACK h, void *address)
-{
- struct sockaddr_in *add = address;
-
- TRC(fprintf(stderr, "tcpip_connect\n"));
- if (connect(h->iofile, (struct sockaddr *) add, sizeof(*add)) < 0)
- {
-#ifdef WINDOWS
- if (errno == WSAEWOULDBLOCK)
-#else
- if (errno == EINPROGRESS)
-#endif
- return 1;
- return -1;
- }
- h->state = CS_DATAXFER;
- return 0;
-}
-
-/*
- * nop
- */
-int tcpip_rcvconnect(COMSTACK h)
-{
- TRC(fprintf(stderr, "tcpip_rcvconnect\n"));
- return 0;
-}
-
-int tcpip_bind(COMSTACK h, void *address, int mode)
-{
- struct sockaddr *addr = address;
- int one = 1;
-
- TRC(fprintf(stderr, "tcpip_bind\n"));
- if (setsockopt(h->iofile, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0)
- {
- h->cerrno = CSYSERR;
- return -1;
- }
- if (bind(h->iofile, addr, sizeof(struct sockaddr_in)) < 0)
- {
- h->cerrno = CSYSERR;
- return -1;
- }
- if (mode == CS_SERVER && listen(h->iofile, 3) < 0)
- {
- h->cerrno = CSYSERR;
- return -1;
- }
- h->state = CS_IDLE;
- return 0;
-}
-
-int tcpip_listen(COMSTACK h, char *raddr, int *addrlen)
-{
- struct sockaddr_in addr;
- int len = sizeof(addr);
-
- TRC(fprintf(stderr, "tcpip_listen\n"));
- if (h->state != CS_IDLE)
- {
- h->cerrno = CSOUTSTATE;
- return -1;
- }
- if ((h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len)) < 0)
- {
-#ifdef WINDOWS
- if (errno == WSAEWOULDBLOCK)
-#else
- if (errno == EWOULDBLOCK)
-#endif
-
- h->cerrno = CSNODATA;
- else
- h->cerrno = CSYSERR;
- return -1;
- }
- if (addrlen && *addrlen > sizeof(struct sockaddr_in))
- memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_in));
- else if (addrlen)
- *addrlen = 0;
- h->state = CS_INCON;
- return 0;
-}
-
-COMSTACK tcpip_accept(COMSTACK h)
-{
- COMSTACK new;
- tcpip_state *state;
-
- TRC(fprintf(stderr, "tcpip_accept\n"));
- if (h->state != CS_INCON)
- {
- h->cerrno = CSOUTSTATE;
- return 0;
- }
- if (!(new = malloc(sizeof(*new))))
- {
- h->cerrno = CSYSERR;
- return 0;
- }
- memcpy(new, h, sizeof(*h));
- new->iofile = h->newfd;
- if (!(state = new->private = malloc(sizeof(tcpip_state))))
- {
- h->cerrno = CSYSERR;
- return 0;
- }
-#ifdef WINDOWS
- if (!new->blocking && ioctlsocket(s, FIONBIO, &tru) < 0)
-#else
- if (!new->blocking && fcntl(new->iofile, F_SETFL, O_NONBLOCK) < 0)
-#endif
- return 0;
- state->altbuf = 0;
- state->altsize = state->altlen = 0;
- state->towrite = state->written = -1;
- new->state = CS_DATAXFER;
- h->state = CS_IDLE;
- return new;
-}
-
-#define CS_TCPIP_BUFCHUNK 4096
-
-/*
- * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,
- * 0=connection closed.
- */
-int tcpip_get(COMSTACK h, char **buf, int *bufsize)
-{
- tcpip_state *sp = h->private;
- char *tmpc;
- int tmpi, berlen, rest, req, tomove;
- int hasread = 0, res;
-
- TRC(fprintf(stderr, "tcpip_get: bufsize=%d\n", *bufsize));
- if (sp->altlen) /* switch buffers */
- {
- TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
- (unsigned) sp->altbuf));
- tmpc = *buf;
- tmpi = *bufsize;
- *buf = sp->altbuf;
- *bufsize = sp->altsize;
- hasread = sp->altlen;
- sp->altlen = 0;
- sp->altbuf = tmpc;
- sp->altsize = tmpi;
- }
- while (!(berlen = completeBER((unsigned char *)*buf, hasread)))
- {
- if (!*bufsize)
- {
- if (!(*buf = malloc(*bufsize = CS_TCPIP_BUFCHUNK)))
- return -1;
- }
- else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
- if (!(*buf = realloc(*buf, *bufsize *= 2)))
- return -1;
- if ((res = recv(h->iofile, *buf + hasread, CS_TCPIP_BUFCHUNK, 0)) < 0)
-#ifdef WINDOWS
- if (errno == WSAEWOULDBLOCK)
-#else
- if (errno == EWOULDBLOCK)
-#endif
- break;
- else
- return -1;
- if (!res)
- return 0;
- hasread += res;
- TRC(fprintf(stderr, " res=%d, hasread=%d\n", res, hasread));
- }
- TRC(fprintf(stderr, " Out of read loop with hasread=%d, berlen=%d\n",
- hasread, berlen));
- /* move surplus buffer (or everything if we didn't get a BER rec.) */
- if (hasread > berlen)
- {
- tomove = req = hasread - berlen;
- rest = tomove % CS_TCPIP_BUFCHUNK;
- if (rest)
- req += CS_TCPIP_BUFCHUNK - rest;
- if (!sp->altbuf)
- {
- if (!(sp->altbuf = malloc(sp->altsize = req)))
- return -1;
- } else if (sp->altsize < req)
- if (!(sp->altbuf = realloc(sp->altbuf, sp->altsize = req)))
- return -1;
- TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
- (unsigned) sp->altbuf));
- memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
- }
- if (berlen < CS_TCPIP_BUFCHUNK - 1)
- *(*buf + berlen) = '\0';
- return berlen ? berlen : 1;
-}
-
-/*
- * Returns 1, 0 or -1
- * In nonblocking mode, you must call again with same buffer while
- * return value is 1.
- */
-int tcpip_put(COMSTACK h, char *buf, int size)
-{
- int res;
- struct tcpip_state *state = h->private;
-
- TRC(fprintf(stderr, "tcpip_put: size=%d\n", size));
- if (state->towrite < 0)
- {
- state->towrite = size;
- state->written = 0;
- }
- else if (state->towrite != size)
- {
- h->cerrno = CSWRONGBUF;
- return -1;
- }
- while (state->towrite > state->written)
- {
- if ((res = send(h->iofile, buf + state->written, size -
- state->written, 0)) < 0)
- {
-#ifdef WINDOWS
- if (errno == WSAEWOULDBLOCK)
-#else
- if (errno == EAGAIN)
-#endif
- {
- TRC(fprintf(stderr, " Flow control stop\n"));
- return 1;
- }
- h->cerrno = CSYSERR;
- return -1;
- }
- state->written += res;
- TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
- res, state->written, size));
- }
- state->towrite = state->written = -1;
- TRC(fprintf(stderr, " Ok\n"));
- return 0;
-}
-
-int tcpip_close(COMSTACK h)
-{
- tcpip_state *sp = h->private;
-
- TRC(fprintf(stderr, "tcpip_close\n"));
- close(h->iofile);
- if (sp->altbuf)
- free(sp->altbuf);
- free(sp);
- free(h);
- return 0;
-}
+ * Revision 1.5 1995-09-29 17:01:48 quinn
+ * More Windows work
+ *\r
+ * Revision 1.4 1995/09/28 10:12:26 quinn\r
+ * Windows-support changes\r
+ *\r
+ * Revision 1.3 1995/09/27 15:02:45 quinn\r
+ * Modified function heads & prototypes.\r
+ *\r
+ * Revision 1.2 1995/06/15 12:30:06 quinn\r
+ * Added @ as hostname alias for INADDR ANY.\r
+ *\r
+ * Revision 1.1 1995/06/14 09:58:20 quinn\r
+ * Renamed yazlib to comstack.\r
+ *\r
+ * Revision 1.20 1995/05/16 08:51:16 quinn\r
+ * License, documentation, and memory fixes\r
+ *\r
+ * Revision 1.19 1995/04/10 10:24:08 quinn\r
+ * Some bug-fixes.\r
+ *\r
+ * Revision 1.18 1995/03/30 13:29:27 quinn\r
+ * Added REUSEADDR in tcpip_bind\r
+ *\r
+ * Revision 1.17 1995/03/27 08:36:10 quinn\r
+ * Some work on nonblocking operation in xmosi.c and rfct.c.\r
+ * Added protocol parameter to cs_create()\r
+ *\r
+ * Revision 1.16 1995/03/21 15:53:41 quinn\r
+ * Added rcvconnect\r
+ *\r
+ * Revision 1.15 1995/03/21 12:31:27 quinn\r
+ * Added check for EINPROGRESS on connect.\r
+ *\r
+ * Revision 1.14 1995/03/20 09:47:21 quinn\r
+ * Added server-side support to xmosi.c\r
+ * Fixed possible problems in rfct\r
+ * Other little mods\r
+ *\r
+ * Revision 1.13 1995/03/15 16:15:13 adam\r
+ * Removed p_write.\r
+ *\r
+ * Revision 1.12 1995/03/15 15:36:27 quinn\r
+ * Mods to support nonblocking I/O\r
+ *\r
+ * Revision 1.11 1995/03/15 08:37:57 quinn\r
+ * Now we're pretty much set for nonblocking I/O.\r
+ *\r
+ * Revision 1.10 1995/03/14 17:00:07 quinn\r
+ * Bug-fixes - added tracing info to tcpip.c\r
+ *\r
+ * Revision 1.9 1995/03/14 10:28:42 quinn\r
+ * Adding server-side support to tcpip.c and fixing bugs in nonblocking I/O\r
+ *\r
+ * Revision 1.8 1995/03/10 14:22:50 quinn\r
+ * Removed debug output.\r
+ *\r
+ * Revision 1.7 1995/03/10 11:44:59 quinn\r
+ * Fixes and debugging\r
+ *\r
+ * Revision 1.6 1995/03/07 10:26:55 quinn\r
+ * Initialized type field in the comstacks.\r
+ *\r
+ * Revision 1.5 1995/02/14 20:40:07 quinn\r
+ * Various stuff.\r
+ *\r
+ * Revision 1.4 1995/02/14 11:54:49 quinn\r
+ * Beginning to add full CCL.\r
+ *\r
+ * Revision 1.3 1995/02/10 18:58:10 quinn\r
+ * Fixed tcpip_get (formerly tcpip_read).\r
+ * Turned tst (cli) into a proper, event-driven thingy.\r
+ *\r
+ * Revision 1.2 1995/02/10 15:55:47 quinn\r
+ * Small things.\r
+ *\r
+ * Revision 1.1 1995/02/09 15:51:52 quinn\r
+ * Works better now.\r
+ *\r
+ */\r
+\r
+#include <stdio.h>\r
+#include <string.h>\r
+#include <stdlib.h>\r
+#include <unistd.h>\r
+#include <errno.h>\r
+#include <fcntl.h>\r
+\r
+#include <comstack.h>\r
+#include <tcpip.h>\r
+\r
+#ifndef WINDOWS\r
+#include <sys/time.h>\r
+#endif\r
+\r
+int tcpip_close(COMSTACK h);\r
+int tcpip_put(COMSTACK h, char *buf, int size);\r
+int tcpip_get(COMSTACK h, char **buf, int *bufsize);\r
+int tcpip_connect(COMSTACK h, void *address);\r
+int tcpip_more(COMSTACK h);\r
+int tcpip_rcvconnect(COMSTACK h);\r
+int tcpip_bind(COMSTACK h, void *address, int mode);\r
+int tcpip_listen(COMSTACK h, char *addrp, int *addrlen);\r
+COMSTACK tcpip_accept(COMSTACK h);\r
+\r
+int completeBER(unsigned char *buf, int len);\r
+\r
+#ifdef TRACE_TCPIP\r
+#define TRC(x) x\r
+#else\r
+#define TRC(X)\r
+#endif\r
+\r
+static int initialized = 0;\r
+\r
+typedef struct tcpip_state\r
+{\r
+ char *altbuf; /* alternate buffer for surplus data */\r
+ int altsize; /* size as malloced */\r
+ int altlen; /* length of data or 0 if none */\r
+\r
+ int written; /* -1 if we aren't writing */\r
+ int towrite; /* to verify against user input */\r
+} tcpip_state;\r
+\r
+COMSTACK MDF tcpip_type(int blocking, int protocol)\r
+{\r
+ COMSTACK p;\r
+ tcpip_state *state;\r
+ int s;\r
+#ifdef WINDOWS\r
+ unsigned long tru = 1;\r
+#else\r
+ struct protoent *proto;\r
+#endif\r
+\r
+ if (!initialized)\r
+ {\r
+#ifdef WINDOWS\r
+ WORD requested;\r
+ WSADATA wd;\r
+\r
+ requested = MAKEWORD(1, 1);\r
+ if (WSAStartup(requested, &wd))\r
+ return 0;\r
+#endif\r
+ initialized = 1;\r
+ }\r
+\r
+#ifndef WINDOWS\r
+ if (!(proto = getprotobyname("tcp")))\r
+ return 0;\r
+ if ((s = socket(AF_INET, SOCK_STREAM, proto->p_proto)) < 0)\r
+#else\r
+ if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)\r
+#endif\r
+ return 0;\r
+ if (!(p = malloc(sizeof(struct comstack))))\r
+ return 0;\r
+ if (!(state = p->private = malloc(sizeof(tcpip_state))))\r
+ return 0;\r
+#ifdef WINDOWS\r
+ if (!(p->blocking = blocking) && ioctlsocket(s, FIONBIO, &tru) < 0)\r
+#else\r
+ if (!(p->blocking = blocking) && fcntl(s, F_SETFL, O_NONBLOCK) < 0)\r
+#endif\r
+ return 0;\r
+ p->iofile = s;\r
+ p->type = tcpip_type;\r
+ p->protocol = protocol;\r
+\r
+ p->f_connect = tcpip_connect;\r
+ p->f_rcvconnect = tcpip_rcvconnect;\r
+ p->f_get = tcpip_get;\r
+ p->f_put = tcpip_put;\r
+ p->f_close = tcpip_close;\r
+ p->f_more = tcpip_more;\r
+ p->f_bind = tcpip_bind;\r
+ p->f_listen = tcpip_listen;\r
+ p->f_accept = tcpip_accept;\r
+\r
+ p->state = CS_UNBND;\r
+ p->event = CS_NONE;\r
+ p->cerrno = 0;\r
+ p->stackerr = 0;\r
+\r
+ state->altbuf = 0;\r
+ state->altsize = state->altlen = 0;\r
+ state->towrite = state->written = -1;\r
+\r
+ p->timeout = COMSTACK_DEFAULT_TIMEOUT;\r
+ TRC(fprintf(stderr, "Created new TCPIP comstack\n"));\r
+\r
+ return p;\r
+}\r
+\r
+struct sockaddr_in MDF *tcpip_strtoaddr(const char *str)\r
+{\r
+ static struct sockaddr_in add;\r
+ struct hostent *hp;\r
+ char *p, buf[512];\r
+ short int port = 210;\r
+ unsigned tmpadd;\r
+\r
+ TRC(fprintf(stderr, "tcpip_strtoaddress: %s\n", str ? str : "NULL"));\r
+ add.sin_family = AF_INET;\r
+ strcpy(buf, str);\r
+ if ((p = strchr(buf, ':')))\r
+ {\r
+ *p = 0;\r
+ port = atoi(p + 1);\r
+ }\r
+ add.sin_port = htons(port);\r
+ if (!strcmp("@", buf))\r
+ add.sin_addr.s_addr = INADDR_ANY;\r
+ else if ((hp = gethostbyname(buf)))\r
+ memcpy(&add.sin_addr.s_addr, *hp->h_addr_list, sizeof(struct in_addr));\r
+ else if ((tmpadd = (unsigned) inet_addr(buf)) != 0)\r
+ memcpy(&add.sin_addr.s_addr, &tmpadd, sizeof(struct in_addr));\r
+ else\r
+ return 0;\r
+ return &add;\r
+}\r
+\r
+int tcpip_more(COMSTACK h)\r
+{\r
+ tcpip_state *sp = h->private;\r
+\r
+ return sp->altlen && completeBER((unsigned char *) sp->altbuf, sp->altlen);\r
+}\r
+\r
+/*\r
+ * connect(2) will block (sometimes) - nothing we can do short of doing\r
+ * weird things like spawning subprocesses or threading or some weird junk\r
+ * like that.\r
+ */\r
+int tcpip_connect(COMSTACK h, void *address)\r
+{\r
+ struct sockaddr_in *add = address;\r
+\r
+ TRC(fprintf(stderr, "tcpip_connect\n"));\r
+ if (connect(h->iofile, (struct sockaddr *) add, sizeof(*add)) < 0)\r
+ {\r
+#ifdef WINDOWS\r
+ if (WSAGetLastError() == WSAEWOULDBLOCK)\r
+#else\r
+ if (errno == EINPROGRESS)\r
+#endif\r
+ return 1;\r
+ return -1;\r
+ }\r
+ h->state = CS_DATAXFER;\r
+ return 0;\r
+}\r
+\r
+/*\r
+ * nop\r
+ */\r
+int tcpip_rcvconnect(COMSTACK h)\r
+{\r
+ TRC(fprintf(stderr, "tcpip_rcvconnect\n"));\r
+ return 0;\r
+}\r
+\r
+int tcpip_bind(COMSTACK h, void *address, int mode)\r
+{\r
+ struct sockaddr *addr = address;\r
+ unsigned long one = 1;\r
+\r
+ TRC(fprintf(stderr, "tcpip_bind\n"));\r
+ if (setsockopt(h->iofile, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0)\r
+ {\r
+ h->cerrno = CSYSERR;\r
+ return -1;\r
+ }\r
+ if (bind(h->iofile, addr, sizeof(struct sockaddr_in)) < 0)\r
+ {\r
+ h->cerrno = CSYSERR;\r
+ return -1;\r
+ }\r
+ if (mode == CS_SERVER && listen(h->iofile, 3) < 0)\r
+ {\r
+ h->cerrno = CSYSERR;\r
+ return -1;\r
+ }\r
+ h->state = CS_IDLE;\r
+ return 0;\r
+}\r
+\r
+int tcpip_listen(COMSTACK h, char *raddr, int *addrlen)\r
+{\r
+ struct sockaddr_in addr;\r
+ int len = sizeof(addr);\r
+\r
+ TRC(fprintf(stderr, "tcpip_listen\n"));\r
+ if (h->state != CS_IDLE)\r
+ {\r
+ h->cerrno = CSOUTSTATE;\r
+ return -1;\r
+ }\r
+ if ((h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len)) < 0)\r
+ {\r
+#ifdef WINDOWS\r
+ if (WSAGetLastError() == WSAEWOULDBLOCK)\r
+#else\r
+ if (errno == EWOULDBLOCK)\r
+#endif\r
+\r
+ h->cerrno = CSNODATA;\r
+ else\r
+ h->cerrno = CSYSERR;\r
+ return -1;\r
+ }\r
+ if (addrlen && *addrlen > sizeof(struct sockaddr_in))\r
+ memcpy(raddr, &addr, *addrlen = sizeof(struct sockaddr_in));\r
+ else if (addrlen)\r
+ *addrlen = 0;\r
+ h->state = CS_INCON;\r
+ return 0;\r
+}\r
+\r
+COMSTACK tcpip_accept(COMSTACK h)\r
+{\r
+ COMSTACK new;\r
+ tcpip_state *state;\r
+#ifdef WINDOWS\r
+ unsigned long tru = 1;\r
+#endif\r
+\r
+ TRC(fprintf(stderr, "tcpip_accept\n"));\r
+ if (h->state != CS_INCON)\r
+ {\r
+ h->cerrno = CSOUTSTATE;\r
+ return 0;\r
+ }\r
+ if (!(new = malloc(sizeof(*new))))\r
+ {\r
+ h->cerrno = CSYSERR;\r
+ return 0;\r
+ }\r
+ memcpy(new, h, sizeof(*h));\r
+ new->iofile = h->newfd;\r
+ if (!(state = new->private = malloc(sizeof(tcpip_state))))\r
+ {\r
+ h->cerrno = CSYSERR;\r
+ return 0;\r
+ }\r
+#ifdef WINDOWS\r
+ if (!new->blocking && ioctlsocket(new->iofile, FIONBIO, &tru) < 0)\r
+#else\r
+ if (!new->blocking && fcntl(new->iofile, F_SETFL, O_NONBLOCK) < 0)\r
+#endif\r
+ return 0;\r
+ state->altbuf = 0;\r
+ state->altsize = state->altlen = 0;\r
+ state->towrite = state->written = -1;\r
+ new->state = CS_DATAXFER;\r
+ h->state = CS_IDLE;\r
+ return new;\r
+}\r
+\r
+#define CS_TCPIP_BUFCHUNK 4096\r
+\r
+/*\r
+ * Return: -1 error, >1 good, len of buffer, ==1 incomplete buffer,\r
+ * 0=connection closed.\r
+ */\r
+int tcpip_get(COMSTACK h, char **buf, int *bufsize)\r
+{\r
+ tcpip_state *sp = h->private;\r
+ char *tmpc;\r
+ int tmpi, berlen, rest, req, tomove;\r
+ int hasread = 0, res;\r
+\r
+ TRC(fprintf(stderr, "tcpip_get: bufsize=%d\n", *bufsize));\r
+ if (sp->altlen) /* switch buffers */\r
+ {\r
+ TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,\r
+ (unsigned) sp->altbuf));\r
+ tmpc = *buf;\r
+ tmpi = *bufsize;\r
+ *buf = sp->altbuf;\r
+ *bufsize = sp->altsize;\r
+ hasread = sp->altlen;\r
+ sp->altlen = 0;\r
+ sp->altbuf = tmpc;\r
+ sp->altsize = tmpi;\r
+ }\r
+ while (!(berlen = completeBER((unsigned char *)*buf, hasread)))\r
+ {\r
+ if (!*bufsize)\r
+ {\r
+ if (!(*buf = malloc(*bufsize = CS_TCPIP_BUFCHUNK)))\r
+ return -1;\r
+ }\r
+ else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)\r
+ if (!(*buf = realloc(*buf, *bufsize *= 2)))\r
+ return -1;\r
+ if ((res = recv(h->iofile, *buf + hasread, CS_TCPIP_BUFCHUNK, 0)) < 0)\r
+#ifdef WINDOWS\r
+ if (WSAGetLastError() == WSAEWOULDBLOCK)\r
+#else\r
+ if (errno == EWOULDBLOCK)\r
+#endif\r
+ break;\r
+ else\r
+ return -1;\r
+ if (!res)\r
+ return 0;\r
+ hasread += res;\r
+ TRC(fprintf(stderr, " res=%d, hasread=%d\n", res, hasread));\r
+ }\r
+ TRC(fprintf(stderr, " Out of read loop with hasread=%d, berlen=%d\n",\r
+ hasread, berlen));\r
+ /* move surplus buffer (or everything if we didn't get a BER rec.) */\r
+ if (hasread > berlen)\r
+ {\r
+ tomove = req = hasread - berlen;\r
+ rest = tomove % CS_TCPIP_BUFCHUNK;\r
+ if (rest)\r
+ req += CS_TCPIP_BUFCHUNK - rest;\r
+ if (!sp->altbuf)\r
+ {\r
+ if (!(sp->altbuf = malloc(sp->altsize = req)))\r
+ return -1;\r
+ } else if (sp->altsize < req)\r
+ if (!(sp->altbuf = realloc(sp->altbuf, sp->altsize = req)))\r
+ return -1;\r
+ TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,\r
+ (unsigned) sp->altbuf));\r
+ memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);\r
+ }\r
+ if (berlen < CS_TCPIP_BUFCHUNK - 1)\r
+ *(*buf + berlen) = '\0';\r
+ return berlen ? berlen : 1;\r
+}\r
+\r
+/*\r
+ * Returns 1, 0 or -1\r
+ * In nonblocking mode, you must call again with same buffer while\r
+ * return value is 1.\r
+ */\r
+int tcpip_put(COMSTACK h, char *buf, int size)\r
+{\r
+ int res;\r
+ struct tcpip_state *state = h->private;\r
+\r
+ TRC(fprintf(stderr, "tcpip_put: size=%d\n", size));\r
+ if (state->towrite < 0)\r
+ {\r
+ state->towrite = size;\r
+ state->written = 0;\r
+ }\r
+ else if (state->towrite != size)\r
+ {\r
+ h->cerrno = CSWRONGBUF;\r
+ return -1;\r
+ }\r
+ while (state->towrite > state->written)\r
+ {\r
+ if ((res = send(h->iofile, buf + state->written, size -\r
+ state->written, 0)) < 0)\r
+ {\r
+#ifdef WINDOWS\r
+ if (WSAGetLastError() == WSAEWOULDBLOCK)\r
+#else\r
+ if (errno == EAGAIN)\r
+#endif\r
+ {\r
+ TRC(fprintf(stderr, " Flow control stop\n"));\r
+ return 1;\r
+ }\r
+ h->cerrno = CSYSERR;\r
+ return -1;\r
+ }\r
+ state->written += res;\r
+ TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",\r
+ res, state->written, size));\r
+ }\r
+ state->towrite = state->written = -1;\r
+ TRC(fprintf(stderr, " Ok\n"));\r
+ return 0;\r
+}\r
+\r
+int tcpip_close(COMSTACK h)\r
+{\r
+ tcpip_state *sp = h->private;\r
+\r
+ TRC(fprintf(stderr, "tcpip_close\n"));\r
+ close(h->iofile);\r
+ if (sp->altbuf)\r
+ free(sp->altbuf);\r
+ free(sp);\r
+ free(h);\r
+ return 0;\r
+}\r
* OF THIS SOFTWARE.
*
* $Log: comstack.h,v $
- * Revision 1.9 1995-09-28 10:12:36 quinn
+ * Revision 1.10 1995-09-29 17:01:48 quinn
+ * More Windows work
+ *
+ * Revision 1.9 1995/09/28 10:12:36 quinn
* Windows-support changes
*
* Revision 1.8 1995/09/27 15:02:46 quinn
#define cs_getproto(handle) ((handle)->protocol)
const char MDF *cs_strerror(COMSTACK h);
+const char *cs_errmsg(int n);
/*
* error management.
* OF THIS SOFTWARE.
*
* $Log: odr.h,v $
- * Revision 1.12 1995-09-27 15:02:48 quinn
+ * Revision 1.13 1995-09-29 17:01:49 quinn
+ * More Windows work
+ *
+ * Revision 1.12 1995/09/27 15:02:48 quinn
* Modified function heads & prototypes.
*
* Revision 1.11 1995/08/15 12:00:09 quinn
unsigned char *base; /* starting point of data */
int base_offset;
int len; /* length of data, if known, else -1
- (decoding only) */
+ (decoding only) */
unsigned char *lenb; /* where to encode length */
int len_offset;
int lenlen; /* length of length-field */
odr_constack stack[ODR_MAX_STACK];
} *ODR;
-typedef int (*Odr_fun)();
+typedef int MDF (*Odr_fun)();
typedef struct odr_arm
{
/*
* Error control.
*/
-#define ONONE 0
-#define OMEMORY 1
-#define OSYSERR 2
-#define OSPACE 3
-#define OREQUIRED 4
-#define OUNEXPECTED 5
-#define OOTHER 6
-#define OPROTO 7
-#define ODATA 8
+#define ONONE 0
+#define OMEMORY 1
+#define OSYSERR 2
+#define OSPACE 3
+#define OREQUIRED 4
+#define OUNEXPECTED 5
+#define OOTHER 6
+#define OPROTO 7
+#define ODATA 8
#define OSTACK 9
#define OCONLEN 10
void MDF odr_release_mem(ODR_MEM p);
#define odr_implicit(o, t, p, cl, tg, opt)\
- (odr_implicit_settag((o), cl, tg), t ((o), (p), opt) )
+ (odr_implicit_settag((o), cl, tg), t ((o), (p), opt) )
#define odr_explicit(o, t, p, cl, tg, opt)\
- ((int) (odr_constructed_begin((o), (p), (cl), (tg)) ? \
- t ((o), (p), (opt)) &&\
- odr_constructed_end(o) : opt))
+ ((int) (odr_constructed_begin((o), (p), (cl), (tg)) ? \
+ t ((o), (p), (opt)) &&\
+ odr_constructed_end(o) : opt))
#define ODR_MASK_ZERO(mask)\
((void) (memset((mask)->bits, 0, ODR_BITMASK_SIZE),\
#define odr_putc(o, c) \
( \
( \
- (o)->ecb.pos < (o)->ecb.size ? \
- ( \
- (o)->ecb.buf[(o)->ecb.pos++] = (c), \
- 0 \
- ) : \
- ( \
- odr_grow_block(&(o)->ecb, 1) == 0 ? \
- ( \
- (o)->ecb.buf[(o)->ecb.pos++] = (c), \
- 0 \
- ) : \
- ( \
- (o)->error = OSPACE, \
- -1 \
- ) \
- ) \
+ (o)->ecb.pos < (o)->ecb.size ? \
+ ( \
+ (o)->ecb.buf[(o)->ecb.pos++] = (c), \
+ 0 \
+ ) : \
+ ( \
+ odr_grow_block(&(o)->ecb, 1) == 0 ? \
+ ( \
+ (o)->ecb.buf[(o)->ecb.pos++] = (c), \
+ 0 \
+ ) : \
+ ( \
+ (o)->error = OSPACE, \
+ -1 \
+ ) \
+ ) \
) == 0 ? \
( \
- (o)->ecb.pos > (o)->ecb.top ? \
- ( \
- (o)->ecb.top = (o)->ecb.pos, \
- 0 \
- ) : \
- 0 \
+ (o)->ecb.pos > (o)->ecb.top ? \
+ ( \
+ (o)->ecb.top = (o)->ecb.pos, \
+ 0 \
+ ) : \
+ 0 \
) : \
- -1 \
+ -1 \
) \
#define odr_tell(o) ((o)->ecb.pos)
int MDF odr_dumpBER(FILE *f, char *buf, int len);
void MDF odr_choice_bias(ODR o, int what);
int MDF odr_total(ODR o);
+char *odr_errmsg(int n);
#endif
#define YCONFIG_H
#ifdef WINDOWS
-#define MDF pascal
+#define MDF
#else
#ifndef MDF
#define MDF
* Sebastian Hammer, Adam Dickmeiss
*
* $Log: ber_int.c,v $
- * Revision 1.9 1995-09-28 10:12:39 quinn
+ * Revision 1.10 1995-09-29 17:01:50 quinn
+ * More Windows work
+ *
+ * Revision 1.9 1995/09/28 10:12:39 quinn
* Windows-support changes
*
* Revision 1.8 1995/09/27 15:02:55 quinn
*
*/
-#include <odr.h>
+
#include <sys/types.h>
+
#ifdef WINDOWS
#include <winsock.h>
#else
#include <netinet/in.h> /* for htons... */
#endif
+
#include <string.h>
-static int ber_encinteger(ODR o, int val);
-static int ber_decinteger(unsigned char *buf, int *val);
+#include <odr.h>
+#include <prt.h>
+
+static int MDF ber_encinteger(ODR o, int val);
+static int MDF ber_decinteger(unsigned char *buf, int *val);
int MDF ber_integer(ODR o, int *val)
{
switch (o->direction)
{
- case ODR_DECODE:
- if ((res = ber_decinteger(o->bp, val)) <= 0)
- {
- o->error = OPROTO;
- return 0;
- }
- o->bp += res;
- o->left -= res;
- return 1;
- case ODR_ENCODE:
- if ((res = ber_encinteger(o, *val)) < 0)
- return 0;
- return 1;
- case ODR_PRINT: return 1;
- default: o->error = OOTHER; return 0;
+ case ODR_DECODE:
+ if ((res = ber_decinteger(o->bp, val)) <= 0)
+ {
+ o->error = OPROTO;
+ return 0;
+ }
+ o->bp += res;
+ o->left -= res;
+ return 1;
+ case ODR_ENCODE:
+ if ((res = ber_encinteger(o, *val)) < 0)
+ return 0;
+ return 1;
+ case ODR_PRINT: return 1;
+ default: o->error = OOTHER; return 0;
}
}
/*
* Returns: number of bytes written or -1 for error (out of bounds).
*/
-int ber_encinteger(ODR o, int val)
+int MDF ber_encinteger(ODR o, int val)
{
int lenpos;
int a, len;
lenpos = odr_tell(o);
if (odr_putc(o, 0) < 0) /* dummy */
- return -1;
-
+ return -1;
tmp.i = htonl(val); /* ensure that that we're big-endian */
for (a = 0; a < sizeof(int) - 1; a++) /* skip superfluous octets */
- if (!((tmp.c[a] == 0 && !(tmp.c[a+1] & 0X80)) ||
- (tmp.c[a] == 0XFF && (tmp.c[a+1] & 0X80))))
- break;
+ if (!((tmp.c[a] == 0 && !(tmp.c[a+1] & 0X80)) ||
+ (tmp.c[a] == 0XFF && (tmp.c[a+1] & 0X80))))
+ break;
len = sizeof(int) - a;
if (odr_write(o, (unsigned char*) tmp.c + a, len) < 0)
- return -1;
+ return -1;
odr_seek(o, ODR_S_SET, lenpos);
if (ber_enclen(o, len, 1, 1) != 1)
- return -1;
+ return -1;
odr_seek(o, ODR_S_END, 0);
#ifdef ODR_DEBUG
fprintf(stderr, "[val=%d]", val);
/*
* Returns: Number of bytes read or 0 if no match, -1 if error.
*/
-int ber_decinteger(unsigned char *buf, int *val)
+int MDF ber_decinteger(unsigned char *buf, int *val)
{
unsigned char *b = buf, fill;
int res, len, remains;
union { int i; unsigned char c[sizeof(int)]; } tmp;
if ((res = ber_declen(b, &len)) < 0)
- return -1;
+ return -1;
if (len > sizeof(int)) /* let's be reasonable, here */
- return -1;
+ return -1;
b+= res;
remains = sizeof(int) - len;
memcpy(tmp.c + remains, b, len);
if (*b & 0X80)
- fill = 0XFF;
+ fill = 0XFF;
else
- fill = 0X00;
+ fill = 0X00;
memset(tmp.c, fill, remains);
*val = ntohl(tmp.i);
* Sebastian Hammer, Adam Dickmeiss
*
* $Log: odr.c,v $
- * Revision 1.16 1995-09-27 15:02:57 quinn
+ * Revision 1.17 1995-09-29 17:01:50 quinn
+ * More Windows work
+ *
+ * Revision 1.16 1995/09/27 15:02:57 quinn
* Modified function heads & prototypes.
*
* Revision 1.15 1995/08/15 12:00:22 quinn
"Length of constructed type different from sum of members"
};
+char *odr_errmsg(int n)
+{
+ return odr_errlist[n];
+}
+
void MDF odr_perror(ODR o, char *message)
{
fprintf(stderr, "%s: %s\n", message, odr_errlist[o->error]);
struct odr *r;
if (!(r = malloc(sizeof(*r))))
- return 0;
+ return 0;
r->direction = direction;
r->print = stderr;
r->buf = 0;
{
odr_release_mem(o->mem);
if (o->ecb.buf && o->ecb.can_grow)
- free(o->ecb.buf);
+ free(o->ecb.buf);
if (o->print != stderr)
- fclose(o->print);
+ fclose(o->print);
free(o);
}
{
*len = o->ecb.top;
if (size)
- *size = o->ecb.size;
+ *size = o->ecb.size;
return (char*) o->ecb.buf;
}
* Sebastian Hammer, Adam Dickmeiss
*
* $Log: odr_int.c,v $
- * Revision 1.8 1995-09-27 15:02:59 quinn
+ * Revision 1.9 1995-09-29 17:01:50 quinn
+ * More Windows work
+ *
+ * Revision 1.8 1995/09/27 15:02:59 quinn
* Modified function heads & prototypes.
*
* Revision 1.7 1995/05/16 08:50:54 quinn
*/
#include <odr.h>
+#include <prt.h>
/*
* Top level integer en/decoder.
int res, cons = 0;
if (o->error)
- return 0;
+ return 0;
if (o->t_class < 0)
{
- o->t_class = ODR_UNIVERSAL;
- o->t_tag = ODR_INTEGER;
+ o->t_class = ODR_UNIVERSAL;
+ o->t_tag = ODR_INTEGER;
}
if ((res = ber_tag(o, p, o->t_class, o->t_tag, &cons, opt)) < 0)
- return 0;
+ return 0;
if (!res)
- return opt;
+ return opt;
if (o->direction == ODR_PRINT)
{
- fprintf(o->print, "%s%d\n", odr_indent(o), **p);
- return 1;
+ fprintf(o->print, "%s%d\n", odr_indent(o), **p);
+ return 1;
}
if (cons)
{
- o->error = OPROTO;
- return 0;
+ o->error = OPROTO;
+ return 0;
}
if (o->direction == ODR_DECODE)
- *p = odr_malloc(o, sizeof(int));
+ *p = odr_malloc(o, sizeof(int));
return ber_integer(o, *p);
}
* Sebastian Hammer, Adam Dickmeiss
*
* $Log: dmalloc.c,v $
- * Revision 1.6 1995-09-27 15:03:02 quinn
+ * Revision 1.7 1995-09-29 17:01:50 quinn
+ * More Windows work
+ *
+ * Revision 1.6 1995/09/27 15:03:02 quinn
* Modified function heads & prototypes.
*
* Revision 1.5 1995/05/16 08:51:10 quinn
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
+#include <yconfig.h>
static const unsigned char head[] = {44, 33, 22, 11};
static const unsigned char tail[] = {11, 22, 33, 44};
int long len;
if (!(res = malloc(nbytes + 3 * sizeof(long))))
- return 0;
+ return 0;
fprintf(stderr, "---d_malloc, '%s':%d, %d->%p\n",
- file, line, nbytes, res + 2 * sizeof(long));
+ file, line, nbytes, res + 2 * sizeof(long));
len = nbytes;
memcpy(res, &head, sizeof(long));
memcpy(res + sizeof(long), &len, sizeof(long));
long len;
if (memcmp(&head, ptr - 2 * sizeof(long), sizeof(long)))
- abort();
+ abort();
memcpy(ptr - 2 * sizeof(long), &freed, sizeof(long));
memcpy(&len, ptr - sizeof(long), sizeof(long));
if (memcmp(ptr + len, &tail, sizeof(long)))
- abort();
+ abort();
fprintf(stderr, "---d_free, '%s':%d, %p (%d)\n",
- file, line, ptr, len);
+ file, line, ptr, len);
free(ptr - 2 * sizeof(long));
return;
}
char *r;
if (memcmp(&head, ptr - 2 * sizeof(long), sizeof(long)))
- abort();
+ abort();
memcpy(&len, ptr - sizeof(long), sizeof(long));
if (memcmp(ptr + len, &tail, sizeof(long)))
- abort();
+ abort();
if (!(r = realloc(ptr - 2 * sizeof(long), nbytes + 3 * sizeof(long))))
- return 0;
+ return 0;
fprintf(stderr, "---d_realloc, '%s':%d, %d->%d, %p->%p\n",
- file, line, len, nbytes, p, r + 2 * sizeof(long));
+ file, line, len, nbytes, p, r + 2 * sizeof(long));
memcpy(r, &head, sizeof(long));
memcpy(r + sizeof(long), &nlen, sizeof(long));
r += 2 * sizeof(long);
* Sebastian Hammer, Adam Dickmeiss
*
* $Log: oid.c,v $
- * Revision 1.4 1995-09-27 15:03:03 quinn
+ * Revision 1.5 1995-09-29 17:01:51 quinn
+ * More Windows work
+ *
+ * Revision 1.4 1995/09/27 15:03:03 quinn
* Modified function heads & prototypes.
*
* Revision 1.3 1995/09/12 11:32:06 quinn
{PROTO_Z3950, CLASS_TAGSET, VAL_GILS, {14,3,-1}, "GILS" },
/* SR definitions. Note that some of them aren't defined by the
- standard (yet), but are borrowed from Z3950v3 */
+ standard (yet), but are borrowed from Z3950v3 */
{PROTO_SR, CLASS_ABSYN, VAL_APDU, {2,1,-1}, "SR-APDU" },
{PROTO_SR, CLASS_APPCTX, VAL_BASIC_CTX, {1,1,-1}, "SR-BASIC" },
{PROTO_SR, CLASS_ATTSET, VAL_BIB1, {3,1,-1}, "Bib-1" },
void MDF oid_oidcat(int *t, int *s)
{
while (*t > -1)
- t++;
+ t++;
while ((*(t++) = *(s++)) > -1);
}
{
while (*o1 == *o2 && *o1 > -1)
{
- o1++;
- o2++;
+ o1++;
+ o2++;
}
if (*o1 == *o2)
- return 0;
+ return 0;
else if (*o1 > *o2)
- return 1;
+ return 1;
else
- return -1;
+ return -1;
}
int MDF oid_oidlen(int *o)
int len = 0;
while (*(o++) >= 0)
- len++;
+ len++;
return len;
}
for (len = 0; *look == *prefix; look++, prefix++, len++);
if (*prefix < 0) /* did we reach the end of the prefix? */
- return len;
+ return len;
return 0;
}
-struct MDF oident *oid_getentbyoid(int *o)
+struct oident MDF *oid_getentbyoid(int *o)
{
enum oid_proto proto;
int prelen;
/* determine protocol type */
if (!o)
- return 0;
+ return 0;
if ((prelen = match_prefix(o, z3950_prefix)))
- proto = PROTO_Z3950;
+ proto = PROTO_Z3950;
else if ((prelen = match_prefix(o, sr_prefix)))
- proto = PROTO_SR;
+ proto = PROTO_SR;
else
- proto = PROTO_GENERAL;
+ proto = PROTO_GENERAL;
for (p = oids; *p->oidsuffix >= 0; p++)
- if (p->proto == proto && !oid_oidcmp(o + prelen, p->oidsuffix))
- return p;
+ if (p->proto == proto && !oid_oidcmp(o + prelen, p->oidsuffix))
+ return p;
return 0;
}
static int ret[OID_SIZE];
for (p = oids; *p->oidsuffix >= 0; p++)
- if (ent->proto == p->proto &&
- ent->class == p->class &&
- ent->value == p->value)
- {
- if (ent->proto == PROTO_Z3950)
- oid_oidcpy(ret, z3950_prefix);
- else if (ent->proto == PROTO_SR)
- oid_oidcpy(ret, sr_prefix);
- else
- ret[0] = -1;
- oid_oidcat(ret, p->oidsuffix);
- return ret;
- }
+ if (ent->proto == p->proto &&
+ ent->class == p->class &&
+ ent->value == p->value)
+ {
+ if (ent->proto == PROTO_Z3950)
+ oid_oidcpy(ret, z3950_prefix);
+ else if (ent->proto == PROTO_SR)
+ oid_oidcpy(ret, sr_prefix);
+ else
+ ret[0] = -1;
+ oid_oidcat(ret, p->oidsuffix);
+ return ret;
+ }
return 0;
}
struct oident *p;
for (p = oids; *p->oidsuffix >= 0; p++)
- if (!strcmp(p->desc, name))
- return p->value;
+ if (!strcmp(p->desc, name))
+ return p->value;
return VAL_NONE;
}