.\" Copyright (c) 2001-2002 Packet Design, LLC. .\" All rights reserved. .\" .\" Subject to the following obligations and disclaimer of warranty, .\" use and redistribution of this software, in source or object code .\" forms, with or without modifications are expressly permitted by .\" Packet Design; provided, however, that: .\" .\" (i) Any and all reproductions of the source or object code .\" must include the copyright notice above and the following .\" disclaimer of warranties; and .\" (ii) No rights are granted, in any manner or form, to use .\" Packet Design trademarks, including the mark "PACKET DESIGN" .\" on advertising, endorsements, or otherwise except as such .\" appears in the above copyright notice or in the software. .\" .\" THIS SOFTWARE IS BEING PROVIDED BY PACKET DESIGN "AS IS", AND .\" TO THE MAXIMUM EXTENT PERMITTED BY LAW, PACKET DESIGN MAKES NO .\" REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING .\" THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED .\" WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, .\" OR NON-INFRINGEMENT. PACKET DESIGN DOES NOT WARRANT, GUARANTEE, .\" OR MAKE ANY REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS .\" OF THE USE OF THIS SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, .\" RELIABILITY OR OTHERWISE. IN NO EVENT SHALL PACKET DESIGN BE .\" LIABLE FOR ANY DAMAGES RESULTING FROM OR ARISING OUT OF ANY USE .\" OF THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, ANY DIRECT, .\" INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, PUNITIVE, OR CONSEQUENTIAL .\" DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, LOSS OF .\" USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY THEORY OF .\" LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF .\" THE USE OF THIS SOFTWARE, EVEN IF PACKET DESIGN IS ADVISED OF .\" THE POSSIBILITY OF SUCH DAMAGE. .\" .\" Author: Archie Cobbs .\" .\" $Id: tcp_server.3,v 1.1.1.1 2012/02/21 23:25:53 misho Exp $ .\" .Dd April 22, 2002 .Dt TCP_SERVER 3 .Os .Sh NAME .Nm tcp_server .Nd generic TCP server .Sh LIBRARY PDEL Library (libpdel, \-lpdel) .Sh SYNOPSIS .In sys/types.h .In netinet/in.h .In stdio.h .In pdel/net/tcp_server.h .Ft "struct tcp_server *" .Fn tcp_server_start "struct pevent_ctx *ctx" "void *cookie" "const char *mtype" "struct in_addr ip" "u_int16_t port" "u_int max_conn" "u_int conn_timeout" "tcp_setup_t *setup" "tcp_handler_t *handler" "tcp_teardown_t *teardown" .Ft "void" .Fn tcp_server_stop "struct tcp_server **servp" .Ft "void *" .Fn tcp_server_get_cookie "struct tcp_server *serv" .Ft "struct tcp_server *" .Fn tcp_connection_get_server "struct tcp_connection *conn" .Ft "void *" .Fn tcp_connection_get_cookie "struct tcp_connection *conn" .Ft "int" .Fn tcp_connection_get_fd "struct tcp_connection *conn" .Ft "FILE *" .Fn tcp_connection_get_fp "struct tcp_connection *conn" .Ft "void" .Fn tcp_connection_get_peer "struct tcp_connection *conn" "struct sockaddr_in *sin" .Sh DESCRIPTION These functions provide support for implementing TCP servers. .Pp .Fn tcp_server_start creates a new TCP server listening for connections on IP address .Fa ip and port .Fa port . Memory for the .Nm tcp_server is dynamically allocated with .Xr typed_mem 3 type .Fa mtype . At most .Fa max_conn simultaneous connections will be allowed. If .Fa conn_timeout is non-zero, then if an attempt to read from or write to a peer blocks for more than .Fa conn_timeout seconds, the connection will be terminated. .Fa ctx is a .Xr pevent 3 event context used to listen for incoming connections. .Pp The .Fa setup , .Fa handler , and .Fa teardown arguments are pointers to functions having these types: .Pp .Bd -literal -compact -offset 3n typedef void *tcp_setup_t(struct tcp_connection *conn); typedef void tcp_handler_t(struct tcp_connection *conn); typedef void tcp_teardown_t(struct tcp_connection *conn); .Ed .Pp Each connection is represented by a .Nm tcp_connection object. For each new connection, .Fn setup is called to initialize user state for the connection. If setup is unsuccessful, .Fn setup should return .Dv NULL and set .Va errno appropriately, and the connection will be closed. Otherwise, it should return a non-NULL cookie; this cookie can be accessed later in .Fn handler and .Fn teardown by calling .Fn tcp_connection_get_cookie . .Pp .Fn handler is used to service the connection. The thread calling this function may be canceled at any cancellation point; therefore, .Fn handler may need to register cleanup hooks to clean up allocated resources should that happen. .Pp .Fn teardown is called when .Fn handler returns, or the thread is canceled. If .Fn setup returns successfully, .Fn teardown is guaranteed to be called exactly once. .Pp .Fn tcp_server_stop stops and destroys a .Nm tcp_server . All existing connection threads are canceled and the current thread blocks until .Fn teardown has been called for each connection. .Pp .Fn tcp_connection_get_peer retrieves the peer's address. .Pp .Fn tcp_connection_get_server returns the .Nm tcp_server object associated with the connection .Fa conn . .Pp .Fn tcp_connection_get_fd returns the TCP socket associated with the connection. .Pp .Fn tcp_connection_get_fp returns a stream opened on top of the socket for use with stream based I/O. .Pp The user code should .Em not close either the socket or the stream; these will be closed automatically when the connection is terminated. .Sh RETURN VALUES .Fn tcp_server_start returns .Dv NULL to indicate an error, with .Va errno set appropriately. .Sh SEE ALSO .Xr libpdel 3 , .Xr pevent 3 , .Xr typed_mem 3 .Sh HISTORY The PDEL library was developed at Packet Design, LLC. .Dv "http://www.packetdesign.com/" .Sh AUTHORS .An Archie Cobbs Aq archie@freebsd.org