]> git.lyx.org Git - features.git/blob - src/support/socktools.C
Jo�o Assirati's lyxsocket patch.
[features.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::strerror;
26
27 using std::string;
28
29
30 namespace lyx {
31 namespace support {
32 namespace socktools {
33
34 // Returns a local socket already in the "listen" state (or -1 in case
35 // of error). The first argument is the socket address, the second
36 // is the length of the queue for connections. If successful, a socket
37 // special file 'name' will be created in the filesystem.
38 int listen(string const & name, int queue)
39 {
40         int fd; // File descriptor for the socket
41         sockaddr_un addr; // Structure that hold the socket address
42
43         // We use 'name' to fill 'addr'
44         string::size_type len = name.size();
45         // the field sun_path in sockaddr_un is a char[108]
46         if (len > 107) {
47                 lyxerr << "lyx: Socket address '" << name << "' too long."
48                        << endl;
49                 return -1;
50         }
51         // Synonims for AF_UNIX are AF_LOCAL and AF_FILE
52         addr.sun_family = AF_UNIX;
53         name.copy(addr.sun_path, 107);
54         addr.sun_path[len] = '\0';
55
56         // This creates a file descriptor for the socket
57         // Synonims for PF_UNIX are PF_LOCAL and PF_FILE
58         // For local sockets, the protocol is always 0
59         // socket() returns -1 in case of error
60         if ((fd = ::socket(PF_UNIX, SOCK_STREAM, 0))== -1) {
61                 lyxerr << "lyx: Could not create socket descriptor: "
62                        << strerror(errno) << endl;
63                 return -1;
64         }
65
66         // Set NONBLOCK mode for the file descriptor
67         if (::fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
68                 lyxerr << "lyx: Could not set NONBLOCK mode for socket descriptor: "
69                      << strerror(errno) << endl;
70                 ::close(fd);
71                 return -1;
72         }
73
74         // bind() gives the local address 'name' for 'fd', also creating
75         // the socket special file in the filesystem. bind() returns -1
76         // in case of error
77         if ((::bind (fd, reinterpret_cast<sockaddr *>(&addr), SUN_LEN(&addr))) == -1) {
78                 lyxerr << "lyx: Could not bind address '" << name
79                        << "' to socket descriptor: " << strerror(errno) << endl;
80                 ::close(fd);
81                 lyx::support::unlink(name);
82                 return -1;
83         }
84
85         // Puts the socket in listen state, that is, ready to accept
86         // connections. The second parameter of listen() defines the
87         // maximum length the queue of pending connections may grow to.
88         // It is not a restriction on the number of connections the socket
89         // can accept. Returns -1 in case of error
90         if (::listen (fd, queue) == -1) {
91                 lyxerr << "lyx: Could not put socket in 'listen' state: "
92                        << strerror(errno) << endl;
93                 ::close(fd);
94                 lyx::support::unlink(name);
95                 return -1;
96         }
97
98         return fd;
99 }
100
101 // Returns a file descriptor for a new connection from the socket
102 // descriptor 'sd' (or -1 in case of error)
103 int accept(int sd)
104 {
105         int fd;
106
107         // Returns the new file descriptor or -1 in case of error
108         // Using null pointers for the second and third arguments
109         // dismiss all information about the connecting client
110         if ((fd = accept(sd, reinterpret_cast<sockaddr *>(0), reinterpret_cast<socklen_t *>(0))) == -1) {
111                 lyxerr << "lyx: Could not accept connection: "
112                        << strerror(errno) << endl;
113                 return -1;
114         }
115
116         // Sets NONBLOCK mode for the file descriptor
117         if (::fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
118                 lyxerr << "lyx: Could not set NONBLOCK mode for connection: "
119                        << strerror(errno) << endl;
120                 ::close(fd);
121                 return -1;
122         }
123         return fd;
124 }
125
126 } // namespace socktools
127 } // namespace support
128 } // namespace lyx