ninenano

Client implementation of the 9P protocol for constrained devices

git clone https://git.8pit.net/ninenano.git

  1#include <stdio.h>
  2#include <stdlib.h>
  3
  4#include "shell.h"
  5#include "xtimer.h"
  6
  7#include "9p.h"
  8#include "9pfs.h"
  9#include "vfs.h"
 10
 11#include "net/af.h"
 12#include "net/gnrc/ipv6.h"
 13#include "net/gnrc/tcp.h"
 14
 15#ifndef NINEPFS_HOST
 16  #error "NINEPFS_HOST was not defined."
 17#endif
 18
 19#ifndef NINEPFS_PORT
 20  #error "NINEPFS_PORT was not defined."
 21#endif
 22
 23/**
 24 * GNRC transmission control block.
 25 */
 26static gnrc_tcp_tcb_t tcb;
 27
 28/**
 29 * 9P connection context.
 30 */
 31static _9pfs fs;
 32
 33/* import "ifconfig" shell command, used for printing addresses */
 34extern int _netif_config(int argc, char **argv);
 35
 36static ssize_t
 37recvfn(void *buf, size_t count)
 38{
 39	return gnrc_tcp_recv(&tcb, buf,
 40		count, GNRC_TCP_CONNECTION_TIMEOUT_DURATION);
 41}
 42
 43static ssize_t
 44sendfn(void *buf, size_t count)
 45{
 46	return gnrc_tcp_send(&tcb, buf,
 47		count, GNRC_TCP_CONNECTION_TIMEOUT_DURATION);
 48}
 49
 50int
 51main(void)
 52{
 53	int ret;
 54	ipv6_addr_t remote;
 55	char line_buf[SHELL_DEFAULT_BUFSIZE];
 56	vfs_mount_t mountp;
 57
 58	if (!ipv6_addr_from_str(&remote, NINEPFS_HOST)) {
 59		fprintf(stderr, "Address '%s' is malformed\n", NINEPFS_HOST);
 60		return EXIT_FAILURE;
 61	}
 62
 63	mountp.mount_point = "/9pfs";
 64	mountp.fs = &_9p_file_system;
 65	mountp.private_data = &fs;
 66
 67	puts("Waiting for address autoconfiguration...");
 68	xtimer_sleep(3);
 69
 70	puts("Configured network interfaces:");
 71	_netif_config(0, NULL);
 72
 73	gnrc_tcp_tcb_init(&tcb);
 74	if ((ret = gnrc_tcp_open_active(&tcb, AF_INET6,
 75			(uint8_t *)&remote, NINEPFS_PORT, 0))) {
 76		fprintf(stderr, "Couldn't open TCP connection\n");
 77		return EXIT_FAILURE;
 78	}
 79
 80	fs.uname = "glenda";
 81	fs.aname = NULL;
 82
 83	_9pinit(&fs.ctx, recvfn, sendfn);
 84
 85	if ((ret = vfs_mount(&mountp))) {
 86		fprintf(stderr, "vfs_mount failed, err code: %d\n", ret);
 87		return EXIT_FAILURE;
 88	}
 89
 90	puts("All up, running the shell now");
 91	shell_run(NULL, line_buf, SHELL_DEFAULT_BUFSIZE);
 92
 93	if ((ret = vfs_umount(&mountp))) {
 94		fprintf(stderr, "vfs_unmount failed, err code: %d\n", ret);
 95		return EXIT_FAILURE;
 96	}
 97
 98	gnrc_tcp_close(&tcb);
 99	return EXIT_SUCCESS;
100}