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