Kevin Rocard | 93250d1 | 2012-07-19 17:48:30 +0200 | [diff] [blame] | 1 | /* |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 2 | * INTEL CONFIDENTIAL |
| 3 | * Copyright © 2011 Intel |
| 4 | * Corporation All Rights Reserved. |
| 5 | * |
| 6 | * The source code contained or described herein and all documents related to |
| 7 | * the source code ("Material") are owned by Intel Corporation or its suppliers |
| 8 | * or licensors. Title to the Material remains with Intel Corporation or its |
| 9 | * suppliers and licensors. The Material contains trade secrets and proprietary |
| 10 | * and confidential information of Intel or its suppliers and licensors. The |
| 11 | * Material is protected by worldwide copyright and trade secret laws and |
| 12 | * treaty provisions. No part of the Material may be used, copied, reproduced, |
| 13 | * modified, published, uploaded, posted, transmitted, distributed, or |
| 14 | * disclosed in any way without Intel’s prior express written permission. |
| 15 | * |
| 16 | * No license under any patent, copyright, trade secret or other intellectual |
| 17 | * property right is granted to or conferred upon you by disclosure or delivery |
| 18 | * of the Materials, either expressly, by implication, inducement, estoppel or |
| 19 | * otherwise. Any license under such intellectual property rights must be |
| 20 | * express and approved by Intel in writing. |
| 21 | * |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 22 | * CREATED: 2011-06-01 |
| 23 | * UPDATED: 2011-07-27 |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 24 | */ |
| 25 | #include "ListeningSocket.h" |
| 26 | #include <sys/types.h> |
| 27 | #include <sys/socket.h> |
| 28 | #include <netinet/in.h> |
| 29 | #include <unistd.h> |
| 30 | #include <assert.h> |
| 31 | #include <netdb.h> |
| 32 | #include <strings.h> |
Frédéric Boisnard | 6c55e9a | 2014-01-10 18:46:33 +0100 | [diff] [blame^] | 33 | #include <sstream> |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 34 | |
| 35 | #include <stdio.h> |
| 36 | #include <errno.h> |
| 37 | |
| 38 | #define base CSocket |
| 39 | |
| 40 | CListeningSocket::CListeningSocket() |
| 41 | { |
| 42 | int iOption = true; |
| 43 | // Reuse option |
| 44 | setsockopt(getFd(), SOL_SOCKET, SO_REUSEADDR, &iOption, sizeof(iOption)); |
| 45 | } |
| 46 | |
| 47 | // Listen |
| 48 | bool CListeningSocket::listen(uint16_t uiPort) |
| 49 | { |
| 50 | struct sockaddr_in server_addr; |
| 51 | |
| 52 | // Fill server address |
| 53 | initSockAddrIn(&server_addr, INADDR_ANY, uiPort); |
| 54 | |
| 55 | // Bind |
| 56 | if (bind(getFd(), (struct sockaddr*)&server_addr, sizeof(struct sockaddr)) == -1) { |
| 57 | |
Frédéric Boisnard | 6c55e9a | 2014-01-10 18:46:33 +0100 | [diff] [blame^] | 58 | ostringstream oss; |
| 59 | oss << "CListeningSocket::listen::bind port " << uiPort; |
| 60 | perror(oss.str().c_str()); |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 61 | |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | if (::listen(getFd(), 5) == -1) { |
| 66 | |
Frédéric Boisnard | 6c55e9a | 2014-01-10 18:46:33 +0100 | [diff] [blame^] | 67 | ostringstream oss; |
| 68 | oss << "CListeningSocket::listen::bind port " << uiPort; |
| 69 | perror(oss.str().c_str()); |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 70 | |
| 71 | return false; |
| 72 | } |
| 73 | return true; |
| 74 | } |
| 75 | |
| 76 | // Accept |
| 77 | CSocket* CListeningSocket::accept() |
| 78 | { |
| 79 | struct sockaddr_in client_addr; |
| 80 | socklen_t ulClientAddrLen = sizeof(client_addr); |
| 81 | |
| 82 | int iSockId = ::accept(getFd(), (struct sockaddr*)&client_addr, &ulClientAddrLen); |
| 83 | |
| 84 | if (iSockId == -1) { |
| 85 | |
| 86 | perror("CListeningSocket::accept::accept"); |
| 87 | |
| 88 | return NULL; |
| 89 | } |
| 90 | return new CSocket(iSockId); |
| 91 | } |