Debian Codegraph @

src/dbus/socket.h

raw
1 #pragma once
2
3 /*
4 * D-Bus Socket Abstraction
5 */
6
7 #include <c-list.h>
8 #include <c-stdaux.h>
9 #include <stdlib.h>
10 #include "dbus/message.h"
11 #include "dbus/queue.h"
12 #include "util/user.h"
13
14 typedef struct FDList FDList;
15 typedef struct Socket Socket;
16 typedef struct SocketBuffer SocketBuffer;
17
18 #define SOCKET_LINE_PREALLOC (64UL) /* fits the longest sane SASL exchange */
19 #define SOCKET_FD_MAX (253UL) /* taken from kernel SCM_MAX_FD */
20 #define SOCKET_MMSG_MAX (16) /* randomly picked, no tuning done so far */
21
22 enum {
23 _SOCKET_E_SUCCESS,
24
25 /* I/O handling */
26 SOCKET_E_LOST_INTEREST,
27 SOCKET_E_PREEMPTED,
28
29 /* socket errors */
30 SOCKET_E_EOF,
31 SOCKET_E_QUOTA,
32 SOCKET_E_SHUTDOWN,
33 };
34
35 /* socket IO */
36
37 struct Socket {
38 User *user;
39 int fd;
40
41 bool shutdown : 1;
42 bool reset : 1;
43 bool hup_in : 1;
44 bool hup_out : 1;
45
46 struct {
47 IQueue queue;
48 MessageHeader header;
49 Message *message;
50 } in;
51
52 struct SocketOut {
53 CList queue;
54 CList pending;
55 } out;
56 };
57
58 #define SOCKET_NULL(_x) { \
59 .fd = -1, \
60 .in.queue = IQUEUE_NULL((_x).in.queue), \
61 .out.queue = C_LIST_INIT((_x).out.queue), \
62 .out.pending = C_LIST_INIT((_x).out.pending), \
63 }
64
65 void socket_init(Socket *socket, User *user, int fd);
66 void socket_deinit(Socket *socket);
67
68 int socket_dequeue_line(Socket *socket, const char **linep, size_t *np);
69 int socket_dequeue(Socket *socket, Message **messagep);
70
71 int socket_queue_line(Socket *socket, User *user, const char *line, size_t n);
72 int socket_queue(Socket *socket, User *user, Message *message);
73
74 int socket_dispatch(Socket *socket, uint32_t event);
75 void socket_shutdown(Socket *socket);
76 void socket_close(Socket *socket);
77 void socket_get_stats(Socket *socket,
78 unsigned int *n_in_bytesp,
79 unsigned int *n_in_fdsp,
80 unsigned int *n_out_bytesp,
81 unsigned int *n_out_fdsp);
82
83 C_DEFINE_CLEANUP(Socket *, socket_deinit);
84
85 /* inline helpers */
86
87 static inline bool socket_is_running(Socket *socket) {
88 return !socket->reset;
89 }