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