Update contrib.
1 // Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // Support for hostname & hostaddr related functions
22 #include <sys/errno.h>
24 #include <sys/socket.h>
25 #include <libc/netinet/in.h>
29 #include <networking/dnd_err.h>
34 Get the internet name of this host. Actually this will always return a null
35 string with TCPIP 030 and onwards because the "name" of a mobile host
36 isn't really very meaningful - in practice the IP address is chosen dynamically
37 once you start doing real networking, at which time the ISP can resolve the
38 IP address into a name of some sort if you really want.
43 EXPORT_C int gethostname (char *name, size_t size)
45 int* perrno=__errno();
47 TInt err=ss.Connect(1);
51 err=r.Open(ss, AF_INET, KProtocolInetUdp);
55 err=r.GetHostName(hostname);
58 if (size>(size_t)hostname.Length())
60 TPtr8 retval((TText8*)name,size);
61 retval.Copy(hostname);
71 return MapError(err,*perrno);
75 Get the internet name of the host by address.
77 @param addr Address of the host
81 EXPORT_C struct hostent* gethostbyaddr (const char* addr, int length, int format)
83 return _gethostbyaddr_r(_REENT,addr,length,format);
87 Get the internet name of the host by name.
89 @param name Name of the host
91 EXPORT_C struct hostent* gethostbyname (const char* name)
93 return _gethostbyname_r(_REENT,name);
96 enum GetHostType {ByName = 1, ByAddr};
98 #define MAX_ADDR_OR_ALIAS_LIST 5
102 struct hostent iHostent;
103 char *iAliasesPtr[MAX_ADDR_OR_ALIAS_LIST+1];
104 char *iAddrListPtr[MAX_ADDR_OR_ALIAS_LIST+1];
105 THostName iHostAliases[MAX_ADDR_OR_ALIAS_LIST];
106 struct sockaddr iHostAddress[MAX_ADDR_OR_ALIAS_LIST];
109 struct hostent* mapNameRecord(struct _reent* rp, TFixedArray<TNameRecord, MAX_ADDR_OR_ALIAS_LIST>& aRecord, const TInt aCount, const GetHostType aHostType, const int aLength, const int aFormat)
112 struct hostent_buf* hbp = (struct hostent_buf*)calloc(1,sizeof(struct hostent_buf));
120 // Set-up Aliases & Address list pointer
121 hbp->iHostent.h_aliases = &hbp->iAliasesPtr[0];
122 hbp->iHostent.h_addr_list = &hbp->iAddrListPtr[0];
125 hbp->iHostent.h_name = (char*) &hbp->iHostAliases[0];
126 TPtr8 name((TText8*)&hbp->iHostAliases[0], aRecord[0].iName.Length()+1);
127 name.Copy(aRecord[0].iName);
128 name.ZeroTerminate();
130 hbp->iHostent.h_addrtype = aFormat;
131 hbp->iHostent.h_length = aLength;
134 hbp->iAddrListPtr[0] = (char*)&hbp->iHostAddress[0].sa_data[0]; // ... which is iAddr
135 unsigned long len=sizeof(struct sockaddr);
136 aRecord[0].iAddr.SetFamily(aFormat); // not set by GetByName(_L(""));
137 STATIC_CAST(TUSockAddr*,&aRecord[0].iAddr)->Get(&hbp->iHostAddress[0],&len);
141 // If there are multiple host name or address
143 if (aHostType == ByName)
145 // there are multiple addresses for a given host
146 hbp->iAliasesPtr[0] = NULL;
147 for (i=1; i<aCount; i++)
149 hbp->iAddrListPtr[i] = (char*)&hbp->iHostAddress[i].sa_data[0];
150 len=sizeof(struct sockaddr);
151 aRecord[i].iAddr.SetFamily(aFormat); // not set by GetByName(_L(""));
152 STATIC_CAST(TUSockAddr*,&aRecord[i].iAddr)->Get(&hbp->iHostAddress[i],&len);
154 hbp->iAddrListPtr[i] = NULL;
158 // there are multiple host/aliases name for a given address
159 hbp->iAddrListPtr[1] = NULL;
160 for (i=1; i<aCount; i++)
162 hbp->iAliasesPtr[i-1] = (char*) &hbp->iHostAliases[i];
163 name.Set((TText8*)&hbp->iHostAliases[i], 0, aRecord[i].iName.Length()+1);
164 name.Copy(aRecord[i].iName);
165 name.ZeroTerminate();
167 hbp->iAliasesPtr[i-1] = NULL;
172 hbp->iAliasesPtr[0] = NULL;
173 hbp->iAddrListPtr[1] = NULL;
175 return &hbp->iHostent;
179 Get the internet name of the host by address.
186 EXPORT_C struct hostent* _gethostbyaddr_r (struct _reent* rp, const char* addr, int length, int format)
191 TInt err=ss.Connect();
192 struct hostent* retval=0;
196 err=r.Open(ss, AF_INET, KProtocolInetUdp);
200 buf.sa_family=(unsigned short)format;
201 memcpy(buf.sa_data,addr,length);
202 TUSockAddr addr(&buf,length+4);
204 TFixedArray<TNameRecord, MAX_ADDR_OR_ALIAS_LIST> record;
207 err=r.GetByAddress(addr,entry);
210 record[count++]=entry();
211 while ((r.Next(entry) == KErrNone) && (count < MAX_ADDR_OR_ALIAS_LIST))
213 record[count++] = entry();
215 retval = mapNameRecord(rp, record, count, ByAddr, length, format);
221 if (err==-3004) // NETDIAL busy
224 // KErrDndAddrNotFound=-5121 should be excluded (Returned when no data found for GetByAddr)
225 if ((err<-3000) && (err!=KErrDndAddrNotFound)) // i.e. a NETDIAL error
227 MapError(err,rp->_errno);
232 Get the internet name of the host by name.
235 @param name name of the host
237 EXPORT_C struct hostent* _gethostbyname_r (struct _reent* rp, const char* name)
240 TInt err=ss.Connect();
241 struct hostent* retval=0;
245 err=r.Open(ss, AF_INET, KProtocolInetUdp);
248 TFixedArray<TNameRecord, MAX_ADDR_OR_ALIAS_LIST> record;
251 TPtrC8 ptr(REINTERPRET_CAST(const TUint8*,name));
255 TPtrC8 hostname(REINTERPRET_CAST(const TUint8*,name));
256 #endif /* _UNICODE */
259 err=r.GetByName(hostname,entry);
262 record[count++]=entry();
263 while ((r.Next(entry) == KErrNone) && (count < MAX_ADDR_OR_ALIAS_LIST))
265 record[count++] = entry();
267 retval = mapNameRecord(rp, record, count, ByName, sizeof(struct in_addr), AF_INET);
273 if (err==-3004) // NETDIAL busy
276 // KErrDndNameNotFound=-5120 should be excluded (Returned when no data found for GetByName)
277 if ((err<-3000) && (err!=KErrDndNameNotFound)) // i.e. a NETDIAL error
279 MapError(err,rp->_errno);