~eivnaes/sstp-client/1.0.13

1 by eivnaes
Moving files into trunk
1
/*!
2
 * @brief Functions for libsstp-api
3
 *
4
 * @file sstp-api.c
5
 *
6
 * @author Copyright (C) 2011 Eivind Naess, 
7
 *      All Rights Reserved
8
 *
9
 * @par License:
10
 *  This program is free software; you can redistribute it and/or modify
11
 *  it under the terms of the GNU General Public License as published by
12
 *  the Free Software Foundation; either version 2 of the License, or
13
 *  (at your option) any later version.
14
 *
15
 *  This program is distributed in the hope that it will be useful,
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 *  GNU General Public License for more details.
19
 *
20
 *  You should have received a copy of the GNU General Public License along
21
 *  with this program; if not, write to the Free Software Foundation, Inc.,
22
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23
 */
24
63 by eivnaes
Make sure we detect the pty.h by configure, and fixing unit test for sstp-task.c for darwin / mac.
25
#include <config.h>
1 by eivnaes
Moving files into trunk
26
#include <stdint.h>
27
#include <string.h>
28
29
#include <sstp-api.h>
30
31
32
SSTP_API 
33
sstp_api_msg_st *sstp_api_msg_new(unsigned char *buf, sstp_api_msg_t type)
34
{
35
    sstp_api_msg_st *msg = (sstp_api_msg_st*) buf;
36
    msg->msg_magic = SSTP_API_MSG_MAGIC;
37
    msg->msg_type  = type;
38
    msg->msg_len   = 0;
39
    return msg;
40
}
41
42
43
SSTP_API 
44
int sstp_api_msg_len(sstp_api_msg_st *msg)
45
{
46
    return (sizeof(*msg) + msg->msg_len);
47
}
48
49
50
SSTP_API
51
int sstp_api_msg_type(sstp_api_msg_st *msg, sstp_api_msg_t *type)
52
{
53
    int retval = (-1);
54
55
    /* Check the signature */
56
    if (msg->msg_magic != SSTP_API_MSG_MAGIC)
57
    {
58
        goto done;
59
    }
60
61
    /* Return the message type */
62
    *type = msg->msg_type;
63
64
    /* Success! */
65
    retval = 0;
66
67
done:
68
69
    return (retval);
70
}
71
72
73
SSTP_API 
74
void sstp_api_attr_add(sstp_api_msg_st *msg, sstp_api_attr_t type, 
75
    unsigned int len, void *data)
76
{
77
    sstp_api_attr_st *attr = (sstp_api_attr_st*) 
78
            &msg->msg_data[msg->msg_len];
79
80
    attr->attr_type = type;
81
    attr->attr_len  = len;
82
    memcpy(&attr->attr_data[0], data, attr->attr_len);
83
    msg->msg_len += (sizeof(*attr) + ALIGN32(attr->attr_len));
84
}
85
86
87
SSTP_API 
88
int sstp_api_attr_parse(char *buf, int length, sstp_api_attr_st *list[],
89
        int count)
90
{
91
    int index = 0;
92
93
    /* Reset the list of attribute pointers */
94
    memset(list, 0, sizeof(sstp_api_attr_st*) * count);
95
    
96
    /* Iterate over the memory */
97
    while (index < length)
98
    {
99
        /* Get the attribute */
100
        sstp_api_attr_st* attr = (sstp_api_attr_st*) &buf[index];
101
        if (attr->attr_type >  SSTP_API_ATTR_MAX ||
102
            attr->attr_type <= SSTP_API_ATTR_UNKNOWN)
103
        {
104
            return -1;
105
        }
106
107
        /* Assign the attribute type and increment length */
108
        list[attr->attr_type] = attr;
109
        index += (sizeof(*attr) + ALIGN32(attr->attr_len));
110
    }
111
112
    return 0;
113
}
114
115