]> git.cworth.org Git - ttt/blob - src/ttt-socket.h
2005-11-28 Carl Worth <cworth@cworth.org>
[ttt] / src / ttt-socket.h
1 /* ttt-socket.c - Simple interface for creating sockets.
2  *
3  * Copyright © 2005 Carl Worth
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Author: Carl Worth <cworth@cworth.org>
20  */
21
22 #ifndef _TTT_SOCKET_H_
23 #define _TTT_SOCKET_H_
24
25 #include "ttt.h"
26
27 typedef void (*ttt_socket_accept_func_t) (void  *closure,
28                                           int    connected_socket);
29
30 /* Create a socket, bind it to the given host and port, and listen in
31  * preparation for incoming connections. See ttt_socket_accept for a
32  * way to actually accept those incoming connections.
33  *
34  * Lookup for host (e.g. /etc/hosts and DNA) and port (e.g /etc/services)
35  * will be performed if necessary.
36  *
37  * A special host value of "0.0.0.0" may be used to prepare a server
38  * socket to bind to all available addresses.
39  *
40  * Return value: The created socket as a file descriptor.
41  *
42  * Errors: If any error occurs, this function will not return.
43  */
44 int
45 ttt_socket_create_server (const char *host, const char *port);
46
47 /* Wait for an incoming connection on listen_socket, (which should be
48  * a valid socket on which bind and listen have already been
49  * called---see ttt_socket_create_server), then call the accept
50  * function with the closure argument and the new socket from the
51  * connection.
52  */
53 void
54 ttt_socket_accept (int                           listen_socket,
55                    ttt_socket_accept_func_t      accept,
56                    void                         *closure);
57
58 /* Performa a blocking read, until all count bytes are read from the
59  * socket to buf, which must be of size count or larger.
60  *
61  * Errors: If any errors occur, this function does not return.
62  */
63 void
64 ttt_socket_read (int     socket,
65                  void   *buf,
66                  size_t  count);
67
68 /* Perform a blocking write, until all count bytes are written from
69  * buf to the socket.
70  *
71  * Errors: If any errors occur, this function does not return.
72  */
73 void
74 ttt_socket_write (int            socket,
75                   const void    *buf,
76                   size_t         count);
77
78 #endif