Adam Langley | 4866a02 | 2016-09-01 11:24:21 -0700 | [diff] [blame] | 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> |
Ankur Mittal | bde0239 | 2017-03-22 17:48:25 -0700 | [diff] [blame] | 21 | #include <stdio.h> |
Adam Langley | 4866a02 | 2016-09-01 11:24:21 -0700 | [diff] [blame] | 22 | |
Ankur Mittal | bde0239 | 2017-03-22 17:48:25 -0700 | [diff] [blame] | 23 | #include "logging.h" |
Adam Langley | 4866a02 | 2016-09-01 11:24:21 -0700 | [diff] [blame] | 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. |
Aaron Green | 4185cd3 | 2018-08-31 14:59:22 -0700 | [diff] [blame] | 29 | constexpr uint8_t root_private_key[roughtime::kPrivateKeyLength] = { |
Adam Langley | 4866a02 | 2016-09-01 11:24:21 -0700 | [diff] [blame] | 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; |
Ankur Mittal | bde0239 | 2017-03-22 17:48:25 -0700 | [diff] [blame] | 40 | ROUGHTIME_INIT_LOGGER(argv[0]); |
Adam Langley | 4866a02 | 2016-09-01 11:24:21 -0700 | [diff] [blame] | 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); |
Adam Langley | a5d2c83 | 2016-09-21 17:10:22 -0700 | [diff] [blame] | 65 | std::unique_ptr<roughtime::TimeSource> time_source( |
| 66 | new roughtime::SystemTimeSource); |
Adam Langley | 4866a02 | 2016-09-01 11:24:21 -0700 | [diff] [blame] | 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 | } |