roughtime.googlesource.com/roughtime.git@v0.0.0-20201210012726-dd529367052d/simple_server.h (about) 1 /* Copyright 2016 The Roughtime Authors. 2 * 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. */ 14 15 #ifndef SECURITY_ROUGHTIME_SIMPLE_SERVER_H_ 16 #define SECURITY_ROUGHTIME_SIMPLE_SERVER_H_ 17 18 #include <arpa/inet.h> 19 #include <netinet/in.h> 20 21 #include "server.h" 22 #include "time_source.h" 23 #include "udp_processor.h" 24 25 namespace roughtime { 26 27 // SimpleServer wraps |Server| with enough intelligence to read requests from, 28 // and write requests to, a supplied socket. There's no reason in principle 29 // that one couldn't run multiple instances per socket. 30 // 31 // This class exists for testing and to provide an example rather than as a 32 // basis for a production-quality server. 33 class SimpleServer { 34 public: 35 // |identity| is the server's certificate. |fd| is the socket to be used to 36 // receive requests and send responses. 37 SimpleServer(std::unique_ptr<Identity> identity, 38 std::unique_ptr<TimeSource> time_source, int fd); 39 40 // RunUntilError receives and responds to a batches of requests until an 41 // unexpected error occurs. 42 void RunUntilError(); 43 44 // ProcessBatch calls UdpProcessor::ProcessBatch. 45 bool ProcessBatch(); 46 47 // MakeIdentity creates a dummy server certificate that is valid for the 48 // given time range. 49 static std::unique_ptr<Identity> MakeIdentity( 50 const uint8_t root_private_key[kPrivateKeyLength], 51 rough_time_t mint, rough_time_t maxt); 52 53 private: 54 const int fd_; 55 Server server_; 56 UdpProcessor udp_processor_; 57 }; 58 59 } // namespace roughtime 60 61 #endif // SECURITY_ROUGHTIME_SIMPLE_SERVER_H_