Coverage Report

Created: 2026-12-01 07:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/logging-log4cxx/src/main/cpp/aprserversocket.cpp
Line
Count
Source
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 * contributor license agreements.  See the NOTICE file distributed with
4
 * this work for additional information regarding copyright ownership.
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 * (the "License"); you may not use this file except in compliance with
7
 * the License.  You may obtain a copy of the License at
8
 *
9
 *      http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
18
#include <log4cxx/private/aprserversocket.h>
19
#include <log4cxx/private/serversocket_priv.h>
20
#include <log4cxx/private/aprsocket.h>
21
#include <log4cxx/helpers/pool.h>
22
#include <log4cxx/helpers/transcoder.h>
23
#include <mutex>
24
#include "apr_network_io.h"
25
#include "apr_pools.h"
26
#include "apr_poll.h"
27
28
namespace LOG4CXX_NS
29
{
30
namespace helpers
31
{
32
33
517
#define _priv static_cast<APRServerSocketPriv*>(m_priv.get())
34
35
struct APRServerSocket::APRServerSocketPriv : public ServerSocketPrivate {
36
  Pool pool;
37
  std::mutex mutex;
38
  apr_socket_t* socket{ 0 };
39
  apr_pollset_t* pSet{ 0 };
40
  bool can_accept_incoming_network_connections{ false };
41
};
42
43
APRServerSocket::APRServerSocket()
44
17
  : ServerSocket(std::make_unique<APRServerSocketPriv>())
45
17
{
46
17
}
47
48
APRServerSocket::~APRServerSocket()
49
17
{
50
17
  if (_priv->socket)
51
0
    close();
52
17
}
53
54
bool APRServerSocket::is_open()
55
0
{
56
0
  std::lock_guard<std::mutex> lock(_priv->mutex);
57
0
  return _priv->can_accept_incoming_network_connections;
58
0
}
59
60
void APRServerSocket::open()
61
17
{
62
17
  apr_status_t status =
63
17
    apr_socket_create(&_priv->socket, APR_INET, SOCK_STREAM,
64
17
      APR_PROTO_TCP, _priv->pool.getAPRPool());
65
66
17
  if (status != APR_SUCCESS)
67
0
  {
68
0
    throw SocketException(status);
69
0
  }
70
71
17
  status = apr_socket_opt_set(_priv->socket, APR_SO_NONBLOCK, 1);
72
73
17
  if (status != APR_SUCCESS)
74
0
  {
75
0
    throw SocketException(status);
76
0
  }
77
78
17
  if (_priv->reuseAddress)
79
0
  {
80
0
    apr_status_t status = apr_socket_opt_set(_priv->socket, APR_SO_REUSEADDR, 1);
81
82
0
    if (status != APR_SUCCESS)
83
0
    {
84
0
      throw SocketException(status);
85
0
    }
86
0
  }
87
88
  // Create server socket address (including port number)
89
17
  std::string hostname_str;
90
17
  const char* hostname_ptr = NULL;
91
17
  if (!_priv->hostname.empty()) {
92
0
    Transcoder::encode(_priv->hostname, hostname_str);
93
0
    hostname_ptr = hostname_str.c_str();
94
0
  }
95
96
17
  apr_sockaddr_t* server_addr;
97
17
  status =
98
17
    apr_sockaddr_info_get(&server_addr, hostname_ptr, APR_INET,
99
17
      _priv->port, 0, _priv->pool.getAPRPool());
100
101
17
  if (status != APR_SUCCESS)
102
0
  {
103
0
    throw ConnectException(status);
104
0
  }
105
106
  // bind the socket to the address
107
17
  status = apr_socket_bind(_priv->socket, server_addr);
108
109
17
  if (status != APR_SUCCESS)
110
2
  {
111
2
    throw BindException(status);
112
2
  }
113
114
115
15
  status = apr_socket_listen(_priv->socket, 50);
116
117
15
  if (status != APR_SUCCESS)
118
0
  {
119
0
    throw SocketException(status);
120
0
  }
121
122
15
  _priv->can_accept_incoming_network_connections = true;
123
15
}
124
125
void APRServerSocket::close()
126
34
{
127
34
  std::lock_guard<std::mutex> lock(_priv->mutex);
128
129
34
  if (_priv->pSet)
130
29
    apr_pollset_wakeup(_priv->pSet);
131
34
  if (_priv->socket != 0)
132
17
  {
133
17
    apr_socket_close(_priv->socket);
134
17
    _priv->socket = 0;
135
17
  }
136
34
  _priv->can_accept_incoming_network_connections = false;
137
34
}
138
139
SocketPtr APRServerSocket::accept()
140
15
{
141
15
  apr_socket_t* s;
142
15
  {
143
15
    std::lock_guard<std::mutex> lock(_priv->mutex);
144
15
    s = _priv->socket;
145
15
  }
146
15
  if (s == 0)
147
0
  {
148
0
    throw NullPointerException(LOG4CXX_STR("socket"));
149
0
  }
150
151
15
  if (!_priv->pSet)
152
15
  {
153
15
    apr_pollfd_t poll;
154
15
    poll.p = _priv->pool.getAPRPool();
155
15
    poll.desc_type = APR_POLL_SOCKET;
156
15
    poll.reqevents = APR_POLLIN;
157
15
    poll.rtnevents = 0;
158
15
    poll.desc.s = s;
159
15
    poll.client_data = NULL;
160
15
    auto status = apr_pollset_create(&_priv->pSet, 1, _priv->pool.getAPRPool(), APR_POLLSET_WAKEABLE);
161
15
    if (status != APR_SUCCESS)
162
0
    {
163
0
      throw SocketException(status);
164
0
    }
165
15
    apr_pollset_add(_priv->pSet, &poll);
166
15
  }
167
168
15
  const apr_pollfd_t* descriptors;
169
15
  apr_int32_t signaled;
170
15
  apr_interval_time_t to = _priv->timeout * 1000;
171
15
  apr_status_t status = apr_pollset_poll(_priv->pSet, to, &signaled, &descriptors);
172
173
15
  if (APR_STATUS_IS_TIMEUP(status))
174
1
  {
175
1
    throw SocketTimeoutException();
176
1
  }
177
14
  else if (status != APR_SUCCESS)
178
14
  {
179
14
    throw SocketException(status);
180
14
  }
181
182
0
  apr_pool_t* newPool;
183
0
  status = apr_pool_create(&newPool, 0);
184
185
0
  if (status != APR_SUCCESS)
186
0
  {
187
0
    throw PoolException(status);
188
0
  }
189
190
0
  apr_socket_t* newSocket;
191
0
  status = apr_socket_accept(&newSocket, s, newPool);
192
193
0
  if (status != APR_SUCCESS)
194
0
  {
195
0
    apr_pool_destroy(newPool);
196
0
    throw SocketException(status);
197
0
  }
198
199
0
  status = apr_socket_opt_set(newSocket, APR_SO_NONBLOCK, 0);
200
201
0
  if (status != APR_SUCCESS)
202
0
  {
203
0
    apr_pool_destroy(newPool);
204
0
    throw SocketException(status);
205
0
  }
206
207
0
  return std::make_shared<APRSocket>(newSocket, newPool);
208
0
}
209
210
} //namespace helpers
211
} //namespace log4cxx