~mingw-w64/mingw-w64/experimental

« back to all changes in this revision

Viewing changes to ros-privexp/mingw-w64-crt/libsrc/wspiapi/WspiapiLegacyGetAddrInfo.c

  • Committer: NightStrike
  • Date: 2010-08-11 22:20:57 UTC
  • Revision ID: svn-v4:4407c894-4637-0410-b4f5-ada5f102cad1:experimental:3266
Branch for adding option for supporting ros

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef WIN32_LEAN_AND_MEAN
 
2
#define WIN32_LEAN_AND_MEAN
 
3
#endif
 
4
#undef  __CRT__NO_INLINE
 
5
#define __CRT__NO_INLINE
 
6
#include <winsock2.h>
 
7
#include <wspiapi.h>
 
8
 
 
9
int WINAPI
 
10
WspiapiLegacyGetAddrInfo(const char *pszNodeName,
 
11
                         const char *pszServiceName,
 
12
                         const struct addrinfo *ptHints,
 
13
                         struct addrinfo **pptResult)
 
14
{
 
15
  int err = 0, iFlags = 0, iFamily = PF_UNSPEC, iSocketType = 0, iProtocol = 0;
 
16
  struct in_addr inAddress;
 
17
  struct servent *svc = NULL;
 
18
  char *pc = NULL;
 
19
  WINBOOL isCloned = FALSE;
 
20
  WORD tcpPort = 0, udpPort = 0, port = 0;
 
21
 
 
22
  *pptResult = NULL;
 
23
  if (!pszNodeName && !pszServiceName)
 
24
    return EAI_NONAME;
 
25
  if (ptHints)
 
26
    {
 
27
        if (ptHints->ai_addrlen != 0 || ptHints->ai_canonname != NULL
 
28
            || ptHints->ai_addr!=NULL || ptHints->ai_next != NULL)
 
29
          return EAI_FAIL;
 
30
        iFlags = ptHints->ai_flags;
 
31
        if ((iFlags & AI_CANONNAME) != 0 && !pszNodeName)
 
32
          return EAI_BADFLAGS;
 
33
        iFamily = ptHints->ai_family;
 
34
        if (iFamily != PF_UNSPEC && iFamily != PF_INET)
 
35
          return EAI_FAMILY;
 
36
        iSocketType = ptHints->ai_socktype;
 
37
        if (iSocketType != 0 && iSocketType != SOCK_STREAM && iSocketType != SOCK_DGRAM
 
38
            && iSocketType != SOCK_RAW)
 
39
          return EAI_SOCKTYPE;
 
40
        iProtocol = ptHints->ai_protocol;
 
41
    }
 
42
 
 
43
  if (pszServiceName)
 
44
    {
 
45
        port = (WORD) strtoul (pszServiceName, &pc, 10);
 
46
        if(*pc == 0)
 
47
          {
 
48
            port = tcpPort = udpPort = htons (port);
 
49
            if (iSocketType == 0)
 
50
              {
 
51
                isCloned = TRUE;
 
52
                iSocketType = SOCK_STREAM;
 
53
              }
 
54
          }
 
55
        else
 
56
          {
 
57
            if (iSocketType == 0 || iSocketType == SOCK_DGRAM)
 
58
              {
 
59
                svc = getservbyname(pszServiceName, "udp");
 
60
                if (svc)
 
61
                  port = udpPort = svc->s_port;
 
62
              }
 
63
            if (iSocketType == 0 || iSocketType == SOCK_STREAM)
 
64
              {
 
65
                svc = getservbyname(pszServiceName, "tcp");
 
66
                if (svc)
 
67
                  port = tcpPort = svc->s_port;
 
68
              }
 
69
            if (port == 0)
 
70
              return (iSocketType ? EAI_SERVICE : EAI_NONAME);
 
71
            if (iSocketType==0)
 
72
              {
 
73
                iSocketType = (tcpPort) ? SOCK_STREAM : SOCK_DGRAM;
 
74
                isCloned = (tcpPort && udpPort);
 
75
              }
 
76
          }
 
77
    }
 
78
  if (!pszNodeName || WspiapiParseV4Address(pszNodeName,&inAddress.s_addr))
 
79
    {
 
80
        if (!pszNodeName)
 
81
          {
 
82
            inAddress.s_addr = htonl ((iFlags & AI_PASSIVE) ? INADDR_ANY : INADDR_LOOPBACK);
 
83
          }
 
84
      *pptResult = WspiapiNewAddrInfo(iSocketType, iProtocol, port, inAddress.s_addr);
 
85
      if (!(*pptResult))
 
86
          err = EAI_MEMORY;
 
87
        if (!err && pszNodeName)
 
88
          {
 
89
            (*pptResult)->ai_flags |= AI_NUMERICHOST;
 
90
            if (iFlags & AI_CANONNAME)
 
91
              {
 
92
                (*pptResult)->ai_canonname =
 
93
                  WspiapiStrdup (inet_ntoa (inAddress));
 
94
                if (!(*pptResult)->ai_canonname)
 
95
                  err = EAI_MEMORY;
 
96
              }
 
97
          }
 
98
    }
 
99
  else if (iFlags & AI_NUMERICHOST)
 
100
    err = EAI_NONAME;
 
101
  else
 
102
    err = WspiapiLookupNode (pszNodeName, iSocketType, iProtocol, port,
 
103
                               (iFlags & AI_CANONNAME), pptResult);
 
104
  if (!err && isCloned)
 
105
    err = WspiapiClone(udpPort, *pptResult);
 
106
  if (err)
 
107
    {
 
108
        WspiapiLegacyFreeAddrInfo (*pptResult);
 
109
        *pptResult = NULL;
 
110
    }
 
111
  return err;
 
112
}