]> git.lyx.org Git - lyx.git/blob - src/support/socktools.C
* lyxfunctional.h: delete compare_memfun and helper classes
[lyx.git] / src / support / socktools.C
1 /**
2  * \file socktools.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author João Luis M. Assirati
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "socktools.h"
14 #include "debug.h"
15 #include "lyxlib.h"
16
17 #include <sys/socket.h>
18 #include <sys/un.h>
19 #include <fcntl.h>
20 #include <unistd.h>
21
22 #include <cerrno>
23
24 using std::endl;
25 using std::string;
26
27 #ifndef CXX_GLOBAL_CSTD
28 using std::strerror;
29 #endif
30
31 // This MACRO eppears to be defined only on Linux.
32 #if !defined(SUN_LEN)
33 #define SUN_LEN(su) \
34         (sizeof (*(su)) - sizeof ((su)->sun_path) + strlen((su)->sun_path))
35 #endif
36
37
38 namespace lyx {
39 namespace support {
40 namespace socktools {
41
42 // Returns a local socket already in the "listen" state (or -1 in case
43 // of error). The first argument is the socket address, the second
44 // is the length of the queue for connections. If successful, a socket
45 // special file 'name' will be created in the filesystem.
46 int listen(string const & name, int queue)
47 {
48         int fd; // File descriptor for the socket
49         sockaddr_un addr; // Structure that hold the socket address
50
51         // We use 'name' to fill 'addr'
52         string::size_type len = name.size();
53         // the field sun_path in sockaddr_un is a char[108]
54         if (len > 107) {
55                 lyxerr << "lyx: Socket address '" << name << "' too long."
56                        << endl;
57                 return -1;
58         }
59         // Synonims for AF_UNIX are AF_LOCAL and AF_FILE
60         addr.sun_family = AF_UNIX;
61         name.copy(addr.sun_path, 107);
62         addr.sun_path[len] = '\0';
63
64         // This creates a file descriptor for the socket
65         // Synonims for PF_UNIX are PF_LOCAL and PF_FILE
66         // For local sockets, the protocol is always 0
67         // socket() returns -1 in case of error
68         if ((fd = ::socket(PF_UNIX, SOCK_STREAM, 0))== -1) {
69                 lyxerr << "lyx: Could not create socket descriptor: "
70                        << strerror(errno) << endl;
71                 return -1;
72         }
73
74         // Set NONBLOCK mode for the file descriptor
75         if (::fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
76                 lyxerr << "lyx: Could not set NONBLOCK mode for socket descriptor: "
77                      << strerror(errno) << endl;
78                 ::close(fd);
79                 return -1;
80         }
81
82         // bind() gives the local address 'name' for 'fd', also creating
83         // the socket special file in the filesystem. bind() returns -1
84         // in case of error
85         if ((::bind (fd, reinterpret_cast<sockaddr *>(&addr), SUN_LEN(&addr))) == -1) {
86                 lyxerr << "lyx: Could not bind address '" << name
87                        << "' to socket descriptor: " << strerror(errno) << endl;
88                 ::close(fd);
89                 lyx::support::unlink(name);
90                 return -1;
91         }
92
93         // Puts the socket in listen state, that is, ready to accept
94         // connections. The second parameter of listen() defines the
95         // maximum length the queue of pending connections may grow to.
96         // It is not a restriction on the number of connections the socket
97         // can accept. Returns -1 in case of error
98         if (::listen (fd, queue) == -1) {
99                 lyxerr << "lyx: Could not put socket in 'listen' state: "
100                        << strerror(errno) << endl;
101                 ::close(fd);
102                 lyx::support::unlink(name);
103                 return -1;
104         }
105
106         return fd;
107 }
108
109 // Returns a file descriptor for a new connection from the socket
110 // descriptor 'sd' (or -1 in case of error)
111 int accept(int sd)
112 {
113         int fd;
114
115         // Returns the new file descriptor or -1 in case of error
116         // Using null pointers for the second and third arguments
117         // dismiss all information about the connecting client
118         if ((fd = accept(sd, reinterpret_cast<sockaddr *>(0), reinterpret_cast<socklen_t *>(0))) == -1) {
119                 lyxerr << "lyx: Could not accept connection: "
120                        << strerror(errno) << endl;
121                 return -1;
122         }
123
124         // Sets NONBLOCK mode for the file descriptor
125         if (::fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
126                 lyxerr << "lyx: Could not set NONBLOCK mode for connection: "
127                        << strerror(errno) << endl;
128                 ::close(fd);
129                 return -1;
130         }
131         return fd;
132 }
133
134 } // namespace socktools
135 } // namespace support
136 } // namespace lyx