The SDClient API lets an external application connect to SD, open files, read and write records, execute commands, call subroutines, and manage select lists — all through a single shared library.
The API is a normal way to use SD, not a facility reserved for developers
and administrators. A person running a custom GUI program that talks to SD needs
API access and may need nothing else. On SD Core Solo for Linux the API is off
unless you chose it when installing (a computer a server manages needs it open), and it signs
in to the one account, sduser, with the account password — see API access in
the GettingStarted set.
The library#
The client library is written new for SD Core (a clean-room library under the
Blue Oak Model License 1.0.0), so an application of any licence may link it,
and it is the same library for every SD Core product. It is delivered as
binaries in the release package's clients folder; its source is not
published. The same library has two names:
sdclilib.so |
what existing applications ask for |
libsdcli.so |
the ordinary Unix lib*.so convention (-lsdcli at link time) |
The package holds two builds of it: clients/Linux64 for OpenSSL 3 and
clients/Linux64-openssl4 for OpenSSL 4.
There is no 32-bit client, for Linux or for Windows: a 32-bit application must be built again as a 64-bit one. A program written for QM does not work with SD out of the box, because of the changes SD has made.
Where it is, and how to use it#
The installer puts it in ~/SDCoreSolo/bin when it is run from the release
package, taking the build that matches the OpenSSL the server uses. An install
made from a copy of the source has none, because the source does not contain
one; copy the files from a release package's clients folder. Link against it (-lsdcli) or load it at
runtime (dlopen("sdclilib.so", ...)) the ordinary way for a Linux shared
library — there is no PATH-order concern the way a Windows DLL search has,
because Linux resolves a shared library by its own rules (rpath,
LD_LIBRARY_PATH, or the standard library paths), not by which directory
happens to be searched first.
The header, sdclilib.h, is installed in ~/SDCoreSolo/syscom with the
other language headers (see SD Client API: Language Bindings).
Connection#
SDConnect(host, port, user, pass, account) |
over the network, to port 4249. Use 127.0.0.1 for this computer; user is sduser. Always give the port: the library is the same one SD Core for Linux uses, and with no port it tries SD Core's, 4247 |
SDConnectLocal(account) |
not available on Solo. Returns false |
SDConnectUDS(Unix Domain Socket) is not available, on either port. It sent its credential over the socket in a way that predates SCRAM, and was removed for the same reason the old cleartext API login was.
Why SDConnectLocal is not available#
SDConnectLocal sends no password, and every SD Core Solo for Linux session
proves the account password, so the Solo server refuses a local session. The
client library is the one SD Core ships: its SDConnectLocal starts SD Core's
server, which it finds from /etc/sd.conf (or $SD_CONFIG). A computer with
only SD Core Solo has neither, so the call returns false and SDError() says
why. On a computer that also has SD Core, SDConnectLocal reaches SD Core,
not Solo - connect to Solo with SDConnect, port 4249 and the account
password.
The login is SCRAM-SHA-256. A client that sends a password in clear is refused. The server sets a puzzle only someone who knows the password can answer, and the password itself is never sent in any form. The server also proves itself to the client — another program that grabbed the port before SD started cannot pretend to be SD.
A session is confined to its own account on the multiuser product: an API session can open everything inside its own account and the shipped SDSYS files every account needs, but cannot open, rename, delete or list anything else. Do not rely on that confinement on Solo: treat an API session as having the reach of the Solo user — see API access in the GettingStarted set.
How big a request may be, and what the server does with one it cannot accept#
Every request starts with a 4-byte length that counts the whole packet, itself included, and a 2-byte request code, so the smallest packet is 6 bytes. The client library never sends one that breaks the rules below, so a program using it does not meet them; they matter to a program that speaks the protocol itself, and to anyone probing the port.
| When | The length of a packet may be |
|---|---|
before the client has logged in (the server has not yet entered the account; on Solo it is never running as root) |
6 to 65,536 bytes. The login messages are all under 4 KB |
| after the login | 6 to 1,073,741,832 bytes: the largest SD string (1,073,741,822) plus the 10 bytes of header the client library allows for, the number SD_MAX_PACKET_BYTES in the library |
A packet that declares anything else (below 6, negative, or over the limit) is not read: the server ends the session at once and writes one line to the system log (API packet length ... refused (before login) or (after login)). The same test applies to the reply a client sends to an INPUT prompt. A client that closes its connection, however early, also ends its session at once, and so does one that sends four bytes that read as a huge length: neither leaves the server process running at 100% of a processor.
Server status codes#
| Constant | Value | Meaning |
|---|---|---|
SV_OK |
0 | success |
SV_ON_ERROR |
1 | an ON ERROR clause fired |
SV_ELSE |
2 | an ELSE clause fired |
SV_ERROR |
3 | an error occurred |
SV_LOCKED |
4 | the record is locked by another session |
SV_PROMPT |
5 | the command issued a prompt and is waiting for input |
Function reference#
Connection management#
| Function | Returns | Description |
|---|---|---|
SDConnect(host, port, user, pass, account) |
Boolean | connect over TCP. A connection that fails returns false and SDError() says why; the session number it was given is the current one afterwards, so select the session you were using with SDSetSession before going on |
SDConnectLocal(account) |
Boolean | not available on Solo — false (see Why SDConnectLocal is not available) |
SDConnected() |
Boolean | is a session active |
SDDisconnect() |
none | end the current session |
SDDisconnectAll() |
none | end all sessions |
SDGetSession() |
Integer | get the current session number |
SDSetSession(session) |
Boolean | switch to a session |
SDLogto(account) |
Boolean | switch to another account: true if the server moved there, false (and SDError() says why) if not, in which case the session stays where it was. Solo has one account, so there is nowhere else to go |
Sessions. One program can hold up to 1024 sessions at once, each with its own server process and number. The server's own user table (USERPOOL) is the real ceiling. Open more than one with SDConnect, one after another, noting SDGetSession() each time, and move between them with SDSetSession.
Threads. The client library may be used from several threads of one process, as OpenQM's QMClient may, on these rules:
- The current session belongs to the thread.
SDSetSessionandSDGetSessionact on the calling thread's own selection, so two threads can work on two sessions at the same time. A thread that has not selected a session starts on session 0, which is the one the first connection of the process received; a thread given a session by another thread mustSDSetSessionit before use. - A session must not be used by two threads at the same time. The library does not serialize requests on one session. If more than one thread can reach it, take your own lock round each action, and treat an
SDExecuteand theSDRespondcalls that answer it as one action. - The table of sessions is shared, so
SDConnect,SDDisconnectandSDDisconnectAllmay be called from any thread; each connect is given a slot of its own however many start at the same moment. - The transfer buffer and the
SDCallx/SDGetArgarguments are per thread, so one thread's call does not overwrite another's results. They are released when the thread ends, or when no session is left. - A connection that fails keeps its slot for the thread that made it, so that
SDError()still says why after another thread has connected and disconnected; the slot is given back by that thread's nextSDConnect,SDSetSessionorSDDisconnect, by its end, or bySDDisconnectAll. - The same applies to a session that dies under its thread (
SDConnected()is false for it andSDError()says why).
A session whose server has ended — its AUTOLOGOUT period ran out, it was logged out, the process was killed — does not hang the client: the next request on it comes back empty, SDError() says the connection failed, and SDDisconnect() cleans it up. A session can set its own limit by executing the AUTOLOGOUT command (SDExecute("AUTOLOGOUT 10"): ten minutes idle; the period is in minutes, 0 turns it off).
File operations#
| Function | Returns | Description |
|---|---|---|
SDOpen(filename) |
Integer (file number) | open a file |
SDClose(fileNo) |
none | close a file |
SDRead(fileNo, id, err) |
String | read a record |
SDReadl(fileNo, id, wait, err) |
String | read with a shared lock |
SDReadu(fileNo, id, wait, err) |
String | read with an update lock |
SDWrite(fileNo, id, data) |
none | write a record |
SDWriteu(fileNo, id, data) |
none | write with an update lock |
SDDelete(fileNo, id) |
none | delete a record |
SDDeleteu(fileNo, id) |
none | delete with an update lock |
SDRecordlock(fileNo, id, updateLock, wait) |
none | set a record lock |
SDRecordlocked(fileNo, id) |
Integer | query the lock on a record, as recordlocked() does: -3 another user holds a file lock, -2 an update lock, -1 a read lock; 0 not locked; 1 this session holds a read lock, 2 an update lock, 3 a file lock. A file number that is not open gives 0 and SDError() says so |
SDRelease(fileNo, id) |
none | release a record lock |
SDMarkMapping(fileNo, state) |
none | enable/disable mark mapping |
SDTrans(file, id, field, action) |
String | trans() on the server. field is a dictionary name, a field number or -1 for the whole record; id may be several ids separated by value marks; action is X (null if the record is missing, the default) or C (the id). The marks of the answer are lowered one level, as trans() lowers them. The B (binary open) action is refused |
SDRTrans(file, id, field, action) |
String | the same with the marks left as they are |
SDTrans and SDRTrans open the file and its dictionary on the server, so a session that the administrator has restricted with SDCLIENT is refused them (message 5262, "File access is disabled").
On Solo only a session signed in with the global password may send the requests that administer a file (create or delete a file or an index, change a file's configuration; see SDNet - Files on Another SD Server): one signed in with the account password is refused with This session cannot administer files here: sign in with the global password, and a Solo with no global password (the delivered state) allows none of them.
Record manipulation#
| Function | Returns | Description |
|---|---|---|
SDExtract(src, fno, vno, svno) |
String | extract a field, value or subvalue |
SDIns(src, fno, vno, svno, newdata) |
String | insert into a dynamic array |
SDDel(src, fno, vno, svno) |
String | delete from a dynamic array |
SDReplace(src, fno, vno, svno, newdata) |
String | replace a field, value or subvalue |
SDLocate(item, src, fno, vno, svno, pos, order) |
Boolean | find a position for ordered insert |
String functions#
| Function | Returns | Description |
|---|---|---|
SDField(str, delim, first, occurrences) |
String | extract a field from a delimited string |
SDDcount(src, delim) |
Long | the number of delimited items: 1 more than the number of times delim occurs, counting non-overlapping occurrences from the left; 0 for a null string or a null delimiter. The delimiter may be more than one character (a::b::c by :: is 3) |
SDChange(str, old, new, occurrences, start) |
String | replace substrings |
SDMatch(src, template) |
Boolean | match a string against a pattern |
SDMatchfield(src, template, component) |
String | extract a matched component |
SDSubstr(...) |
String | substring (language-specific) |
SDChecksum(data) |
Long | the number SD's own checksum() gives for the same string, worked out in the library with no connection to SD. A byte of 128 or more is taken as a signed character, as the server reads it. A check for accidental damage, not a hash for security (the message digests are the server's digest()); the null string is 0 |
Command execution#
| Function | Returns | Description |
|---|---|---|
SDExecute(cmnd, err) |
String | execute a TCL command |
SDRespond(response, err) |
String | respond to a prompt from SDExecute |
SDEndCommand() |
none | end a multi-line command |
SDCall(name, argcount, ...) |
none | call a cataloged subroutine; the arguments the subroutine changed are put back into yours |
SDCallx(name, argcount, ...) |
none | the same call, but your arguments are not changed: read each one the subroutine changed with SDGetArg(n) afterwards |
SDGetArg(argNo) |
String | argument n (1 to 20) of the last SDCallx; free it with SDFree |
SDCallandSDCallxare variadic: 0 to 20 arguments. The PureBasic header provides per-arity wrappers —SDCall0throughSDCall20, andSDCallx0throughSDCallx20.
Continued in#
SD Client API: Select Lists to Error Handling — the rest of the function reference.