1 /** @file
2  *
3  *  A brief file description
4  *
5  *  @section license License
6  *
7  *  Licensed to the Apache Software Foundation (ASF) under one
8  *  or more contributor license agreements.  See the NOTICE file
9  *  distributed with this work for additional information
10  *  regarding copyright ownership.  The ASF licenses this file
11  *  to you under the Apache License, Version 2.0 (the
12  *  "License"); you may not use this file except in compliance
13  *  with the License.  You may obtain a copy of the License at
14  *
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  *
17  *  Unless required by applicable law or agreed to in writing, software
18  *  distributed under the License is distributed on an "AS IS" BASIS,
19  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  *  See the License for the specific language governing permissions and
21  *  limitations under the License.
22  */
23 #include "QUICTokenCreator.h"
24 
25 bool
will_generate_frame(QUICEncryptionLevel level,size_t current_packet_size,bool ack_eliciting,uint32_t seq_num)26 QUICTokenCreator::will_generate_frame(QUICEncryptionLevel level, size_t current_packet_size, bool ack_eliciting, uint32_t seq_num)
27 {
28   if (!this->_is_level_matched(level)) {
29     return false;
30   }
31 
32   return !this->_is_resumption_token_sent;
33 }
34 
35 QUICFrame *
generate_frame(uint8_t * buf,QUICEncryptionLevel level,uint64_t connection_credit,uint16_t maximum_frame_size,size_t current_packet_size,uint32_t seq_num)36 QUICTokenCreator::generate_frame(uint8_t *buf, QUICEncryptionLevel level, uint64_t connection_credit, uint16_t maximum_frame_size,
37                                  size_t current_packet_size, uint32_t seq_num)
38 {
39   QUICFrame *frame = nullptr;
40 
41   if (!this->_is_level_matched(level)) {
42     return frame;
43   }
44 
45   if (this->_is_resumption_token_sent) {
46     return frame;
47   }
48 
49   if (this->_context->connection_info()->direction() == NET_VCONNECTION_IN) {
50     // TODO Make expiration period configurable
51     QUICResumptionToken token(this->_context->connection_info()->five_tuple().source(),
52                               this->_context->connection_info()->connection_id(), Thread::get_hrtime() + HRTIME_HOURS(24));
53     frame = QUICFrameFactory::create_new_token_frame(buf, token, this->_issue_frame_id(), this);
54     if (frame) {
55       if (frame->size() < maximum_frame_size) {
56         this->_is_resumption_token_sent = true;
57       } else {
58         // Cancel generating frame
59         frame = nullptr;
60       }
61     }
62   }
63 
64   return frame;
65 }
66 
67 void
_on_frame_lost(QUICFrameInformationUPtr & info)68 QUICTokenCreator::_on_frame_lost(QUICFrameInformationUPtr &info)
69 {
70   this->_is_resumption_token_sent = false;
71 }
72