1 #include <sys/socket.h>
7 #include "../tlv11.h" /* FIXME: .. */
8 #include "libradsec-base.h"
12 const struct sockaddr *addr,
17 s = socket(AF_INET, type, 0); /* FIXME: do AF_INET6 too */
19 if (connect(s, addr, addrlen)) {
27 _list_new(const struct rs_config *ctx)
29 struct list *list = rs_malloc(ctx, sizeof(struct list));
31 memset(list, 0, sizeof(struct list));
36 _list_push(const struct rs_config *ctx, /* FIXME: code duplicate, list.c */
40 struct list_node *node;
42 node = rs_malloc(ctx, sizeof(struct list_node));
50 list->last->next = node;
60 _list_destroy(const struct rs_config *ctx, /* FIXME: code dup */
63 struct list_node *node, *next;
66 for (node = list->first; node; node = next) {
67 rs_free(ctx, node->data);
75 /* ------------------------------------------------------- */
77 rs_connect(const struct rs_config *conf,
78 const struct sockaddr *addr,
81 switch (conf->conn_type)
83 case RS_CONN_TYPE_UDP:
84 return _do_connect(SOCK_DGRAM, addr, addrlen);
85 case RS_CONN_TYPE_TCP:
86 return _do_connect(SOCK_STREAM, addr, addrlen);
88 case RS_CONN_TYPE_TLS:
90 case RS_CONN_TYPE_DTLS:
99 rs_disconnect( const struct rs_config *conf, int fd)
101 switch (conf->conn_type)
103 case RS_CONN_TYPE_UDP:
105 case RS_CONN_TYPE_TCP:
106 shutdown(fd, SHUT_RDWR);
108 case RS_CONN_TYPE_TLS:
110 case RS_CONN_TYPE_DTLS:
119 rs_packet_new(const struct rs_config *ctx,
120 const uint8_t buf[RS_HEADER_LEN],
123 struct rs_packet *p = rs_malloc(ctx, sizeof(struct rs_packet));
125 p->attrs = _list_new(ctx);
130 *count = (buf[2] << 8) + buf[3];
133 rs_packet_free(ctx, &p);
139 rs_packet_parse(const struct rs_config *ctx,
140 struct rs_packet **packet,
144 struct rs_packet *p = *packet;
151 rs_packet_free(ctx, &p);
156 while (i + 2 < buflen) {
162 "%s: DEBUG: attribute (type %d, len %d) has an invalid length\n",
163 __func__, atype, alen);
166 rs_packet_free(ctx, &p);
170 if (alen + i >= buflen) {
173 "%s: DEBUG: attribute (type %d, len %d) wouldn't fit packet\n",
174 __func__, atype, alen);
177 rs_packet_free(ctx, &p);
180 tlv = maketlv(atype, alen, (void *) (buf + i));
182 _list_push(ctx, p->attrs, tlv);
185 rs_packet_free(ctx, &p);
189 memcpy(p->auth, buf, 16);
194 rs_packet_free(const struct rs_config *ctx,
195 struct rs_packet **packet)
197 _list_destroy(ctx, (*packet)->attrs);
198 rs_free(ctx, *packet);
203 rs_packet_serialize(const struct rs_packet *packet,
207 struct list_node *ln;
211 for (ln = list_first(packet->attrs), pktlen = 20; ln; ln = list_next(ln))
212 pktlen += ((struct rs_attribute *)(ln->data))->length;
214 return -(pktlen - buflen);
216 buf[0] = packet->code;
218 buf[2] = (pktlen & 0xff00) >> 8;
219 buf[3] = pktlen & 0xff;
221 memcpy(buf + 4, packet->auth, 16);
223 for (ln = list_first(packet->attrs), i = 20; ln; ln = list_next(ln)) {
224 struct rs_attribute *a = (struct rs_attribute *)(ln->data);
226 buf[i++] = a->length;
227 memcpy(buf + i, a->value, a->length - 2);
234 /* Local Variables: */
235 /* c-file-style: "stroustrup" */