1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements.  See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License.  You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "ajp.h"
18
19/*
20 * Handle the CPING/CPONG
21 */
22apr_status_t ajp_handle_cping_cpong(apr_socket_t *sock,
23                                    request_rec *r,
24                                    apr_interval_time_t timeout)
25{
26    ajp_msg_t *msg;
27    apr_status_t rc, rv;
28    apr_interval_time_t org;
29    apr_byte_t result;
30
31    ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
32                         "Into ajp_handle_cping_cpong");
33
34    rc = ajp_msg_create(r->pool, AJP_PING_PONG_SZ, &msg);
35    if (rc != APR_SUCCESS) {
36        ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
37               "ajp_handle_cping_cpong: ajp_msg_create failed");
38        return rc;
39    }
40
41    rc = ajp_msg_serialize_cping(msg);
42    if (rc != APR_SUCCESS) {
43        ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
44               "ajp_handle_cping_cpong: ajp_marshal_into_msgb failed");
45        return rc;
46    }
47
48    rc = ajp_ilink_send(sock, msg);
49    if (rc != APR_SUCCESS) {
50        ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
51               "ajp_handle_cping_cpong: ajp_ilink_send failed");
52        return rc;
53    }
54
55    rc = apr_socket_timeout_get(sock, &org);
56    if (rc != APR_SUCCESS) {
57        ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
58               "ajp_handle_cping_cpong: apr_socket_timeout_get failed");
59        return rc;
60    }
61
62    /* Set CPING/CPONG response timeout */
63    rc = apr_socket_timeout_set(sock, timeout);
64    if (rc != APR_SUCCESS) {
65        ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
66               "ajp_handle_cping_cpong: apr_socket_timeout_set failed");
67        return rc;
68    }
69    ajp_msg_reuse(msg);
70
71    /* Read CPONG reply */
72    rv = ajp_ilink_receive(sock, msg);
73    if (rv != APR_SUCCESS) {
74        ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
75               "ajp_handle_cping_cpong: ajp_ilink_receive failed");
76        goto cleanup;
77    }
78
79    rv = ajp_msg_get_uint8(msg, &result);
80    if (rv != APR_SUCCESS) {
81        ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
82               "ajp_handle_cping_cpong: invalid CPONG message");
83        goto cleanup;
84    }
85    if (result != CMD_AJP13_CPONG) {
86        ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
87               "ajp_handle_cping_cpong: awaited CPONG, received %d ",
88               result);
89        rv = APR_EGENERAL;
90        goto cleanup;
91    }
92
93cleanup:
94    /* Restore original socket timeout */
95    rc = apr_socket_timeout_set(sock, org);
96    if (rc != APR_SUCCESS) {
97        ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
98               "ajp_handle_cping_cpong: apr_socket_timeout_set failed");
99        return rc;
100    }
101
102    ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
103                         "ajp_handle_cping_cpong: Done");
104    return rv;
105}
106