2 #include <event2/event.h>
11 #include <trp_internal.h>
14 TRPS_INSTANCE *trps_new (TALLOC_CTX *mem_ctx)
16 TRPS_INSTANCE *trps=talloc(mem_ctx, TRPS_INSTANCE);
22 trps->mq=tr_mq_new(trps);
24 /* failed to allocate mq */
32 void trps_free (TRPS_INSTANCE *trps)
38 TR_MQ_MSG *trps_mq_pop(TRPS_INSTANCE *trps)
40 return tr_mq_pop(trps->mq);
43 void trps_mq_append(TRPS_INSTANCE *trps, TR_MQ_MSG *msg)
45 tr_mq_append(trps->mq, msg);
48 /* stand-in for a function that finds the connection for a particular peer */
50 static TRP_CONNECTION *trps_find_connection(TRPS_INSTANCE *trps)
56 void trps_add_connection(TRPS_INSTANCE *trps, TRP_CONNECTION *new)
61 trp_connection_append(trps->conn, new);
63 talloc_steal(trps, new);
66 /* ok to call more than once; guarantees connection no longer in the list */
67 void trps_remove_connection(TRPS_INSTANCE *trps, TRP_CONNECTION *remove)
69 trps->conn=trp_connection_remove(trps->conn, remove);
72 int trps_send_msg (TRPS_INSTANCE *trps,
75 const char *msg_content)
80 /* Send the request over the connection */
81 if (err = gsscon_write_encrypted_token (conn,
84 strlen(msg_content))) {
85 tr_err( "trps_send_msg: Error sending message over connection.\n");
92 static int trps_listen (TRPS_INSTANCE *trps, int port)
99 struct sockaddr_storage storage;
100 struct sockaddr_in in4;
103 struct sockaddr_in *saddr = (struct sockaddr_in *) &addr.in4;
105 saddr->sin_port = htons (port);
106 saddr->sin_family = AF_INET;
107 saddr->sin_addr.s_addr = INADDR_ANY;
109 if (0 > (conn = socket (AF_INET, SOCK_STREAM, 0)))
112 setsockopt(conn, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
114 if (0 > (rc = bind (conn, (struct sockaddr *) saddr, sizeof(struct sockaddr_in))))
117 if (0 > (rc = listen(conn, 512)))
120 tr_debug("trps_listen: TRP Server listening on port %d", port);
124 /* returns EACCES if authorization is denied */
125 int trps_auth_cb(gss_name_t clientName, gss_buffer_t displayName, void *data)
127 TRPS_INSTANCE *inst = (TRPS_INSTANCE *)data;
130 if (0!=inst->auth_handler(clientName, displayName, inst->cookie)) {
131 tr_debug("trps_auth_cb: client '%.*s' denied authorization.", displayName->length, displayName->value);
132 result=EACCES; /* denied */
138 static TRP_RC trps_read_message(TRPS_INSTANCE *trps, TRP_CONNECTION *conn, TR_MSG **msg)
144 tr_debug("trps_read_message: started");
145 if (err = gsscon_read_encrypted_token(trp_connection_get_fd(conn),
146 *(trp_connection_get_gssctx(conn)),
149 tr_debug("trps_read_message: error");
155 tr_debug("trps_read_message(): Request Received, %u bytes.", (unsigned) buflen);
156 tr_debug("trps_read_message(): %.*s", buflen, buf);
158 *msg=tr_msg_decode(buf, buflen);
163 int trps_get_listener(TRPS_INSTANCE *trps,
164 TRPS_MSG_FUNC msg_handler,
165 TRP_AUTH_FUNC auth_handler,
166 const char *hostname,
172 if (0 > (listen = trps_listen(trps, port))) {
174 if (0 == strerror_r(errno, errbuf, 256)) {
175 tr_debug("trps_get_listener: Error opening port %d: %s.", port, errbuf);
177 tr_debug("trps_get_listener: Unknown error openining port %d.", port);
182 /* opening port succeeded */
183 tr_debug("trps_get_listener: Opened port %d.", port);
185 /* make this socket non-blocking */
186 if (0 != fcntl(listen, F_SETFL, O_NONBLOCK)) {
187 tr_debug("trps_get_listener: Error setting O_NONBLOCK.");
194 /* store the caller's request handler & cookie */
195 trps->msg_handler = msg_handler;
196 trps->auth_handler = auth_handler;
197 trps->hostname = talloc_strdup(trps, hostname);
199 trps->cookie = cookie;
205 void trps_handle_connection(TRPS_INSTANCE *trps, TRP_CONNECTION *conn)
207 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
211 /* try to establish a GSS context */
212 if (0!=trp_connection_auth(conn, trps->auth_handler, trps->cookie)) {
213 tr_notice("tr_trps_conn_thread: failed to authorize connection");
216 tr_notice("trps_handle_connection: authorized connection");
218 /* loop as long as the connection exists */
219 while (trp_connection_get_status(conn)==TRP_CONNECTION_UP) {
220 rc=trps_read_message(trps, conn, &msg);
223 trps->msg_handler(trps, conn, msg); /* send the TR_MSG off to the callback */
227 trp_connection_close(conn);
231 tr_debug("trps_handle_connection: trps_read_message failed (%d)", rc);
235 tr_debug("trps_handle_connection: connection closed.");
236 talloc_free(tmp_ctx);