Imported Upstream version 0.8~alpha1
[platform/upstream/syncevolution.git] / src / client-api / src / c++ / posix / push / FSocket.cpp
1 /*
2  * Funambol is a mobile platform developed by Funambol, Inc. 
3  * Copyright (C) 2003 - 2007 Funambol, Inc.
4  * 
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU Affero General Public License version 3 as published by
7  * the Free Software Foundation with the addition of the following permission 
8  * added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
9  * WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE 
10  * WARRANTY OF NON INFRINGEMENT  OF THIRD PARTY RIGHTS.
11  * 
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
15  * details.
16  * 
17  * You should have received a copy of the GNU Affero General Public License 
18  * along with this program; if not, see http://www.gnu.org/licenses or write to
19  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20  * MA 02110-1301 USA.
21  * 
22  * You can contact Funambol, Inc. headquarters at 643 Bair Island Road, Suite 
23  * 305, Redwood City, CA 94063, USA, or at email address info@funambol.com.
24  * 
25  * The interactive user interfaces in modified source and object code versions
26  * of this program must display Appropriate Legal Notices, as required under
27  * Section 5 of the GNU Affero General Public License version 3.
28  * 
29  * In accordance with Section 7(b) of the GNU Affero General Public License
30  * version 3, these Appropriate Legal Notices must retain the display of the
31  * "Powered by Funambol" logo. If the display of the logo is not reasonably 
32  * feasible for technical reasons, the Appropriate Legal Notices must display
33  * the words "Powered by Funambol".
34  */
35
36 #include <errno.h>
37 #include <string.h>
38
39 #include "FSocket.h"
40
41 StringBuffer FSocket::lIP;
42
43 FSocket::FSocket() : unixSock ( -1 )
44 {
45     memset ( &unixAddr, 0, sizeof ( unixAddr ) );
46 }
47
48 FSocket::~FSocket() {
49     close();
50 }
51
52 bool FSocket::isValid() {
53     return unixSock != -1;
54 }
55
56 FSocket* FSocket::createSocket(const StringBuffer& peer, int32_t port) {
57     int sock = socket ( AF_INET, SOCK_STREAM, 0 );
58
59     if ( sock == -1 )
60         return NULL;
61
62     // TIME_WAIT - argh
63     int on = 1;
64     if ( setsockopt ( sock, SOL_SOCKET, SO_REUSEADDR, ( const char* ) &on, sizeof ( on ) ) == -1 ) {
65         return NULL;
66     }
67
68     sockaddr_in addr;
69     memset((void*)&addr, 0, sizeof(addr));
70     addr.sin_family = AF_INET;
71     addr.sin_port = htons ( port );
72     //int status = inet_pton ( AF_INET, "85.18.37.27", &addr.sin_addr );
73     struct hostent *he = gethostbyname(peer.c_str());
74     if (he == NULL) { // do some error checking
75         return NULL;
76     }
77
78     int status = inet_pton (AF_INET, inet_ntoa(*(struct in_addr*)he->h_addr),
79                             &addr.sin_addr);
80     if ( errno == EAFNOSUPPORT ) {
81         return NULL;
82     }
83
84     status = ::connect ( sock, ( sockaddr * ) &addr, sizeof ( addr ) );
85
86     if ( status == 0 ) {
87         FSocket* newSocket = new FSocket();
88         newSocket->unixSock = sock;
89         newSocket->unixAddr = addr;
90         return newSocket;
91     } else {
92         return NULL;
93     }
94 }
95
96
97 int32_t FSocket::writeBuffer(const int8_t* const buffer, int32_t len)
98 {
99     int32_t status = ::send ( unixSock, buffer, len, MSG_NOSIGNAL );
100     return status;
101 }
102
103
104 int32_t FSocket::readBuffer(int8_t* buffer, int32_t maxLen)
105 {
106     memset ( buffer, 0, maxLen );
107     int32_t status = ::recv ( unixSock, buffer, maxLen, 0 );
108
109     if ( status < 0 ) {
110         return -1;
111     } else if ( status == 0 ) {
112         return 0;
113     } else {
114         return status;
115     }
116 }
117
118 const StringBuffer& FSocket::address() const {
119     return lAddress;
120 }
121
122 const StringBuffer& FSocket::peerAddress() const {
123     return pAddress;
124 }
125
126
127 void FSocket::close() {
128     if ( isValid() )
129         ::close ( unixSock );
130 }
131
132 const StringBuffer& FSocket::localIP() {
133     return lIP;
134 }
135
136
137
138
139