.TH gensio 5 01/02/19 "Specifying a gensio" .SH NAME gensio \- How to specify a gensio .SH SYNOPSIS .B [(options)][,gensio|terminaloptions] .SH DESCRIPTION .BR gensio stands for GENeral Stream Input Output. It provides an abstraction for all kinds of stream I/O, and even makes some packet I/O look like stream I/O (like UDP). In particular, gensio makes it easy to create encrypted and authenticated connections. The .BR gensio library specifies a connection (gensio) using a string format. This consists of a gensio type, optional options in parenthesis. For a terminal gensio (one that is at the bottom of the stack), it may take more options separated by a comma. For filter gensios (ones not on the bottom of the stack) another gensio must be specified after the comma. For instance: .IP serialdev,/dev/ttyS0 .PP specifies a serial port gensio. Or: .IP tcp(readbuf=100),localhost,4000 .PP specifies a TCP connection with a 100 byte read buffer, to connect to port 4000 on localhost. Or: .IP telnet,tcp,localhost,4000 .PP specifies a telnet filter on top of a TCP connection. When specifying a gensio, you can add quotes (single or double) to remove the special meaning for some characters, so you can have commas in options and such. They may also be escaped with a "\\". For instance, if you are specifying a laddr in an sctp address, you need to do this. The following address: .IP sctp(laddr=localhost,4001),localhost,3023 .PP will result in a failure because the option splitting code will split at the commas. Instead, do: .IP sctp(laddr="localhost,4001"),localhost,3023 .PP and it will work. Accepter gensios, gensios used for accepting connections, as opposed to connecting gensios, are specified in the same way. Each individual type can vary, and some gensios are only connecting gensios. The options can vary from the accepter and connecting gensios of the same type. For instance, an accepting TCP gensio does not have to have a hostname, but a connecting one does. When an accepter gensio receives a connection, it will create an accepted gensio. This works mostly like a connecting gensio, except some functions may be limited. You may not be able to close and then open an accepted gensio. The gensio library has a concept of client and server. The accepted gensios are generally considered servers. Connecting gensios are generally considered clients. Some gensios may allow this to be overridden. A gensio may be reliable or not. A reliable gensio will reliably deliver all data in sequence, like TCP. An gensio that is not reliable may drop data or deliver data out of sequence, like UDP. This can be queried with .B gensio_is_reliable(). A gensio may be packet or not. A packet gensio will exactly match up writes and reads. So if you write 15 bytes into one side, a 15 byte read for that data will appear on the other side. A gensio that is not packet will not respect write boundaries, that 15 byte write may result in multiple reads or it may be combined with another write into more than 15 bytes. Packet I/O requires careful use to make it work correctly. Particularly, you must set the max read and write size to the same value, and you must accept all data on receipt and write complete packets all at once. A gensio may be message oriented. This implementation is stolen from SCTP (even though it's not really supported on Linux at the moment). It basically means you can explicitly mark message boundaries when sending data, and that explicit mark will be set on the read side. You do this by adding an "eom" auxdata on the write; the end of that write it assumed to be the end of a message. If the write does not accept all the data, the "eom" is ignored, you must write the remaning data again with "eom" set. You may also do partial write of messages and set "eom" at the end. On the receive side, "eom" will be set when the end of a message is delivered. The data delivered in the receive callback will be only the data for that message. If the user does not accept all the data, the data left in the message is again presented to the user with "eom" set. The options vary greatly between the different gensios. Each gensio type will be covered in a separate section. Also note that gensio types can be dynamically added by the user, so there may be gensios available that are not described here. Unless othersize noted, every gensio takes a: .TP .B readbuf= option to specify the read buffer size. .SH "DEFAULTS" Every option to a gensio (including the serialdev and ipmisol options), unless othersize stated, is available as a default for the gensios. You can use gensio_set_default() to set the default value used by all gensios allocated after that point. If you leave the class NULL, it will set the base default, which will affect all gensios unless they have an override. If you set the class, it will only affect gensios with that class name. Be very careful with some defaults. Setting "mode" default, for instance, could really screw things up. For string defaults, setting the default value to NULL causes the gensio to use it's backup default. .SH "Serial gensios" Some gensio types support serial port setting options. Standard serial ports, IPMI Serial Over LAN, and telnet with RFC2217 enabled. A client serial gensio can set and get serial port options using the .B sergensio_xxx() functions. Server serial gensios receive requests from the client via .I GENSIO_EVENT_SER_xxx events in the callback. .SH "Streams and Channels" Some gensios support the concept of a stream and/or a channel. A stream is delivered as part of the normal data stream of a gensio. The "default" stream will be treated normally. All other streams will have "stream=" given in the auxdata to specify which stream to write on or which stream was read from. Streams cannot be individually flow controlled. A channel is a flow of data like a stream, but it can be individually flow controlled. It appears as a new gensio in the .I GENSIO\_EVENT\_NEW\_CHANNEL callback. You can create a channel with .B gensio\_alloc\_channel() and then open it with .B gensio\_open(). Once open, a channel works like a normal gensio. If you close the main channel for a gensio, the other channels will stay open; the resources for the main channel will still be kept around until all channels are closed. See the indvidual gensio description for more information on streams and channels. .SH "PUBLIC KEY CRYPTOGRAPHY" The ssl and certauth gensios use public key cryptography. This section gives a little overview of how that works. You can safely skip this section if you already understand these concepts. Public key cryptography is used to authenticate and encrypt information at the beginning of a session. It is a fairly expensive operation and is not generally used to encrypt information after the beginning. In public key cryptography, you have three basic things: A private key, a certificate (or public key), and a certificate authority (CA). The private key is just that: private. You don't even send your private key to a certificate authority for signing of your certificate. Keep it private, non-readable by everyone else. Protect it, if it becomes known your certificate becomes useless to you, anyone can impersonate you. The certificate is the public key, and is mathematically associated with a single private key. It's just that, public, anyone can use it to test that you have the private key by asking you to sign some data. The data in the certificate (like the Common Name) is part of the certificate. If that data is modified, the certificate validation will fail. The CA is generally a trusted third-party that validates you and signs your certificate (generally for a fee). CAs issue their own public certificates that are well-known and generally available on your system. The CA certificates are used to prove that your certificate is valid. .SS "SIGNING" The process if signing has been mentioned already, but not described. Basically, you use your private key to generate a value over some given data that proves you have the private key. The certificate is ised to mathematically verify the signature. Two things are normally done with this: In a public key exchange, the entity wishing to be authorized sends a certificate. The authorizing entity will look through it's CA for a certificate that has signed the sent certificate. If the authorizing entity finds a certificate that can be used to validate the sent certificate, the sent certificate is valid. After that, the authorizing entity sends some generally random data to the other entity. The other entity will take that data, perhaps some other important data that it want to make sure is not modified in the transfer, and signs that data. It sends the signature back to the authorizing entity. The authorizing entity can then use the data and the signature to validate that the sending entity has the private key associated with the certificate. This is basically how https works. Note it is the .B web client that authenticates the .B web server, not the other way around. This proves that you are connecting to the entity you say you are connecting to. The authentication of a web client to the web server is generally done via a different mechanism (though SSL/TLS used by the ssl gensio has a way to do it, it is not generally used for that purpose). In the web server scenario, data in the certificate (specifically the Common Name and Subject Alternate Name) must match the name of the web page to which you are connecting. The ssl and certauth gensios do not do this authentication, that is up to the user if it is necessary. .SS "ENCRYPTING" The certificate can be used to encrypt data that can only be decrypted with the private key. When establishing an encrypted connection, this is generally used to transfer a symmetric cryptography key from one entity to another (the authorizing entity to the requesting entity in the case above). You could encrypt all the data with the public key, but that is very expensive and in our example above would require certificates and private keys on both ends. .SS "SELF-SIGNED CERTIFICATES" It is possible to create a certificate that can act as its own certificate authority. This is how ssh works. You create a public and private key. You put the public key in the .ssh/authorized_keys directory on systems where you want to log in. The certificate acts as both the public key (as part of the initial transfer) and the CA (in the authorized_key directory) for authorizing you use of the system you are logging in to. ssh also stores places it has connected to in .ssh/known_hosts, which is the CA in the opposite direction. This is why it asks you if you have never connected to a system before, it doesn't have the key in its CA. Or why, if you connect to a system you have connected to before and the certificates don't match or fail validation, it complains about it. So if you are using self-signed certificates, you need to be careful to put only ones you trust in the CA. This is obviously not possible in a large system like the world wide web, thus the creation of third-party trusted CAs. .SS "TRUST AND CRYPTOGRAPHY" The above discussions mention trust several times. Cryptography does not remove the need for trust. It just makes trust more convenient. If someone sends you a certificate, you need to validate that it was actually them that sent you the certificate, and that it was not modified in transit. If you add that certificate to your CA, you are trusting the certificate, and you better make sure (with fingerprints, generally, see the openssl docs for details) that it came from a trusted entity. The hardest part of cryptography in general is key management. Breaking cryptographic algorithms is really hard. Getting people to divulge private keys or use the wrong keys is a lot easier. For more on cryptography in general, and cryptography and trust, Bruce Schneier has some excellent books on the subject. .SH "IPv6, IPV4, and host names" Note that a single hostname may result in more than one address. For instance, it may have both an IPv4 and IPv6 address. These are treated just like multiple hostnames. The hostname may be prefixed with ipv4 or ipv6, which will force the connections to those protocols. Specifying ipv6n4 will create a socket that is IPv6 but will handle IPv4 connections. This is the default. In general IPv6 addresses are preferred if both are available. If you do not specify a hostname on an accepting gensio (like sctp,1234) it will only create an IPv6 socket that is IPv4 mapped. Even though getaddrinfo would normally return two addresses, only the IPv6 one is used unless there are no IPv6 addresses configured where it will return an IPv4 address. In general, for connecting gensios only the first address that is found will be used. SCTP is the exception, it will do multi-homing on all the addresses that come up. Do you may need to be fairly specific with addresses. .SH "TCP" .B tcp[()][,],[[,],[...]] .br .B hostname = [ipv4|ipv6|ipv6n4,] A TCP connecting gensio must have the hostname specified. Mulitiple hostname/port pairs may be specified. For a connecting TCP gensio, each one will be tried in sequence until a connection is established. For acceptor gensios, every specified hostname/port pair will be listened to. .SS Dynamic Ports For accepters, if the port is specified as zero, a random port in the dynamic port range specified by IANA will be chosen. If more than one address is present, the same port will be chosen for all addresses. You can fetch the port using the gensio_acc_control() function with the option GENSIO_ACC_CONTROL_LPORT. .SS Out Of Band Data TCP supports out of band (oob) data, which is data that will be delivered out of order as soon as possible. This comes in a normal read, but with "oob" in the auxdata. You can send oob data by adding "oob" to the write auxdata, but oob writes are limited to 1 byte and writing any more than this results in undefined behavior. Note that "oobtcp" is also delivered and accepted in auxdata, so you can tell TCP oob data from other oob data. .SS Options In addition to readbuf, the tcp gensio takes the following options: .TP .B nodelay[=true|false] Sets nodelay on the socket. .TP .B laddr= An address specification to bind to on the local socket to set the local address. .TP .B reuseaddr[=true|false] Set SO_REUSEADDR on the socket, good for accepting gensios only. Defaults to true. .TP .B tcpdname= Accepter only, sets the name to use for tcpd access control. This defaults to "gensio", and the default can be overriden with gensio_set_progname(). This option allows you to override it on a per-gensio accepter basis. .SS Remote Address String The remote address will be in the format "[ipv4|ipv6],," where the address is in numeric format, IPv4, or IPv6. .SS Remote Address TCP returns a standard struct sockaddr for GENSIO_CONTROL_RADDR_BIN control. .SH "UDP" .B udp[()][,],[[,],[...]] .br .B hostname = [ipv4|ipv6|ipv6n4,] A UDP gensio creates a UDP socket, but it makes it look like an unrealiable stream of data. The specification is the same as a TCP socket, except that a UDP socket is created, obviously. The semantics of a UDP socket are a little bit strange. A connecting UDP socket is fairly straightforward, it opens a local socket and sends data to the remote socket. An accepter gensio is not so straightforward. The accepter gensio will create a new accepted gensio for any packet it receives from a new remote host. If you disable read on any of the accepted gensio or disable accepts on the accepting gensio, it will stop all reads on all gensios associated with that accepting gensio. Note that UDP accepter gensios are not really required for using UDP, the are primarily there for handling ser2net accepter semantics. You can create two connecting UDP gensios and communicate between them. UDP gensios are not reliable, but are obviously packet-oriented. Port 0 is supported just like TCP for accepters, see Dynamic Ports in the TCP section above for details. The destination address defaults to the one specified on the gensio specifier (for connecting gensios) or the remote address that initiated the connection (for accepting gensios), but may be overridden using "addr:" in the write auxdata. .SS Options In addition to readbuf, the udp gensio takes the following options: .TP .B laddr= An address specification to bind to on the local socket to set the local address. .TP .B nocon[=true|false] Don't be connection oriented, just receive all packets and deliver them without establishing connections. Only valid for the client gensio. The receive address is passed into the auxdata prefixed by "addr:", this is the address formatted by gensio_addr_to_str(). .TP .B mloop[=true|false] If false, multicast packets transmitted will not be received on the local host. If true, they will. .TP .B mcast= Add an address to receive multicast packets on. There is no port number, this is just addresses. You can specify multiple addresses in a single multicast option and/or the multicast option can be used multiple times to add multiple multicast addresses. .TP .B reuseaddr[=true|false] Set SO_REUSEADDR on the socket, good for connecting and accepting gensios. Defaults to false. .SS "Remote Address String" The remote address will be in the format "[ipv4|ipv6],," where the address is in numeric format, IPv4, or IPv6. .SS "Remote Address" UDP returns a standard struct sockaddr for GENSIO_CONTROL_RADDR_BIN control. .SS "UDP Multicast" Multicast can be used on UDP gensios with the .B nocon, maddr and .B laddr options. To set up a multicast, create a client UDP gensio and set the laddr for the receive port and the destination address to the multicast and enable nocon, like: .IP "udp(mcast='ff02::1',laddr='ipv6,3000',nocon),ff02::1,3000" .PP and you will receive and send data on the multicast address. The .B laddr option is required to set the port to receive on. It means you will have a local address, too, and will receive packets on that, too. .SH "SCTP" .B sctp[()][,],[[,],[...]] .br .B hostname = [ipv4|ipv6|ipv6n4,] An SCTP gensio is specified like a UDP or TCP one. However, the semantics are different. For a connecting gensio, it will attempt to create a multi-homed connect with all the specified hostnames and ports. All the ports must be the same. For an accepter gensio, it will create a single socket with all the specified addresses as possible destinations. Again, all the ports must be the same. SCTP gensios are reliable. They are not, at the moment, packet oriented. There is currently no support of SCTP_EXPLICIT_EOR in the Linux implementation of SCTP, and without that it would be hard to make it packet oriented. When specifying IPv6 addresses that might map to IPv4, you must be careful. If one side can do IPv4 and the other side can only do IPv6, the connection may come up, but will disconnect quickly because it cannot communicate on the IPv4 side. For instance, the following accepter: .IP tools/gensiot -a "sctp,ipv6,::,1234" .PP and the following connector: .IP tools/gensiot "sctp,::1,1234" .PP will fail this way because the connector will support IPv4 add but the accepter will not. .SS Nagle and SCTP SCTP implements the Nagle algorithm by default, which can interact badly if .B sack_freq is set to more than one. At least Linux defaults .B sack_freq to 2, but the gensio overrides this to avoid surprising behaviour. What happens is in some situations you can get an outstanding packet that is unacked, since .B sack_freq is greater than one. The Nagle algorithm will not send any new data until any already sent data is acked. So one end is waiting for a new packet to send a sack, and the other end is holding data until it gets a sack. So you get stuck waiting for the .B sack_delay where the sack will go out and kick things back off again. You need to be aware of this if you modify sack_freq. .SS Options In addition to readbuf, the sctp gensio takes the following options: .TP .B instreams= .TP .B ostreams= These specify the number of incoming and outgoing streams for the connection. The default is one. The stream is given in the auxdata for read and write in the format "stream=". .TP .B sack_freq= .TP .B sack_delay= These specify the handling of selective acknowledgements (sacks). .B sack_freq sets the number of outstanding packets that must be received before sending a sack. The default is 1, meaning it doesn't wait at all. .B sack_delay sets the maximum time before a sack is sent if outstanding packets are present, in milliseconds. The default is 10, but this is disabled if .B sack_freq is set to 1. Setting either of these to 0 enables the system defaults. .TP .B nodelay[=true|false] Sets nodelay on the socket. .TP .B laddr= An address specification to bind to on the local socket to set the local address. .TP .B reuseaddr[=true|false] Set SO_REUSEADDR on the socket, good for accepting gensios only. Defaults to true. Port 0 is supported just like TCP for accepters, see Dynamic Ports in the TCP section above for details. SCTP support out of band (oob) data, which is data that will be delivered out of order as soon as possible. This comes in a normal read, but with "oob" in the auxdata. You can send oob data by adding "oob" to the write auxdata. See documentation on SCTP for more details. .SS "Remote Address String" The remote address will be in the format "[ipv4|ipv6],,[;[ipv4|ipv6],,[...]]" where the address is in numeric format, IPv4, or IPv6. Each remote address for the SCTP connection is listed. .SS "Remote Address" SCTP returns a packed struct sockaddr for GENSIO_CONTROL_RADDR_BIN control, per SCTP semantics. .SH "UNIX" .B unix[()], Create a unix domain socket as an accepter, or connect to a unix domain socket as a connecter. A file will be created with the given socket path, you must have permissions to create a writeable file in that location. If the file already exists, an error will be returned on an accepter socket unless you specify .I delsock which will cause the file to be deleted. You should read the unix(7) man page for details on the semantics of these sockets, especially permissions. The options below allow setting various permission and ownership of the file, but this may not have any effect on who can open socket depending on the particular operating system. .B Portable programs should not rely on these permissions for security. Also note that Linux remote credentials are not currently implemented. .SS Options In addition to readbuf, the unix gensio takes the following options: .TP .B delsock[=true|false] If the socket path already exists, delete it before opening the socket. .TP .B umode=[0-7|[rwx]*] Set the user file mode for the unix socket file. This is the usual read(4)/write(2)/execute(2) bitmask per chmod, but only for the user portion. If a mode is specified, all other modes default to "6" (rw) +unless they are specified, and the final mode is modified by the umask +per standard *nix semantics. If no mode is specified, it is set to +the default and not modified. Note that the perm option below is +probably a better way to set this. .TP .B gmode=[0-7|[rwx]*] Set the group file mode for the unix socket file, see umode for details. .TP .B omode=[0-7|[rwx]*] Set the other file mode for the uix socket file, see umode for details. .TP .B perm=[0-7][0-7][0-7] Set the full mode for the unix socket file per standard *nix semantics, modified by umask as the above mode operations are. .TP .B owner= Set the owner of the unix socket file to the given user. .TP .B group= Set the group of the unix socket file to the given group. .SS Remote Address String The remote address will be: "unix,". .SS Remote Address UNIX returns a standard struct sockaddr_un for GENSIO_CONTROL_RADDR_BIN control. .SH "serialdev" .B serialdev[()],[,[,]] A serialdev connection is a local serial port. The device is a .B /dev/xxx type, and should be real stream device of some type that normal termios work on (except for wronly). This is, no surprise, a serial gensio. One problem with serialdev and UUCP locking is that if you fork() a process while one is open, the forked process will have the serialdev but the value in the UUCP lockfile will be incorrect. There's not much that can be done about this, so be careful. .SS Options In addition to readbuf, the serialdev gensio takes the following options: .TP .B nouucplock[=true|false] disables UUCP locking on the device. Useful for /dev/tty, which shouldn't use locking. This is not available as a default. .PP There are a plethora of serialoptions, available as defaults: .TP .B [speed=] This is a normal serial port configuration specification, like "9600N81". The "speed=" at the beginning is optional, but "speed" is a default type for this. .TP .B wronly[=true|false] Set the device to write only. No termios definition is done on the port. This can be done to talk to a line printer port, for instance. False by default. .TP .B nobreak[=true|false] Clear the break line at start (or don't clear it). Default it to not clear it (false). .TP .B rs485=off|:[:[:]] Set up RS-485 for the serial port. The first two parameters set the RTS delay (in milliseconds) of RTS before and after sending. The conf values can be: "rts_on_send" - RTS set when sending, "rts_after_send" - RTS set after sending, "rx_during_tx" - can receive and transmit at the same time, and "terminate_bus" - enable bus termination. Using "off" will disable rs485, that is useful for overriding a default. default is "off". .TP .B xonxoff[=true|false] Enable/disable xon/xoff flow control. Default is false. .TP .B rtscts[=true|false] Enable/disable rts/cts flow control. Default is false. .TP .B local[=true|false] Ignore/don't ignore the modem control lines. The default it to not ignore them (false). However, if you don't ignore the modem control lines, it can result in long shutdown delays. .TP .B hangup-when-done[=true|false] Lower/don't lower the modem control lines when the gensio is closed. The default is to not lower the modem control lines on close (false). .B custspeed[=true|false] Allow/don't allow setting of custom baud rates. Ignored if custom baud rates are not supported. Normally only standard baud rates are supported (1200, 2400, etc). If supported by the hardware, this allows any arbitrary value to be set. .SS "Remote Address String" The remote address string is the device and serial parms, in a format like "9600N81[,