2 * Copyright (c) 2016, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of JANET(UK) nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31 * OF THE POSSIBILITY OF SUCH DAMAGE.
43 #include <trp_internal.h>
45 static int trpc_destructor(void *object)
47 TRPC_INSTANCE *trpc=talloc_get_type_abort(object, TRPC_INSTANCE);
48 if (trpc->gssname!=NULL)
49 tr_free_name(trpc->gssname);
53 /* also allocates the incoming mq */
54 TRPC_INSTANCE *trpc_new (TALLOC_CTX *mem_ctx)
56 TRPC_INSTANCE *trpc=talloc(mem_ctx, TRPC_INSTANCE);
62 trpc->shutting_down = 0;
63 trpc->mq=tr_mq_new(trpc);
68 talloc_set_destructor((void *)trpc, trpc_destructor);
74 void trpc_free (TRPC_INSTANCE *trpc)
80 TRPC_INSTANCE *trpc_get_next(TRPC_INSTANCE *trpc)
85 void trpc_set_next(TRPC_INSTANCE *trpc, TRPC_INSTANCE *next)
90 /* Ok to call more than once; guarantees trpc no longer in the list. Does not free removed element.
91 * Returns handle to new list, you must replace your old handle on the list with this. */
92 TRPC_INSTANCE *trpc_remove(TRPC_INSTANCE *trpc, TRPC_INSTANCE *remove)
94 TRPC_INSTANCE *cur=trpc;
95 TRPC_INSTANCE *last=NULL;
100 /* first element is a special case */
102 trpc=trpc_get_next(cur); /* advance list head */
104 /* it was not the first element */
106 cur=trpc_get_next(cur);
109 trpc_set_next(last, trpc_get_next(cur));
113 cur=trpc_get_next(cur);
119 static TRPC_INSTANCE *trpc_get_tail(TRPC_INSTANCE *trpc)
121 while((trpc!=NULL)&&(trpc_get_next(trpc)!=NULL))
122 trpc=trpc_get_next(trpc);
126 void trpc_append(TRPC_INSTANCE *trpc, TRPC_INSTANCE *new)
128 trpc_set_next(trpc_get_tail(trpc), new);
131 char *trpc_get_server(TRPC_INSTANCE *trpc)
136 void trpc_set_server(TRPC_INSTANCE *trpc, char *server)
141 TR_NAME *trpc_get_gssname(TRPC_INSTANCE *trpc)
143 return trpc->gssname;
146 /* takes responsibility for freeing gssname */
147 void trpc_set_gssname(TRPC_INSTANCE *trpc, TR_NAME *gssname)
149 trpc->gssname=gssname;
152 unsigned int trpc_get_port(TRPC_INSTANCE *trpc)
157 void trpc_set_port(TRPC_INSTANCE *trpc, unsigned int port)
162 TRP_CONNECTION *trpc_get_conn(TRPC_INSTANCE *trpc)
167 void trpc_set_conn(TRPC_INSTANCE *trpc, TRP_CONNECTION *conn)
172 TRP_CONNECTION_STATUS trpc_get_status(TRPC_INSTANCE *trpc)
174 return trp_connection_get_status(trpc_get_conn(trpc));
177 TR_MQ *trpc_get_mq(TRPC_INSTANCE *trpc)
182 void trpc_set_mq(TRPC_INSTANCE *trpc, TR_MQ *mq)
187 /* submit msg to trpc for transmission */
188 void trpc_mq_add(TRPC_INSTANCE *trpc, TR_MQ_MSG *msg)
190 tr_mq_add(trpc->mq, msg);
193 TR_MQ_MSG *trpc_mq_pop(TRPC_INSTANCE *trpc, struct timespec *ts_abort)
195 return tr_mq_pop(trpc->mq, ts_abort);
198 void trpc_mq_clear(TRPC_INSTANCE *trpc)
200 tr_mq_clear(trpc->mq);
203 TRP_RC trpc_connect(TRPC_INSTANCE *trpc)
205 return trp_connection_initiate(trpc_get_conn(trpc), trpc_get_server(trpc), trpc_get_port(trpc));
208 /* simple function, based on tidc_send_req */
209 TRP_RC trpc_send_msg (TRPC_INSTANCE *trpc,
210 const char *msg_content)
213 TRP_RC rc=TRP_SUCCESS;
215 /* Send the request over the connection */
216 if (err = gsscon_write_encrypted_token(trp_connection_get_fd(trpc_get_conn(trpc)),
217 *trp_connection_get_gssctx(trpc_get_conn(trpc)),
219 strlen(msg_content))) {
220 tr_err( "trpc_send_msg: Error sending message over connection.");