roughtime.googlesource.com/roughtime.git@v0.0.0-20201210012726-dd529367052d/simple_server_main.cc (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 #include <arpa/inet.h> 16 #include <netinet/in.h> 17 #include <stdlib.h> 18 #include <sys/socket.h> 19 #include <sys/types.h> 20 #include <unistd.h> 21 #include <stdio.h> 22 23 #include "logging.h" 24 #include "simple_server.h" 25 #include "sys_time.h" 26 27 // root_private_key is an Ed25519 private key used by simple_server. The 28 // private part consists of all zeros and so is only for use in this example. 29 constexpr uint8_t root_private_key[roughtime::kPrivateKeyLength] = { 30 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 31 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 32 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3b, 0x6a, 0x27, 0xbc, 33 0xce, 0xb6, 0xa4, 0x2d, 0x62, 0xa3, 0xa8, 0xd0, 0x2a, 0x6f, 0x0d, 0x73, 34 0x65, 0x32, 0x15, 0x77, 0x1d, 0xe2, 0x43, 0xa6, 0x3a, 0xc0, 0x48, 0xa1, 35 0x8b, 0x59, 0xda, 0x29, 36 }; 37 38 int main(int argc, char **argv) { 39 int requested_port = -1; 40 ROUGHTIME_INIT_LOGGER(argv[0]); 41 if (argc == 2) { 42 char *endptr; 43 requested_port = strtoul(argv[1], &endptr, 10); 44 if (*endptr != 0) { 45 requested_port = -1; 46 } 47 } 48 49 if (requested_port < 0 || requested_port > 65535) { 50 fprintf(stderr, "Usage: %s <port number>\n", argv[0]); 51 return 1; 52 } 53 54 int fd; 55 uint16_t port; 56 if (!roughtime::UdpProcessor::MakeSocket(requested_port, &fd, &port)) { 57 return 1; 58 } 59 60 fprintf(stderr, "Listening on port %d.\n", port); 61 62 std::unique_ptr<roughtime::Identity> identity = 63 roughtime::SimpleServer::MakeIdentity(root_private_key, 0, 64 2147483647000000); 65 std::unique_ptr<roughtime::TimeSource> time_source( 66 new roughtime::SystemTimeSource); 67 68 auto server = 69 std::unique_ptr<roughtime::SimpleServer>(new roughtime::SimpleServer( 70 std::move(identity), std::move(time_source), fd)); 71 server->RunUntilError(); 72 return 1; 73 }