1 /*
2  * Copyright 2022 Young Mei
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifdef __ZEPHYR__
8 #include <zephyr/kernel.h>
9 #endif
10 
11 #include <cstdio>
12 #include <cstdlib>
13 
14 #include <thrift/protocol/TBinaryProtocol.h>
15 #include <thrift/protocol/TCompactProtocol.h>
16 #include <thrift/server/TSimpleServer.h>
17 #include <thrift/transport/TBufferTransports.h>
18 #include <thrift/transport/TSSLServerSocket.h>
19 #include <thrift/transport/TSSLSocket.h>
20 #include <thrift/transport/TServerSocket.h>
21 
22 #include "Hello.h"
23 #include "HelloHandler.h"
24 
25 using namespace ::apache::thrift;
26 using namespace ::apache::thrift::protocol;
27 using namespace ::apache::thrift::transport;
28 using namespace ::apache::thrift::server;
29 
30 #ifndef IS_ENABLED
31 #define IS_ENABLED(flag) flag
32 #endif
33 
34 #ifndef CONFIG_THRIFT_COMPACT_PROTOCOL
35 #define CONFIG_THRIFT_COMPACT_PROTOCOL 0
36 #endif
37 
38 #ifndef CONFIG_THRIFT_SSL_SOCKET
39 #define CONFIG_THRIFT_SSL_SOCKET 0
40 #endif
41 
42 #ifdef __ZEPHYR__
main(void)43 int main(void)
44 #else
45 int main(int argc, char **argv)
46 #endif
47 {
48 	std::string my_addr;
49 
50 #ifdef __ZEPHYR__
51 	my_addr = CONFIG_NET_CONFIG_MY_IPV4_ADDR;
52 #else
53 	if (IS_ENABLED(CONFIG_THRIFT_SSL_SOCKET)) {
54 		if (argc != 5) {
55 			printf("usage: %s <ip> <native-cert.pem> <native-key.pem> "
56 			       "<qemu-cert.pem>\n",
57 			       argv[0]);
58 			return EXIT_FAILURE;
59 		}
60 	} else {
61 		if (argc != 2) {
62 			printf("usage: %s <ip>\n", argv[0]);
63 			return EXIT_FAILURE;
64 		}
65 	}
66 
67 	my_addr = std::string(argv[1]);
68 #endif
69 
70 	const int port = 4242;
71 	std::shared_ptr<TServerTransport> serverTransport;
72 	std::shared_ptr<TTransportFactory> transportFactory;
73 	std::shared_ptr<TProtocolFactory> protocolFactory;
74 	std::shared_ptr<HelloHandler> handler(new HelloHandler());
75 	std::shared_ptr<TProcessor> processor(new HelloProcessor(handler));
76 
77 	if (IS_ENABLED(CONFIG_THRIFT_SSL_SOCKET)) {
78 		std::shared_ptr<TSSLSocketFactory> socketFactory(new TSSLSocketFactory());
79 		socketFactory->server(true);
80 #ifdef __ZEPHYR__
81 		static const char qemu_cert_pem[] = {
82 #include "qemu_cert.pem.inc"
83 			'\0'
84 		};
85 
86 		static const char qemu_key_pem[] = {
87 #include "qemu_key.pem.inc"
88 			'\0'
89 		};
90 
91 		static const char native_cert_pem[] = {
92 #include "native_cert.pem.inc"
93 			'\0'
94 		};
95 
96 		socketFactory->loadCertificateFromBuffer(qemu_cert_pem);
97 		socketFactory->loadPrivateKeyFromBuffer(qemu_key_pem);
98 		socketFactory->loadTrustedCertificatesFromBuffer(native_cert_pem);
99 #else
100 		socketFactory->loadCertificate(argv[2]);
101 		socketFactory->loadPrivateKey(argv[3]);
102 		socketFactory->loadTrustedCertificates(argv[4]);
103 #endif
104 		serverTransport =
105 			std::make_shared<TSSLServerSocket>("0.0.0.0", port, socketFactory);
106 	} else {
107 		serverTransport = std::make_shared<TServerSocket>(my_addr, port);
108 	}
109 
110 	transportFactory = std::make_shared<TBufferedTransportFactory>();
111 	if (IS_ENABLED(CONFIG_THRIFT_COMPACT_PROTOCOL)) {
112 		protocolFactory = std::make_shared<TCompactProtocolFactory>();
113 	} else {
114 		protocolFactory = std::make_shared<TBinaryProtocolFactory>();
115 	}
116 
117 	TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory);
118 
119 	try {
120 		server.serve();
121 	} catch (std::exception &e) {
122 		printf("caught exception: %s\n", e.what());
123 		return EXIT_FAILURE;
124 	}
125 
126 	return EXIT_SUCCESS;
127 }
128