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