blob: 620aed1986e789ed13a48e50273958477cfc3793 [file] [log] [blame]
Kevin Rocard93250d12012-07-19 17:48:30 +02001/*
Patrick Benavoli68a91282011-08-31 11:23:23 +02002 * 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 Benavoli68a91282011-08-31 11:23:23 +020022 * CREATED: 2011-06-01
23 * UPDATED: 2011-07-27
Patrick Benavoli68a91282011-08-31 11:23:23 +020024 */
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 Boisnard6c55e9a2014-01-10 18:46:33 +010033#include <sstream>
Patrick Benavoli68a91282011-08-31 11:23:23 +020034
35#include <stdio.h>
36#include <errno.h>
37
38#define base CSocket
39
40CListeningSocket::CListeningSocket()
41{
42 int iOption = true;
43 // Reuse option
44 setsockopt(getFd(), SOL_SOCKET, SO_REUSEADDR, &iOption, sizeof(iOption));
45}
46
47// Listen
48bool 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 Boisnard6c55e9a2014-01-10 18:46:33 +010058 ostringstream oss;
59 oss << "CListeningSocket::listen::bind port " << uiPort;
60 perror(oss.str().c_str());
Patrick Benavoli68a91282011-08-31 11:23:23 +020061
62 return false;
63 }
64
65 if (::listen(getFd(), 5) == -1) {
66
Frédéric Boisnard6c55e9a2014-01-10 18:46:33 +010067 ostringstream oss;
68 oss << "CListeningSocket::listen::bind port " << uiPort;
69 perror(oss.str().c_str());
Patrick Benavoli68a91282011-08-31 11:23:23 +020070
71 return false;
72 }
73 return true;
74}
75
76// Accept
77CSocket* 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}