libpktlab - The PacketLab Utility Library
pktctrl.h
Go to the documentation of this file.
1 // pktctrl.h
2 // The pktctrl module for handling the pktlab control session.
3 //
4 
13 #ifndef _PKTCTRL_H_
14 #define _PKTCTRL_H_
15 
16 #include <pktlab.h>
17 
18 #include <stdbool.h>
19 #include <stddef.h>
20 #include <stdint.h>
21 
22 #include <poll.h>
23 #include <sys/select.h>
24 
25 //
26 // EXPORTED CONSTANTS
27 //
28 
39 #define PKTCTRL_SNIKEY_LEN_MIN 1
40 #define PKTCTRL_SNIKEY_LEN_MAX 63 // i.e. the maximum domain name label length
41 
47  PKTCTRL_SUCCESS = 0,
48  PKTCTRL_INVAL = 1,
49  PKTCTRL_MAP_ERR = 2,
50  PKTCTRL_KEY_NOT_FOUND = 3,
51  PKTCTRL_SSL_FAILURE = 4,
52  PKTCTRL_BAD_CERT = 5,
53  PKTCTRL_BAD_CERTKEY = 6,
54  PKTCTRL_IN_PROGRESS = 7,
55  PKTCTRL_UNKNOWN_FAULT = 127,
56 };
57 
58 //
59 // EXPORTED FUNCTION DECLARATIONS
60 //
61 
66 struct pktctrl_obj; // opaque
71 struct pktctrl_keycert_store; // opaque
72 
79 extern struct pktctrl_obj * pktctrl_create_obj(void);
88 
112 extern int pktctrl_set_server_keycert (
113  struct pktctrl_keycert_store * store, const char * snikey,
114  struct pktlab_certificate ** cert_ls, uint_fast32_t certnum,
115  struct pktlab_publickey ** root_ls, uint_fast32_t rootnum);
133 extern int pktctrl_get_server_keycert (
134  struct pktctrl_keycert_store * store, const char * snikey,
135  struct pktlab_certificate *** cert_ls, uint_fast32_t * certnum,
136  struct pktlab_publickey *** root_ls, uint_fast32_t * rootnum);
152 extern int pktctrl_clear_server_keycert (
153  struct pktctrl_keycert_store * store, const char * snikey);
154 
171  struct pktctrl_keycert_store * store, const char * snikey);
183  struct pktctrl_keycert_store * store);
184 
206 extern int pktctrl_set_client_keycert (
207  struct pktctrl_keycert_store * store,
208  struct pktlab_certificate ** cert_ls, uint_fast32_t certnum,
209  struct pktlab_publickey ** root_ls, uint_fast32_t rootnum);
225 extern int pktctrl_get_client_keycert (
226  struct pktctrl_keycert_store * store,
227  struct pktlab_certificate *** cert_ls, uint_fast32_t * certnum,
228  struct pktlab_publickey *** root_ls, uint_fast32_t * rootnum);
241 extern int pktctrl_clear_client_keycert(struct pktctrl_keycert_store * store);
242 
243 // normal pktctrl session setup functions
244 // uses TLS while incorporating pktlab access control checks
270 extern int pktctrl_accept (
271  int fd, int auth_mode,
272  struct pktlab_privatekey * serverkey,
273  struct pktctrl_keycert_store * store,
274  struct pktctrl_obj * ctrlobj, char ** client_snikey,
275  struct pktlab_certificate *** peer_cert_ls,
276  uint_fast32_t * peer_cert_num);
303 extern int pktctrl_connect (
304  int fd, int auth_mode, const char * snikey,
305  struct pktlab_privatekey * clientkey,
306  struct pktctrl_keycert_store * store, struct pktctrl_obj * ctrlobj,
307  struct pktlab_certificate *** peer_cert_ls, uint_fast32_t * peer_cert_num);
308 
309 // special no setup alternative
310 // uses bare-bone TCP connection with no access control checks conducted
325 extern int pktctrl_raw_session (
326  int fd, struct pktctrl_obj * ctrlobj);
327 
345 extern int pktctrl_prepare_select (
346  int * nfds, fd_set * restrict rset, fd_set * restrict wset,
347  bool want_read, bool want_write, struct pktctrl_obj * ctrlobj);
363 extern int pktctrl_process_select (
364  const fd_set * restrict rset, const fd_set * restrict wset,
365  struct pktctrl_obj * ctrlobj, bool * readable, bool * writable);
366 
367 extern int pktctrl_prepare_poll (
368  struct pollfd * restrict pfd,
369  bool want_read, bool want_write,
370  struct pktctrl_obj * ctrlobj);
371 extern int pktctrl_process_poll (
372  const struct pollfd * restrict pfd,
373  struct pktctrl_obj * ctrlobj,
374  bool * readable, bool * writable, bool * err);
375 
384 extern int pktctrl_read_message (
385  struct pktctrl_obj * ctrlobj,
386  struct pktlab_message ** msgptr);
395 extern int pktctrl_write_message (
396  struct pktctrl_obj * ctrlobj,
397  const struct pktlab_message * msg);
405 extern int pktctrl_flush_write(struct pktctrl_obj * ctrlobj);
413 extern size_t pktctrl_write_unsent(const struct pktctrl_obj * ctrlobj);
414 
415 // for non-buffered raw bytes reading
416 // should not be used after using pktctrl_read_message/pktctrl_write_message
427 extern ssize_t pktctrl_read (
428  struct pktctrl_obj * obj,
429  void * buf, size_t nbyte);
440 extern ssize_t pktctrl_write (
441  struct pktctrl_obj * obj,
442  const void * buf, size_t nbyte);
443 
455 extern int pktctrl_close(struct pktctrl_obj * obj);
466 extern int pktctrl_cleanup_keycert_store(struct pktctrl_keycert_store * store);
467 
473 extern bool pktctrl_is_established(struct pktctrl_obj * obj);
474 
482 extern bool pktctrl_is_valid_snikey(const char * snikey);
493 extern int pktctrl_gen_snikey(char ** buf);
494 
495 #endif
pktctrl_read
ssize_t pktctrl_read(struct pktctrl_obj *obj, void *buf, size_t nbyte)
Read bytes from established pktctrl session.
pktctrl_keycert_store
pktctrl key certificate storage opaque struct.
Definition: pktctrl.h:66
pktctrl_clear_server_keycert_default
int pktctrl_clear_server_keycert_default(struct pktctrl_keycert_store *store)
Clear server pktctrl key certificate storage default entry.
pktlab_publickey
pktlab public key opaque struct.
pktctrl_obj
pktctrl session object opaque struct.
pktctrl_err
pktctrl_err
pktctrl module defined error values.
Definition: pktctrl.h:46
pktctrl_connect
int pktctrl_connect(int fd, int auth_mode, const char *snikey, struct pktlab_privatekey *clientkey, struct pktctrl_keycert_store *store, struct pktctrl_obj *ctrlobj, struct pktlab_certificate ***peer_cert_ls, uint_fast32_t *peer_cert_num)
Perform TLS client-side handshake to establish TLS pktctrl session.
pktctrl_set_server_keycert_default
int pktctrl_set_server_keycert_default(struct pktctrl_keycert_store *store, const char *snikey)
Set server pktctrl key certificate storage default entry.
pktctrl_write_unsent
size_t pktctrl_write_unsent(const struct pktctrl_obj *ctrlobj)
Get established pktctrl session internal writer buffered content length.
pktctrl_is_valid_snikey
bool pktctrl_is_valid_snikey(const char *snikey)
Check if '\0'-terminated string is a valid snikey.
pktctrl_get_client_keycert
int pktctrl_get_client_keycert(struct pktctrl_keycert_store *store, struct pktlab_certificate ***cert_ls, uint_fast32_t *certnum, struct pktlab_publickey ***root_ls, uint_fast32_t *rootnum)
Get loaded client certificate chain and trusted roots from client pktctrl key certificate storage str...
pktctrl_create_obj
struct pktctrl_obj * pktctrl_create_obj(void)
Get initialized pktctrl session object struct.
pktctrl_clear_client_keycert
int pktctrl_clear_client_keycert(struct pktctrl_keycert_store *store)
Delete client certificate chain and trusted roots from client pktctrl key certificate storage struct.
pktctrl_cleanup_keycert_store
int pktctrl_cleanup_keycert_store(struct pktctrl_keycert_store *store)
Free an allocated pktctrl key certificate storage struct.
pktctrl_read_message
int pktctrl_read_message(struct pktctrl_obj *ctrlobj, struct pktlab_message **msgptr)
Read pktctrl message from established pktctrl session.
pktlab.h
General utilities for pktlab.
pktctrl_gen_snikey
int pktctrl_gen_snikey(char **buf)
Generate random '\0'-terminated valid snikey of PKTCTRL_SNIKEY_LEN_MAX length.
pktlab_privatekey
pktlab private key opaque struct.
pktctrl_set_server_keycert
int pktctrl_set_server_keycert(struct pktctrl_keycert_store *store, const char *snikey, struct pktlab_certificate **cert_ls, uint_fast32_t certnum, struct pktlab_publickey **root_ls, uint_fast32_t rootnum)
Load server certificate chain and trusted roots for some snikey into pktctrl key certificate storage ...
pktctrl_write
ssize_t pktctrl_write(struct pktctrl_obj *obj, const void *buf, size_t nbyte)
Write bytes to established pktctrl session.
pktctrl_is_established
bool pktctrl_is_established(struct pktctrl_obj *obj)
Check if a pktctrl session object is established.
pktlab_message
Structure for storing the decoded form of pktlab messages.
Definition: pktlab.h:930
pktctrl_create_keycert_store
struct pktctrl_keycert_store * pktctrl_create_keycert_store(void)
Get initialized pktctrl key certificate storage struct.
pktlab_certificate
pktlab certificate opaque struct.
pktctrl_set_client_keycert
int pktctrl_set_client_keycert(struct pktctrl_keycert_store *store, struct pktlab_certificate **cert_ls, uint_fast32_t certnum, struct pktlab_publickey **root_ls, uint_fast32_t rootnum)
Load client certificate chain and trusted roots into pktctrl key certificate storage struct.
pktctrl_close
int pktctrl_close(struct pktctrl_obj *obj)
Close and free an allocated pktctrl session object struct.
pktctrl_write_message
int pktctrl_write_message(struct pktctrl_obj *ctrlobj, const struct pktlab_message *msg)
Write pktctrl message to established pktctrl session.
pktctrl_flush_write
int pktctrl_flush_write(struct pktctrl_obj *ctrlobj)
Flush established pktctrl session internal writer buffer.
pktctrl_prepare_select
int pktctrl_prepare_select(int *nfds, fd_set *restrict rset, fd_set *restrict wset, bool want_read, bool want_write, struct pktctrl_obj *ctrlobj)
Set up arguments to select() for read/write operation on established pktctrl session based on intenti...
pktctrl_process_select
int pktctrl_process_select(const fd_set *restrict rset, const fd_set *restrict wset, struct pktctrl_obj *ctrlobj, bool *readable, bool *writable)
Process select() results to identify if read/write operation on established pktctrl session can be pe...
pktctrl_accept
int pktctrl_accept(int fd, int auth_mode, struct pktlab_privatekey *serverkey, struct pktctrl_keycert_store *store, struct pktctrl_obj *ctrlobj, char **client_snikey, struct pktlab_certificate ***peer_cert_ls, uint_fast32_t *peer_cert_num)
Perform TLS server-side handshake to establish TLS pktctrl session.
pktctrl_raw_session
int pktctrl_raw_session(int fd, struct pktctrl_obj *ctrlobj)
Set up pktctrl session object struct to establish barebone TCP pktctrl session.
pktctrl_get_server_keycert
int pktctrl_get_server_keycert(struct pktctrl_keycert_store *store, const char *snikey, struct pktlab_certificate ***cert_ls, uint_fast32_t *certnum, struct pktlab_publickey ***root_ls, uint_fast32_t *rootnum)
Get loaded server certificate chain and trusted roots for some snikey from server pktctrl key certifi...
pktctrl_clear_server_keycert
int pktctrl_clear_server_keycert(struct pktctrl_keycert_store *store, const char *snikey)
Delete server certificate chain and trusted roots entry for some snikey from server pktctrl key certi...