### Example: TCP Server Setup with libdill Source: https://github.com/sustrik/libdill/blob/master/website/tcp_listen.html Demonstrates setting up a TCP listener, accepting a connection, sending and receiving data, and then closing both the accepted connection and the listener socket. It uses functions like ipaddr_local, tcp_listen, tcp_accept, bsend, brecv, and tcp_close. ```c struct ipaddr addr; ipaddr_local(&addr, NULL, 5555, 0); int ls = tcp_listen(&addr, 10); int s = tcp_accept(ls, NULL, -1); bsend(s, "ABC", 3, -1); char buf[3]; brecv(s, buf, sizeof(buf), -1); tcp_close(s); tcp_close(ls); ``` -------------------------------- ### IPC Listen Example Usage Source: https://github.com/sustrik/libdill/blob/master/website/ipc_listen.html An example demonstrating how to use ipc_listen to start listening, accept a connection, send and receive data, and then close the connections. ```c int ls = ipc_listen("/tmp/test.ipc", 10); int s = ipc_accept(ls, -1); bsend(s, "ABC", 3, -1); char buf[3]; brecv(s, buf, sizeof(buf), -1); ipc_close(s); ipc_close(ls); ``` -------------------------------- ### Channel Creation Example Source: https://github.com/sustrik/libdill/blob/master/website/chmake_mem.html An example demonstrating the creation of a channel using the `chmake` function and handling potential errors. ```c int ch[2]; int rc = chmake(ch); if(rc == -1) { perror("Cannot create channel"); exit(1); } ``` -------------------------------- ### Example: Create and use an IPC listener (C) Source: https://github.com/sustrik/libdill/blob/master/website/ipc_listener_fromfd_mem.html Demonstrates the creation of an IPC listener using a file descriptor and the ipc_listener_fromfd function. This example shows the basic setup for listening on a UNIX domain socket. ```c int fd = socket(AF_UNIX, SOCK_STREAM, 0); bind(fd, addr, sizeof(addr)); listen(fd, 10); int s = ipc_listener_fromfd(fd); ``` -------------------------------- ### TCP Listen Example Source: https://github.com/sustrik/libdill/blob/master/website/tcp_listen_mem.html An example demonstrating how to listen for TCP connections, accept incoming connections, send and receive data, and close the sockets. ```c struct ipaddr addr; ipaddr_local(&addr, NULL, 5555, 0); int ls = tcp_listen(&addr, 10); int s = tcp_accept(ls, NULL, -1); bsend(s, "ABC", 3, -1); char buf[3]; brecv(s, buf, sizeof(buf), -1); tcp_close(s); tcp_close(ls); ``` -------------------------------- ### Download and Install libdill Source: https://github.com/sustrik/libdill/blob/master/website/download.html This snippet shows the shell commands to download, extract, configure, build, and install the libdill library. It requires standard Unix-like build tools. ```shell $ wget http://libdill.org/libdill-2.14.tar.gz $ tar -xzf libdill-2.14.tar.gz $ cd libdill-2.14 $ ./configure $ make $ sudo make install ``` -------------------------------- ### Client and Server Socket Attachment/Detachment Example (C) Source: https://github.com/sustrik/libdill/blob/master/website/src/tutorial-protocol.md Demonstrates how to attach a custom protocol (quux) to an underlying IPC socket using `quux_attach` and detach it using `quux_detach`. The `client` coroutine attaches, performs an action, detaches, and closes the socket. The `main` function sets up the IPC pair, starts the client, attaches the server, performs an action, detaches, and closes the socket. ```c coroutine void client(int s) { int q = quux_attach(s); assert(q >= 0); /* Do something useful here! */ s = quux_detach(q); assert(s >= 0); int rc = hclose(s); assert(rc == 0); } int main(void) { int ss[2]; int rc = ipc_pair(ss); assert(rc == 0); go(client(ss[0])); int q = quux_attach(ss[1]); assert(q >= 0); /* Do something useful here! */ int s = quux_detach(q); assert(s >= 0); rc = hclose(s); assert(rc == 0); return 0; } ``` -------------------------------- ### IPC Connection Example using libdill Source: https://github.com/sustrik/libdill/blob/master/website/ipc_done.html An example demonstrating the usage of libdill functions for IPC communication. It shows setting up a listener, accepting a connection, sending and receiving data, and then closing the connection and listener. ```c int ls = ipc_listen("/tmp/test.ipc", 10); int s = ipc_accept(ls, -1); bsend(s, "ABC", 3, -1); char buf[3]; brecv(s, buf, sizeof(buf), -1);ipc_close(s); ipc_close(ls); ``` -------------------------------- ### hquery Usage Example in C Source: https://github.com/sustrik/libdill/blob/master/website/hquery.html This C example demonstrates how to use hquery to retrieve a virtual file system structure and call its methods. It illustrates a common pattern for accessing handle functionality. ```c struct quux_vfs { int (*foo)(struct quux_vfs *vfs, int a, int b); void (*bar)(struct quux_vfs *vfs, char *c); }; int quux_foo(int h, int a, int b) { struct foobar_vfs *vfs = hquery(h, foobar_type); if(!vfs) return -1; return vfs->foo(vfs, a, b); } void quux_bar(int h, char *c) { struct foobar_vfs *vfs = hquery(h, foobar_type); if(!vfs) return -1; vfs->bar(vfs, c); } ``` -------------------------------- ### Shell: Compiling libdill example Source: https://github.com/sustrik/libdill/blob/master/website/src/tutorial-protocol.md Provides the command to compile a C program that uses the libdill library. This command links the source file with the libdill library. ```shell gcc -o step1 step1.c -ldill ``` -------------------------------- ### Example Usage of libdill functions Source: https://github.com/sustrik/libdill/blob/master/website/prefix_attach_mem.html Demonstrates how to establish a TCP connection, attach a PREFIX protocol with a 2-byte header, send data, receive data, detach the protocol, and close the socket. This example showcases basic message-based communication flow. ```c int s = tcp_connect(&addr, -1); s = prefix_attach(s, 2, 0); msend(s, "ABC", 3, -1); char buf[256]; ssize_t sz = mrecv(s, buf, sizeof(buf), -1); s = prefix_detach(s, -1); tcp_close(s); ``` -------------------------------- ### UDP Send Example (C) Source: https://github.com/sustrik/libdill/blob/master/website/udp_sendl.html An example demonstrating how to use libdill functions for UDP communication. It includes opening a UDP socket, sending data, receiving data, and closing the socket. ```c struct ipaddr local; ipaddr_local(&local, NULL, 5555, 0); struct ipaddr remote; ipaddr_remote(&remote, "server.example.org", 5555, 0, -1); int s = udp_open(&local, &remote); udp_send(s1, NULL, "ABC", 3); char buf[2000]; ssize_t sz = udp_recv(s, NULL, buf, sizeof(buf), -1); hclose(s); ``` -------------------------------- ### Main Function Setup with Port Argument Source: https://github.com/sustrik/libdill/blob/master/website/src/tutorial-basics.md Sets up the main function for the server, allowing an optional port number to be passed as a command-line argument. If no argument is provided, it defaults to port 5555. ```c int main(int argc, char *argv[]) { int port = 5555; if(argc > 1) port = atoi(argv[1]); return 0; } ``` -------------------------------- ### Example: TCP Server Accept and Data Exchange (C) Source: https://github.com/sustrik/libdill/blob/master/website/tcp_accept_mem.html Demonstrates the usage of libdill functions to set up a TCP listener, accept an incoming connection, send data, receive data, and then close the connection and listener. This example illustrates a basic TCP server interaction flow. ```c struct ipaddr addr; ipaddr_local(&addr, NULL, 5555, 0); int ls = tcp_listen(&addr, 10); int s = tcp_accept(ls, NULL, -1); bsend(s, "ABC", 3, -1); char buf[3]; brecv(s, buf, sizeof(buf), -1); tcp_close(s); tcp_close(ls); ``` -------------------------------- ### Build libdill from Source Source: https://github.com/sustrik/libdill/blob/master/website/faq.html Instructions to clone the libdill repository and build it from source using autotools. Includes commands for configuration, compilation, testing, and installation. ```shell git clone https://github.com/sustrik/libdill.git cd libdill ./autogen.sh ./configure make make check sudo make install ``` -------------------------------- ### WebSocket Client Example (C) Source: https://github.com/sustrik/libdill/blob/master/website/ws_attach_server.html A basic example demonstrating the usage of libdill's WebSocket functions, including attaching a TLS server, sending data, receiving data, detaching, and closing the socket. ```c int s = tcp_accept(listener, NULL, -1); s = tls_attach_server(s, -1); bsend(s, "ABC", 3, -1); char buf[3]; ssize_t sz = brecv(s, buf, sizeof(buf), -1); s = tls_detach(s, -1); tcp_close(s); ``` -------------------------------- ### Example of using tcp_listener_fromfd (C) Source: https://github.com/sustrik/libdill/blob/master/website/tcp_listener_fromfd_mem.html A basic example demonstrating how to create a TCP listener using an existing file descriptor. This involves creating a socket, binding it to an address, listening for connections, and then wrapping the file descriptor with tcp_listener_fromfd. ```c int fd = socket(AF_INET, SOCK_STREAM, 0); bind(fd, addr, sizeof(addr)); listen(fd, 10); int s = tcp_listener_fromfd(fd); ``` -------------------------------- ### Example: Bundle Go Usage (C) Source: https://github.com/sustrik/libdill/blob/master/website/bundle_go.html Demonstrates launching multiple worker coroutines within a bundle, waiting for them to complete, and then closing the bundle. ```c int b = bundle(); bundle_go(b, worker()); bundle_go(b, worker()); bundle_go(b, worker()); /* Give wrokers 1 second to finish. */ bundle_wait(b, now() + 1000); /* Cancel any remaining workers. */ hclose(b); ``` -------------------------------- ### TCP socket usage example (C) Source: https://github.com/sustrik/libdill/blob/master/website/tcp_fromfd_mem.html An example demonstrating the usage of libdill's TCP functions. It shows creating a socket, connecting, sending data with bsend, receiving data with brecv, and closing the socket with tcp_close. ```c int fd = socket(AF_INET, SOCK_STREAM, 0); connect(fd, addr, sizeof(addr)); int s = tcp_fromfd(fd); bsend(s, "ABC", 3, -1); char buf[3]; brecv(s, buf, sizeof(buf), -1); tcp_close(s); ``` -------------------------------- ### Example HTTP Request-Response Cycle with libdill Source: https://github.com/sustrik/libdill/blob/master/website/http_sendrequest.html Demonstrates a complete HTTP request and response cycle using libdill functions. It includes connecting to a server, attaching to an HTTP context, sending a request and headers, receiving the status and headers, and finally detaching and closing the connection. This example showcases the interrelation of several libdill HTTP functions. ```c int s = tcp_connect(&addr, -1); s = http_attach(s); http_sendrequest(s, "GET", "/", -1); http_sendfield(s, "Host", "www.example.org", -1); http_done(s, -1); char reason[256]; http_recvstatus(s, reason, sizeof(reason), -1); while(1) { char name[256]; char value[256]; int rc = http_recvfield(s, name, sizeof(name), value, sizeof(value), -1); if(rc == -1 && errno == EPIPE) break; } s = http_detach(s, -1); tcp_close(s); ``` -------------------------------- ### C: Example of WebSocket Communication Flow Source: https://github.com/sustrik/libdill/blob/master/website/ws_attach_server_mem.html Demonstrates a typical sequence for establishing a TLS-secured WebSocket connection, sending data, receiving data, and then closing the connection. This example illustrates the interaction between various libdill functions for network communication. ```c int s = tcp_accept(listener, NULL, -1); s = tls_attach_server(s, -1); bsend(s, "ABC", 3, -1); char buf[3]; ssize_t sz = brecv(s, buf, sizeof(buf), -1); s = tls_detach(s, -1); tcp_close(s); ``` -------------------------------- ### WebSocket Send Example (C) Source: https://github.com/sustrik/libdill/blob/master/website/ws_send.html An example demonstrating how to use ws_send to send a 'Hello, world!' message over a WebSocket connection. It includes steps for establishing a TCP connection, attaching a WebSocket client, sending the message, receiving a response, detaching the WebSocket, and closing the TCP connection. ```c struct ipaddr addr; ipaddr_remote(&addr, "www.example.org", 80, 0, -1); int s = tcp_connect(&addr, -1); s = ws_attach_client(s, "/", "www.example.org", WS_TEXT, -1); ws_send(s, WS_TEXT, "Hello, world!", 13, -1); int flags; char buf[256]; ssize_t sz = ws_recv(s, &flags, buf, sizeof(buf), -1); assert(flags & WS_TEXT); s = ws_detach(s, -1); tcp_close(s, -1); ``` -------------------------------- ### TCP Connection Example (C) Source: https://github.com/sustrik/libdill/blob/master/website/tcp_connect_mem.html An example demonstrating how to establish a TCP connection using `tcp_connect`, send data using `bsend`, receive data using `brecv`, and then close the connection using `tcp_close`. It includes setting up an IP address for a remote host. ```c struct ipaddr addr; ipaddr_remote(&addr, "www.example.org", 80, 0, -1); int s = tcp_connect(&addr, -1); bsend(s, "ABC", 3, -1); char buf[3]; brecv(s, buf, sizeof(buf), -1); tcp_close(s); ``` -------------------------------- ### WebSocket Client Example (C) Source: https://github.com/sustrik/libdill/blob/master/website/ws_done.html Demonstrates establishing a WebSocket connection to a server, sending a message, receiving a response, and then cleanly closing the connection. It utilizes functions like tcp_connect, ws_attach_client, ws_send, ws_recv, ws_detach, and tcp_close. ```c struct ipaddr addr; ipaddr_remote(&addr, "www.example.org", 80, 0, -1); int s = tcp_connect(&addr, -1); s = ws_attach_client(s, "/", "www.example.org", WS_TEXT, -1); ws_send(s, WS_TEXT, "Hello, world!", 13, -1); int flags; char buf[256]; ssize_t sz = ws_recv(s, &flags, buf, sizeof(buf), -1); assert(flags & WS_TEXT); s = ws_detach(s, -1); tcp_close(s, -1); ``` -------------------------------- ### IPC Connection Example (C) Source: https://github.com/sustrik/libdill/blob/master/website/ipc_accept_mem.html Demonstrates the usage of libdill functions for establishing and communicating over an IPC connection. It includes listening for connections, accepting them, sending and receiving data, and closing the connection. ```c int ls = ipc_listen("/tmp/test.ipc", 10); int s = ipc_accept(ls, -1); bsend(s, "ABC", 3, -1); char buf[3]; brecv(s, buf, sizeof(buf), -1); ipc_close(s); ipc_close(ls); ``` -------------------------------- ### Example Usage of IPC Connection (C) Source: https://github.com/sustrik/libdill/blob/master/website/ipc_connect_mem.html Demonstrates the basic usage of an IPC connection. It connects to an IPC endpoint, sends data, receives data, and then closes the connection. This example showcases the flow of data transfer over an IPC socket. ```C int s = ipc_connect("/tmp/test.ipc", -1); bsend(s, "ABC", 3, -1); char buf[3]; brecv(s, buf, sizeof(buf), -1); ipc_close(s); ``` -------------------------------- ### WebSocket Client Connection Example Source: https://github.com/sustrik/libdill/blob/master/website/ws_recvl.html Demonstrates establishing a WebSocket client connection, sending a message, receiving a response, and closing the connection using libdill functions like tcp_connect, ws_attach_client, ws_send, ws_recv, and tcp_close. ```c struct ipaddr addr; ipaddr_remote(&addr, "www.example.org", 80, 0, -1); int s = tcp_connect(&addr, -1); s = ws_attach_client(s, "/", "www.example.org", WS_TEXT, -1); ws_send(s, WS_TEXT, "Hello, world!", 13, -1); int flags; char buf[256]; ssize_t sz = ws_recv(s, &flags, buf, sizeof(buf), -1); assert(flags & WS_TEXT); s = ws_detach(s, -1); tcp_close(s, -1); ``` -------------------------------- ### Example: Using prefix_detach with libdill (C) Source: https://github.com/sustrik/libdill/blob/master/website/prefix_detach.html This example demonstrates the usage of prefix_detach in conjunction with other libdill functions like tcp_connect, prefix_attach, msend, and mrecv. It shows a typical flow of establishing a connection, sending data, receiving data, detaching the prefix protocol, and closing the socket. ```c int s = tcp_connect(&addr, -1); s = prefix_attach(s, 2, 0); msend(s, "ABC", 3, -1); char buf[256]; ssize_t sz = mrecv(s, buf, sizeof(buf), -1); s = prefix_detach(s, -1); tcp_close(s); ``` -------------------------------- ### Example: Using ipc_fromfd and bsend/brecv in C Source: https://github.com/sustrik/libdill/blob/master/website/ipc_fromfd_mem.html This example demonstrates how to create a UNIX domain socket, connect to it, wrap the file descriptor using ipc_fromfd (a similar function to ipc_fromfd_mem), send data using bsend, receive data using brecv, and finally close the IPC handle. ```c int fd = socket(AF_UNIX, SOCK_STREAM, 0); connect(fd, addr, sizeof(addr)); int s = ipc_fromfd(fd); bsend(s, "ABC", 3, -1); char buf[3]; brecv(s, buf, sizeof(buf), -1);ipc_close(s); ``` -------------------------------- ### Build libdill from Source Source: https://github.com/sustrik/libdill/blob/master/website/src/faq.md Builds the libdill library from its source code, requiring automake and libtool. Includes steps for generation, configuration, compilation, testing, and installation. ```shell $ ./autogen.sh $ ./configure $ make $ make check $ sudo make install ``` -------------------------------- ### Accept Incoming TCP Connections with libdill Source: https://github.com/sustrik/libdill/blob/master/website/src/tutorial-basics.md Illustrates how to accept new TCP connections using `tcp_accept`. The example shows blocking indefinitely (`-1` deadline) until a connection is available and closing the accepted socket immediately. ```c while(1) { int s = tcp_accept(ls, NULL, -1); assert(s >= 0); printf("New connection!\n"); rc = hclose(s); assert(rc == 0); } ``` -------------------------------- ### Example: Performing HTTP Request and Response with libdill in C Source: https://github.com/sustrik/libdill/blob/master/website/http_attach_mem.html This C code example demonstrates the usage of libdill functions to establish an HTTP connection, send a request, receive a response, and properly detach the HTTP protocol from the socket. It includes connecting via TCP, attaching the HTTP protocol, sending a request and fields, receiving status and fields, and finally closing the connection. ```c int s = tcp_connect(&addr, -1); s = http_attach(s); http_sendrequest(s, "GET", "/", -1); http_sendfield(s, "Host", "www.example.org", -1); http_done(s, -1); char reason[256]; http_recvstatus(s, reason, sizeof(reason), -1); while(1) { char name[256]; char value[256]; int rc = http_recvfield(s, name, sizeof(name), value, sizeof(value), -1); if(rc == -1 && errno == EPIPE) break; } s = http_detach(s, -1); tcp_close(s); ``` -------------------------------- ### Run libdill Server Source: https://github.com/sustrik/libdill/blob/master/website/src/tutorial-basics.md Shows the command to execute the compiled libdill server program. ```bash ./greetserver ``` -------------------------------- ### Launch Statistics Coroutine Source: https://github.com/sustrik/libdill/blob/master/website/tutorial-basics.html Shows how to initiate the `statistics` coroutine by passing one end of the created channel to it using `go`. ```c int ch[2]; rc = chmake(ch); assert(rc == 0); int cr = go(statistics(ch[0])); assert(cr >= 0); ``` -------------------------------- ### Example: WebSocket client communication with ws_detach (C) Source: https://github.com/sustrik/libdill/blob/master/website/ws_detach.html This C code example demonstrates a typical client-side interaction using libdill's WebSocket functions. It shows connecting to a server, attaching a WebSocket client, sending a text message, receiving a response, detaching the WebSocket protocol to get the raw socket, and finally closing the underlying TCP connection. The example utilizes ws_attach_client, ws_send, ws_recv, and ws_detach. ```C struct ipaddr addr; ipaddr_remote(&addr, "www.example.org", 80, 0, -1); int s = tcp_connect(&addr, -1); s = ws_attach_client(s, "/", "www.example.org", WS_TEXT, -1); ws_send(s, WS_TEXT, "Hello, world!", 13, -1); int flags; char buf[256]; ssize_t sz = ws_recv(s, &flags, buf, sizeof(buf), -1); assert(flags & WS_TEXT); s = ws_detach(s, -1); tcp_close(s, -1); ``` -------------------------------- ### Example Usage of tls_detach Source: https://github.com/sustrik/libdill/blob/master/website/tls_detach.html This C code snippet demonstrates how to use tls_detach. It connects to a TCP socket, attaches TLS to it, sends data, receives data, detaches the TLS layer to get the original socket, and then closes the underlying socket. ```c int s = tcp_connect(&addr, -1); s = tls_attach_client(s, -1); bsend(s, "ABC", 3, -1); char buf[3]; ssize_t sz = brecv(s, buf, sizeof(buf), -1); s = tls_detach(s, -1); tcp_close(s); ``` -------------------------------- ### Installation Rules for Library, Headers, and Config Files Source: https://github.com/sustrik/libdill/blob/master/CMakeLists.txt Defines the installation rules for the 'dill' library, its headers, and the generated CMake configuration files. This ensures that the library can be properly installed and found by other projects. ```cmake # Targets: # * /lib/libdill.a # * header location after install: /include/libdill.h install( TARGETS dill EXPORT "${TARGETS_EXPORT_NAME}" LIBRARY DESTINATION "lib" ARCHIVE DESTINATION "lib" RUNTIME DESTINATION "bin" INCLUDES DESTINATION "${include_install_dir}" ) # Headers: # * libdill.h -> /include/libdill.h install( FILES libdill.h DESTINATION "${include_install_dir}" ) # Config # * /lib/cmake/libdill/libdillConfig.cmake # * /lib/cmake/libdill/libdillConfigVersion.cmake install( FILES "${project_config}" "${version_config}" DESTINATION "${config_install_dir}" ) # Config # * /lib/cmake/libdill/libdillTargets.cmake install( EXPORT "${TARGETS_EXPORT_NAME}" NAMESPACE "${namespace}" DESTINATION "${config_install_dir}" ) ``` -------------------------------- ### Include libdill and standard headers Source: https://github.com/sustrik/libdill/blob/master/website/tutorial-basics.html Includes necessary headers for libdill functionality and standard C library functions like input/output, assertions, error handling, and string manipulation. ```c #include #include #include #include #include #include #include ``` -------------------------------- ### Create and Use Channels for Coroutine Communication Source: https://github.com/sustrik/libdill/blob/master/website/tutorial-basics.html Demonstrates creating a libdill channel using `chmake` and passing it to coroutines. It also shows launching coroutines with `go` and attaching data to sockets with `suffix_attach`. ```c int ch[2]; rc = chmake(ch); assert(rc == 0); int s = tcp_accept(ls, NULL, -1); assert(s >= 0); s = suffix_attach(s, "\r\n", 2); assert(s >= 0); int cr = go(dialogue(s, ch[1])); assert(cr >= 0); ``` -------------------------------- ### Server Main Function with Coroutine Creation (C) Source: https://github.com/sustrik/libdill/blob/master/website/src/tutorial-basics.md Illustrates the main server loop in C, which accepts TCP connections, attaches a suffix, and spawns a new coroutine for each connection using `dialogue()`. It also creates a statistics coroutine and initializes a channel for communication. ```c coroutine void dialogue(int s, int ch) { ... } int main(int argc, char *argv[]) { ... int ch[2]; rc = chmake(ch); assert(rc == 0); while(1) { int s = tcp_accept(ls, NULL, -1); assert(s >= 0); s = suffix_attach(s, "\r\n", 2); assert(s >= 0); int cr = go(dialogue(s, ch[1])); assert(cr >= 0); } } ``` -------------------------------- ### Include libdill and Standard Library Headers Source: https://github.com/sustrik/libdill/blob/master/website/src/tutorial-basics.md Includes necessary headers for libdill functionality and standard C library operations like assertions, error handling, input/output, memory allocation, string manipulation, and POSIXunistd. ```c #include #include #include #include #include #include #include ``` -------------------------------- ### IPC Socket Pair Creation Example Source: https://github.com/sustrik/libdill/blob/master/website/ipc_pair_mem.html A simple example demonstrating the creation of an IPC socket pair using the ipc_pair function, which is recommended over ipc_pair_mem for most scenarios. ```c int s[2]; int rc = ipc_pair(s); ``` -------------------------------- ### Example Usage of ipaddr_equal in C Source: https://github.com/sustrik/libdill/blob/master/website/ipaddr_equal.html This example demonstrates how to use ipaddr_equal by first obtaining an IP address using ipaddr_remote, creating a socket, and then connecting to the remote address. ```c ipaddr addr; ipaddr_remote(&addr, "www.example.org", 80, 0, -1); int s = socket(ipaddr_family(addr), SOCK_STREAM, 0); connect(s, ipaddr_sockaddr(&addr), ipaddr_len(&addr)); ``` -------------------------------- ### Example usage of bsend Source: https://github.com/sustrik/libdill/blob/master/website/bsend.html An example demonstrating how to use the bsend function to send data. It shows a call to bsend with a socket, a string literal, its length, and a no-deadline parameter. ```c int rc = bsend(s, "ABC", 3, -1); ``` -------------------------------- ### C: Basic Handle Creation and Closing with libdill Source: https://github.com/sustrik/libdill/blob/master/website/src/tutorial-protocol.md Demonstrates the creation of a custom handle using libdill's hmake function and its subsequent closing via hclose. It involves defining a structure for the handle and its virtual function table. ```c #include ``` ```c int main(void) { int h = quux_open(); assert(h >= 0); int rc = hclose(h); assert(rc == 0); return 0; } ``` ```c struct quux { struct hvfs hvfs; }; ``` ```c static void *quux_hquery(struct hvfs *hvfs, const void *id); static void quux_hclose(struct hvfs *hvfs); ``` ```c int quux_open(void) { int err; struct quux *self = malloc(sizeof(struct quux)); if(!self) {err = ENOMEM; goto error1;} self->hvfs.query = quux_hquery; self->hvfs.close = quux_hclose; int h = hmake(&self->hvfs); if(h < 0) {err = errno; goto error2;} return h; error2: free(self); error1: errno = err; return -1; } ``` ```c static void quux_hclose(struct hvfs *hvfs) { struct quux *self = (struct quux*)hvfs; free(self); } ``` -------------------------------- ### ipc_listen - Start listening for incoming IPC connections Source: https://github.com/sustrik/libdill/blob/master/website/ipc_listen.html This function starts listening for incoming IPC connections on a specified address. It's an equivalent to POSIX AF_LOCAL sockets. ```APIDOC ## ipc_listen ### Description Starts listening for incoming IPC connections. The connections can be accepted using the **ipc_accept** function. ### Method Not Applicable (C function) ### Endpoint Not Applicable (C function) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```c int ls = ipc_listen("/tmp/test.ipc", 10); ``` ### Response #### Success Response (0) Returns a newly created socket descriptor. #### Response Example ```c int ls = 5; ``` ### Errors * **EACCES**: The process does not have appropriate privileges. * **EADDRINUSE**: The specified address is already in use. * **EINVAL**: Invalid argument. * **ELOOP**: A loop exists in symbolic links encountered during resolution of the pathname in address. * **EMFILE**: The maximum number of file descriptors in the process are already open. * **ENAMETOOLONG**: A component of a pathname exceeded NAME_MAX characters, or an entire pathname exceeded PATH_MAX characters. * **ENFILE**: The maximum number of file descriptors in the system are already open. * **ENOENT**: A component of the pathname does not name an existing file or the pathname is an empty string. * **ENOMEM**: Not enough memory. * **ENOTDIR**: A component of the path prefix of the pathname in address is not a directory. ``` -------------------------------- ### Compile libdill program Source: https://github.com/sustrik/libdill/blob/master/website/tutorial-basics.html Demonstrates the command to compile a C program that uses the libdill library. It links against `libdill` using the `-ldill` flag. ```bash gcc -o greetserver step1.c -ldill ``` -------------------------------- ### UDP Receive Example (C) Source: https://github.com/sustrik/libdill/blob/master/website/udp_recvl.html An example demonstrating the usage of UDP functions in libdill. It shows opening a UDP socket, sending a message, receiving a response, and closing the socket. ```c struct ipaddr local; ipaddr_local(&local, NULL, 5555, 0); struct ipaddr remote; ipaddr_remote(&remote, "server.example.org", 5555, 0, -1); int s = udp_open(&local, &remote); udp_send(s1, NULL, "ABC", 3); char buf[2000]; ssize_t sz = udp_recv(s, NULL, buf, sizeof(buf), -1); hclose(s); ``` -------------------------------- ### Compile libdill Server Source: https://github.com/sustrik/libdill/blob/master/website/src/tutorial-basics.md Provides the command-line instruction to compile a C program that uses the libdill library, linking against it with the `-ldill` flag. ```bash gcc -o greetserver step1.c -ldill ``` -------------------------------- ### Transfer Handle Ownership Example - C Source: https://github.com/sustrik/libdill/blob/master/website/hown.html An example demonstrating the usage of the 'hown' function to transfer ownership of a handle obtained from 'tcp_connect'. The original handle becomes invalid after the transfer. ```c int h1 = tcp_connect(&addr, deadline); int h2 = hown(h1); /* h1 is invalid here */ hclose(h2); ``` -------------------------------- ### Example Usage of ipaddr_remote in C Source: https://github.com/sustrik/libdill/blob/master/website/ipaddr_remotes.html An example demonstrating how to use ipaddr_remote to resolve a hostname, create a socket, and connect to the resolved address. This showcases the typical workflow for establishing a network connection using libdill. ```c ipaddr addr; ipaddr_remote(&addr, "www.example.org", 80, 0, -1); int s = socket(ipaddr_family(addr), SOCK_STREAM, 0); connect(s, ipaddr_sockaddr(&addr), ipaddr_len(&addr)); ``` -------------------------------- ### libdill C: Basic Handle Creation and Closing Source: https://github.com/sustrik/libdill/blob/master/website/tutorial-protocol.html Demonstrates the basic structure for creating a custom handle in libdill. It includes opening a handle, asserting its validity, and closing it. This requires including 'libdillimpl.h'. ```c #include #include #include #include #include struct quux { struct hvfs hvfs; }; static void *quux_hquery(struct hvfs *hvfs, const void *id); static void quux_hclose(struct hvfs *hvfs); int quux_open(void) { int err; struct quux *self = malloc(sizeof(struct quux)); if(!self) {err = ENOMEM; goto error1;} self->hvfs.query = quux_hquery; self->hvfs.close = quux_hclose; int h = hmake(&self->hvfs); if(h < 0) {err = errno; goto error2;} return h; error2: free(self); error1: errno = err; return -1; } static void quux_hclose(struct hvfs *hvfs) { struct quux *self = (struct quux*)hvfs; free(self); } // Dummy implementation for quux_hquery static void *quux_hquery(struct hvfs *hvfs, const void *id) { // Placeholder implementation return NULL; } int main(void) { int h = quux_open(); assert(h >= 0); int rc = hclose(h); assert(rc == 0); printf("Handle opened and closed successfully.\n"); return 0; } ``` -------------------------------- ### WebSocket Client Example (C) Source: https://github.com/sustrik/libdill/blob/master/website/ws_status.html This example demonstrates establishing a WebSocket client connection, sending a message, receiving a response, and detaching from the WebSocket protocol. It includes socket creation, attachment, sending, receiving, and closing operations. ```c struct ipaddr addr; ipaddr_remote(&addr, "www.example.org", 80, 0, -1); int s = tcp_connect(&addr, -1); s = ws_attach_client(s, "/", "www.example.org", WS_TEXT, -1); ws_send(s, WS_TEXT, "Hello, world!", 13, -1); int flags; char buf[256]; ssize_t sz = ws_recv(s, &flags, buf, sizeof(buf), -1); assert(flags & WS_TEXT); s = ws_detach(s, -1); tcp_close(s, -1); ``` -------------------------------- ### Create Listening TCP Socket with libdill Source: https://github.com/sustrik/libdill/blob/master/website/src/tutorial-basics.md Demonstrates using libdill's `tcp_listen` function to create a listening TCP socket. It also shows how `ipaddr_local` is used to resolve a local IP address for binding, defaulting to all available network interfaces if NULL is provided. ```c struct ipaddr addr; int rc = ipaddr_local(&addr, NULL, port, 0); if (rc < 0) { perror("Can't open listening socket"); return 1; } int ls = tcp_listen(&addr, 10); assert( ls >= 0 ); ``` -------------------------------- ### Compiling libdill with TLS Support Source: https://github.com/sustrik/libdill/blob/master/website/tutorial-sockets.html These shell commands show how to recompile libdill with TLS support using OpenSSL 1.1.0 or later. It involves configuring with `--enable-tls`, building with `make`, and installing with `sudo make install`. ```shell ./configure --enable-tls make sudo make install ``` -------------------------------- ### Example Usage of udp_open Source: https://github.com/sustrik/libdill/blob/master/website/udp_open.html Demonstrates how to use the udp_open function to create an UDP socket, send data, receive data, and then close the socket. It involves setting up IP addresses using helper functions and handling data transfer. ```c struct ipaddr local; ipaddr_local(&local, NULL, 5555, 0); struct ipaddr remote; ipaddr_remote(&remote, "server.example.org", 5555, 0, -1); int s = udp_open(&local, &remote); udp_send(s1, NULL, "ABC", 3); char buf[2000]; ssize_t sz = udp_recv(s, NULL, buf, sizeof(buf), -1); hclose(s); ``` -------------------------------- ### Basic CMake Project Setup and Library Definition Source: https://github.com/sustrik/libdill/blob/master/CMakeLists.txt Initializes the CMake project, sets the project name and version, and includes necessary modules for symbol and function checks. It then glob sources, sets include directories, and adds the library target. ```cmake cmake_minimum_required(VERSION 3.1) project(libdill VERSION 1.6 LANGUAGES C) include(CheckSymbolExists) include(CheckFunctionExists) file(GLOB sources ${CMAKE_CURRENT_LIST_DIR}/*.c ${CMAKE_CURRENT_LIST_DIR}/dns/dns.c) include_directories(${PROJECT_SOURCE_DIR} "${PROJECT_SOURCE_DIR}/dns") set_source_files_properties(dns/dns.c PROPERTIES COMPILE_FLAGS -std=c99) add_library(dill ${sources}) ``` -------------------------------- ### Example usage of http_sendfield in C Source: https://github.com/sustrik/libdill/blob/master/website/http_sendfield.html An example demonstrating the usage of http_sendfield within a typical HTTP request flow using libdill. This includes connecting, attaching to HTTP, sending a request, sending fields, completing the request, and receiving the response. ```c int s = tcp_connect(&addr, -1); s = http_attach(s); http_sendrequest(s, "GET", "/", -1); http_sendfield(s, "Host", "www.example.org", -1); http_done(s, -1); char reason[256]; http_recvstatus(s, reason, sizeof(reason), -1); while(1) { char name[256]; char value[256]; int rc = http_recvfield(s, name, sizeof(name), value, sizeof(value), -1); if(rc == -1 && errno == EPIPE) break; } s = http_detach(s, -1); tcp_close(s); ``` -------------------------------- ### Building and testing libdill with TLS support Source: https://github.com/sustrik/libdill/blob/master/website/src/tutorial-sockets.md This section provides build instructions for libdill with TLS support, requiring OpenSSL 1.1.0 or later. It details the configuration, compilation, and installation steps for libdill, followed by compiling a test C program that links against the libdill library. ```shell $ ./configure --enable-tls $ make $ sudo make install ``` ```shell cc -o wget wget.c -ldill ``` ```shell $ ./wget http www.example.org /index.html ``` -------------------------------- ### TLS Server Example Usage (C) Source: https://github.com/sustrik/libdill/blob/master/website/tls_attach_server.html A C example demonstrating the use of tls_attach_server to secure a TCP connection. It shows accepting a TCP connection, attaching TLS to it as a server, sending data, receiving data, detaching TLS, and closing the socket. ```c int s = tcp_accept(listener, NULL, -1); s = tls_attach_server(s, -1); bsend(s, "ABC", 3, -1); char buf[3]; ssize_t sz = brecv(s, buf, sizeof(buf), -1); s = tls_detach(s, -1); tcp_close(s); ``` -------------------------------- ### Base Protocol Termination: Start Handshake (libdill) Source: https://github.com/sustrik/libdill/blob/master/rfc/bsd-socket-api-revamp.md Shows the implementation of a libdill base protocol's 'done' function where the protocol defines and starts the terminal handshake using 'foo_start_terminal_handshake'. It includes error handling and state management. ```c int foo_done(int h, int64_t deadline) { struct foo_object *self = foo_data(h); if(self->done) {errno = EPIPE; return -1;} int rc = foo_start_terminal_handshake(h, deadline); if(rc < 0) return -1; self->done = 1; return 0; } ``` -------------------------------- ### Example of HTTP Communication with http_recvstatus Source: https://github.com/sustrik/libdill/blob/master/website/http_recvstatus.html This example demonstrates a typical HTTP request-response cycle using libdill functions, including connecting, sending a request and fields, completing the request, receiving the status with http_recvstatus, reading response fields, and then detaching and closing the socket. ```c int s = tcp_connect(&addr, -1); s = http_attach(s); http_sendrequest(s, "GET", "/", -1); http_sendfield(s, "Host", "www.example.org", -1); http_done(s, -1); char reason[256]; http_recvstatus(s, reason, sizeof(reason), -1); while(1) { char name[256]; char value[256]; int rc = http_recvfield(s, name, sizeof(name), value, sizeof(value), -1); if(rc == -1 && errno == EPIPE) break; } s = http_detach(s, -1); tcp_close(s); ``` -------------------------------- ### Basic libdill concurrent program example in C Source: https://github.com/sustrik/libdill/blob/master/website/index.html This example demonstrates launching two concurrent worker functions using libdill. Each worker prints a message at random intervals. The main function ensures the program runs for a specified duration before shutting down. ```c #include #include #include coroutine void worker(const char *text) { while(1) { printf("%s\n", text); msleep(now() + random() % 500); } } int main() { go(worker("Hello!")); go(worker("World!")); msleep(now() + 5000); return 0; } ``` -------------------------------- ### Define and launch a coroutine for client dialogue Source: https://github.com/sustrik/libdill/blob/master/website/tutorial-basics.html Defines a `dialogue` coroutine to handle communication with a single client and launches it using the `go()` construct. This allows concurrent handling of multiple clients. ```c coroutine void dialogue(int s) { int rc = msend(s, "What's your name?", 17, -1); // ... (rest of the dialogue logic) cleanup: rc = hclose(s); assert(rc == 0); } int main(int argc, char *argv[]) { // ... (server setup) while(1) { int s = tcp_accept(ls, NULL, -1); assert(s >= 0); s = suffix_attach(s, "\r\n", 2); assert(s >= 0); int cr = go(dialogue(s)); assert(cr >= 0); } // ... (server shutdown) } ```