3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author João Luis M. Assirati
8 * Full author contact details are available in file CREDITS.
13 #include "support/socktools.h"
15 #if !defined (HAVE_FCNTL)
16 // We provide stubs because we don't (yet?) support the native OS API.
22 int listen(std::string const &, int)
33 } // namespace socktools
34 } // namespace support
37 #else // defined (HAVE_FCNTL)
39 #include "support/lyxlib.h"
43 #include <sys/socket.h>
55 #ifndef CXX_GLOBAL_CSTD
59 // This MACRO eppears to be defined only on Linux.
62 (sizeof (*(su)) - sizeof ((su)->sun_path) + strlen((su)->sun_path))
70 // Returns a local socket already in the "listen" state (or -1 in case
71 // of error). The first argument is the socket address, the second
72 // is the length of the queue for connections. If successful, a socket
73 // special file 'name' will be created in the filesystem.
74 int listen(string const & name, int queue)
76 int fd; // File descriptor for the socket
77 sockaddr_un addr; // Structure that hold the socket address
79 // We use 'name' to fill 'addr'
80 string::size_type len = name.size();
81 // the field sun_path in sockaddr_un is a char[108]
83 lyxerr << "lyx: Socket address '" << name << "' too long."
87 // Synonims for AF_UNIX are AF_LOCAL and AF_FILE
88 addr.sun_family = AF_UNIX;
89 name.copy(addr.sun_path, 107);
90 addr.sun_path[len] = '\0';
92 // This creates a file descriptor for the socket
93 // Synonims for PF_UNIX are PF_LOCAL and PF_FILE
94 // For local sockets, the protocol is always 0
95 // socket() returns -1 in case of error
96 if ((fd = ::socket(PF_UNIX, SOCK_STREAM, 0))== -1) {
97 lyxerr << "lyx: Could not create socket descriptor: "
98 << strerror(errno) << endl;
102 // Set NONBLOCK mode for the file descriptor
103 if (::fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
104 lyxerr << "lyx: Could not set NONBLOCK mode for socket descriptor: "
105 << strerror(errno) << endl;
110 // bind() gives the local address 'name' for 'fd', also creating
111 // the socket special file in the filesystem. bind() returns -1
113 if ((::bind (fd, reinterpret_cast<sockaddr *>(&addr), SUN_LEN(&addr))) == -1) {
114 lyxerr << "lyx: Could not bind address '" << name
115 << "' to socket descriptor: " << strerror(errno) << endl;
117 lyx::support::unlink(name);
121 // Puts the socket in listen state, that is, ready to accept
122 // connections. The second parameter of listen() defines the
123 // maximum length the queue of pending connections may grow to.
124 // It is not a restriction on the number of connections the socket
125 // can accept. Returns -1 in case of error
126 if (::listen (fd, queue) == -1) {
127 lyxerr << "lyx: Could not put socket in 'listen' state: "
128 << strerror(errno) << endl;
130 lyx::support::unlink(name);
137 // Returns a file descriptor for a new connection from the socket
138 // descriptor 'sd' (or -1 in case of error)
143 // Returns the new file descriptor or -1 in case of error
144 // Using null pointers for the second and third arguments
145 // dismiss all information about the connecting client
146 if ((fd = accept(sd, reinterpret_cast<sockaddr *>(0), reinterpret_cast<socklen_t *>(0))) == -1) {
147 lyxerr << "lyx: Could not accept connection: "
148 << strerror(errno) << endl;
152 // Sets NONBLOCK mode for the file descriptor
153 if (::fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
154 lyxerr << "lyx: Could not set NONBLOCK mode for connection: "
155 << strerror(errno) << endl;
162 } // namespace socktools
163 } // namespace support
166 #endif // defined (HAVE_FCNTL)