Main Page Modules Class Hierarchy Data Structures File List Data Fields Globals Related Pages

# osclconfig_io.h

Go to the documentation of this file.

    00001 // -*- c++ -*-
    00002 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
    00003
    00004 //     O S C L C O N F I G _ I O
    00005
    00006 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
    00007
    00008
    00017 #ifndef OSCLCONFIG_IO_H_INCLUDED
    00018 #define OSCLCONFIG_IO_H_INCLUDED
    00019
    00020 #ifndef OSCLCONFIG_H_INCLUDED
    00021 #include "osclconfig.h"
    00022 #endif
    00023
    00024 #include <stdio.h>
    00025 #include <stdlib.h>
    00026 #include <stdarg.h>
    00027 #include <sys/socket.h>
    00028 #include <netinet/in.h>
    00029 #include <arpa/inet.h>
    00030 #include <fcntl.h>
    00031 #include <signal.h>
    00032 #include <netdb.h>
    00033 #include <sys/mman.h>
    00034 #include <sys/types.h>
    00035 #include <errno.h>
    00036 #include <sys/vfs.h>
    00037 #include <dirent.h>
    00038 //#include <glob.h>
    00039 #if (OSCL_HAS_ANSI_STDLIB_SUPPORT)
    00040 #if (OSCL_HAS_UNIX_SUPPORT)
    00041 #include <sys/stat.h>
    00042 #endif
    00043 #endif
    00044
    00045 //For File I/O
    00046 #define OSCL_HAS_GLOB 0
    00047 #define OSCL_HAS_ANSI_FILE_IO_SUPPORT 1
    00048 #define OSCL_HAS_ANSI_64BIT_FILE_IO_SUPPORT 0
    00049 #define OSCL_HAS_MSWIN_FILE_IO_SUPPORT 0
    00050 #define OSCL_HAS_SYMBIAN_COMPATIBLE_IO_FUNCTION 0
    00051 #define OSCL_HAS_NATIVE_FILE_CACHE_ENABLE 1
    00052 #define OSCL_FILE_BUFFER_MAX_SIZE   32768
    00053 #define OSCL_HAS_PV_FILE_CACHE  0
    00054 #define OSCL_HAS_LARGE_FILE_SUPPORT 1
    00055
    00056 //For Sockets
    00057 #define OSCL_HAS_SYMBIAN_SOCKET_SERVER 0
    00058 #define OSCL_HAS_SYMBIAN_DNS_SERVER 0
    00059 #define OSCL_HAS_BERKELEY_SOCKETS 1
    00060 #define OSCL_HAS_SOCKET_SUPPORT 1
    00061
    00062 //basic socket types
    00063 typedef int TOsclSocket;
    00064 typedef struct sockaddr_in TOsclSockAddr;
    00065 typedef socklen_t TOsclSockAddrLen;
    00066 typedef struct ip_mreq TIpMReq;
    00067
    00068 //Init addr macro, inet_addr returns an uint32
    00069 #define OsclValidInetAddr(addr) (inet_addr(addr)!=INADDR_NONE)
    00070
    00071 //address conversion macro-- from string to network address.
    00072 #define OsclMakeSockAddr(sockaddr,port,addrstr,ok)\
    00073     sockaddr.sin_family=OSCL_AF_INET;\
    00074     sockaddr.sin_port=htons(port);\
    00075     int32 result=inet_aton((const char*)addrstr,&sockaddr.sin_addr);\
    00076     ok=(result!=0);
    00077
    00078 //address conversion macro-- from network address to string
    00079 #define OsclUnMakeSockAddr(sockaddr,addrstr)\
    00080     addrstr=inet_ntoa(sockaddr.sin_addr);
    00081 //address conversion macro-- from string to inaddr
    00082 #define OsclMakeInAddr(in_addr,addrstr,ok)\
    00083     int32 result = inet_aton((const char*)addrstr, &in_addr);\
    00084     ok=(result!=0);
    00085
    00086 //address conversion macro-- from inaddr to string
    00087 #define OsclUnMakeInAddr(in_addr,addrstr)\
    00088     addrstr=inet_ntoa(in_addr);
    00089
    00090 //wrappers for berkeley socket calls
    00091 #define OsclSetRecvBufferSize(s,val,ok,err) \
    00092         ok=(setsockopt(s,SOL_SOCKET,SO_RCVBUF,(char*)&val, sizeof(int)) !=-1);\
    00093         if (!ok)err=errno
    00094
    00095 #define OsclBind(s,addr,ok,err)\
    00096     TOsclSockAddr* tmpadr = &addr;\
    00097     sockaddr* sadr = OSCL_STATIC_CAST(sockaddr*, tmpadr);\
    00098     ok=(bind(s,sadr,sizeof(addr))!=(-1));\
    00099     if (!ok)err=errno
    00100
    00101 #define OsclSetSockOpt(s,optLevel,optName,optVal,optLen,ok,err)\
    00102     ok=(setsockopt(s,optLevel,optName,OSCL_STATIC_CAST(const char*,optVal),optLen) != (-1));\
    00103     if (!ok)err=errno
    00104 #define OsclJoin(s,addr,ok,err)\
    00105 {\
    00106         struct ip_mreq mreq; \
    00107             void* p = &addr; \
    00108         ok=(bind(s,(sockaddr*)p,sizeof(addr))!=(-1));\
    00109         mreq.imr_multiaddr.s_addr = addr.sin_addr.s_addr ; \
    00110         mreq.imr_interface.s_addr = htonl(INADDR_ANY); \
    00111         ok=(setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(struct ip_mreq))!=(-1)); \
    00112         if (!ok)err=errno;\
    00113 }
    00114
    00115
    00116 #define OsclListen(s,size,ok,err)\
    00117     ok=(listen(iSocket,qSize)!=(-1));\
    00118     if (!ok)err=errno
    00119
    00120 #define OsclAccept(s,accept_s,ok,err,wouldblock)\
    00121     accept_s=accept(s,NULL,NULL);\
    00122     ok=(accept_s!=(-1));\
    00123     if (!ok){err=errno;wouldblock=(err==EAGAIN||err==EWOULDBLOCK);}
    00124
    00125 #define OsclSetNonBlocking(s,ok,err)\
    00126     ok=(fcntl(s,F_SETFL,O_NONBLOCK)!=(-1));\
    00127     if (!ok)err=errno
    00128
    00129 #define OsclShutdown(s,how,ok,err)\
    00130     ok=(shutdown(iSocket,how)!=(-1));\
    00131     if (!ok)err=errno
    00132
    00133 #define OsclSocket(s,fam,type,prot,ok,err)\
    00134     s=socket(fam,type,prot);\
    00135     ok=(s!=(-1));\
    00136     if (!ok)err=errno
    00137
    00138 #define OsclSendTo(s,buf,len,addr,ok,err,nbytes,wouldblock)\
    00139     TOsclSockAddr* tmpadr = &addr;\
    00140     sockaddr* sadr = OSCL_STATIC_CAST(sockaddr*, tmpadr);\
    00141     nbytes=sendto(s,(const void*)(buf),(size_t)(len),0,sadr,(socklen_t)sizeof(addr));\
    00142     ok=(nbytes!=(-1));\
    00143     if (!ok){err=errno;wouldblock=(err==EAGAIN||err==EWOULDBLOCK);}
    00144
    00145 #define OsclSend(s,buf,len,ok,err,nbytes,wouldblock)\
    00146     nbytes=send(s,(const void*)(buf),(size_t)(len),0);\
    00147     ok=(nbytes!=(-1));\
    00148     if (!ok){err=errno;wouldblock=(err==EAGAIN||err==EWOULDBLOCK);}
    00149
    00150 #define OsclCloseSocket(s,ok,err)\
    00151     ok=(close(s)!=(-1));\
    00152     if (!ok)err=errno
    00153
    00154 #define OsclConnect(s,addr,ok,err,wouldblock)\
    00155     TOsclSockAddr* tmpadr = &addr;\
    00156     sockaddr* sadr = OSCL_STATIC_CAST(sockaddr*, tmpadr);\
    00157     ok=(connect(s,sadr,sizeof(addr))!=(-1));\
    00158     if (!ok){err=errno;wouldblock=(err==EINPROGRESS);}
    00159 #define OsclGetPeerName(s,name,namelen,ok,err)\
    00160     ok=(getpeername(s,(sockaddr*)&name,(socklen_t*)&namelen) != (-1) );\
    00161     if (!ok)err=errno
    00162
    00163 #define OsclGetAsyncSockErr(s,ok,err)\
    00164     int opterr;socklen_t optlen=sizeof(opterr);\
    00165     ok=(getsockopt(s,SOL_SOCKET,SO_ERROR,(void *)&opterr,&optlen)!=(-1));\
    00166     if(ok)err=opterr;else err=errno;
    00167
    00168 #define OsclPipe(x)         pipe(x)
    00169 #define OsclReadFD(fd,buf,cnt)  read(fd,buf,cnt)
    00170 #define OsclWriteFD(fd,buf,cnt) write(fd,buf,cnt)
    00171
    00172 //unix reports connect completion in write set in the getsockopt
    00173 //error.
    00174 #define OsclConnectComplete(s,wset,eset,success,fail,ok,err)\
    00175     success=fail=false;\
    00176     if (FD_ISSET(s,&eset))\
    00177     {fail=true;OsclGetAsyncSockErr(s,ok,err);}\
    00178     else if (FD_ISSET(s,&wset))\
    00179     {OsclGetAsyncSockErr(s,ok,err);if (ok && err==0)success=true;else fail=true;}
    00180
    00181 #define OsclRecv(s,buf,len,ok,err,nbytes,wouldblock)\
    00182     nbytes=recv(s,(void *)(buf),(size_t)(len),0);\
    00183     ok=(nbytes!=(-1));\
    00184     if (!ok){err=errno;wouldblock=(err==EAGAIN);}
    00185
    00186 #define OsclRecvFrom(s,buf,len,paddr,paddrlen,ok,err,nbytes,wouldblock)\
    00187 {\
    00188 void* p=paddr;\
    00189 nbytes=recvfrom(s,(void*)(buf),(size_t)(len),0,(struct sockaddr*)p,paddrlen);\
    00190     ok=(nbytes!=(-1));\
    00191     if (!ok){err=errno;wouldblock=(err==EAGAIN);}\
    00192 }
    00193
    00194
    00195 #define OsclSocketSelect(nfds,rd,wr,ex,timeout,ok,err,nhandles)\
    00196     nhandles=select(nfds,&rd,&wr,&ex,&timeout);\
    00197     ok=(nhandles!=(-1));\
    00198     if (!ok)err=errno
    00199
    00200 //there's not really any socket startup needed on unix, but
    00201 //you need to define a signal handler for SIGPIPE to avoid
    00202 //broken pipe crashes.
    00203 #define OsclSocketStartup(ok)\
    00204     signal(SIGPIPE,SIG_IGN);\
    00205     ok=true
    00206
    00207 #define OsclSocketCleanup(ok)\
    00208     signal(SIGPIPE,SIG_DFL);\
    00209     ok=true
    00210
    00211 //hostent type
    00212 typedef struct hostent TOsclHostent;
    00213
    00214 //wrapper for gethostbyname
    00215 #define OsclGethostbyname(name,hostent,ok,err)\
    00216     hostent=gethostbyname((const char*)name);\
    00217     ok=(hostent!=NULL);\
    00218     if (!ok)err=errno;
    00219
    00220 //extract dotted address from a hostent
    00221 #define OsclGetDottedAddr(hostent,dottedaddr,ok)\
    00222     long *_hostaddr=(long*)hostent->h_addr_list[0];\
    00223     struct in_addr _inaddr;\
    00224     _inaddr.s_addr=*_hostaddr;\
    00225     dottedaddr=inet_ntoa(_inaddr);\
    00226     ok=(dottedaddr!=NULL);
    00227
    00228 //extract dotted address from a hostent into the vector of OsclNetworkAddress
    00229 #define OsclGetDottedAddrVector(hostent,dottedaddr,dottedaddrvect,ok)\
    00230     if(dottedaddrvect)\
    00231     {\
    00232     long **_addrlist=(long**)hostent->h_addr_list;\
    00233     for(int i = 0; _addrlist[i] != NULL; i++){\
    00234         struct in_addr _inaddr;\
    00235         _inaddr.s_addr=*_addrlist[i];\
    00236         OsclNetworkAddress addr(inet_ntoa(_inaddr), 0);\
    00237         dottedaddrvect->push_back(addr);\
    00238     }\
    00239     if (!dottedaddrvect->empty())\
    00240         {dottedaddr->port = dottedaddrvect->front().port; dottedaddr->ipAddr.Set(dottedaddrvect->front().ipAddr.Str());}\
    00241     ok=(!dottedaddrvect->empty() && (((*dottedaddrvect)[0]).ipAddr.Str() != NULL));\
    00242     }\
    00243     else\
    00244     {\
    00245         char *add;\
    00246         OsclGetDottedAddr(hostent,add,ok);\
    00247         if(ok) dottedaddr->ipAddr.Set(add);\
    00248     }
    00249
    00250 //socket shutdown codes
    00251 #define OSCL_SD_RECEIVE SHUT_RD
    00252 #define OSCL_SD_SEND SHUT_WR
    00253 #define OSCL_SD_BOTH SHUT_RDWR
    00254
    00255 //address family codes
    00256 #define OSCL_AF_INET AF_INET
    00257
    00258 //socket type codes
    00259 #define OSCL_SOCK_STREAM SOCK_STREAM
    00260 #define OSCL_SOCK_DATAGRAM SOCK_DGRAM
    00261
    00262 //IP protocol codes
    00263 #define OSCL_IPPROTO_IP  IPPROTO_IP
    00264 #define OSCL_IPPROTO_TCP IPPROTO_TCP
    00265 #define OSCL_IPPROTO_UDP IPPROTO_UDP
    00266
    00267 //Socket option Levels
    00268 #define OSCL_SOL_SOCKET SOL_SOCKET
    00269 #define OSCL_SOL_IP     IPPROTO_IP
    00270 #define OSCL_SOL_TCP    IPPROTO_TCP
    00271 #define OSCL_SOL_UDP    IPPROTO_UDP
    00272
    00273 //Socket Option Values (level = IP)
    00274 #define OSCL_SOCKOPT_IP_MULTICAST_TTL   IP_MULTICAST_TTL
    00275 #define OSCL_SOCKOPT_IP_ADDMEMBERSHIP   IP_ADD_MEMBERSHIP
    00276 #define OSCL_SOCKOPT_IP_TOS             IP_TOS
    00277
    00278 //Socket Option Values (level = Socket)
    00279 #define OSCL_SOCKOPT_SOL_REUSEADDR      SO_REUSEADDR
    00280 //End sockets
    00281
    00282 // file IO support
    00283 #if (OSCL_HAS_LARGE_FILE_SUPPORT)
    00284 #define _FILE_OFFSET_BITS 64
    00285 typedef off_t TOsclFileOffset;
    00286 #else
    00287 typedef int32 TOsclFileOffset;
    00288 #endif
    00289
    00290 #include "osclconfig_io_check.h"
    00291
    00292 #endif
    00293