]> git.cworth.org Git - ttt/blob - src/ttt-socket.h
Add a dependency of ttt-client.c on ttt-lex.h to fix the build.
[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 /* Create a socket, and connect it to the given host and port,
59  * returing it in socket_ret.
60  *
61  * Lookup for host (e.g. /etc/hosts and DNS) and port (e.g /etc/services)
62  * will be performed if necessary.
63  *
64  * Return value: TTT_STATUS_SUCCESS if successful.
65  *               TTT_STATUS_CONNECTION_REFUSED: no server listening.
66  *               TTT_STATUS_NETWORK_UNREACHABLE: <obvious meaning>
67  *
68  * Errors: If any error other than those listed above occurs, this
69  * function will not return.
70  */
71 ttt_status_t
72 ttt_socket_create_client (const char    *host,
73                           const char    *port,
74                           int           *socket_ret);
75
76 /* Perform a blocking read, until all count bytes are read from the
77  * socket to buf, which must be of size count or larger.
78  *
79  * Errors: If any errors occur, this function does not return.
80  */
81 ssize_t
82 ttt_socket_read (int     socket,
83                  void   *buf,
84                  size_t  count);
85
86 /* Perform a blocking write, until all count bytes are written from
87  * buf to the socket.
88  *
89  * Errors: If any errors occur, this function does not return.
90  */
91 void
92 ttt_socket_write (int            socket,
93                   const void    *buf,
94                   size_t         count);
95
96 #endif