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