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