• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* ------------------------------------------------------------------
2  * Copyright (C) 1998-2009 PacketVideo
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13  * express or implied.
14  * See the License for the specific language governing permissions
15  * and limitations under the License.
16  * -------------------------------------------------------------------
17  */
18 // -*- c++ -*-
19 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
20 
21 //     O S C L C O N F I G _ I O
22 
23 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
24 
25 
26 /*! \file osclconfig_io.h
27  *  \brief This file contains common typedefs based on the ANSI C limits.h header
28  *
29  *  This header file should work for any ANSI C compiler to determine the
30  *  proper native C types to use for OSCL integer types.
31  */
32 
33 
34 #ifndef OSCLCONFIG_IO_H_INCLUDED
35 #define OSCLCONFIG_IO_H_INCLUDED
36 
37 #ifndef OSCLCONFIG_H_INCLUDED
38 #include "osclconfig.h"
39 #endif
40 
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <stdarg.h>
44 #include <sys/socket.h>
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
47 #include <fcntl.h>
48 #include <signal.h>
49 #include <netdb.h>
50 #include <sys/types.h>
51 #include <sys/vfs.h>
52 #include <glob.h>
53 
54 
55 //For File I/O
56 #define OSCL_HAS_GLOB 1
57 #define OSCL_HAS_ANSI_FILE_IO_SUPPORT 1
58 #define OSCL_HAS_SYMBIAN_COMPATIBLE_IO_FUNCTION 0
59 #define OSCL_HAS_NATIVE_FILE_CACHE_ENABLE 1
60 #define OSCL_FILE_BUFFER_MAX_SIZE   32768
61 #define OSCL_HAS_PV_FILE_CACHE  1
62 #define OSCL_HAS_LARGE_FILE_SUPPORT 1
63 
64 //For Sockets
65 #define OSCL_HAS_SYMBIAN_SOCKET_SERVER 0
66 #define OSCL_HAS_SYMBIAN_DNS_SERVER 0
67 #define OSCL_HAS_BERKELEY_SOCKETS 1
68 #define OSCL_HAS_SOCKET_SUPPORT 1
69 #define OSCL_HAS_SELECTABLE_PIPES 1
70 
71 //basic socket types
72 typedef int TOsclSocket;
73 typedef struct sockaddr_in TOsclSockAddr;
74 typedef socklen_t TOsclSockAddrLen;
75 
76 //Init addr macro
77 #define OsclValidInetAddr(addr) (inet_addr(addr)!=((in_addr_t)(-1)))
78 
79 //address conversion macro-- from string to network address.
80 #define OsclMakeSockAddr(sockaddr,port,addrstr,ok)\
81     sockaddr.sin_family=OSCL_AF_INET;\
82     sockaddr.sin_port=htons(port);\
83     int32 result=inet_aton((const char*)addrstr,&sockaddr.sin_addr);\
84     ok=(result!=0);
85 
86 //address conversion macro-- from network address to string
87 #define OsclUnMakeSockAddr(sockaddr,addrstr)\
88     addrstr=inet_ntoa(sockaddr.sin_addr);
89 
90 //wrappers for berkeley socket calls
91 #define OsclSetRecvBufferSize(s,val,ok,err) \
92     ok=(setsockopt(s,SOL_SOCKET,SO_RCVBUF,(char*)&val, sizeof(int)) !=-1);\
93     if (!ok)err=errno
94 
95 #define OsclBind(s,addr,ok,err)\
96     TOsclSockAddr* tmpadr = &addr;\
97     sockaddr* sadr = OSCL_STATIC_CAST(sockaddr*, tmpadr);\
98     ok=(bind(s,sadr,sizeof(addr))!=(-1));\
99         if (!ok)err=errno
100 
101 #define OsclJoin(s,addr,ok,err)\
102         struct ip_mreq mreq; \
103         ok=(bind(s,(sockaddr*)&addr,sizeof(addr))!=(-1));\
104         mreq.imr_multiaddr.s_addr = addr.sin_addr.s_addr ; \
105         mreq.imr_interface.s_addr = htonl(INADDR_ANY); \
106         ok=(setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(struct ip_mreq))!=(-1)); \
107         if (!ok)err=errno
108 
109 #define OsclListen(s,size,ok,err)\
110     ok=(listen(iSocket,qSize)!=(-1));\
111     if (!ok)err=errno
112 
113 #define OsclAccept(s,accept_s,ok,err,wouldblock)\
114     accept_s=accept(s,NULL,NULL);\
115     ok=(accept_s!=(-1));\
116     if (!ok){err=errno;wouldblock=(err==EAGAIN||err==EWOULDBLOCK);}
117 
118 #define OsclSetNonBlocking(s,ok,err)\
119     ok=(fcntl(s,F_SETFL,O_NONBLOCK)!=(-1));\
120     if (!ok)err=errno
121 
122 #define OsclShutdown(s,how,ok,err)\
123     ok=(shutdown(iSocket,how)!=(-1));\
124     if (!ok)err=errno
125 
126 #define OsclSocket(s,fam,type,prot,ok,err)\
127     s=socket(fam,type,prot);\
128     ok=(s!=(-1));\
129     if (!ok)err=errno
130 
131 #define OsclSendTo(s,buf,len,addr,ok,err,nbytes,wouldblock)\
132     TOsclSockAddr* tmpadr = &addr;\
133     sockaddr* sadr = OSCL_STATIC_CAST(sockaddr*, tmpadr);\
134     nbytes=sendto(s,buf,(size_t)(len),0,sadr,(socklen_t)sizeof(addr));\
135     ok=(nbytes!=(-1));\
136     if (!ok){err=errno;wouldblock=(err==EAGAIN||err==EWOULDBLOCK);}
137 
138 #define OsclSend(s,buf,len,ok,err,nbytes,wouldblock)\
139     nbytes=send(s,(const void*)(buf),(size_t)(len),0);\
140     ok=(nbytes!=(-1));\
141     if (!ok){err=errno;wouldblock=(err==EAGAIN||err==EWOULDBLOCK);}
142 
143 #define OsclCloseSocket(s,ok,err)\
144     ok=(close(s)!=(-1));\
145     if (!ok)err=errno
146 
147 #define OsclConnect(s,addr,ok,err,wouldblock)\
148     TOsclSockAddr* tmpadr = &addr;\
149     sockaddr* sadr = OSCL_STATIC_CAST(sockaddr*, tmpadr);\
150     ok=(connect(s,sadr,sizeof(addr))!=(-1));\
151     if (!ok){err=errno;wouldblock=(err==EINPROGRESS);}
152 
153 #define OsclGetAsyncSockErr(s,ok,err)\
154     int opterr;socklen_t optlen=sizeof(opterr);\
155     ok=(getsockopt(s,SOL_SOCKET,SO_ERROR,(void *)&opterr,&optlen)!=(-1));\
156     if(ok)err=opterr;else err=errno;
157 
158 #define OsclPipe(x)         pipe(x)
159 #define OsclReadFD(fd,buf,cnt)  read(fd,buf,cnt)
160 #define OsclWriteFD(fd,buf,cnt) write(fd,buf,cnt)
161 
162 //unix reports connect completion in write set in the getsockopt
163 //error.
164 #define OsclConnectComplete(s,wset,eset,success,fail,ok,err)\
165     success=fail=false;\
166     if (FD_ISSET(s,&eset))\
167     {fail=true;OsclGetAsyncSockErr(s,ok,err);}\
168     else if (FD_ISSET(s,&wset))\
169     {OsclGetAsyncSockErr(s,ok,err);if (ok && err==0)success=true;else fail=true;}
170 
171 #define OsclRecv(s,buf,len,ok,err,nbytes,wouldblock)\
172     nbytes=recv(s,(void *)(buf),(size_t)(len),0);\
173     ok=(nbytes!=(-1));\
174     if (!ok){err=errno;wouldblock=(err==EAGAIN);}
175 
176 #define OsclRecvFrom(s,buf,len,paddr,paddrlen,ok,err,nbytes,wouldblock)\
177     nbytes=recvfrom(s,(void*)(buf),(size_t)(len),0,(struct sockaddr*)paddr,paddrlen);\
178     ok=(nbytes!=(-1));\
179     if (!ok){err=errno;wouldblock=(err==EAGAIN);}
180 
181 #define OsclSocketSelect(nfds,rd,wr,ex,timeout,ok,err,nhandles)\
182     nhandles=select(nfds,&rd,&wr,&ex,&timeout);\
183     ok=(nhandles!=(-1));\
184     if (!ok)err=errno
185 
186 //there's not really any socket startup needed on unix, but
187 //you need to define a signal handler for SIGPIPE to avoid
188 //broken pipe crashes.
189 #define OsclSocketStartup(ok)\
190     signal(SIGPIPE,SIG_IGN);\
191     ok=true
192 
193 #define OsclSocketCleanup(ok)\
194     signal(SIGPIPE,SIG_DFL);\
195     ok=true
196 
197 //hostent type
198 typedef struct hostent TOsclHostent;
199 
200 //wrapper for gethostbyname
201 #define OsclGethostbyname(name,hostent,ok,err)\
202     hostent=gethostbyname((const char*)name);\
203     ok=(hostent!=NULL);\
204     if (!ok)err=errno;
205 
206 //extract dotted address from a hostent
207 #define OsclGetDottedAddr(hostent,dottedaddr,ok)\
208     long *_hostaddr=(long*)hostent->h_addr_list[0];\
209     struct in_addr _inaddr;\
210     _inaddr.s_addr=*_hostaddr;\
211     dottedaddr=inet_ntoa(_inaddr);\
212     ok=(dottedaddr!=NULL);
213 
214 //socket shutdown codes
215 #define OSCL_SD_RECEIVE SHUT_RD
216 #define OSCL_SD_SEND SHUT_WR
217 #define OSCL_SD_BOTH SHUT_RDWR
218 
219 //address family codes
220 #define OSCL_AF_INET AF_INET
221 
222 //socket type codes
223 #define OSCL_SOCK_STREAM SOCK_STREAM
224 #define OSCL_SOCK_DATAGRAM SOCK_DGRAM
225 
226 //IP protocol codes
227 #define OSCL_IPPROTO_TCP IPPROTO_TCP
228 #define OSCL_IPPROTO_UDP IPPROTO_UDP
229 
230 //End sockets
231 
232 // file IO support
233 #if (OSCL_HAS_LARGE_FILE_SUPPORT)
234 #define _FILE_OFFSET_BITS 64
235 typedef off_t TOsclFileOffset;
236 #else
237 typedef int32 TOsclFileOffset;
238 #endif
239 
240 
241 #include "osclconfig_io_check.h"
242 
243 #endif
244 
245