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
19APLOG_USE_MODULE(proxy_ajp);
20
21/*
22 * Handle the CPING/CPONG
23 */
24apr_status_t ajp_handle_cping_cpong(apr_socket_t *sock,
25                                    request_rec *r,
26                                    apr_interval_time_t timeout)
27{
28    ajp_msg_t *msg;
29    apr_status_t rc, rv;
30    apr_interval_time_t org;
31    apr_byte_t result;
32
33    ap_log_rerror(APLOG_MARK, APLOG_TRACE8, 0, r,
34                         "Into ajp_handle_cping_cpong");
35
36    rc = ajp_msg_create(r->pool, AJP_PING_PONG_SZ, &msg);
37    if (rc != APR_SUCCESS) {
38        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01007)
39               "ajp_handle_cping_cpong: ajp_msg_create failed");
40        return rc;
41    }
42
43    rc = ajp_msg_serialize_cping(msg);
44    if (rc != APR_SUCCESS) {
45        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01008)
46               "ajp_handle_cping_cpong: ajp_marshal_into_msgb failed");
47        return rc;
48    }
49
50    rc = ajp_ilink_send(sock, msg);
51    ajp_msg_log(r, msg, "ajp_handle_cping_cpong: ajp_ilink_send packet dump");
52    if (rc != APR_SUCCESS) {
53        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01009)
54               "ajp_handle_cping_cpong: ajp_ilink_send failed");
55        return rc;
56    }
57
58    rc = apr_socket_timeout_get(sock, &org);
59    if (rc != APR_SUCCESS) {
60        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01010)
61               "ajp_handle_cping_cpong: apr_socket_timeout_get failed");
62        return rc;
63    }
64
65    /* Set CPING/CPONG response timeout */
66    rc = apr_socket_timeout_set(sock, timeout);
67    if (rc != APR_SUCCESS) {
68        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01011)
69               "ajp_handle_cping_cpong: apr_socket_timeout_set failed");
70        return rc;
71    }
72    ajp_msg_reuse(msg);
73
74    /* Read CPONG reply */
75    rv = ajp_ilink_receive(sock, msg);
76    if (rv != APR_SUCCESS) {
77        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01012)
78               "ajp_handle_cping_cpong: ajp_ilink_receive failed");
79        goto cleanup;
80    }
81
82    ajp_msg_log(r, msg, "ajp_handle_cping_cpong: ajp_ilink_receive packet dump");
83    rv = ajp_msg_get_uint8(msg, &result);
84    if (rv != APR_SUCCESS) {
85        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01013)
86               "ajp_handle_cping_cpong: invalid CPONG message");
87        goto cleanup;
88    }
89    if (result != CMD_AJP13_CPONG) {
90        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01014)
91               "ajp_handle_cping_cpong: awaited CPONG, received %d ",
92               result);
93        rv = APR_EGENERAL;
94        goto cleanup;
95    }
96
97cleanup:
98    /* Restore original socket timeout */
99    rc = apr_socket_timeout_set(sock, org);
100    if (rc != APR_SUCCESS) {
101        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01015)
102               "ajp_handle_cping_cpong: apr_socket_timeout_set failed");
103        return rc;
104    }
105
106    ap_log_rerror(APLOG_MARK, APLOG_TRACE8, 0, r,
107                         "ajp_handle_cping_cpong: Done");
108    return rv;
109}
110
111
112#define case_to_str(x)    case CMD_AJP13_##x:\
113                              return #x;\
114                              break;
115
116/**
117 * Convert numeric message type into string
118 * @param type      AJP message type
119 * @return          AJP message type as a string
120 */
121const char *ajp_type_str(int type)
122{
123    switch (type) {
124        case_to_str(FORWARD_REQUEST)
125        case_to_str(SEND_BODY_CHUNK)
126        case_to_str(SEND_HEADERS)
127        case_to_str(END_RESPONSE)
128        case_to_str(GET_BODY_CHUNK)
129        case_to_str(SHUTDOWN)
130        case_to_str(PING)
131        case_to_str(CPONG)
132        case_to_str(CPING)
133        default:
134            return "CMD_AJP13_UNKNOWN";
135    }
136
137}
138