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