|
| 1 | +// This file is a part of the IncludeOS unikernel - www.includeos.org |
| 2 | +// |
| 3 | +// Copyright 2015-2016 Oslo and Akershus University College of Applied Sciences |
| 4 | +// and Alfred Bratterud |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | + |
| 18 | +#include <os> |
| 19 | +#include <net/inet> |
| 20 | + |
| 21 | +/** |
| 22 | + * An example to show incoming and outgoing TCP Connections. |
| 23 | + * In this example, IncludeOS is listening on port 80. |
| 24 | + * |
| 25 | + * Data received on port 80 will be redirected to a |
| 26 | + * outgoing connection to a (in this case) python server (server.py) |
| 27 | + * |
| 28 | + * Data received from the python server connection |
| 29 | + * will be redirected back to the client. |
| 30 | + * |
| 31 | + * To try it out, use netcat to connect to this IncludeOS instance. |
| 32 | +**/ |
| 33 | + |
| 34 | +using Connection_ptr = net::tcp::Connection_ptr; |
| 35 | +using Disconnect = net::tcp::Connection::Disconnect; |
| 36 | + |
| 37 | +// Address to our python server: 10.0.2.2:1337 |
| 38 | +// @note: This may have to be modified depending on network and server settings. |
| 39 | +net::Socket python_server{ {10,0,0,1} , 1337}; |
| 40 | + |
| 41 | +void Service::start() |
| 42 | +{ |
| 43 | +#ifdef USERSPACE_LINUX |
| 44 | + extern void create_network_device(int N, const char* route, const char* ip); |
| 45 | + create_network_device(0, "10.0.0.0/24", "10.0.0.1"); |
| 46 | +#endif |
| 47 | + auto& inet = net::Super_stack::get(0); |
| 48 | + inet.network_config( |
| 49 | + { 10, 0, 0, 42 }, // IP |
| 50 | + { 255,255,255, 0 }, // Netmask |
| 51 | + { 10, 0, 0, 1 }, // Gateway |
| 52 | + { 10, 0, 0, 1 }); // DNS |
| 53 | + |
| 54 | + // Set up a TCP server on port 81 |
| 55 | + auto& server = inet.tcp().listen(81); |
| 56 | + printf("Server listening: %s \n", server.local().to_string().c_str()); |
| 57 | + |
| 58 | + // When someone connects to our server |
| 59 | + server.on_connect( |
| 60 | + [&inet] (Connection_ptr client) { |
| 61 | + printf("Connected [Client]: %s\n", client->to_string().c_str()); |
| 62 | + // Make an outgoing connection to our python server |
| 63 | + auto outgoing = inet.tcp().connect(python_server); |
| 64 | + // When outgoing connection to python sever is established |
| 65 | + outgoing->on_connect( |
| 66 | + [client] (Connection_ptr python) { |
| 67 | + if (!python) { |
| 68 | + printf("Connection failed!\n"); |
| 69 | + return; |
| 70 | + } |
| 71 | + printf("Connected [Python]: %s\n", python->to_string().c_str()); |
| 72 | + |
| 73 | + // Setup handlers for when data is received on client and python connection |
| 74 | + // When client reads data |
| 75 | + client->on_read(1024, [python](auto buf) { |
| 76 | + python->write(buf); |
| 77 | + }); |
| 78 | + |
| 79 | + // When client is disconnecting |
| 80 | + client->on_disconnect([python](Connection_ptr, Disconnect reason) { |
| 81 | + printf("Disconnected [Client]: %s\n", reason.to_string().c_str()); |
| 82 | + python->close(); |
| 83 | + }); |
| 84 | + |
| 85 | + // When python is disconnecting |
| 86 | + python->on_disconnect([client](Connection_ptr, Disconnect reason) { |
| 87 | + printf("Disconnected [Python]: %s\n", reason.to_string().c_str()); |
| 88 | + client->close(); |
| 89 | + }); |
| 90 | + }); // << onConnect (outgoing (python)) |
| 91 | + }); // << onConnect (client) |
| 92 | +} |
0 commit comments