]>
git.saurik.com Git - apt.git/blob - methods/rfc2553emu.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: rfc2553emu.cc,v 1.4 1999/12/09 03:45:56 jgg Exp $
4 /* ######################################################################
6 RFC 2553 Emulation - Provides emulation for RFC 2553 getaddrinfo,
7 freeaddrinfo and getnameinfo
9 This is really C code, it just has a .cc extensions to play nicer with
12 Originally written by Jason Gunthorpe <jgg@debian.org> and placed into
13 the Public Domain, do with it what you will.
15 ##################################################################### */
17 #include "rfc2553emu.h"
19 #include <arpa/inet.h>
23 #ifndef HAVE_GETADDRINFO
24 // getaddrinfo - Resolve a hostname /*{{{*/
25 // ---------------------------------------------------------------------
27 int getaddrinfo(const char *nodename
, const char *servname
,
28 const struct addrinfo
*hints
,
29 struct addrinfo
**res
)
31 struct addrinfo
**Result
;
38 Addr
= gethostbyname(nodename
);
41 if (h_errno
== TRY_AGAIN
)
43 if (h_errno
== NO_RECOVERY
)
49 if (Addr
->h_addr_list
[0] == 0)
52 // Try to convert the service as a number
53 Port
= htons(strtol(servname
,(char **)&End
,0));
56 if (hints
!= 0 && hints
->ai_socktype
!= 0)
57 Proto
= hints
->ai_socktype
;
59 // Not a number, must be a name.
60 if (End
!= servname
+ strlen(servname
))
62 struct servent
*Srv
= 0;
64 // Do a lookup in the service database
65 if (hints
== 0 || hints
->ai_socktype
== SOCK_STREAM
)
66 Srv
= getservbyname(servname
,"tcp");
67 if (hints
!= 0 && hints
->ai_socktype
== SOCK_DGRAM
)
68 Srv
= getservbyname(servname
,"udp");
72 // Get the right protocol
74 if (strcmp(Srv
->s_proto
,"tcp") == 0)
78 if (strcmp(Srv
->s_proto
,"udp") == 0)
84 if (hints
!= 0 && hints
->ai_socktype
!= Proto
&&
85 hints
->ai_socktype
!= 0)
89 // Start constructing the linked list
91 for (CurAddr
= Addr
->h_addr_list
; *CurAddr
!= 0; CurAddr
++)
93 // New result structure
94 *Result
= (struct addrinfo
*)calloc(sizeof(**Result
),1);
103 (*Result
)->ai_family
= AF_INET
;
104 (*Result
)->ai_socktype
= Proto
;
106 // If we have the IPPROTO defines we can set the protocol field
108 if (Proto
== SOCK_STREAM
)
109 (*Result
)->ai_protocol
= IPPROTO_TCP
;
110 if (Proto
== SOCK_DGRAM
)
111 (*Result
)->ai_protocol
= IPPROTO_UDP
;
114 // Allocate space for the address
115 (*Result
)->ai_addrlen
= sizeof(struct sockaddr_in
);
116 (*Result
)->ai_addr
= (struct sockaddr
*)calloc(sizeof(sockaddr_in
),1);
117 if ((*Result
)->ai_addr
== 0)
124 ((struct sockaddr_in
*)(*Result
)->ai_addr
)->sin_family
= AF_INET
;
125 ((struct sockaddr_in
*)(*Result
)->ai_addr
)->sin_port
= Port
;
126 ((struct sockaddr_in
*)(*Result
)->ai_addr
)->sin_addr
= *(in_addr
*)(*CurAddr
);
128 Result
= &(*Result
)->ai_next
;
134 // freeaddrinfo - Free the result of getaddrinfo /*{{{*/
135 // ---------------------------------------------------------------------
137 void freeaddrinfo(struct addrinfo
*ai
)
139 struct addrinfo
*Tmp
;
149 #endif // HAVE_GETADDRINFO
151 #ifndef HAVE_GETNAMEINFO
152 // getnameinfo - Convert a sockaddr to a string /*{{{*/
153 // ---------------------------------------------------------------------
155 int getnameinfo(const struct sockaddr
*sa
, socklen_t salen
,
156 char *host
, size_t hostlen
,
157 char *serv
, size_t servlen
,
160 struct sockaddr_in
*sin
= (struct sockaddr_in
*)sa
;
162 // This routine only supports internet addresses
163 if (sa
->sa_family
!= AF_INET
)
164 return EAI_ADDRFAMILY
;
168 // Try to resolve the hostname
169 if ((flags
& NI_NUMERICHOST
) != NI_NUMERICHOST
)
171 struct hostent
*Ent
= gethostbyaddr((char *)&sin
->sin_addr
,sizeof(sin
->sin_addr
),
174 strncpy(host
,Ent
->h_name
,hostlen
);
177 if ((flags
& NI_NAMEREQD
) == NI_NAMEREQD
)
179 if (h_errno
== TRY_AGAIN
)
181 if (h_errno
== NO_RECOVERY
)
186 flags
|= NI_NUMERICHOST
;
190 // Resolve as a plain numberic
191 if ((flags
& NI_NUMERICHOST
) == NI_NUMERICHOST
)
193 strncpy(host
,inet_ntoa(sin
->sin_addr
),hostlen
);
199 // Try to resolve the hostname
200 if ((flags
& NI_NUMERICSERV
) != NI_NUMERICSERV
)
203 if ((flags
& NI_DATAGRAM
) == NI_DATAGRAM
)
204 Ent
= getservbyport(sin
->sin_port
,"udp");
206 Ent
= getservbyport(sin
->sin_port
,"tcp");
209 strncpy(serv
,Ent
->s_name
,servlen
);
212 if ((flags
& NI_NAMEREQD
) == NI_NAMEREQD
)
215 flags
|= NI_NUMERICSERV
;
219 // Resolve as a plain numberic
220 if ((flags
& NI_NUMERICSERV
) == NI_NUMERICSERV
)
222 snprintf(serv
,servlen
,"%u",sin
->sin_port
);
229 #endif // HAVE_GETNAMEINFO