summaryrefslogtreecommitdiff
path: root/lib/base.c
blob: 38086c0de6054ae18544b665427babf508f6c826 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <sys/socket.h>
#include <errno.h>
#include <stdlib.h>
#include "libradsec-base.h"

static int
do_connect(int type,
	   const struct sockaddr *addr,
	   socklen_t addrlen)
{
  int s;

  s = socket(AF_INET, type, 0); /* FIXME: do AF_INET6 too */
  if (s >= 0)
      if (connect(s, addr, addrlen)) {
	  close(s);
	  s = -1;
      }
  return s;
}

int
rs_connect(const struct rs_config *conf,
	   const struct sockaddr *addr,
	   socklen_t addrlen)
{
    switch (conf->conn_type)
    {
    case RS_CONN_TYPE_UDP:
	return do_connect(SOCK_DGRAM, addr, addrlen);
    case RS_CONN_TYPE_TCP:
	return do_connect(SOCK_STREAM, addr, addrlen);
	/* fall thru */
    case RS_CONN_TYPE_TLS:
	/* fall thru */
    case RS_CONN_TYPE_DTLS:
	/* fall thru */
    default:
	errno = ENOSYS;
	return -1;
    }
}

int
rs_disconnect( const struct rs_config *conf, int fd)
{
    switch (conf->conn_type)
    {
    case RS_CONN_TYPE_UDP:
	return close(fd);
    case RS_CONN_TYPE_TCP:
	shutdown(fd, SHUT_RDWR);
	return close(fd);
    case RS_CONN_TYPE_TLS:
	/* fall thru */
    case RS_CONN_TYPE_DTLS:
	/* fall thru */
    default:
	errno = ENOSYS;
	return -1;
    }
}

struct rs_packet *
rs_packet_new(const struct rs_config *ctx,
	      const uint8_t buf[RS_HEADER_LEN],
	      size_t *count)
{
    struct rs_packet *p =
	(ctx->alloc_scheme.malloc ? ctx->alloc_scheme.malloc : malloc)(20);
    if (p) {
	p->code = buf[0];
	p->id = buf[1];
	if (count)
	    *count = 256 * buf[2] + buf[3];
    }
    return p;
}

struct rs_packet *
rs_packet_parse(const struct rs_config *ctx,
		struct rs_packet *packet,
		const uint8_t *buf,
		size_t buflen)
{
    if (buflen < 16) {
	rs_packet_free(ctx, packet);
	errno = EPROTO;
	return NULL;
    }
    memcpy(packet->auth, buf, 16);
    /* TODO: copy attributes starting at buf[16].  */
    return packet;
}

void
rs_packet_free(const struct rs_config *ctx,
	       struct rs_packet *packet)
{
    (ctx->alloc_scheme.free ? ctx->alloc_scheme.free : free)(packet);
}

ssize_t
rs_packet_serialize(const struct rs_packet *packet,
		    uint8_t *buf,
		    size_t buflen)
{
    fixme;
}

/* Local Variables: */
/* c-file-style: "stroustrup" */
/* End: */