![]() | :: | Software | :: | weblib |
The ipc library works with local-domain sockets.
#include <ipc.h> int s; char *p; s = ipc_stream(); ipc_connect(s,p); ipc_bind(s,p);
ipc_stream creates a non-blocking local-domain socket and returns a file descriptor pointing to that socket. If it encounters an error, ipc_stream returns -1, setting errno appropriately without allocating any resources. Otherwise it returns 0.
ipc_connect attempts to connect from the local-domain socket s to a local-domain address given by the null-terminated string at p.
ipc_bind sets the local address of the local-domain socket s to the null-terminated string at p. ipc_bind sets errno and returns -1 in case of error, and returns 0 otherwise.
#include <ipc.h> int s; int fd; char *p; int n; int uid; int gid; int trunc; ipc_bind_reuse(s,p); ipc_listen(s,n); fd = ipc_accept(s,p,n,&trunc); ipc_eid(s,&uid,&gid);
ipc_bind_reuse behaves like ipc_bind, but invokes unlink on the address before binding it to the socket. This allows a server to reuse the same address after the socket is closed.
ipc_listen causes a socket s to allow local-domain connections. It sets a backlog of approximately n connections. If it encounters an error, ipc_listen sets errno appropriately, and returns -1. Otherwise, it returns 0.
ipc_accept accepts connections from a listening socket s. ipc_accept stores the client address A in buffer of length n at p. There are three cases:
n=0: Store nothing; set trunc=1.
0<n<len(A)+1: Store A[0] ... A[n-1] + null; set trunc=1.
n>=len(A)+1: Store A + null; set trunc=0.
ipc_eid queries a server socket s produced by ipc_accept for the effective user ID and group ID of the client process that called the associated connect, and stores the results in uid and gid. If it encounters an error, ipc_eid sets errno appropriately, and returns -1, without altering the uid or gid. Normally, ipc_eid returns 0.
#include <ipc.h> char *buf; int len; char *p; int l; int trunc; ipc_datagram(); ipc_send(s,buf,len,p); ipc_recv(s,buf,len,p,l,&trunc); ipc_tryreservein(s,l);
ipc_datagram creates a non-blocking local-domain datagram socket, and returns a file descriptor pointing to the new socket. If it encounters an error, ipc_datagram sets errno appropriately, and returns -1. Otherwise, ipc_datagram returns 0.
ipc_send sends a datagram with data buf[0]...buf[len-1] through the local-domain datagram socket s to a local-domain socket bound to the address in the null-terminated string p. If it encounters an error, ipc_send sets errno appropriately, and returns -1. Otherwise, ipc_send returns a nonnegative number.
ipc_recv receives a datagram through the local-domain datagram socket s. It copies the datagram to buf[0]...buf[len-1], discarding the rest of the bytes received. ipc_recv stores the sender address for the datagram in the buffer of length l at p and sets trunc using the same rules as ipc_accept. If it encounters an error, ipc_recv sets errno appropriately, and returns -1. Otherwise it returns the number of bytes received.
ipc_tryreservein attempts to allocate n bytes of space for the input buffer of the socket s.
#include <ipc.h> char *p; int trunc; ipc_local(s,p,l,&trunc); ipc_remote(s,p,l,&trunc);
ipc_local stores the address address associated with the local-domain socket s in the buffer of length l at p and sets trunc, using the same rules as ipc_accept. If it encounters an error, ipc_local sets errno appropriately, and returns -1, without modifying p or trunc. Otherwise it returns 0.
ipc_remote stores address of the peer of the connected local-domain socket s in the buffer of length l at p and sets trunc, using the same rules as ipc_accept. If it encounters an error, ipc_remote sets errno appropriately, and returns -1, without modifying p or trunc. Otherwise it returns 0.