]>
git.saurik.com Git - apt.git/blob - methods/rfc2553emu.cc
22daa22315a47582dcacd1cf9534935761a67bb7
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: rfc2553emu.cc,v 1.7 2000/06/18 06:04:45 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>
20 #include <netinet/in.h>
24 #ifndef HAVE_GETADDRINFO
25 // getaddrinfo - Resolve a hostname /*{{{*/
26 // ---------------------------------------------------------------------
28 int getaddrinfo(const char *nodename
, const char *servname
,
29 const struct addrinfo
*hints
,
30 struct addrinfo
**res
)
32 struct addrinfo
**Result
= res
;
39 Addr
= gethostbyname(nodename
);
42 if (h_errno
== TRY_AGAIN
)
44 if (h_errno
== NO_RECOVERY
)
50 if (Addr
->h_addr_list
[0] == 0)
53 // Try to convert the service as a number
54 Port
= htons(strtol(servname
,(char **)&End
,0));
57 if (hints
!= 0 && hints
->ai_socktype
!= 0)
58 Proto
= hints
->ai_socktype
;
60 // Not a number, must be a name.
61 if (End
!= servname
+ strlen(servname
))
63 struct servent
*Srv
= 0;
65 // Do a lookup in the service database
66 if (hints
== 0 || hints
->ai_socktype
== SOCK_STREAM
)
67 Srv
= getservbyname(servname
,"tcp");
68 if (hints
!= 0 && hints
->ai_socktype
== SOCK_DGRAM
)
69 Srv
= getservbyname(servname
,"udp");
73 // Get the right protocol
75 if (strcmp(Srv
->s_proto
,"tcp") == 0)
79 if (strcmp(Srv
->s_proto
,"udp") == 0)
85 if (hints
!= 0 && hints
->ai_socktype
!= Proto
&&
86 hints
->ai_socktype
!= 0)
90 // Start constructing the linked list
92 for (CurAddr
= Addr
->h_addr_list
; *CurAddr
!= 0; CurAddr
++)
94 // New result structure
95 *Result
= (struct addrinfo
*)calloc(sizeof(**Result
),1);
104 (*Result
)->ai_family
= AF_INET
;
105 (*Result
)->ai_socktype
= Proto
;
107 // If we have the IPPROTO defines we can set the protocol field
109 if (Proto
== SOCK_STREAM
)
110 (*Result
)->ai_protocol
= IPPROTO_TCP
;
111 if (Proto
== SOCK_DGRAM
)
112 (*Result
)->ai_protocol
= IPPROTO_UDP
;
115 // Allocate space for the address
116 (*Result
)->ai_addrlen
= sizeof(struct sockaddr_in
);
117 (*Result
)->ai_addr
= (struct sockaddr
*)calloc(sizeof(sockaddr_in
),1);
118 if ((*Result
)->ai_addr
== 0)
125 ((struct sockaddr_in
*)(*Result
)->ai_addr
)->sin_family
= AF_INET
;
126 ((struct sockaddr_in
*)(*Result
)->ai_addr
)->sin_port
= Port
;
127 ((struct sockaddr_in
*)(*Result
)->ai_addr
)->sin_addr
= *(in_addr
*)(*CurAddr
);
129 Result
= &(*Result
)->ai_next
;
135 // freeaddrinfo - Free the result of getaddrinfo /*{{{*/
136 // ---------------------------------------------------------------------
138 void freeaddrinfo(struct addrinfo
*ai
)
140 struct addrinfo
*Tmp
;
150 #endif // HAVE_GETADDRINFO
152 #ifndef HAVE_GETNAMEINFO
153 // getnameinfo - Convert a sockaddr to a string /*{{{*/
154 // ---------------------------------------------------------------------
156 int getnameinfo(const struct sockaddr
*sa
, socklen_t salen
,
157 char *host
, size_t hostlen
,
158 char *serv
, size_t servlen
,
161 struct sockaddr_in
*sin
= (struct sockaddr_in
*)sa
;
163 // This routine only supports internet addresses
164 if (sa
->sa_family
!= AF_INET
)
165 return EAI_ADDRFAMILY
;
169 // Try to resolve the hostname
170 if ((flags
& NI_NUMERICHOST
) != NI_NUMERICHOST
)
172 struct hostent
*Ent
= gethostbyaddr((char *)&sin
->sin_addr
,sizeof(sin
->sin_addr
),
175 strncpy(host
,Ent
->h_name
,hostlen
);
178 if ((flags
& NI_NAMEREQD
) == NI_NAMEREQD
)
180 if (h_errno
== TRY_AGAIN
)
182 if (h_errno
== NO_RECOVERY
)
187 flags
|= NI_NUMERICHOST
;
191 // Resolve as a plain numberic
192 if ((flags
& NI_NUMERICHOST
) == NI_NUMERICHOST
)
194 strncpy(host
,inet_ntoa(sin
->sin_addr
),hostlen
);
200 // Try to resolve the hostname
201 if ((flags
& NI_NUMERICSERV
) != NI_NUMERICSERV
)
204 if ((flags
& NI_DATAGRAM
) == NI_DATAGRAM
)
205 Ent
= getservbyport(ntohs(sin
->sin_port
),"udp");
207 Ent
= getservbyport(ntohs(sin
->sin_port
),"tcp");
210 strncpy(serv
,Ent
->s_name
,servlen
);
213 if ((flags
& NI_NAMEREQD
) == NI_NAMEREQD
)
216 flags
|= NI_NUMERICSERV
;
220 // Resolve as a plain numberic
221 if ((flags
& NI_NUMERICSERV
) == NI_NUMERICSERV
)
223 snprintf(serv
,servlen
,"%u",ntohs(sin
->sin_port
));
230 #endif // HAVE_GETNAMEINFO