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