]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/socket.cpp
e32bda90168358ac2d09c66d7b94c0c26cb2069f
[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 == '*')
31                 addr = "";
32
33         if (*addr)
34         {
35                 irc::sockets::aptosa(addr, port, &servaddr);
36         }
37         else
38         {
39                 memset(&servaddr, 0, sizeof(servaddr));
40                 if (port == -1)
41                 {
42                         /* Port -1: Means UDP IPV4 port binding - Special case
43                          * used by DNS engine.
44                          */
45                         servaddr.in4.sin_family = AF_INET;
46                 }
47                 else
48                 {
49                         /* No address */
50 #ifdef IPV6
51                         /* Default to ipv6 bind to all */
52                         servaddr.in6.sin6_family = AF_INET6;
53                         servaddr.in6.sin6_port = htons(port);
54 #else
55                         /* Bind ipv4 to all */
56                         servaddr.in4.sin_family = AF_INET;
57                         servaddr.in4.sin_port = htons(port);
58 #endif
59                 }
60         }
61         ret = SE->Bind(sockfd, &servaddr.sa, sa_size(servaddr));
62
63         if (ret < 0)
64         {
65                 return false;
66         }
67         else
68         {
69                 if (dolisten)
70                 {
71                         if (SE->Listen(sockfd, Config->MaxConn) == -1)
72                         {
73                                 this->Logs->Log("SOCKET",DEFAULT,"ERROR in listen(): %s",strerror(errno));
74                                 return false;
75                         }
76                         else
77                         {
78                                 this->Logs->Log("SOCKET",DEBUG,"New socket binding for %d with listen: %s:%d", sockfd, addr, port);
79                                 SE->NonBlocking(sockfd);
80                                 return true;
81                         }
82                 }
83                 else
84                 {
85                         this->Logs->Log("SOCKET",DEBUG,"New socket binding for %d without listen: %s:%d", sockfd, addr, port);
86                         return true;
87                 }
88         }
89 }
90
91 // Open a TCP Socket
92 int irc::sockets::OpenTCPSocket(const char* addr, int socktype)
93 {
94         int sockfd;
95         int on = 1;
96         addr = addr;
97         struct linger linger = { 0, 0 };
98         if (!*addr)
99         {
100 #ifdef IPV6
101                 sockfd = socket (PF_INET6, socktype, 0);
102                 if (sockfd < 0)
103 #endif
104                         sockfd = socket (PF_INET, socktype, 0);
105         }
106         else if (strchr(addr,':'))
107                 sockfd = socket (PF_INET6, socktype, 0);
108         else
109                 sockfd = socket (PF_INET, socktype, 0);
110
111         if (sockfd < 0)
112         {
113                 return ERROR;
114         }
115         else
116         {
117                 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
118                 /* This is BSD compatible, setting l_onoff to 0 is *NOT* http://web.irc.org/mla/ircd-dev/msg02259.html */
119                 linger.l_onoff = 1;
120                 linger.l_linger = 1;
121                 setsockopt(sockfd, SOL_SOCKET, SO_LINGER, &linger, sizeof(linger));
122                 return (sockfd);
123         }
124 }
125
126 // XXX: it would be VERY nice to genericize this so all listen stuff (server/client) could use the one function. -- w00t
127 int InspIRCd::BindPorts(FailedPortList &failed_ports)
128 {
129         char configToken[MAXBUF], Addr[MAXBUF], Type[MAXBUF], Desc[MAXBUF];
130         int bound = 0;
131         bool started_with_nothing = (ports.size() == 0);
132         std::vector<std::pair<std::string, int> > old_ports;
133
134         /* XXX: Make a copy of the old ip/port pairs here */
135         for (std::vector<ListenSocketBase *>::iterator o = ports.begin(); o != ports.end(); ++o)
136                 old_ports.push_back(make_pair((*o)->GetIP(), (*o)->GetPort()));
137
138         for (int count = 0; count < Config->ConfValueEnum("bind"); count++)
139         {
140                 Config->ConfValue("bind", "port", count, configToken, MAXBUF);
141                 Config->ConfValue("bind", "address", count, Addr, MAXBUF);
142                 Config->ConfValue("bind", "type", count, Type, MAXBUF);
143                 Config->ConfValue("bind", "ssl", count, Desc, MAXBUF);
144
145                 if (strncmp(Addr, "::ffff:", 7) == 0)
146                         this->Logs->Log("SOCKET",DEFAULT, "Using 4in6 (::ffff:) isn't recommended. You should bind IPv4 addresses directly instead.");
147
148                 if ((!*Type) || (!strcmp(Type,"clients")))
149                 {
150                         irc::portparser portrange(configToken, false);
151                         int portno = -1;
152                         while (0 != (portno = portrange.GetToken()))
153                         {
154                                 if (*Addr == '*')
155                                         *Addr = 0;
156
157                                 bool skip = false;
158                                 for (std::vector<ListenSocketBase *>::iterator n = ports.begin(); n != ports.end(); ++n)
159                                 {
160                                         if (((*n)->GetIP() == Addr) && ((*n)->GetPort() == portno))
161                                         {
162                                                 skip = true;
163                                                 /* XXX: Here, erase from our copy of the list */
164                                                 for (std::vector<std::pair<std::string, int> >::iterator k = old_ports.begin(); k != old_ports.end(); ++k)
165                                                 {
166                                                         if ((k->first == Addr) && (k->second == portno))
167                                                         {
168                                                                 old_ports.erase(k);
169                                                                 break;
170                                                         }
171                                                 }
172                                         }
173                                 }
174                                 if (!skip)
175                                 {
176                                         ClientListenSocket *ll = new ClientListenSocket(this, portno, Addr);
177                                         if (ll->GetFd() > -1)
178                                         {
179                                                 bound++;
180                                                 ll->SetDescription(*Desc ? Desc : "plaintext");
181                                                 ports.push_back(ll);
182                                         }
183                                         else
184                                         {
185                                                 failed_ports.push_back(std::make_pair((*Addr ? Addr : "*") + std::string(":") + ConvToStr(portno), strerror(errno)));
186                                         }
187                                 }
188                         }
189                 }
190         }
191
192         /* XXX: Here, anything left in our copy list, close as removed */
193         if (!started_with_nothing)
194         {
195                 for (size_t k = 0; k < old_ports.size(); ++k)
196                 {
197                         for (std::vector<ListenSocketBase *>::iterator n = ports.begin(); n != ports.end(); ++n)
198                         {
199                                 if (((*n)->GetIP() == old_ports[k].first) && ((*n)->GetPort() == old_ports[k].second))
200                                 {
201                                         this->Logs->Log("SOCKET",DEFAULT,"Port binding %s:%d was removed from the config file, closing.", old_ports[k].first.c_str(), old_ports[k].second);
202                                         delete *n;
203                                         ports.erase(n);
204                                         break;
205                                 }
206                         }
207                 }
208         }
209
210         return bound;
211 }
212
213 bool irc::sockets::aptosa(const char* addr, int port, irc::sockets::sockaddrs* sa)
214 {
215         memset(sa, 0, sizeof(*sa));
216         if (!addr || !*addr)
217         {
218 #ifdef IPV6
219                 sa->in6.sin6_family = AF_INET6;
220                 sa->in6.sin6_port = htons(port);
221 #else
222                 sa->in4.sin_family = AF_INET;
223                 sa->in4.sin_port = htons(port);
224 #endif
225                 return true;
226         }
227         else if (inet_pton(AF_INET, addr, &sa->in4.sin_addr) > 0)
228         {
229                 sa->in4.sin_family = AF_INET;
230                 sa->in4.sin_port = htons(port);
231                 return true;
232         }
233         else if (inet_pton(AF_INET6, addr, &sa->in6.sin6_addr) > 0)
234         {
235                 sa->in6.sin6_family = AF_INET6;
236                 sa->in6.sin6_port = htons(port);
237                 return true;
238         }
239         return false;
240 }
241
242 bool irc::sockets::satoap(const irc::sockets::sockaddrs* sa, std::string& addr, int &port) {
243         char addrv[INET6_ADDRSTRLEN+1];
244         if (sa->sa.sa_family == AF_INET)
245         {
246                 if (!inet_ntop(AF_INET, &sa->in4.sin_addr, addrv, sizeof(addrv)))
247                         return false;
248                 addr = addrv;
249                 port = ntohs(sa->in4.sin_port);
250                 return true;
251         }
252         else if (sa->sa.sa_family == AF_INET6)
253         {
254                 if (!inet_ntop(AF_INET6, &sa->in6.sin6_addr, addrv, sizeof(addrv)))
255                         return false;
256                 addr = addrv;
257                 port = ntohs(sa->in6.sin6_port);
258                 return true;
259         }
260         return false;
261 }
262
263 static const char all_zero[16] = {0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0 };
264
265 std::string irc::sockets::satouser(const irc::sockets::sockaddrs* sa) {
266         char buffer[MAXBUF];
267         if (sa->sa.sa_family == AF_INET)
268         {
269                 if (sa->in4.sin_addr.s_addr == 0)
270                 {
271                         sprintf(buffer, "*:%u", ntohs(sa->in4.sin_port));
272                 }
273                 else
274                 {
275                         const uint8_t* bits = reinterpret_cast<const uint8_t*>(&sa->in4.sin_addr);
276                         sprintf(buffer, "%d.%d.%d.%d:%u", bits[0], bits[1], bits[2], bits[3], ntohs(sa->in4.sin_port));
277                 }
278         }
279         else if (sa->sa.sa_family == AF_INET6)
280         {
281                 if (!memcmp(all_zero, &sa->in6.sin6_addr, 16))
282                 {
283                         sprintf(buffer, "*:%u", ntohs(sa->in6.sin6_port));
284                 }
285                 else
286                 {
287                         buffer[0] = '[';
288                         if (!inet_ntop(AF_INET6, &sa->in6.sin6_addr, buffer+1, MAXBUF - 10))
289                                 return "<unknown>"; // should never happen, buffer is large enough
290                         int len = strlen(buffer);
291                         // no need for snprintf, buffer has at least 9 chars left, max short len = 5
292                         sprintf(buffer + len, "]:%u", ntohs(sa->in6.sin6_port));
293                 }
294         }
295         else
296                 return "<unknown>";
297         return std::string(buffer);
298 }
299
300 int irc::sockets::sa_size(irc::sockets::sockaddrs& sa)
301 {
302         if (sa.sa.sa_family == AF_INET)
303                 return sizeof(sa.in4);
304         if (sa.sa.sa_family == AF_INET6)
305                 return sizeof(sa.in6);
306         return 0;
307 }