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