]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/socket.cpp
Compile fixes for 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                 /* This is BSD compatible, setting l_onoff to 0 is *NOT* http://web.irc.org/mla/ircd-dev/msg02259.html */
97                 linger.l_onoff = 1;
98                 linger.l_linger = 1;
99 #ifdef WINDOWS
100                 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof(on));
101                 setsockopt(sockfd, SOL_SOCKET, SO_LINGER, (char*)&linger, sizeof(linger));
102 #elif
103                 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
104                 setsockopt(sockfd, SOL_SOCKET, SO_LINGER, &linger, sizeof(linger));
105 #endif
106                 return (sockfd);
107         }
108 }
109
110 // XXX: it would be VERY nice to genericize this so all listen stuff (server/client) could use the one function. -- w00t
111 int InspIRCd::BindPorts(FailedPortList &failed_ports)
112 {
113         char configToken[MAXBUF], Addr[MAXBUF], Type[MAXBUF], Desc[MAXBUF];
114         int bound = 0;
115         std::vector<ListenSocketBase*> old_ports(ports.begin(), ports.end());
116
117         for (int count = 0; count < Config->ConfValueEnum("bind"); count++)
118         {
119                 Config->ConfValue("bind", "port", count, configToken, MAXBUF);
120                 Config->ConfValue("bind", "address", count, Addr, MAXBUF);
121                 Config->ConfValue("bind", "type", count, Type, MAXBUF);
122                 Config->ConfValue("bind", "ssl", count, Desc, MAXBUF);
123
124                 if (strncmp(Addr, "::ffff:", 7) == 0)
125                         this->Logs->Log("SOCKET",DEFAULT, "Using 4in6 (::ffff:) isn't recommended. You should bind IPv4 addresses directly instead.");
126
127                 if ((!*Type) || (!strcmp(Type,"clients")))
128                 {
129                         irc::portparser portrange(configToken, false);
130                         int portno = -1;
131                         while (0 != (portno = portrange.GetToken()))
132                         {
133                                 if (*Addr == '*')
134                                         *Addr = 0;
135
136                                 irc::sockets::sockaddrs bindspec;
137                                 irc::sockets::aptosa(Addr, portno, &bindspec);
138                                 std::string bind_readable = irc::sockets::satouser(&bindspec);
139
140                                 bool skip = false;
141                                 for (std::vector<ListenSocketBase *>::iterator n = old_ports.begin(); n != old_ports.end(); ++n)
142                                 {
143                                         if ((*n)->GetBindDesc() == bind_readable)
144                                         {
145                                                 skip = true;
146                                                 old_ports.erase(n);
147                                                 break;
148                                         }
149                                 }
150                                 if (!skip)
151                                 {
152                                         ClientListenSocket *ll = new ClientListenSocket(portno, Addr, "clients", *Desc ? Desc : "plaintext");
153                                         if (ll->GetFd() > -1)
154                                         {
155                                                 bound++;
156                                                 ports.push_back(ll);
157                                         }
158                                         else
159                                         {
160                                                 failed_ports.push_back(std::make_pair(bind_readable, strerror(errno)));
161                                                 delete ll;
162                                         }
163                                 }
164                         }
165                 }
166         }
167
168         std::vector<ListenSocketBase *>::iterator n = ports.begin();
169         for (std::vector<ListenSocketBase *>::iterator o = old_ports.begin(); o != old_ports.end(); ++o)
170         {
171                 while (n != ports.end() && *n != *o)
172                         n++;
173                 if (n == ports.end())
174                 {
175                         this->Logs->Log("SOCKET",ERROR,"Port bindings slipped out of vector, aborting close!");
176                         break;
177                 }
178
179                 this->Logs->Log("SOCKET",DEFAULT, "Port binding %s was removed from the config file, closing.",
180                         (*n)->GetBindDesc().c_str());
181                 delete *n;
182
183                 // this keeps the iterator valid, pointing to the next element
184                 n = ports.erase(n);
185         }
186
187         return bound;
188 }
189
190 bool irc::sockets::aptosa(const std::string& addr, int port, irc::sockets::sockaddrs* sa)
191 {
192         memset(sa, 0, sizeof(*sa));
193         if (addr.empty())
194         {
195 #ifdef IPV6
196                 sa->in6.sin6_family = AF_INET6;
197                 sa->in6.sin6_port = htons(port);
198 #else
199                 sa->in4.sin_family = AF_INET;
200                 sa->in4.sin_port = htons(port);
201 #endif
202                 return true;
203         }
204         else if (inet_pton(AF_INET, addr.c_str(), &sa->in4.sin_addr) > 0)
205         {
206                 sa->in4.sin_family = AF_INET;
207                 sa->in4.sin_port = htons(port);
208                 return true;
209         }
210         else if (inet_pton(AF_INET6, addr.c_str(), &sa->in6.sin6_addr) > 0)
211         {
212                 sa->in6.sin6_family = AF_INET6;
213                 sa->in6.sin6_port = htons(port);
214                 return true;
215         }
216         return false;
217 }
218
219 bool irc::sockets::satoap(const irc::sockets::sockaddrs* sa, std::string& addr, int &port) {
220         char addrv[INET6_ADDRSTRLEN+1];
221         if (sa->sa.sa_family == AF_INET)
222         {
223                 if (!inet_ntop(AF_INET, &sa->in4.sin_addr, addrv, sizeof(addrv)))
224                         return false;
225                 addr = addrv;
226                 port = ntohs(sa->in4.sin_port);
227                 return true;
228         }
229         else if (sa->sa.sa_family == AF_INET6)
230         {
231                 if (!inet_ntop(AF_INET6, &sa->in6.sin6_addr, addrv, sizeof(addrv)))
232                         return false;
233                 addr = addrv;
234                 port = ntohs(sa->in6.sin6_port);
235                 return true;
236         }
237         return false;
238 }
239
240 static const char all_zero[16] = {0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0 };
241
242 std::string irc::sockets::satouser(const irc::sockets::sockaddrs* sa) {
243         char buffer[MAXBUF];
244         if (sa->sa.sa_family == AF_INET)
245         {
246                 if (sa->in4.sin_addr.s_addr == 0)
247                 {
248                         sprintf(buffer, "*:%u", ntohs(sa->in4.sin_port));
249                 }
250                 else
251                 {
252                         const uint8_t* bits = reinterpret_cast<const uint8_t*>(&sa->in4.sin_addr);
253                         sprintf(buffer, "%d.%d.%d.%d:%u", bits[0], bits[1], bits[2], bits[3], ntohs(sa->in4.sin_port));
254                 }
255         }
256         else if (sa->sa.sa_family == AF_INET6)
257         {
258                 if (!memcmp(all_zero, &sa->in6.sin6_addr, 16))
259                 {
260                         sprintf(buffer, "*:%u", ntohs(sa->in6.sin6_port));
261                 }
262                 else
263                 {
264                         buffer[0] = '[';
265                         if (!inet_ntop(AF_INET6, &sa->in6.sin6_addr, buffer+1, MAXBUF - 10))
266                                 return "<unknown>"; // should never happen, buffer is large enough
267                         int len = strlen(buffer);
268                         // no need for snprintf, buffer has at least 9 chars left, max short len = 5
269                         sprintf(buffer + len, "]:%u", ntohs(sa->in6.sin6_port));
270                 }
271         }
272         else
273                 return "<unknown>";
274         return std::string(buffer);
275 }
276
277 int irc::sockets::sa_size(const irc::sockets::sockaddrs& sa)
278 {
279         if (sa.sa.sa_family == AF_INET)
280                 return sizeof(sa.in4);
281         if (sa.sa.sa_family == AF_INET6)
282                 return sizeof(sa.in6);
283         return 0;
284 }