lcp_opt_magic.c 2.2 KB
Newer Older
1 2 3 4 5 6 7 8
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>

#include "ppp.h"
#include "ppp_lcp.h"
#include "log.h"

D
Dmitry Kozlov 已提交
9 10
#include "memdebug.h"

11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
static struct lcp_option_t *magic_init(struct ppp_lcp_t *lcp);
static void magic_free(struct ppp_lcp_t *lcp, struct lcp_option_t *opt);
static int magic_send_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr);
static int magic_recv_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr);
static void magic_print(void (*print)(const char *fmt,...),struct lcp_option_t*, uint8_t *ptr);

struct magic_option_t
{
	struct lcp_option_t opt;
	int magic;
};

static struct lcp_option_handler_t magic_opt_hnd=
{
	.init=magic_init,
	.send_conf_req=magic_send_conf_req,
	.recv_conf_req=magic_recv_conf_req,
	.free=magic_free,
	.print=magic_print,
};

static struct lcp_option_t *magic_init(struct ppp_lcp_t *lcp)
{
D
Dmitry Kozlov 已提交
34
	struct magic_option_t *magic_opt=_malloc(sizeof(*magic_opt));
35 36 37 38 39
	memset(magic_opt,0,sizeof(*magic_opt));
	magic_opt->magic=random();
	magic_opt->opt.id=CI_MAGIC;
	magic_opt->opt.len=6;

40 41
	lcp->magic = magic_opt->magic;

42 43 44 45 46 47 48
	return &magic_opt->opt;
}

static void magic_free(struct ppp_lcp_t *lcp, struct lcp_option_t *opt)
{
	struct magic_option_t *magic_opt=container_of(opt,typeof(*magic_opt),opt);

D
Dmitry Kozlov 已提交
49
	_free(magic_opt);
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
}

static int magic_send_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr)
{
	struct magic_option_t *magic_opt=container_of(opt,typeof(*magic_opt),opt);
	struct lcp_opt32_t *opt32=(struct lcp_opt32_t*)ptr;
	opt32->hdr.id=CI_MAGIC;
	opt32->hdr.len=6;
	opt32->val=htonl(magic_opt->magic);
	return 6;
}

static int magic_recv_conf_req(struct ppp_lcp_t *lcp, struct lcp_option_t *opt, uint8_t *ptr)
{
	struct magic_option_t *magic_opt=container_of(opt,typeof(*magic_opt),opt);
	struct lcp_opt32_t *opt32=(struct lcp_opt32_t*)ptr;

K
Kozlov Dmitry 已提交
67 68 69
	if (!ptr)
		return LCP_OPT_NAK;

70 71
	if (magic_opt->magic==ntohl(opt32->val))
	{
72
		log_ppp_error("loop detected");
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
		return -1;
	}
	return LCP_OPT_ACK;
}

static void magic_print(void (*print)(const char *fmt,...),struct lcp_option_t *opt, uint8_t *ptr)
{
	struct magic_option_t *magic_opt=container_of(opt,typeof(*magic_opt),opt);

	print("<magic %04x>",magic_opt->magic);
}

static void __init magic_opt_init()
{
	lcp_option_register(&magic_opt_hnd);
}