| 1 |
Metadata-Version: 2.1 |
| 2 |
Name: fbtftp |
| 3 |
Version: 0.5 |
| 4 |
Summary: A python3 framework to build dynamic TFTP servers |
| 5 |
Home-page: https://www.github.com/facebook/fbtftp |
| 6 |
Author: Angelo Failla |
| 7 |
Author-email: [email protected] |
| 8 |
License: BSD |
| 9 |
Keywords: tftp daemon infrastructure provisioning netboot |
| 10 |
Platform: UNKNOWN |
| 11 |
Classifier: Development Status :: 5 - Production/Stable |
| 12 |
Classifier: License :: OSI Approved :: MIT License |
| 13 |
Classifier: Operating System :: POSIX :: Linux |
| 14 |
Classifier: Programming Language :: Python :: 3 :: Only |
| 15 |
Classifier: Programming Language :: Python :: 3.5 |
| 16 |
Classifier: Programming Language :: Python :: 3.6 |
| 17 |
Classifier: Programming Language :: Python :: 3.7 |
| 18 |
Classifier: Programming Language :: Python :: 3.8 |
| 19 |
Classifier: Programming Language :: Python :: 3.9 |
| 20 |
Classifier: Topic :: Software Development :: Libraries :: Application Frameworks |
| 21 |
Classifier: Topic :: System :: Boot |
| 22 |
Classifier: Topic :: Utilities |
| 23 |
Classifier: Intended Audience :: Developers |
| 24 |
Description-Content-Type: text/markdown |
| 25 |
License-File: LICENSE |
| 26 |
License-File: LICENSE-examples |
| 27 |
License-File: AUTHORS |
| 28 |
|
| 29 |
[](https://travis-ci.org/facebook/fbtftp) |
| 30 |
[](https://codebeat.co/projects/github-com-facebook-fbtftp) |
| 31 |
|
| 32 |
# What is fbtftp? |
| 33 |
|
| 34 |
`fbtftp` is Facebook's implementation of a dynamic TFTP server framework. It |
| 35 |
lets you create custom TFTP servers and wrap your own logic into it in a very |
| 36 |
simple manner. |
| 37 |
Facebook currently uses it in production, and it's deployed at global scale |
| 38 |
across all of our data centers. |
| 39 |
|
| 40 |
# Why did you do that? |
| 41 |
|
| 42 |
We love to use existing open source software and to contribute upstream, but |
| 43 |
sometimes it's just not enough at our scale. We ended up writing our own tftp |
| 44 |
framework and decided to open source it. |
| 45 |
|
| 46 |
`fbtftp` was born from the need of having an easy-to-configure and |
| 47 |
easy-to-expand TFTP server, that would work at large scale. The standard |
| 48 |
`in.tftpd` is a 20+ years old piece of software written in C that is very |
| 49 |
difficult to extend. |
| 50 |
|
| 51 |
`fbtftp` is written in `python3` and lets you plug your own logic to: |
| 52 |
|
| 53 |
* publish per session and server wide statistics to your infrastructure |
| 54 |
* define how response data is built: |
| 55 |
* can be a file from disk; |
| 56 |
* can be a file created dynamically; |
| 57 |
* you name it! |
| 58 |
|
| 59 |
# How do you use `fbtftp` at Facebook? |
| 60 |
|
| 61 |
We created our own Facebook-specific server based on the framework to: |
| 62 |
|
| 63 |
* stream static files (initrd and kernels) from our http repositories (no need |
| 64 |
to fill your tftp root directory with files); |
| 65 |
* generate grub2 per-machine configuration dynamically (no need to copy grub2 |
| 66 |
configuration files on disk); |
| 67 |
* publish per-server and per-connection statistics to our internal monitoring |
| 68 |
systems; |
| 69 |
* deployment is easy and "container-ready", just copy the application somewhere, |
| 70 |
start it and you are done. |
| 71 |
|
| 72 |
# Is it better than the other TFTP servers? |
| 73 |
|
| 74 |
It depends on your needs! `fbtftp` is written in Python 3 using a |
| 75 |
multiprocessing model; its primary focus is not speed, but flexibility and |
| 76 |
scalability. Yet it is fast enough at our datacenter scale :) |
| 77 |
It is well-suited for large installations where scalability and custom features |
| 78 |
are needed. |
| 79 |
|
| 80 |
# What does it support? |
| 81 |
|
| 82 |
The framework implements the following RFCs: |
| 83 |
|
| 84 |
* [RFC 1350](https://tools.ietf.org/html/rfc1350) (the main TFTP specification) |
| 85 |
* [RFC 2347](https://tools.ietf.org/html/rfc2347) (Option Extension) |
| 86 |
* [RFC 2348](https://tools.ietf.org/html/rfc2348) (Blocksize option) |
| 87 |
* [RFC 2349](https://tools.ietf.org/html/rfc2349) (Timeout Interval and Transfer |
| 88 |
Size Options). |
| 89 |
|
| 90 |
Note that the server framework only support RRQs (read only) operations. |
| 91 |
(Who uses WRQ TFTP requests in 2019? :P) |
| 92 |
|
| 93 |
# How does it work? |
| 94 |
|
| 95 |
All you need to do is understanding three classes and two callback functions, |
| 96 |
and you are good to go: |
| 97 |
|
| 98 |
* `BaseServer`: This class implements the process which deals with accepting new |
| 99 |
requests on the UDP port provided. Default TFTP parameters like timeout, port |
| 100 |
number and number of retries can be passed. This class doesn't have to be used |
| 101 |
directly, you must inherit from it and override `get_handler()` method to |
| 102 |
return an instance of `BaseHandler`. |
| 103 |
The class accepts a `server_stats_callback`, more about it below. the callback |
| 104 |
is not re-entrant, if you need this you have to implement your own locking |
| 105 |
logic. This callback is executed periodically and you can use it to publish |
| 106 |
server level stats to your monitoring infrastructure. A series of predefined |
| 107 |
counters are provided. Refer to the class documentation to find out more. |
| 108 |
|
| 109 |
* `BaseHandler`: This class deals with talking to a single client. This class |
| 110 |
lives into its separate process, process which is spawned by the `BaserServer` |
| 111 |
class, which will make sure to reap the child properly when the session is |
| 112 |
over. Do not use this class as is, instead inherit from it and override the `get_response_data()` method. Such method must return an instance of a subclass of `ResponseData`. |
| 113 |
|
| 114 |
* `ResponseData`: it's a file-like class that implements `read(num_bytes)`, |
| 115 |
`size()` and `close()`. As the previous two classes you'll have to inherit |
| 116 |
from this and implement those methods. This class basically let you define how |
| 117 |
to return the actual data |
| 118 |
|
| 119 |
* `server_stats_callback`: function that is called periodically (every 60 |
| 120 |
seconds by default). The callback is not re-entrant, if you need this you have |
| 121 |
to implement your own locking logic. This callback is executed periodically |
| 122 |
and you can use it to publish server level stats to your monitoring |
| 123 |
infrastructure. A series of predefined counters are provided. |
| 124 |
Refer to the class documentation to find out more. |
| 125 |
|
| 126 |
* `session_stats_callback`: function that is called when a client session is |
| 127 |
over. |
| 128 |
|
| 129 |
# Requirements |
| 130 |
|
| 131 |
* Linux (or any system that supports [`epoll`](http://linux.die.net/man/4/epoll)) |
| 132 |
* BSD (or any system that supports [`kqueue`](https://www.freebsd.org/cgi/man.cgi?query=kqueue&sektion=2)) |
| 133 |
* Python 3.4+ |
| 134 |
|
| 135 |
# Installation |
| 136 |
|
| 137 |
`fbtftp` is distributed with the standard `distutils` package, so you can build |
| 138 |
it with: |
| 139 |
|
| 140 |
``` |
| 141 |
python setup.py build |
| 142 |
``` |
| 143 |
|
| 144 |
and install it with: |
| 145 |
|
| 146 |
``` |
| 147 |
python setup.py install |
| 148 |
``` |
| 149 |
|
| 150 |
Be sure to run as root if you want to install `fbtftp` system wide. You can also |
| 151 |
use a `virtualenv`, or install it as user by running: |
| 152 |
|
| 153 |
``` |
| 154 |
python setup.py install --user |
| 155 |
``` |
| 156 |
|
| 157 |
# Example |
| 158 |
|
| 159 |
Writing your own server is simple. Let's take a look at how to write a simple |
| 160 |
server that serves files from disk: |
| 161 |
|
| 162 |
```python |
| 163 |
from fbtftp.base_handler import BaseHandler |
| 164 |
from fbtftp.base_handler import ResponseData |
| 165 |
from fbtftp.base_server import BaseServer |
| 166 |
|
| 167 |
import os |
| 168 |
|
| 169 |
class FileResponseData(ResponseData): |
| 170 |
def __init__(self, path): |
| 171 |
self._size = os.stat(path).st_size |
| 172 |
self._reader = open(path, 'rb') |
| 173 |
|
| 174 |
def read(self, n): |
| 175 |
return self._reader.read(n) |
| 176 |
|
| 177 |
def size(self): |
| 178 |
return self._size |
| 179 |
|
| 180 |
def close(self): |
| 181 |
self._reader.close() |
| 182 |
|
| 183 |
def print_session_stats(stats): |
| 184 |
print(stats) |
| 185 |
|
| 186 |
def print_server_stats(stats): |
| 187 |
counters = stats.get_and_reset_all_counters() |
| 188 |
print('Server stats - every {} seconds'.format(stats.interval)) |
| 189 |
print(counters) |
| 190 |
|
| 191 |
class StaticHandler(BaseHandler): |
| 192 |
def __init__(self, server_addr, peer, path, options, root, stats_callback): |
| 193 |
self._root = root |
| 194 |
super().__init__(server_addr, peer, path, options, stats_callback) |
| 195 |
|
| 196 |
def get_response_data(self): |
| 197 |
return FileResponseData(os.path.join(self._root, self._path)) |
| 198 |
|
| 199 |
class StaticServer(BaseServer): |
| 200 |
def __init__(self, address, port, retries, timeout, root, |
| 201 |
handler_stats_callback, server_stats_callback=None): |
| 202 |
self._root = root |
| 203 |
self._handler_stats_callback = handler_stats_callback |
| 204 |
super().__init__(address, port, retries, timeout, server_stats_callback) |
| 205 |
|
| 206 |
def get_handler(self, server_addr, peer, path, options): |
| 207 |
return StaticHandler( |
| 208 |
server_addr, peer, path, options, self._root, |
| 209 |
self._handler_stats_callback) |
| 210 |
|
| 211 |
def main(): |
| 212 |
server = StaticServer(address='::', port=69, retries=3, timeout=5, |
| 213 |
root='/var/tftproot', |
| 214 |
handler_stats_callback=print_session_stats, |
| 215 |
server_stats_callback=print_server_stats) |
| 216 |
try: |
| 217 |
server.run() |
| 218 |
except KeyboardInterrupt: |
| 219 |
server.close() |
| 220 |
|
| 221 |
if __name__ == '__main__': |
| 222 |
main() |
| 223 |
``` |
| 224 |
|
| 225 |
# Who wrote it? |
| 226 |
|
| 227 |
`fbtftp` was created by Marcin Wyszynski (@marcinwyszynski) and Angelo Failla <[email protected]> at Facebook Ireland. |
| 228 |
|
| 229 |
Other honorable contributors: |
| 230 |
* Andrea Barberio <[email protected]> |
| 231 |
|
| 232 |
# License |
| 233 |
|
| 234 |
MIT License |
| 235 |
|
| 236 |
|