]> git.tdb.fi Git - libs/net.git/blob - source/resolve.cpp
Add function to check if handshake is done
[libs/net.git] / source / resolve.cpp
1 /* $Id$
2
3 This file is part of libmspnet
4 Copyright © 2008, 2011  Mikkosoft Productions, Mikko Rasa
5 Distributed under the LGPL
6 */
7
8 #ifdef WIN32
9 #define _WIN32_WINNT 0x0501
10 #include <ws2tcpip.h>
11 #else
12 #include <netdb.h>
13 #endif
14 #include <msp/strings/formatter.h>
15 #include "socket.h"
16 #include "resolve.h"
17
18 using namespace std;
19
20 namespace Msp {
21 namespace Net {
22
23 SockAddr *resolve(const string &s, Family family)
24 {
25         string host, serv;
26         if(s[0]=='[')
27         {
28                 unsigned bracket = s.find(']');
29                 host = s.substr(1, bracket-1);
30                 unsigned colon = s.find(':', bracket);
31                 if(colon!=string::npos)
32                         serv = s.substr(colon+1);
33         }
34         else
35         {
36                 unsigned colon = s.find(':');
37                 if(colon!=string::npos)
38                 {
39                         host = s.substr(0, colon);
40                         serv = s.substr(colon+1);
41                 }
42                 else
43                         host = s;
44         }
45
46         addrinfo hints={0, family, 0, 0, 0, 0, 0, 0};
47         addrinfo *res;
48         const char *chost=(host.empty() ? 0 : host.c_str());
49         const char *cserv=(serv.empty() ? 0 : serv.c_str());
50         int err=getaddrinfo(chost, cserv, &hints, &res);
51         if(err==0)
52         {
53                 SockAddr *addr=SockAddr::create(*res->ai_addr);
54                 freeaddrinfo(res);
55                 return addr;
56         }
57         else
58 #ifdef WIN32
59                 throw Exception(format("Can't resolve '%s': %d", host, err));
60 #else
61                 throw Exception(format("Can't resolve '%s': %s", host, gai_strerror(err)));
62 #endif
63 }
64
65                 /*sockaddr sa;
66                 unsigned size=fill_sockaddr(sa);
67                 char hst[128];
68                 char srv[128];
69                 int err=getnameinfo(&sa, size, hst, 128, srv, 128, 0);
70                 if(err==0)
71                 {
72                         host=hst;
73                         serv=srv;
74                 }*/
75
76 } // namespace Net
77 } // namespace Msp