]>
git.saurik.com Git - apt.git/blob - methods/rfc2553emu.cc
66fe781fb57c814ef7eaaf4ed9edd727b291ca3f
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: rfc2553emu.cc,v 1.1 1999/05/25 05:56:24 jgg Exp $
4 /* ######################################################################
6 RFC 2553 Emulation - Provides emulation for RFC 2553 getaddrinfo,
7 freeaddrinfo and getnameinfo
9 Originally written by Jason Gunthorpe <jgg@debian.org> and placed into
10 the Public Domain, do with it what you will.
12 ##################################################################### */
14 #include "rfc2553emu.h"
16 #include <arpa/inet.h>
19 #ifndef HAVE_GETADDRINFO
20 int getaddrinfo(const char *nodename
, const char *servname
,
21 const struct addrinfo
*hints
,
22 struct addrinfo
**res
)
24 struct addrinfo
**Result
;
31 Addr
= gethostbyname(nodename
);
34 if (h_errno
== TRY_AGAIN
)
36 if (h_errno
== NO_RECOVERY
)
42 if (Addr
->h_addr_list
[0] == 0)
45 // Try to convert the service as a number
46 Port
= htons(strtol(servname
,(char **)&End
,0));
49 if (hints
!= 0 && hints
->ai_socktype
!= 0)
50 Proto
= hints
->ai_socktype
;
52 // Not a number, must be a name.
53 if (End
!= servname
+ strlen(End
))
55 struct servent
*Srv
= 0;
57 // Do a lookup in the service database
58 if (hints
== 0 || hints
->ai_socktype
== SOCK_STREAM
)
59 Srv
= getservbyname(servname
,"tcp");
60 if (hints
!= 0 && hints
->ai_socktype
== SOCK_DGRAM
)
61 Srv
= getservbyname(servname
,"udp");
65 // Get the right protocol
67 if (strcmp(Srv
->s_proto
,"tcp") == 0)
71 if (strcmp(Srv
->s_proto
,"udp") == 0)
77 if (hints
!= 0 && hints
->ai_socktype
!= Proto
&&
78 hints
->ai_socktype
!= 0)
82 // Start constructing the linked list
84 for (CurAddr
= Addr
->h_addr_list
; *CurAddr
!= 0; CurAddr
++)
86 // New result structure
87 *Result
= (struct addrinfo
*)calloc(sizeof(**Result
),1);
96 (*Result
)->ai_family
= AF_INET
;
97 (*Result
)->ai_socktype
= Proto
;
99 // If we have the IPPROTO defines we can set the protocol field
101 if (Proto
== SOCK_STREAM
)
102 (*Result
)->ai_protocol
= IPPROTO_TCP
;
103 if (Proto
== SOCK_DGRAM
)
104 (*Result
)->ai_protocol
= IPPROTO_UDP
;
107 // Allocate space for the address
108 (*Result
)->ai_addrlen
= sizeof(struct sockaddr_in
);
109 (*Result
)->ai_addr
= (struct sockaddr
*)calloc(sizeof(sockaddr_in
),1);
110 if ((*Result
)->ai_addr
== 0)
117 ((struct sockaddr_in
*)(*Result
)->ai_addr
)->sin_family
= AF_INET
;
118 ((struct sockaddr_in
*)(*Result
)->ai_addr
)->sin_port
= Port
;
119 ((struct sockaddr_in
*)(*Result
)->ai_addr
)->sin_addr
= *(in_addr
*)(*CurAddr
);
121 Result
= &(*Result
)->ai_next
;
127 void freeaddrinfo(struct addrinfo
*ai
)
129 struct addrinfo
*Tmp
;
139 #endif // HAVE_GETADDRINFO