1 /* udscs.c Unix Domain Socket Client Server framework. A framework for quickly
2 creating select() based servers capable of handling multiple clients and
3 matching select() based clients using variable size messages.
5 Copyright 2010 Red Hat, Inc.
8 Hans de Goede <hdegoede@redhat.com>
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 3 of the License, or
13 (at your option) any later version.
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.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
32 #include <sys/socket.h>
41 struct udscs_buf
*next
;
44 struct udscs_connection
{
46 const char * const *type_to_string
;
50 #ifndef UDSCS_NO_SERVER
51 struct ucred peer_cred
;
54 /* Read stuff, single buffer, separate header and data buffer */
56 struct udscs_message_header header
;
57 struct udscs_buf data
;
59 /* Writes are stored in a linked list of buffers, with both the header
60 + data for a single message in 1 buffer. */
61 struct udscs_buf
*write_buf
;
64 udscs_read_callback read_callback
;
65 udscs_disconnect_callback disconnect_callback
;
67 struct udscs_connection
*next
;
68 struct udscs_connection
*prev
;
71 struct udscs_connection
*udscs_connect(const char *socketname
,
72 udscs_read_callback read_callback
,
73 udscs_disconnect_callback disconnect_callback
,
74 const char * const type_to_string
[], int no_types
, int debug
)
77 struct sockaddr_un address
;
78 struct udscs_connection
*conn
;
80 conn
= calloc(1, sizeof(*conn
));
84 conn
->type_to_string
= type_to_string
;
85 conn
->no_types
= no_types
;
88 conn
->fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
90 syslog(LOG_ERR
, "creating unix domain socket: %m");
95 address
.sun_family
= AF_UNIX
;
96 snprintf(address
.sun_path
, sizeof(address
.sun_path
), "%s", socketname
);
97 c
= connect(conn
->fd
, (struct sockaddr
*)&address
, sizeof(address
));
100 syslog(LOG_DEBUG
, "connect %s: %m", socketname
);
106 conn
->read_callback
= read_callback
;
107 conn
->disconnect_callback
= disconnect_callback
;
110 syslog(LOG_DEBUG
, "%p connected to %s", conn
, socketname
);
115 void udscs_destroy_connection(struct udscs_connection
**connp
)
117 struct udscs_buf
*wbuf
, *next_wbuf
;
118 struct udscs_connection
*conn
= *connp
;
123 if (conn
->disconnect_callback
)
124 conn
->disconnect_callback(conn
);
126 wbuf
= conn
->write_buf
;
128 next_wbuf
= wbuf
->next
;
134 free(conn
->data
.buf
);
137 conn
->next
->prev
= conn
->prev
;
139 conn
->prev
->next
= conn
->next
;
144 syslog(LOG_DEBUG
, "%p disconnected", conn
);
150 void udscs_set_user_data(struct udscs_connection
*conn
, void *data
)
152 conn
->user_data
= data
;
155 void *udscs_get_user_data(struct udscs_connection
*conn
)
160 return conn
->user_data
;
163 int udscs_write(struct udscs_connection
*conn
, uint32_t type
, uint32_t arg1
,
164 uint32_t arg2
, const uint8_t *data
, uint32_t size
)
166 struct udscs_buf
*wbuf
, *new_wbuf
;
167 struct udscs_message_header header
;
169 new_wbuf
= malloc(sizeof(*new_wbuf
));
174 new_wbuf
->size
= sizeof(header
) + size
;
175 new_wbuf
->next
= NULL
;
176 new_wbuf
->buf
= malloc(new_wbuf
->size
);
177 if (!new_wbuf
->buf
) {
187 memcpy(new_wbuf
->buf
, &header
, sizeof(header
));
188 memcpy(new_wbuf
->buf
+ sizeof(header
), data
, size
);
191 if (type
< conn
->no_types
)
192 syslog(LOG_DEBUG
, "%p sent %s, arg1: %u, arg2: %u, size %u",
193 conn
, conn
->type_to_string
[type
], arg1
, arg2
, size
);
196 "%p sent invalid message %u, arg1: %u, arg2: %u, size %u",
197 conn
, type
, arg1
, arg2
, size
);
200 if (!conn
->write_buf
) {
201 conn
->write_buf
= new_wbuf
;
205 /* maybe we should limit the write_buf stack depth ? */
206 wbuf
= conn
->write_buf
;
210 wbuf
->next
= new_wbuf
;
215 /* A helper for udscs_do_read() */
216 static void udscs_read_complete(struct udscs_connection
**connp
)
218 struct udscs_connection
*conn
= *connp
;
221 if (conn
->header
.type
< conn
->no_types
)
223 "%p received %s, arg1: %u, arg2: %u, size %u",
224 conn
, conn
->type_to_string
[conn
->header
.type
],
225 conn
->header
.arg1
, conn
->header
.arg2
, conn
->header
.size
);
228 "%p received invalid message %u, arg1: %u, arg2: %u, size %u",
229 conn
, conn
->header
.type
, conn
->header
.arg1
, conn
->header
.arg2
,
233 if (conn
->read_callback
) {
234 conn
->read_callback(connp
, &conn
->header
, conn
->data
.buf
);
235 if (!*connp
) /* Was the connection disconnected by the callback ? */
239 conn
->header_read
= 0;
240 memset(&conn
->data
, 0, sizeof(conn
->data
));
243 /* A helper for udscs_client_handle_fds() */
244 static void udscs_do_read(struct udscs_connection
**connp
)
249 struct udscs_connection
*conn
= *connp
;
251 if (conn
->header_read
< sizeof(conn
->header
)) {
252 to_read
= sizeof(conn
->header
) - conn
->header_read
;
253 dest
= (uint8_t *)&conn
->header
+ conn
->header_read
;
255 to_read
= conn
->data
.size
- conn
->data
.pos
;
256 dest
= conn
->data
.buf
+ conn
->data
.pos
;
259 n
= read(conn
->fd
, dest
, to_read
);
263 syslog(LOG_ERR
, "reading unix domain socket: %m, disconnecting %p",
267 udscs_destroy_connection(connp
);
271 if (conn
->header_read
< sizeof(conn
->header
)) {
272 conn
->header_read
+= n
;
273 if (conn
->header_read
== sizeof(conn
->header
)) {
274 if (conn
->header
.size
== 0) {
275 udscs_read_complete(connp
);
279 conn
->data
.size
= conn
->header
.size
;
280 conn
->data
.buf
= malloc(conn
->data
.size
);
281 if (!conn
->data
.buf
) {
282 syslog(LOG_ERR
, "out of memory, disconnecting %p", conn
);
283 udscs_destroy_connection(connp
);
289 if (conn
->data
.pos
== conn
->data
.size
)
290 udscs_read_complete(connp
);
294 /* A helper for udscs_client_handle_fds() */
295 static void udscs_do_write(struct udscs_connection
**connp
)
299 struct udscs_connection
*conn
= *connp
;
301 struct udscs_buf
* wbuf
= conn
->write_buf
;
304 "%p do_write called on a connection without a write buf ?!",
309 to_write
= wbuf
->size
- wbuf
->pos
;
310 n
= write(conn
->fd
, wbuf
->buf
+ wbuf
->pos
, to_write
);
314 syslog(LOG_ERR
, "writing to unix domain socket: %m, disconnecting %p",
316 udscs_destroy_connection(connp
);
321 if (wbuf
->pos
== wbuf
->size
) {
322 conn
->write_buf
= wbuf
->next
;
328 void udscs_client_handle_fds(struct udscs_connection
**connp
, fd_set
*readfds
,
334 if (FD_ISSET((*connp
)->fd
, readfds
))
335 udscs_do_read(connp
);
337 if (*connp
&& FD_ISSET((*connp
)->fd
, writefds
))
338 udscs_do_write(connp
);
341 int udscs_client_fill_fds(struct udscs_connection
*conn
, fd_set
*readfds
,
347 FD_SET(conn
->fd
, readfds
);
349 FD_SET(conn
->fd
, writefds
);
355 #ifndef UDSCS_NO_SERVER
357 /* ---------- Server-side implementation ---------- */
359 struct udscs_server
{
361 const char * const *type_to_string
;
364 struct udscs_connection connections_head
;
365 udscs_connect_callback connect_callback
;
366 udscs_read_callback read_callback
;
367 udscs_disconnect_callback disconnect_callback
;
370 struct udscs_server
*udscs_create_server(const char *socketname
,
371 udscs_connect_callback connect_callback
,
372 udscs_read_callback read_callback
,
373 udscs_disconnect_callback disconnect_callback
,
374 const char * const type_to_string
[], int no_types
, int debug
)
377 struct sockaddr_un address
;
378 struct udscs_server
*server
;
380 server
= calloc(1, sizeof(*server
));
384 server
->type_to_string
= type_to_string
;
385 server
->no_types
= no_types
;
386 server
->debug
= debug
;
388 server
->fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
389 if (server
->fd
== -1) {
390 syslog(LOG_ERR
, "creating unix domain socket: %m");
395 address
.sun_family
= AF_UNIX
;
396 snprintf(address
.sun_path
, sizeof(address
.sun_path
), "%s", socketname
);
397 c
= bind(server
->fd
, (struct sockaddr
*)&address
, sizeof(address
));
399 syslog(LOG_ERR
, "bind %s: %m", socketname
);
404 c
= listen(server
->fd
, 5);
406 syslog(LOG_ERR
, "listen: %m");
411 server
->connect_callback
= connect_callback
;
412 server
->read_callback
= read_callback
;
413 server
->disconnect_callback
= disconnect_callback
;
418 void udscs_destroy_server(struct udscs_server
*server
)
420 struct udscs_connection
*conn
, *next_conn
;
425 conn
= server
->connections_head
.next
;
427 next_conn
= conn
->next
;
428 udscs_destroy_connection(&conn
);
435 struct ucred
udscs_get_peer_cred(struct udscs_connection
*conn
)
437 return conn
->peer_cred
;
440 static void udscs_server_accept(struct udscs_server
*server
) {
441 struct udscs_connection
*new_conn
, *conn
;
442 struct sockaddr_un address
;
443 socklen_t length
= sizeof(address
);
446 fd
= accept(server
->fd
, (struct sockaddr
*)&address
, &length
);
450 syslog(LOG_ERR
, "accept: %m");
454 new_conn
= calloc(1, sizeof(*conn
));
456 syslog(LOG_ERR
, "out of memory, disconnecting new client");
462 new_conn
->type_to_string
= server
->type_to_string
;
463 new_conn
->no_types
= server
->no_types
;
464 new_conn
->debug
= server
->debug
;
465 new_conn
->read_callback
= server
->read_callback
;
466 new_conn
->disconnect_callback
= server
->disconnect_callback
;
468 length
= sizeof(new_conn
->peer_cred
);
469 r
= getsockopt(fd
, SOL_SOCKET
, SO_PEERCRED
, &new_conn
->peer_cred
, &length
);
471 syslog(LOG_ERR
, "Could not get peercred, disconnecting new client");
477 conn
= &server
->connections_head
;
481 new_conn
->prev
= conn
;
482 conn
->next
= new_conn
;
485 syslog(LOG_DEBUG
, "new client accepted: %p, pid: %d",
486 new_conn
, (int)new_conn
->peer_cred
.pid
);
488 if (server
->connect_callback
)
489 server
->connect_callback(new_conn
);
492 int udscs_server_fill_fds(struct udscs_server
*server
, fd_set
*readfds
,
495 struct udscs_connection
*conn
;
496 int nfds
= server
->fd
+ 1;
501 FD_SET(server
->fd
, readfds
);
503 conn
= server
->connections_head
.next
;
505 int conn_nfds
= udscs_client_fill_fds(conn
, readfds
, writefds
);
506 if (conn_nfds
> nfds
)
515 void udscs_server_handle_fds(struct udscs_server
*server
, fd_set
*readfds
,
518 struct udscs_connection
*conn
, *next_conn
;
523 if (FD_ISSET(server
->fd
, readfds
))
524 udscs_server_accept(server
);
526 conn
= server
->connections_head
.next
;
528 /* conn maybe destroyed by udscs_client_handle_fds (when disconnected),
529 so get the next connection first. */
530 next_conn
= conn
->next
;
531 udscs_client_handle_fds(&conn
, readfds
, writefds
);
536 int udscs_server_write_all(struct udscs_server
*server
,
537 uint32_t type
, uint32_t arg1
, uint32_t arg2
,
538 const uint8_t *data
, uint32_t size
)
540 struct udscs_connection
*conn
;
542 conn
= server
->connections_head
.next
;
544 if (udscs_write(conn
, type
, arg1
, arg2
, data
, size
))
552 int udscs_server_for_all_clients(struct udscs_server
*server
,
553 udscs_for_all_clients_callback func
, void *priv
)
556 struct udscs_connection
*conn
, *next_conn
;
561 conn
= server
->connections_head
.next
;
563 /* Get next conn as func may destroy the current conn */
564 next_conn
= conn
->next
;
565 r
+= func(&conn
, priv
);