]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/socket.cpp
* Fix inspsocket to not include uio.h on windows.
[user/henk/code/inspircd.git] / src / socket.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 /* $Core */
15
16 #include "inspircd.h"
17 #include "socket.h"
18 #include "socketengine.h"
19 using irc::sockets::sockaddrs;
20
21 /** This will bind a socket to a port. It works for UDP/TCP.
22  * It can only bind to IP addresses, if you wish to bind to hostnames
23  * you should first resolve them using class 'Resolver'.
24  */
25 bool InspIRCd::BindSocket(int sockfd, int port, const char* addr, bool dolisten)
26 {
27         sockaddrs servaddr;
28         int ret;
29
30         if ((*addr == '*' || *addr == '\0') && port == -1)
31         {
32                 /* Port -1: Means UDP IPV4 port binding - Special case
33                  * used by DNS engine.
34                  */
35                 memset(&servaddr, 0, sizeof(servaddr));
36                 servaddr.in4.sin_family = AF_INET;
37         }
38         else if (!irc::sockets::aptosa(addr, port, &servaddr))
39                 return false;
40
41         ret = SE->Bind(sockfd, &servaddr.sa, sa_size(servaddr));
42
43         if (ret < 0)
44         {
45                 return false;
46         }
47         else
48         {
49                 if (dolisten)
50                 {
51                         if (SE->Listen(sockfd, Config->MaxConn) == -1)
52                         {
53                                 this->Logs->Log("SOCKET",DEFAULT,"ERROR in listen(): %s",strerror(errno));
54                                 return false;
55                         }
56                         else
57                         {
58                                 this->Logs->Log("SOCKET",DEBUG,"New socket binding for %d with listen: %s:%d", sockfd, addr, port);
59                                 SE->NonBlocking(sockfd);
60                                 return true;
61                         }
62                 }
63                 else
64                 {
65                         this->Logs->Log("SOCKET",DEBUG,"New socket binding for %d without listen: %s:%d", sockfd, addr, port);
66                         return true;
67                 }
68         }
69 }
70
71 // Open a TCP Socket
72 int irc::sockets::OpenTCPSocket(const std::string& addr, int socktype)
73 {
74         int sockfd;
75         int on = 1;
76         struct linger linger = { 0, 0 };
77         if (addr.empty())
78         {
79 #ifdef IPV6
80                 sockfd = socket (PF_INET6, socktype, 0);
81                 if (sockfd < 0)
82 #endif
83                         sockfd = socket (PF_INET, socktype, 0);
84         }
85         else if (addr.find(':') != std::string::npos)
86                 sockfd = socket (PF_INET6, socktype, 0);
87         else
88                 sockfd = socket (PF_INET, socktype, 0);
89
90         if (sockfd < 0)
91         {
92                 return ERROR;
93         }
94         else
95         {
96                 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
97                 /* This is BSD compatible, setting l_onoff to 0 is *NOT* http://web.irc.org/mla/ircd-dev/msg02259.html */
98                 linger.l_onoff = 1;
99                 linger.l_linger = 1;
100                 setsockopt(sockfd, SOL_SOCKET, SO_LINGER, &linger, sizeof(linger));
101                 return (sockfd);
102         }
103 }
104
105 // XXX: it would be VERY nice to genericize this so all listen stuff (server/client) could use the one function. -- w00t
106 int InspIRCd::BindPorts(FailedPortList &failed_ports)
107 {
108         char configToken[MAXBUF], Addr[MAXBUF], Type[MAXBUF], Desc[MAXBUF];
109         int bound = 0;
110         std::vector<ListenSocketBase*> old_ports(ports.begin(), ports.end());
111
112         for (int count = 0; count < Config->ConfValueEnum("bind"); count++)
113         {
114                 Config->ConfValue("bind", "port", count, configToken, MAXBUF);
115                 Config->ConfValue("bind", "address", count, Addr, MAXBUF);
116                 Config->ConfValue("bind", "type", count, Type, MAXBUF);
117                 Config->ConfValue("bind", "ssl", count, Desc, MAXBUF);
118
119                 if (strncmp(Addr, "::ffff:", 7) == 0)
120                         this->Logs->Log("SOCKET",DEFAULT, "Using 4in6 (::ffff:) isn't recommended. You should bind IPv4 addresses directly instead.");
121
122                 if ((!*Type) || (!strcmp(Type,"clients")))
123                 {
124                         irc::portparser portrange(configToken, false);
125                         int portno = -1;
126                         while (0 != (portno = portrange.GetToken()))
127                         {
128                                 if (*Addr == '*')
129                                         *Addr = 0;
130
131                                 irc::sockets::sockaddrs bindspec;
132                                 irc::sockets::aptosa(Addr, portno, &bindspec);
133                                 std::string bind_readable = irc::sockets::satouser(&bindspec);
134
135                                 bool skip = false;
136                                 for (std::vector<ListenSocketBase *>::iterator n = old_ports.begin(); n != old_ports.end(); ++n)
137                                 {
138                                         if ((*n)->GetBindDesc() == bind_readable)
139                                         {
140                                                 skip = true;
141                                                 old_ports.erase(n);
142                                                 break;
143                                         }
144                                 }
145                                 if (!skip)
146                                 {
147                                         ClientListenSocket *ll = new ClientListenSocket(portno, Addr, "clients", *Desc ? Desc : "plaintext");
148                                         if (ll->GetFd() > -1)
149                                         {
150                                                 bound++;
151                                                 ports.push_back(ll);
152                                         }
153                                         else
154                                         {
155                                                 failed_ports.push_back(std::make_pair(bind_readable, strerror(errno)));
156                                                 delete ll;
157                                         }
158                                 }
159                         }
160                 }
161         }
162
163         std::vector<ListenSocketBase *>::iterator n = ports.begin();
164         for (std::vector<ListenSocketBase *>::iterator o = old_ports.begin(); o != old_ports.end(); ++o)
165         {
166                 while (n != ports.end() && *n != *o)
167                         n++;
168                 if (n == ports.end())
169                 {
170                         this->Logs->Log("SOCKET",ERROR,"Port bindings slipped out of vector, aborting close!");
171                         break;
172                 }
173
174                 this->Logs->Log("SOCKET",DEFAULT, "Port binding %s was removed from the config file, closing.",
175                         (*n)->GetBindDesc().c_str());
176                 delete *n;
177
178                 // this keeps the iterator valid, pointing to the next element
179                 n = ports.erase(n);
180         }
181
182         return bound;
183 }
184
185 bool irc::sockets::aptosa(const std::string& addr, int port, irc::sockets::sockaddrs* sa)
186 {
187         memset(sa, 0, sizeof(*sa));
188         if (addr.empty())
189         {
190 #ifdef IPV6
191                 sa->in6.sin6_family = AF_INET6;
192                 sa->in6.sin6_port = htons(port);
193 #else
194                 sa->in4.sin_family = AF_INET;
195                 sa->in4.sin_port = htons(port);
196 #endif
197                 return true;
198         }
199         else if (inet_pton(AF_INET, addr.c_str(), &sa->in4.sin_addr) > 0)
200         {
201                 sa->in4.sin_family = AF_INET;
202                 sa->in4.sin_port = htons(port);
203                 return true;
204         }
205         else if (inet_pton(AF_INET6, addr.c_str(), &sa->in6.sin6_addr) > 0)
206         {
207                 sa->in6.sin6_family = AF_INET6;
208                 sa->in6.sin6_port = htons(port);
209                 return true;
210         }
211         return false;
212 }
213
214 bool irc::sockets::satoap(const irc::sockets::sockaddrs* sa, std::string& addr, int &port) {
215         char addrv[INET6_ADDRSTRLEN+1];
216         if (sa->sa.sa_family == AF_INET)
217         {
218                 if (!inet_ntop(AF_INET, &sa->in4.sin_addr, addrv, sizeof(addrv)))
219                         return false;
220                 addr = addrv;
221                 port = ntohs(sa->in4.sin_port);
222                 return true;
223         }
224         else if (sa->sa.sa_family == AF_INET6)
225         {
226                 if (!inet_ntop(AF_INET6, &sa->in6.sin6_addr, addrv, sizeof(addrv)))
227                         return false;
228                 addr = addrv;
229                 port = ntohs(sa->in6.sin6_port);
230                 return true;
231         }
232         return false;
233 }
234
235 static const char all_zero[16] = {0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0 };
236
237 std::string irc::sockets::satouser(const irc::sockets::sockaddrs* sa) {
238         char buffer[MAXBUF];
239         if (sa->sa.sa_family == AF_INET)
240         {
241                 if (sa->in4.sin_addr.s_addr == 0)
242                 {
243                         sprintf(buffer, "*:%u", ntohs(sa->in4.sin_port));
244                 }
245                 else
246                 {
247                         const uint8_t* bits = reinterpret_cast<const uint8_t*>(&sa->in4.sin_addr);
248                         sprintf(buffer, "%d.%d.%d.%d:%u", bits[0], bits[1], bits[2], bits[3], ntohs(sa->in4.sin_port));
249                 }
250         }
251         else if (sa->sa.sa_family == AF_INET6)
252         {
253                 if (!memcmp(all_zero, &sa->in6.sin6_addr, 16))
254                 {
255                         sprintf(buffer, "*:%u", ntohs(sa->in6.sin6_port));
256                 }
257                 else
258                 {
259                         buffer[0] = '[';
260                         if (!inet_ntop(AF_INET6, &sa->in6.sin6_addr, buffer+1, MAXBUF - 10))
261                                 return "<unknown>"; // should never happen, buffer is large enough
262                         int len = strlen(buffer);
263                         // no need for snprintf, buffer has at least 9 chars left, max short len = 5
264                         sprintf(buffer + len, "]:%u", ntohs(sa->in6.sin6_port));
265                 }
266         }
267         else
268                 return "<unknown>";
269         return std::string(buffer);
270 }
271
272 int irc::sockets::sa_size(const irc::sockets::sockaddrs& sa)
273 {
274         if (sa.sa.sa_family == AF_INET)
275                 return sizeof(sa.in4);
276         if (sa.sa.sa_family == AF_INET6)
277                 return sizeof(sa.in6);
278         return 0;
279 }