• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2008, The Android Open Source Project
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *  * Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  *  * Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #define LOG_TAG "wds"
27 #include "config.h"
28 
29 #include "DebugServer.h" // used for ENABLE_WDS
30 #include "Connection.h"
31 #include <arpa/inet.h>
32 #include <string.h>
33 #include <utils/Log.h>
34 
35 #if ENABLE(WDS)
36 
37 #define MAX_CONNECTION_QUEUE 5
38 #define log_errno(x) LOGE("%s: %d", x, strerror(errno))
39 
40 namespace android {
41 
42 namespace WDS {
43 
open()44 bool Socket::open() {
45     m_fd = socket(PF_INET, SOCK_STREAM, 0);
46     if (m_fd < 0) {
47         log_errno("Failed to create file descriptor");
48         return false;
49     }
50     return true;
51 }
52 
connect(short port)53 bool ConnectionServer::connect(short port) {
54     if (!m_socket.open())
55         return false;
56     int fd = m_socket.fd();
57 
58     // Build our sockaddr_in structure use to listen to incoming connections
59     sockaddr_in addr;
60     memset(&addr, 0, sizeof(addr));
61     addr.sin_family = AF_INET;
62     addr.sin_addr.s_addr = htonl(INADDR_ANY);
63     addr.sin_port = htons(port);
64 
65     // Try to bind to the given port
66     if (bind(fd, (sockaddr*) &addr, sizeof(addr)) < 0) {
67         log_errno("Failed to bind to local host");
68         return false;
69     }
70 
71     // Try to listen
72     if (listen(fd, MAX_CONNECTION_QUEUE) < 0) {
73         log_errno("Failed to listen");
74         return false;
75     }
76 
77     return true;
78 }
79 
accept() const80 Connection* ConnectionServer::accept() const {
81     int conn = ::accept(m_socket.fd(), NULL, NULL);
82     if (conn < 0) {
83         log_errno("Accept failed");
84         return NULL;
85     }
86     return new Connection(conn);
87 }
88 
89 } // end namespace WDS
90 
91 } // end namespace android
92 
93 #endif
94