summaryrefslogtreecommitdiff
path: root/accel-pptpd/ctrl
diff options
context:
space:
mode:
authorKozlov Dmitry <dima@server>2010-09-03 18:58:53 +0400
committerKozlov Dmitry <dima@server>2010-09-03 18:58:53 +0400
commite8aa3a1457295f70f8ccc9cd7f2f9073f01a5e2e (patch)
tree17a99610798630e83fc1032c82ee51c404ce19fe /accel-pptpd/ctrl
parenta22573e91dac7971aa4c9b1d874d6751e8502d16 (diff)
downloadaccel-ppp-e8aa3a1457295f70f8ccc9cd7f2f9073f01a5e2e.tar.gz
accel-ppp-e8aa3a1457295f70f8ccc9cd7f2f9073f01a5e2e.zip
project restructured
Diffstat (limited to 'accel-pptpd/ctrl')
-rw-r--r--accel-pptpd/ctrl/CMakeLists.txt4
-rw-r--r--accel-pptpd/ctrl/pptp.c562
-rw-r--r--accel-pptpd/ctrl/pptp_prot.h297
3 files changed, 863 insertions, 0 deletions
diff --git a/accel-pptpd/ctrl/CMakeLists.txt b/accel-pptpd/ctrl/CMakeLists.txt
new file mode 100644
index 0000000..97f8e0d
--- /dev/null
+++ b/accel-pptpd/ctrl/CMakeLists.txt
@@ -0,0 +1,4 @@
+INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})
+
+ADD_LIBRARY(pptp SHARED pptp.c)
+
diff --git a/accel-pptpd/ctrl/pptp.c b/accel-pptpd/ctrl/pptp.c
new file mode 100644
index 0000000..b503d85
--- /dev/null
+++ b/accel-pptpd/ctrl/pptp.c
@@ -0,0 +1,562 @@
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdarg.h>
+#include <errno.h>
+#include <string.h>
+#include <fcntl.h>
+#include <time.h>
+#include <arpa/inet.h>
+#include <netinet/in.h>
+#include <sys/socket.h>
+
+#include "if_pppox.h"
+
+#include "list.h"
+#include "pptp_prot.h"
+#include "triton.h"
+#include "log.h"
+#include "ppp.h"
+
+
+#define STATE_IDLE 0
+#define STATE_ESTB 1
+#define STATE_PPP 2
+#define STATE_FIN 3
+#define STATE_CLOSE 4
+
+struct pptp_conn_t
+{
+ struct triton_ctx_t ctx;
+ struct triton_md_handler_t hnd;
+ struct triton_timer_t timeout_timer;
+ struct triton_timer_t echo_timer;
+ int state;
+ int echo_sent;
+
+ uint8_t *in_buf;
+ int in_size;
+ uint8_t *out_buf;
+ int out_size;
+ int out_pos;
+
+ struct ppp_ctrl_t ctrl;
+ struct ppp_t ppp;
+};
+
+static int conf_timeout = 3;
+static int conf_echo_interval = 0;
+
+static int pptp_read(struct triton_md_handler_t *h);
+static int pptp_write(struct triton_md_handler_t *h);
+static void pptp_timeout(struct triton_timer_t *);
+static void ppp_started(struct ppp_t *);
+static void ppp_finished(struct ppp_t *);
+
+static void disconnect(struct pptp_conn_t *conn)
+{
+ triton_md_unregister_handler(&conn->hnd);
+ close(conn->hnd.fd);
+
+ if (conn->timeout_timer.period) {
+ triton_timer_del(&conn->timeout_timer);
+ conn->timeout_timer.period = 0;
+ }
+
+ if (conn->echo_timer.period) {
+ triton_timer_del(&conn->echo_timer);
+ conn->echo_timer.period = 0;
+ }
+
+ if (conn->state == STATE_PPP) {
+ conn->state = STATE_CLOSE;
+ ppp_terminate(&conn->ppp, 1);
+ }
+
+ triton_unregister_ctx(&conn->ctx);
+
+ free(conn->in_buf);
+ free(conn->out_buf);
+ free(conn);
+}
+
+static int post_msg(struct pptp_conn_t *conn, void *buf, int size)
+{
+ int n;
+ if (conn->out_size) {
+ log_debug("post_msg: buffer is not empty\n");
+ return -1;
+ }
+
+ n=write(conn->hnd.fd, buf, size);
+ if (n < 0) {
+ if (errno == EINTR || errno == EAGAIN)
+ n = 0;
+ else {
+ if (errno != EPIPE)
+ log_debug("post_msg: failed to write socket %i\n",errno);
+ return -1;
+ }
+ }
+
+ if ( n<size ) {
+ memcpy(conn->out_buf, buf + n, size - n);
+ triton_md_enable_handler(&conn->hnd, MD_MODE_WRITE);
+ }
+
+ return 0;
+}
+
+static int send_pptp_stop_ctrl_conn_rqst(struct pptp_conn_t *conn, int reason, int err_code)
+{
+ struct pptp_stop_ctrl_conn msg = {
+ .header = PPTP_HEADER_CTRL(PPTP_STOP_CTRL_CONN_RQST),
+ .reason_result = hton8(reason),
+ .error_code = hton8(err_code),
+ };
+
+ return post_msg(conn, &msg, sizeof(msg));
+}
+
+static int send_pptp_stop_ctrl_conn_rply(struct pptp_conn_t *conn, int reason, int err_code)
+{
+ struct pptp_stop_ctrl_conn msg = {
+ .header = PPTP_HEADER_CTRL(PPTP_STOP_CTRL_CONN_RPLY),
+ .reason_result = hton8(reason),
+ .error_code = hton8(err_code),
+ };
+
+ return post_msg(conn, &msg, sizeof(msg));
+}
+static int pptp_stop_ctrl_conn_rqst(struct pptp_conn_t *conn)
+{
+ struct pptp_stop_ctrl_conn *msg = (struct pptp_stop_ctrl_conn *)conn->in_buf;
+ log_info("PPTP_STOP_CTRL_CONN_RQST reason=%i error_code=%i\n",msg->reason_result, msg->error_code);
+
+ if (conn->state == STATE_PPP) {
+ conn->state = STATE_FIN;
+ ppp_terminate(&conn->ppp, 0);
+ }
+
+ send_pptp_stop_ctrl_conn_rply(conn, PPTP_CONN_STOP_OK, 0);
+ return -1;
+}
+
+static int send_pptp_start_ctrl_conn_rply(struct pptp_conn_t *conn, int res_code, int err_code)
+{
+ struct pptp_start_ctrl_conn msg = {
+ .header = PPTP_HEADER_CTRL(PPTP_START_CTRL_CONN_RPLY),
+ .version = htons(PPTP_VERSION),
+ .result_code = res_code,
+ .error_code = err_code,
+ .framing_cap = htonl(PPTP_FRAME_SYNC),
+ .bearer_cap = htonl(0),
+ .max_channels = htons(1),
+ .firmware_rev = htons(PPTP_FIRMWARE_VERSION),
+ };
+
+ memset(msg.hostname, 0, sizeof(msg.hostname));
+ strcpy((char*)msg.hostname, PPTP_HOSTNAME);
+
+ memset(msg.vendor, 0, sizeof(msg.vendor));
+ strcpy((char*)msg.vendor, PPTP_VENDOR);
+
+ return post_msg(conn, &msg, sizeof(msg));
+}
+static int pptp_start_ctrl_conn_rqst(struct pptp_conn_t *conn)
+{
+ struct pptp_start_ctrl_conn *msg = (struct pptp_start_ctrl_conn *)conn->in_buf;
+
+ if (conn->state != STATE_IDLE) {
+ log_info("unexpected PPTP_START_CTRL_CONN_RQST\n");
+ if (send_pptp_start_ctrl_conn_rply(conn, PPTP_CONN_RES_EXISTS, 0))
+ return -1;
+ return 0;
+ }
+
+ if (msg->version != htons(PPTP_VERSION)) {
+ log_info("PPTP version mismatch: expecting %x, received %s\n", PPTP_VERSION, msg->version);
+ if (send_pptp_start_ctrl_conn_rply(conn, PPTP_CONN_RES_PROTOCOL, 0))
+ return -1;
+ return 0;
+ }
+ if (!(ntohl(msg->framing_cap) & PPTP_FRAME_SYNC)) {
+ log_info("connection does not supports sync mode\n");
+ if (send_pptp_start_ctrl_conn_rply(conn, PPTP_CONN_RES_GE, 0))
+ return -1;
+ return 0;
+ }
+ if (send_pptp_start_ctrl_conn_rply(conn, PPTP_CONN_RES_SUCCESS, 0))
+ return -1;
+
+ triton_timer_mod(&conn->timeout_timer, 0);
+
+ conn->state = STATE_ESTB;
+
+ return 0;
+}
+
+static int send_pptp_out_call_rply(struct pptp_conn_t *conn, struct pptp_out_call_rqst *rqst, int call_id, int res_code, int err_code)
+{
+ struct pptp_out_call_rply msg = {
+ .header = PPTP_HEADER_CTRL(PPTP_OUT_CALL_RPLY),
+ .call_id = htons(call_id),
+ .call_id_peer = rqst->call_id,
+ .result_code = res_code,
+ .error_code = err_code,
+ .cause_code = 0,
+ .speed = rqst->bps_max,
+ .recv_size = rqst->recv_size,
+ .delay = 0,
+ .channel = 0,
+ };
+
+ return post_msg(conn, &msg, sizeof(msg));
+}
+
+static int pptp_out_call_rqst(struct pptp_conn_t *conn)
+{
+ struct pptp_out_call_rqst *msg = (struct pptp_out_call_rqst *)conn->in_buf;
+ struct sockaddr_pppox src_addr, dst_addr;
+ struct sockaddr_in addr;
+ socklen_t addrlen;
+ int pptp_sock;
+
+ if (conn->state != STATE_ESTB) {
+ log_error("unexpected PPTP_OUT_CALL_RQST\n");
+ if (send_pptp_out_call_rply(conn, msg, 0, PPTP_CALL_RES_GE, PPTP_GE_NOCONN))
+ return -1;
+ return 0;
+ }
+
+ src_addr.sa_family = AF_PPPOX;
+ src_addr.sa_protocol = PX_PROTO_PPTP;
+ src_addr.sa_addr.pptp.call_id = 0;
+ addrlen = sizeof(addr);
+ getsockname(conn->hnd.fd, (struct sockaddr*)&addr, &addrlen);
+ src_addr.sa_addr.pptp.sin_addr = addr.sin_addr;
+
+ dst_addr.sa_family = AF_PPPOX;
+ dst_addr.sa_protocol = PX_PROTO_PPTP;
+ dst_addr.sa_addr.pptp.call_id = htons(msg->call_id);
+ addrlen = sizeof(addr);
+ getpeername(conn->hnd.fd, (struct sockaddr*)&addr, &addrlen);
+ dst_addr.sa_addr.pptp.sin_addr = addr.sin_addr;
+
+ pptp_sock = socket(AF_PPPOX, SOCK_STREAM, PX_PROTO_PPTP);
+ if (pptp_sock < 0) {
+ log_error("failed to create PPTP socket (%s)\n", strerror(errno));
+ return -1;
+ }
+ if (bind(pptp_sock, (struct sockaddr*)&src_addr, sizeof(src_addr))) {
+ log_error("failed to bind PPTP socket (%s)\n", strerror(errno));
+ close(pptp_sock);
+ return -1;
+ }
+ addrlen = sizeof(src_addr);
+ getsockname(pptp_sock, (struct sockaddr*)&src_addr, &addrlen);
+
+ if (connect(pptp_sock, (struct sockaddr*)&dst_addr, sizeof(dst_addr))) {
+ log_error("failed to connect PPTP socket (%s)\n", strerror(errno));
+ close(pptp_sock);
+ return -1;
+ }
+
+ if (send_pptp_out_call_rply(conn, msg, src_addr.sa_addr.pptp.call_id, PPTP_CALL_RES_OK, 0))
+ return -1;
+
+ conn->ppp.fd = pptp_sock;
+ conn->ppp.chan_name = strdup(inet_ntoa(dst_addr.sa_addr.pptp.sin_addr));
+ conn->ppp.ctrl = &conn->ctrl;
+ conn->ctrl.ctx = &conn->ctx;
+ conn->ctrl.started = ppp_started;
+ conn->ctrl.finished = ppp_finished;
+ if (establish_ppp(&conn->ppp)) {
+ close(pptp_sock);
+ //if (send_pptp_stop_ctrl_conn_rqst(conn, 0, 0))
+ conn->state = STATE_FIN;
+ return -1;
+ }
+ conn->state = STATE_PPP;
+
+ triton_timer_del(&conn->timeout_timer);
+ conn->timeout_timer.period = 0;
+
+ if (conf_echo_interval) {
+ conn->echo_timer.period = conf_echo_interval * 1000;
+ triton_timer_add(&conn->ctx, &conn->echo_timer, 0);
+ }
+
+ return 0;
+}
+
+static int pptp_echo_rqst(struct pptp_conn_t *conn)
+{
+ struct pptp_echo_rqst *in_msg = (struct pptp_echo_rqst *)conn->in_buf;
+ struct pptp_echo_rply out_msg = {
+ .header = PPTP_HEADER_CTRL(PPTP_ECHO_RQST),
+ .identifier = in_msg->identifier,
+ .result_code = 1,
+ };
+
+ return post_msg(conn, &out_msg, sizeof(out_msg));
+}
+
+static int pptp_echo_rply(struct pptp_conn_t *conn)
+{
+ struct pptp_echo_rply *msg = (struct pptp_echo_rply *)conn->in_buf;
+ if (msg->identifier != conn->echo_sent) {
+ log_error("pptp:echo: identifier mismatch\n");
+ return -1;
+ }
+ conn->echo_sent = 0;
+ return 0;
+}
+static void pptp_send_echo(struct triton_timer_t *t)
+{
+ struct pptp_conn_t *conn = container_of(t, typeof(*conn), echo_timer);
+ struct pptp_echo_rqst msg = {
+ .header = PPTP_HEADER_CTRL(PPTP_ECHO_RQST),
+ };
+
+ if (conn->echo_sent) {
+ log_warn("pptp: no echo reply\n");
+ disconnect(conn);
+ return;
+ }
+
+ conn->echo_sent = random();
+ msg.identifier = conn->echo_sent;
+
+ if (post_msg(conn, &msg, sizeof(msg)))
+ disconnect(conn);
+}
+
+static int process_packet(struct pptp_conn_t *conn)
+{
+ struct pptp_header *hdr = (struct pptp_header *)conn->in_buf;
+ switch(ntohs(hdr->ctrl_type))
+ {
+ case PPTP_START_CTRL_CONN_RQST:
+ return pptp_start_ctrl_conn_rqst(conn);
+ case PPTP_STOP_CTRL_CONN_RQST:
+ return pptp_stop_ctrl_conn_rqst(conn);
+ case PPTP_OUT_CALL_RQST:
+ return pptp_out_call_rqst(conn);
+ case PPTP_ECHO_RQST:
+ return pptp_echo_rqst(conn);
+ case PPTP_ECHO_RPLY:
+ return pptp_echo_rply(conn);
+ }
+ return 0;
+}
+
+static int pptp_read(struct triton_md_handler_t *h)
+{
+ struct pptp_conn_t *conn=container_of(h,typeof(*conn),hnd);
+ struct pptp_header *hdr=(struct pptp_header *)conn->in_buf;
+ int n;
+
+ while(1) {
+ n = read(h->fd,conn->in_buf,PPTP_CTRL_SIZE_MAX-conn->in_size);
+ if (n < 0) {
+ if (errno == EINTR)
+ continue;
+ if (errno == EAGAIN)
+ return 0;
+ log_error("pptp: read: %s\n",strerror(errno));
+ goto drop;
+ }
+ if (n == 0)
+ goto drop;
+ conn->in_size += n;
+ if (conn->in_size >= sizeof(*hdr)) {
+ if (hdr->magic != htonl(PPTP_MAGIC))
+ goto drop;
+ if (ntohs(hdr->length) >= PPTP_CTRL_SIZE_MAX)
+ goto drop;
+ if (ntohs(hdr->length) > conn->in_size)
+ goto drop;
+ if (ntohs(hdr->length) == conn->in_size) {
+ if (ntohs(hdr->length) != PPTP_CTRL_SIZE(ntohs(hdr->ctrl_type)))
+ goto drop;
+ if (process_packet(conn))
+ goto drop;
+ conn->in_size = 0;
+ }
+ }
+ }
+drop:
+ disconnect(conn);
+ return 1;
+}
+static int pptp_write(struct triton_md_handler_t *h)
+{
+ struct pptp_conn_t *conn = container_of(h, typeof(*conn), hnd);
+ int n;
+
+ while (1) {
+ n = write(h->fd, conn->out_buf+conn->out_pos, conn->out_size-conn->out_pos);
+
+ if (n < 0) {
+ if (errno == EINTR)
+ continue;
+ if (errno == EAGAIN)
+ n = 0;
+ else {
+ if (errno != EPIPE)
+ log_error("pptp:post_msg: %s\n", strerror(errno));
+ disconnect(conn);
+ return 1;
+ }
+ }
+
+ conn->out_pos += n;
+ if (conn->out_pos == conn->out_size) {
+ conn->out_pos = 0;
+ conn->out_size = 0;
+ triton_md_disable_handler(h, MD_MODE_WRITE);
+ return 0;
+ }
+ }
+}
+static void pptp_timeout(struct triton_timer_t *t)
+{
+ struct pptp_conn_t *conn = container_of(t, typeof(*conn), timeout_timer);
+ disconnect(conn);
+}
+static void pptp_close(struct triton_ctx_t *ctx)
+{
+ struct pptp_conn_t *conn = container_of(ctx, typeof(*conn), ctx);
+ if (conn->state == STATE_PPP) {
+ conn->state = STATE_FIN;
+ ppp_terminate(&conn->ppp, 0);
+ } else
+ disconnect(conn);
+}
+static void ppp_started(struct ppp_t *ppp)
+{
+ log_msg("ppp_started\n");
+}
+static void ppp_finished(struct ppp_t *ppp)
+{
+ struct pptp_conn_t *conn = container_of(ppp, typeof(*conn), ppp);
+
+ log_msg("ppp_finished\n");
+ close(conn->ppp.fd);
+ //send_pptp_stop_ctrl_conn_rqst(conn, 0, 0);
+ if (conn->state != STATE_CLOSE) {
+ conn->state = STATE_CLOSE;
+ disconnect(conn);
+ }
+}
+
+//==================================
+
+struct pptp_serv_t
+{
+ struct triton_ctx_t ctx;
+ struct triton_md_handler_t hnd;
+};
+
+static int pptp_connect(struct triton_md_handler_t *h)
+{
+ struct sockaddr_in addr;
+ socklen_t size = sizeof(addr);
+ int sock;
+ struct pptp_conn_t *conn;
+
+ while(1) {
+ sock = accept(h->fd, (struct sockaddr *)&addr, &size);
+ if (sock < 0) {
+ if (errno == EAGAIN)
+ return 0;
+ log_error("pptp: accept failed: %s\n", strerror(errno));
+ continue;
+ }
+
+ log_info("pptp: new connection from %s\n", inet_ntoa(addr.sin_addr));
+
+ if (fcntl(sock, F_SETFL, O_NONBLOCK)) {
+ log_error("pptp: failed to set nonblocking mode: %s, closing connection...\n", strerror(errno));
+ close(sock);
+ continue;
+ }
+
+ conn = malloc(sizeof(*conn));
+ memset(conn, 0, sizeof(*conn));
+ conn->hnd.fd = sock;
+ conn->hnd.read = pptp_read;
+ conn->hnd.write = pptp_write;
+ conn->ctx.close = pptp_close;
+ conn->in_buf = malloc(PPTP_CTRL_SIZE_MAX);
+ conn->out_buf = malloc(PPTP_CTRL_SIZE_MAX);
+ conn->timeout_timer.expire = pptp_timeout;
+ conn->timeout_timer.period = conf_timeout * 1000;
+ conn->echo_timer.expire = pptp_send_echo;
+
+ triton_register_ctx(&conn->ctx);
+ triton_md_register_handler(&conn->ctx, &conn->hnd);
+ triton_md_enable_handler(&conn->hnd,MD_MODE_READ);
+ triton_timer_add(&conn->ctx, &conn->timeout_timer, 0);
+ }
+ return 0;
+}
+static void pptp_serv_close(struct triton_ctx_t *ctx)
+{
+ struct pptp_serv_t *s=container_of(ctx,typeof(*s),ctx);
+ triton_md_unregister_handler(&s->hnd);
+ close(s->hnd.fd);
+}
+
+static struct pptp_serv_t serv=
+{
+ .hnd.read=pptp_connect,
+ .ctx.close=pptp_serv_close,
+};
+
+static void __init pptp_init(void)
+{
+ struct sockaddr_in addr;
+ char *opt;
+
+ serv.hnd.fd = socket (PF_INET, SOCK_STREAM, 0);
+ if (serv.hnd.fd < 0) {
+ log_error("pptp: failed to create server socket: %s\n", strerror(errno));
+ return;
+ }
+ addr.sin_family = AF_INET;
+ addr.sin_port = htons (PPTP_PORT);
+ addr.sin_addr.s_addr = htonl (INADDR_ANY);
+ if (bind (serv.hnd.fd, (struct sockaddr *) &addr, sizeof (addr)) < 0) {
+ perror("pptp: bind");
+ log_error("pptp: failed to bind socket: %s\n", strerror(errno));
+ close(serv.hnd.fd);
+ return;
+ }
+
+ if (listen (serv.hnd.fd, 100) < 0) {
+ log_error("pptp: failed to listen socket: %s\n", strerror(errno));
+ close(serv.hnd.fd);
+ return;
+ }
+
+ if (fcntl(serv.hnd.fd, F_SETFL, O_NONBLOCK)) {
+ log_error("pptp: failed to set nonblocking mode: %s\n", strerror(errno));
+ close(serv.hnd.fd);
+ return;
+ }
+
+ triton_register_ctx(&serv.ctx);
+ triton_md_register_handler(&serv.ctx, &serv.hnd);
+ triton_md_enable_handler(&serv.hnd, MD_MODE_READ);
+
+ opt = conf_get_opt("pptp", "timeout");
+ if (opt && atoi(opt) > 0)
+ conf_timeout = atoi(opt);
+
+ opt = conf_get_opt("pptp", "echo-interval");
+ if (opt && atoi(opt) > 0)
+ conf_echo_interval = atoi(opt);
+}
+
diff --git a/accel-pptpd/ctrl/pptp_prot.h b/accel-pptpd/ctrl/pptp_prot.h
new file mode 100644
index 0000000..7e3ebdd
--- /dev/null
+++ b/accel-pptpd/ctrl/pptp_prot.h
@@ -0,0 +1,297 @@
+#ifndef PPTP_PROT_H
+#define PPTP_PROT_H
+
+#include <sys/types.h>
+
+#define hton8(x) (x)
+#define ntoh8(x) (x)
+#define hton16(x) htons(x)
+#define ntoh16(x) ntohs(x)
+#define hton32(x) htonl(x)
+#define ntoh32(x) ntohl(x)
+
+/* PPTP magic numbers: ----------------------------------------- */
+
+#define PPTP_MAGIC 0x1A2B3C4D /* Magic cookie for PPTP datagrams */
+#define PPTP_PORT 1723 /* PPTP TCP port number */
+#define PPTP_PROTO 47 /* PPTP IP protocol number */
+
+/* PPTP result codes:---------------------------------------- */
+#define PPTP_CONN_RES_SUCCESS 1
+#define PPTP_CONN_RES_GE 2
+#define PPTP_CONN_RES_EXISTS 3
+#define PPTP_CONN_RES_AUTH 4
+#define PPTP_CONN_RES_PROTOCOL 5
+
+#define PPTP_CONN_STOP_OK 1
+#define PPTP_CONN_STOP_GE 2
+
+#define PPTP_CALL_RES_OK 1
+#define PPTP_CALL_RES_GE 2
+
+#define PPTP_GE_NOCONN 1
+
+/* Control Connection Message Types: --------------------------- */
+
+#define PPTP_MESSAGE_CONTROL 1
+#define PPTP_MESSAGE_MANAGE 2
+
+/* Control Message Types: -------------------------------------- */
+
+/* (Control Connection Management) */
+#define PPTP_START_CTRL_CONN_RQST 1
+#define PPTP_START_CTRL_CONN_RPLY 2
+#define PPTP_STOP_CTRL_CONN_RQST 3
+#define PPTP_STOP_CTRL_CONN_RPLY 4
+#define PPTP_ECHO_RQST 5
+#define PPTP_ECHO_RPLY 6
+
+/* (Call Management) */
+#define PPTP_OUT_CALL_RQST 7
+#define PPTP_OUT_CALL_RPLY 8
+#define PPTP_IN_CALL_RQST 9
+#define PPTP_IN_CALL_RPLY 10
+#define PPTP_IN_CALL_CONNECT 11
+#define PPTP_CALL_CLEAR_RQST 12
+#define PPTP_CALL_CLEAR_NTFY 13
+
+/* (Error Reporting) */
+#define PPTP_WAN_ERR_NTFY 14
+
+/* (PPP Session Control) */
+#define PPTP_SET_LINK_INFO 15
+
+/* PPTP version information: --------------------------------------*/
+#define PPTP_VERSION_STRING "1.00"
+#define PPTP_VERSION 0x100
+#define PPTP_FIRMWARE_STRING "0.01"
+#define PPTP_FIRMWARE_VERSION 0x001
+
+#define PPTP_HOSTNAME "local"
+#define PPTP_VENDOR "cananian"
+
+/* PPTP capabilities: ---------------------------------------------*/
+
+/* (Framing capabilities for msg sender) */
+#define PPTP_FRAME_ASYNC 1
+#define PPTP_FRAME_SYNC 2
+#define PPTP_FRAME_ANY 3
+
+/* (Bearer capabilities for msg sender) */
+#define PPTP_BEARER_ANALOG 1
+#define PPTP_BEARER_DIGITAL 2
+#define PPTP_BEARER_ANY 3
+
+#define PPTP_RESULT_GENERAL_ERROR 2
+
+/* (Reasons to close a connection) */
+#define PPTP_STOP_NONE 1 /* no good reason */
+#define PPTP_STOP_PROTOCOL 2 /* can't support peer's protocol version */
+#define PPTP_STOP_LOCAL_SHUTDOWN 3 /* requester is being shut down */
+
+/* PPTP datagram structures (all data in network byte order): ----------*/
+
+struct pptp_header
+{
+ uint16_t length; /* message length in octets, including header */
+ uint16_t pptp_type; /* PPTP message type. 1 for control message. */
+ uint32_t magic; /* this should be PPTP_MAGIC. */
+ uint16_t ctrl_type; /* Control message type (0-15) */
+ uint16_t reserved0; /* reserved. MUST BE ZERO. */
+}__attribute__((packed));
+
+struct pptp_start_ctrl_conn /* for control message types 1 and 2 */
+{
+ struct pptp_header header;
+
+ uint16_t version; /* PPTP protocol version. = PPTP_VERSION */
+ uint8_t result_code; /* these two fields should be zero on rqst msg*/
+ uint8_t error_code; /* 0 unless result_code==2 (General Error) */
+ uint32_t framing_cap; /* Framing capabilities */
+ uint32_t bearer_cap; /* Bearer Capabilities */
+ uint16_t max_channels; /* Maximum Channels (=0 for PNS, PAC ignores) */
+ uint16_t firmware_rev; /* Firmware or Software Revision */
+ uint8_t hostname[64]; /* Host Name (64 octets, zero terminated) */
+ uint8_t vendor[64]; /* Vendor string (64 octets, zero term.) */
+}__attribute__((packed));
+
+struct pptp_stop_ctrl_conn /* for control message types 3 and 4 */
+{
+ struct pptp_header header;
+
+ uint8_t reason_result; /* reason for rqst, result for rply */
+ uint8_t error_code; /* MUST be 0, unless rply result==2 (general err)*/
+ uint16_t reserved1; /* MUST be 0 */
+}__attribute__((packed));
+
+struct pptp_echo_rqst /* for control message type 5 */
+{
+ struct pptp_header header;
+ uint32_t identifier; /* arbitrary value set by sender which is used */
+ /* to match up reply and request */
+}__attribute__((packed));
+
+struct pptp_echo_rply /* for control message type 6 */
+{
+ struct pptp_header header;
+ uint32_t identifier; /* should correspond to id of rqst */
+ uint8_t result_code;
+ uint8_t error_code; /* =0, unless result_code==2 (general error) */
+ uint16_t reserved1; /* MUST BE ZERO */
+}__attribute__((packed));
+
+struct pptp_out_call_rqst /* for control message type 7 */
+{
+ struct pptp_header header;
+ uint16_t call_id; /* Call ID (unique id used to multiplex data) */
+ uint16_t call_sernum; /* Call Serial Number (used for logging) */
+ uint32_t bps_min; /* Minimum BPS (lowest acceptable line speed) */
+ uint32_t bps_max; /* Maximum BPS (highest acceptable line speed) */
+ uint32_t bearer; /* Bearer type */
+ uint32_t framing; /* Framing type */
+ uint16_t recv_size; /* Recv. Window Size (no. of buffered packets) */
+ uint16_t delay; /* Packet Processing Delay (in 1/10 sec) */
+ uint16_t phone_len; /* Phone Number Length (num. of valid digits) */
+ uint16_t reserved1; /* MUST BE ZERO */
+ uint8_t phone_num[64]; /* Phone Number (64 octets, null term.) */
+ uint8_t subaddress[64]; /* Subaddress (64 octets, null term.) */
+}__attribute__((packed));
+
+struct pptp_out_call_rply /* for control message type 8 */
+{
+ struct pptp_header header;
+ uint16_t call_id; /* Call ID (used to multiplex data over tunnel)*/
+ uint16_t call_id_peer; /* Peer's Call ID (call_id of pptp_out_call_rqst)*/
+ uint8_t result_code; /* Result Code (1 is no errors) */
+ uint8_t error_code; /* Error Code (=0 unless result_code==2) */
+ uint16_t cause_code; /* Cause Code (addt'l failure information) */
+ uint32_t speed; /* Connect Speed (in BPS) */
+ uint16_t recv_size; /* Recv. Window Size (no. of buffered packets) */
+ uint16_t delay; /* Packet Processing Delay (in 1/10 sec) */
+ uint32_t channel; /* Physical Channel ID (for logging) */
+}__attribute__((packed));
+
+struct pptp_in_call_rqst /* for control message type 9 */
+{
+ struct pptp_header header;
+ uint16_t call_id; /* Call ID (unique id used to multiplex data) */
+ uint16_t call_sernum; /* Call Serial Number (used for logging) */
+ uint32_t bearer; /* Bearer type */
+ uint32_t channel; /* Physical Channel ID (for logging) */
+ uint16_t dialed_len; /* Dialed Number Length (# of valid digits) */
+ uint16_t dialing_len; /* Dialing Number Length (# of valid digits) */
+ uint8_t dialed_num[64]; /* Dialed Number (64 octets, zero term.) */
+ uint8_t dialing_num[64]; /* Dialing Number (64 octets, zero term.) */
+ uint8_t subaddress[64]; /* Subaddress (64 octets, zero term.) */
+}__attribute__((packed));
+
+struct pptp_in_call_rply /* for control message type 10 */
+{
+ struct pptp_header header;
+ uint16_t call_id; /* Call ID (used to multiplex data over tunnel)*/
+ uint16_t call_id_peer; /* Peer's Call ID (call_id of pptp_out_call_rqst)*/
+ uint8_t result_code; /* Result Code (1 is no errors) */
+ uint8_t error_code; /* Error Code (=0 unless result_code==2) */
+ uint16_t recv_size; /* Recv. Window Size (no. of buffered packets) */
+ uint16_t delay; /* Packet Processing Delay (in 1/10 sec) */
+ uint16_t reserved1; /* MUST BE ZERO */
+}__attribute__((packed));
+
+struct pptp_in_call_connect /* for control message type 11 */
+{
+ struct pptp_header header;
+ uint16_t call_id_peer; /* Peer's Call ID (call_id of pptp_out_call_rqst)*/
+ uint16_t reserved1; /* MUST BE ZERO */
+ uint32_t speed; /* Connect Speed (in BPS) */
+ uint16_t recv_size; /* Recv. Window Size (no. of buffered packets) */
+ uint16_t delay; /* Packet Processing Delay (in 1/10 sec) */
+ uint32_t framing; /* Framing type */
+}__attribute__((packed));
+
+struct pptp_call_clear_rqst /* for control message type 12 */
+{
+ struct pptp_header header;
+ uint16_t call_id; /* Call ID (used to multiplex data over tunnel)*/
+ uint16_t reserved1; /* MUST BE ZERO */
+}__attribute__((packed));
+
+struct pptp_call_clear_ntfy /* for control message type 13 */
+{
+ struct pptp_header header;
+ uint16_t call_id; /* Call ID (used to multiplex data over tunnel)*/
+ uint8_t result_code; /* Result Code */
+ uint8_t error_code; /* Error Code (=0 unless result_code==2) */
+ uint16_t cause_code; /* Cause Code (for ISDN, is Q.931 cause code) */
+ uint16_t reserved1; /* MUST BE ZERO */
+ uint8_t call_stats[128]; /* Call Statistics: 128 octets, ascii, 0-term */
+}__attribute__((packed));
+
+struct pptp_wan_err_ntfy /* for control message type 14 */
+{
+ struct pptp_header header;
+ uint16_t call_id_peer; /* Peer's Call ID (call_id of pptp_out_call_rqst)*/
+ uint16_t reserved1; /* MUST BE ZERO */
+ uint32_t crc_errors; /* CRC errors */
+ uint32_t frame_errors; /* Framing errors */
+ uint32_t hard_errors; /* Hardware overruns */
+ uint32_t buff_errors; /* Buffer overruns */
+ uint32_t time_errors; /* Time-out errors */
+ uint32_t align_errors; /* Alignment errors */
+}__attribute__((packed));
+
+struct pptp_set_link_info /* for control message type 15 */
+{
+ struct pptp_header header;
+ uint16_t call_id_peer; /* Peer's Call ID (call_id of pptp_out_call_rqst) */
+ uint16_t reserved1; /* MUST BE ZERO */
+ uint32_t send_accm; /* Send ACCM (for PPP packets; default 0xFFFFFFFF)*/
+ uint32_t recv_accm; /* Receive ACCM (for PPP pack.;default 0xFFFFFFFF)*/
+}__attribute__((packed));
+
+/* helpful #defines: -------------------------------------------- */
+#define pptp_isvalid_ctrl(header, type, length) \
+ (!( ( ntoh16(((struct pptp_header *)header)->length) < (length) ) || \
+ ( ntoh16(((struct pptp_header *)header)->pptp_type) !=(type) ) || \
+ ( ntoh32(((struct pptp_header *)header)->magic) !=PPTP_MAGIC) || \
+ ( ntoh16(((struct pptp_header *)header)->ctrl_type) > PPTP_SET_LINK_INFO) || \
+ ( ntoh16(((struct pptp_header *)header)->reserved0) !=0 ) ))
+
+#define PPTP_HEADER_CTRL(type) \
+{ hton16(PPTP_CTRL_SIZE(type)), \
+ hton16(PPTP_MESSAGE_CONTROL), \
+ hton32(PPTP_MAGIC), \
+ hton16(type), 0 }
+
+#define PPTP_CTRL_SIZE(type) ( \
+(type==PPTP_START_CTRL_CONN_RQST)?sizeof(struct pptp_start_ctrl_conn): \
+(type==PPTP_START_CTRL_CONN_RPLY)?sizeof(struct pptp_start_ctrl_conn): \
+(type==PPTP_STOP_CTRL_CONN_RQST )?sizeof(struct pptp_stop_ctrl_conn): \
+(type==PPTP_STOP_CTRL_CONN_RPLY )?sizeof(struct pptp_stop_ctrl_conn): \
+(type==PPTP_ECHO_RQST )?sizeof(struct pptp_echo_rqst): \
+(type==PPTP_ECHO_RPLY )?sizeof(struct pptp_echo_rply): \
+(type==PPTP_OUT_CALL_RQST )?sizeof(struct pptp_out_call_rqst): \
+(type==PPTP_OUT_CALL_RPLY )?sizeof(struct pptp_out_call_rply): \
+(type==PPTP_IN_CALL_RQST )?sizeof(struct pptp_in_call_rqst): \
+(type==PPTP_IN_CALL_RPLY )?sizeof(struct pptp_in_call_rply): \
+(type==PPTP_IN_CALL_CONNECT )?sizeof(struct pptp_in_call_connect): \
+(type==PPTP_CALL_CLEAR_RQST )?sizeof(struct pptp_call_clear_rqst): \
+(type==PPTP_CALL_CLEAR_NTFY )?sizeof(struct pptp_call_clear_ntfy): \
+(type==PPTP_WAN_ERR_NTFY )?sizeof(struct pptp_wan_err_ntfy): \
+(type==PPTP_SET_LINK_INFO )?sizeof(struct pptp_set_link_info): \
+0)
+#define max(a,b) (((a)>(b))?(a):(b))
+#define PPTP_CTRL_SIZE_MAX ( \
+max(sizeof(struct pptp_start_ctrl_conn), \
+max(sizeof(struct pptp_echo_rqst), \
+max(sizeof(struct pptp_echo_rply), \
+max(sizeof(struct pptp_out_call_rqst), \
+max(sizeof(struct pptp_out_call_rply), \
+max(sizeof(struct pptp_in_call_rqst), \
+max(sizeof(struct pptp_in_call_rply), \
+max(sizeof(struct pptp_in_call_connect), \
+max(sizeof(struct pptp_call_clear_rqst), \
+max(sizeof(struct pptp_call_clear_ntfy), \
+max(sizeof(struct pptp_wan_err_ntfy), \
+max(sizeof(struct pptp_set_link_info), 0)))))))))))))
+
+#endif