1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/genericopenlibs/cstdlib/LPOSIX/NETDB.CPP Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,283 @@
1.4 +// Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +// Support for hostname & hostaddr related functions
1.18 +//
1.19 +//
1.20 +
1.21 +#include "SYSIF.H"
1.22 +#include "FDESC.H"
1.23 +#include <unistd.h>
1.24 +#include <stdlib.h>
1.25 +#include <sys/errno.h>
1.26 +#include "LPOSIX.H"
1.27 +#include <sys/socket.h>
1.28 +#include <libc/netinet/in.h>
1.29 +#include <netdb_r.h>
1.30 +#include <string.h>
1.31 +#include <in_sock.h>
1.32 +#include <networking/dnd_err.h>
1.33 +
1.34 +extern "C" {
1.35 +
1.36 +/**
1.37 +Get the internet name of this host. Actually this will always return a null
1.38 +string with TCPIP 030 and onwards because the "name" of a mobile host
1.39 +isn't really very meaningful - in practice the IP address is chosen dynamically
1.40 +once you start doing real networking, at which time the ISP can resolve the
1.41 +IP address into a name of some sort if you really want.
1.42 +@return
1.43 +@param name
1.44 +@param size
1.45 +*/
1.46 +EXPORT_C int gethostname (char *name, size_t size)
1.47 + {
1.48 + int* perrno=__errno();
1.49 + RSocketServ ss;
1.50 + TInt err=ss.Connect(1);
1.51 + if (err==KErrNone)
1.52 + {
1.53 + RHostResolver r;
1.54 + err=r.Open(ss, AF_INET, KProtocolInetUdp);
1.55 + if (err==KErrNone)
1.56 + {
1.57 + TBuf<128> hostname;
1.58 + err=r.GetHostName(hostname);
1.59 + if (err==KErrNone)
1.60 + {
1.61 + if (size>(size_t)hostname.Length())
1.62 + {
1.63 + TPtr8 retval((TText8*)name,size);
1.64 + retval.Copy(hostname);
1.65 + retval.PtrZ();
1.66 + }
1.67 + else
1.68 + err=ENAMETOOLONG;
1.69 + }
1.70 + r.Close();
1.71 + }
1.72 + ss.Close();
1.73 + }
1.74 + return MapError(err,*perrno);
1.75 + }
1.76 +
1.77 +/**
1.78 +Get the internet name of the host by address.
1.79 +@return
1.80 +@param addr Address of the host
1.81 +@param length
1.82 +@param format
1.83 +*/
1.84 +EXPORT_C struct hostent* gethostbyaddr (const char* addr, int length, int format)
1.85 + {
1.86 + return _gethostbyaddr_r(_REENT,addr,length,format);
1.87 + }
1.88 +
1.89 +/**
1.90 +Get the internet name of the host by name.
1.91 +@return
1.92 +@param name Name of the host
1.93 +*/
1.94 +EXPORT_C struct hostent* gethostbyname (const char* name)
1.95 + {
1.96 + return _gethostbyname_r(_REENT,name);
1.97 + }
1.98 +
1.99 +enum GetHostType {ByName = 1, ByAddr};
1.100 +
1.101 +#define MAX_ADDR_OR_ALIAS_LIST 5
1.102 +
1.103 +struct hostent_buf
1.104 + {
1.105 + struct hostent iHostent;
1.106 + char *iAliasesPtr[MAX_ADDR_OR_ALIAS_LIST+1];
1.107 + char *iAddrListPtr[MAX_ADDR_OR_ALIAS_LIST+1];
1.108 + THostName iHostAliases[MAX_ADDR_OR_ALIAS_LIST];
1.109 + struct sockaddr iHostAddress[MAX_ADDR_OR_ALIAS_LIST];
1.110 + };
1.111 +
1.112 +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)
1.113 + {
1.114 + free(rp->_netdb);
1.115 + struct hostent_buf* hbp = (struct hostent_buf*)calloc(1,sizeof(struct hostent_buf));
1.116 + rp->_netdb = hbp;
1.117 + if (hbp==0)
1.118 + {
1.119 + rp->_errno=ENOMEM;
1.120 + return 0;
1.121 + }
1.122 +
1.123 + // Set-up Aliases & Address list pointer
1.124 + hbp->iHostent.h_aliases = &hbp->iAliasesPtr[0];
1.125 + hbp->iHostent.h_addr_list = &hbp->iAddrListPtr[0];
1.126 +
1.127 + // Fill-up Host Name
1.128 + hbp->iHostent.h_name = (char*) &hbp->iHostAliases[0];
1.129 + TPtr8 name((TText8*)&hbp->iHostAliases[0], aRecord[0].iName.Length()+1);
1.130 + name.Copy(aRecord[0].iName);
1.131 + name.ZeroTerminate();
1.132 +
1.133 + hbp->iHostent.h_addrtype = aFormat;
1.134 + hbp->iHostent.h_length = aLength;
1.135 +
1.136 + // Fill-up Address
1.137 + hbp->iAddrListPtr[0] = (char*)&hbp->iHostAddress[0].sa_data[0]; // ... which is iAddr
1.138 + unsigned long len=sizeof(struct sockaddr);
1.139 + aRecord[0].iAddr.SetFamily(aFormat); // not set by GetByName(_L(""));
1.140 + STATIC_CAST(TUSockAddr*,&aRecord[0].iAddr)->Get(&hbp->iHostAddress[0],&len);
1.141 +
1.142 + if (aCount > 1)
1.143 + {
1.144 + // If there are multiple host name or address
1.145 + TInt i;
1.146 + if (aHostType == ByName)
1.147 + {
1.148 + // there are multiple addresses for a given host
1.149 + hbp->iAliasesPtr[0] = NULL;
1.150 + for (i=1; i<aCount; i++)
1.151 + {
1.152 + hbp->iAddrListPtr[i] = (char*)&hbp->iHostAddress[i].sa_data[0];
1.153 + len=sizeof(struct sockaddr);
1.154 + aRecord[i].iAddr.SetFamily(aFormat); // not set by GetByName(_L(""));
1.155 + STATIC_CAST(TUSockAddr*,&aRecord[i].iAddr)->Get(&hbp->iHostAddress[i],&len);
1.156 + }
1.157 + hbp->iAddrListPtr[i] = NULL;
1.158 + }
1.159 + else
1.160 + {
1.161 + // there are multiple host/aliases name for a given address
1.162 + hbp->iAddrListPtr[1] = NULL;
1.163 + for (i=1; i<aCount; i++)
1.164 + {
1.165 + hbp->iAliasesPtr[i-1] = (char*) &hbp->iHostAliases[i];
1.166 + name.Set((TText8*)&hbp->iHostAliases[i], 0, aRecord[i].iName.Length()+1);
1.167 + name.Copy(aRecord[i].iName);
1.168 + name.ZeroTerminate();
1.169 + }
1.170 + hbp->iAliasesPtr[i-1] = NULL;
1.171 + }
1.172 + }
1.173 + else
1.174 + {
1.175 + hbp->iAliasesPtr[0] = NULL;
1.176 + hbp->iAddrListPtr[1] = NULL;
1.177 + }
1.178 + return &hbp->iHostent;
1.179 + }
1.180 +
1.181 +/**
1.182 +Get the internet name of the host by address.
1.183 +@return
1.184 +@param rp pointer
1.185 +@param addr
1.186 +@param length
1.187 +@param format
1.188 +*/
1.189 +EXPORT_C struct hostent* _gethostbyaddr_r (struct _reent* rp, const char* addr, int length, int format)
1.190 +//
1.191 +// For AF_INET,
1.192 + {
1.193 + RSocketServ ss;
1.194 + TInt err=ss.Connect();
1.195 + struct hostent* retval=0;
1.196 + if (err==KErrNone)
1.197 + {
1.198 + RHostResolver r;
1.199 + err=r.Open(ss, AF_INET, KProtocolInetUdp);
1.200 + if (err==KErrNone)
1.201 + {
1.202 + struct sockaddr buf;
1.203 + buf.sa_family=(unsigned short)format;
1.204 + memcpy(buf.sa_data,addr,length);
1.205 + TUSockAddr addr(&buf,length+4);
1.206 +
1.207 + TFixedArray<TNameRecord, MAX_ADDR_OR_ALIAS_LIST> record;
1.208 + TNameEntry entry;
1.209 + TInt count = 0;
1.210 + err=r.GetByAddress(addr,entry);
1.211 + if (err==KErrNone)
1.212 + {
1.213 + record[count++]=entry();
1.214 + while ((r.Next(entry) == KErrNone) && (count < MAX_ADDR_OR_ALIAS_LIST))
1.215 + {
1.216 + record[count++] = entry();
1.217 + }
1.218 + retval = mapNameRecord(rp, record, count, ByAddr, length, format);
1.219 + }
1.220 + r.Close();
1.221 + }
1.222 + ss.Close();
1.223 + }
1.224 + if (err==-3004) // NETDIAL busy
1.225 + err=TRY_AGAIN;
1.226 +
1.227 + // KErrDndAddrNotFound=-5121 should be excluded (Returned when no data found for GetByAddr)
1.228 + if ((err<-3000) && (err!=KErrDndAddrNotFound)) // i.e. a NETDIAL error
1.229 + err=NO_RECOVERY;
1.230 + MapError(err,rp->_errno);
1.231 + return retval;
1.232 + }
1.233 +
1.234 +/**
1.235 +Get the internet name of the host by name.
1.236 +@return
1.237 +@param rp
1.238 +@param name name of the host
1.239 +*/
1.240 +EXPORT_C struct hostent* _gethostbyname_r (struct _reent* rp, const char* name)
1.241 + {
1.242 + RSocketServ ss;
1.243 + TInt err=ss.Connect();
1.244 + struct hostent* retval=0;
1.245 + if (err==KErrNone)
1.246 + {
1.247 + RHostResolver r;
1.248 + err=r.Open(ss, AF_INET, KProtocolInetUdp);
1.249 + if (err==KErrNone)
1.250 + {
1.251 + TFixedArray<TNameRecord, MAX_ADDR_OR_ALIAS_LIST> record;
1.252 + TNameEntry entry;
1.253 +#ifdef _UNICODE
1.254 + TPtrC8 ptr(REINTERPRET_CAST(const TUint8*,name));
1.255 + TBuf<0x40> hostname;
1.256 + hostname.Copy(ptr);
1.257 +#else
1.258 + TPtrC8 hostname(REINTERPRET_CAST(const TUint8*,name));
1.259 +#endif /* _UNICODE */
1.260 +
1.261 + TInt count = 0;
1.262 + err=r.GetByName(hostname,entry);
1.263 + if (err==KErrNone)
1.264 + {
1.265 + record[count++]=entry();
1.266 + while ((r.Next(entry) == KErrNone) && (count < MAX_ADDR_OR_ALIAS_LIST))
1.267 + {
1.268 + record[count++] = entry();
1.269 + }
1.270 + retval = mapNameRecord(rp, record, count, ByName, sizeof(struct in_addr), AF_INET);
1.271 + }
1.272 + r.Close();
1.273 + }
1.274 + ss.Close();
1.275 + }
1.276 + if (err==-3004) // NETDIAL busy
1.277 + err=TRY_AGAIN;
1.278 +
1.279 + // KErrDndNameNotFound=-5120 should be excluded (Returned when no data found for GetByName)
1.280 + if ((err<-3000) && (err!=KErrDndNameNotFound)) // i.e. a NETDIAL error
1.281 + err=NO_RECOVERY;
1.282 + MapError(err,rp->_errno);
1.283 + return retval;
1.284 + }
1.285 +
1.286 +} // extern "C"