]> git.lyx.org Git - lyx.git/blob - src/support/socktools.cpp
Use *.* to select all files in the file selection dialog on Windows. Using shortcuts...
[lyx.git] / src / support / socktools.cpp
1 /**
2  * \file socktools.cpp
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 "support/socktools.h"
14 #include "support/FileName.h"
15
16 #if !defined (HAVE_FCNTL)
17 // We provide stubs because we don't (yet?) support the native OS API.
18
19 namespace lyx {
20 namespace support {
21 namespace socktools {
22
23 int listen(FileName const &, int)
24 {
25         return -1;
26 }
27
28
29 int accept(int)
30 {
31         return -1;
32 }
33
34 } // namespace socktools
35 } // namespace support
36 } // namespace lyx
37
38 #else // defined (HAVE_FCNTL)
39
40 #include "support/debug.h"
41
42 #include <sys/socket.h>
43 #include <sys/un.h>
44 #include <fcntl.h>
45 #ifdef HAVE_UNISTD_H
46 # include <unistd.h>
47 #endif
48
49 #include <cerrno>
50 #include <string>
51 #include <cstring>
52
53 using namespace std;
54
55
56 // This MACRO eppears to be defined only on Linux.
57 #if !defined(SUN_LEN)
58 #define SUN_LEN(su) \
59         (sizeof (*(su)) - sizeof ((su)->sun_path) + strlen((su)->sun_path))
60 #endif
61
62
63 namespace lyx {
64 namespace support {
65 namespace socktools {
66
67 // Returns a local socket already in the "listen" state (or -1 in case
68 // of error). The first argument is the socket address, the second
69 // is the length of the queue for connections. If successful, a socket
70 // special file 'name' will be created in the filesystem.
71 int listen(FileName const & name, int queue)
72 {
73         int fd; // File descriptor for the socket
74         sockaddr_un addr; // Structure that hold the socket address
75
76         // We use 'localname' to fill 'addr'
77         string const localname = name.toFilesystemEncoding();
78         string::size_type len = localname.size();
79         // the field sun_path in sockaddr_un is a char[108]
80         if (len > 107) {
81                 LYXERR0("lyx: Socket address '" << name.absFilename() << "' too long.");
82                 return -1;
83         }
84         // Synonims for AF_UNIX are AF_LOCAL and AF_FILE
85         addr.sun_family = AF_UNIX;
86         localname.copy(addr.sun_path, 107);
87         addr.sun_path[len] = '\0';
88
89         // This creates a file descriptor for the socket
90         // Synonims for PF_UNIX are PF_LOCAL and PF_FILE
91         // For local sockets, the protocol is always 0
92         // socket() returns -1 in case of error
93         if ((fd = ::socket(PF_UNIX, SOCK_STREAM, 0))== -1) {
94                 LYXERR0("lyx: Could not create socket descriptor: "
95                        << strerror(errno));
96                 return -1;
97         }
98
99         // Set NONBLOCK mode for the file descriptor
100         if (::fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
101                 LYXERR0("lyx: Could not set NONBLOCK mode for socket descriptor: "
102                      << strerror(errno));
103                 ::close(fd);
104                 return -1;
105         }
106
107         // bind() gives the local address 'name' for 'fd', also creating
108         // the socket special file in the filesystem. bind() returns -1
109         // in case of error
110         if ((::bind (fd, reinterpret_cast<sockaddr *>(&addr), SUN_LEN(&addr))) == -1) {
111                 LYXERR0("lyx: Could not bind address '" << name.absFilename()
112                        << "' to socket descriptor: " << strerror(errno));
113                 ::close(fd);
114                 name.removeFile();
115                 return -1;
116         }
117
118         // Puts the socket in listen state, that is, ready to accept
119         // connections. The second parameter of listen() defines the
120         // maximum length the queue of pending connections may grow to.
121         // It is not a restriction on the number of connections the socket
122         // can accept. Returns -1 in case of error
123         if (::listen (fd, queue) == -1) {
124                 LYXERR0("lyx: Could not put socket in 'listen' state: "
125                        << strerror(errno));
126                 ::close(fd);
127                 name.removeFile();
128                 return -1;
129         }
130
131         return fd;
132 }
133
134 // Returns a file descriptor for a new connection from the socket
135 // descriptor 'sd' (or -1 in case of error)
136 int accept(int sd)
137 {
138         int fd;
139
140         // Returns the new file descriptor or -1 in case of error
141         // Using null pointers for the second and third arguments
142         // dismiss all information about the connecting client
143         if ((fd = accept(sd, reinterpret_cast<sockaddr *>(0), reinterpret_cast<socklen_t *>(0))) == -1) {
144                 LYXERR0("lyx: Could not accept connection: "
145                        << strerror(errno));
146                 return -1;
147         }
148
149         // Sets NONBLOCK mode for the file descriptor
150         if (::fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
151                 LYXERR0("lyx: Could not set NONBLOCK mode for connection: "
152                        << strerror(errno));
153                 ::close(fd);
154                 return -1;
155         }
156         return fd;
157 }
158
159 } // namespace socktools
160 } // namespace support
161 } // namespace lyx
162
163 #endif // defined (HAVE_FCNTL)