Skip to content

Commit 65e961c

Browse files
author
someguy
committed
Use an enumeration for TCP/UDP protocol instead of SOCK_STREAM/SOCK_DGRAM directly. Use a string table instead of direct string comparisons.
1 parent 0368271 commit 65e961c

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

src/netlog/netlog-manager.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "fd-util.h"
1313
#include "fileio.h"
1414
#include "string-util.h"
15+
#include "string-table.h"
1516
#include "parse-util.h"
1617
#include "netlog-manager.h"
1718

@@ -26,6 +27,13 @@
2627
#define JOURNAL_FOREACH_DATA_RETVAL(j, data, l, retval) \
2728
for (sd_journal_restart_data(j); ((retval) = sd_journal_enumerate_data((j), &(data), &(l))) > 0; )
2829

30+
static const char *const protocol_table[_SYSLOG_TRANSMISSION_PROTOCOL_MAX] = {
31+
[SYSLOG_TRANSMISSION_PROTOCOL_UDP] = "udp",
32+
[SYSLOG_TRANSMISSION_PROTOCOL_TCP] = "tcp",
33+
};
34+
35+
DEFINE_STRING_TABLE_LOOKUP(protocol, int);
36+
2937
static int parse_field(const void *data, size_t length, const char *field, char **target) {
3038
size_t fl, nl;
3139
void *buf;

src/netlog/netlog-manager.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@
77
#include "sd-network.h"
88
#include "socket-util.h"
99

10+
typedef enum SysLogTransmissionProtocol {
11+
SYSLOG_TRANSMISSION_PROTOCOL_UDP = 1 << 0,
12+
SYSLOG_TRANSMISSION_PROTOCOL_TCP = 1 << 1,
13+
_SYSLOG_TRANSMISSION_PROTOCOL_MAX,
14+
_SYSLOG_TRANSMISSION_PROTOCOL_INVALID = -EINVAL,
15+
} SysLogTransmissionProtocol;
16+
1017
typedef struct Manager Manager;
1118

1219
struct Manager {
@@ -33,7 +40,7 @@ struct Manager {
3340

3441
char *last_cursor, *current_cursor;
3542
char *structured_data;
36-
int protocol;
43+
SysLogTransmissionProtocol protocol;
3744
};
3845

3946
int manager_new(Manager **ret, const char *state_file, const char *cursor);
@@ -51,3 +58,6 @@ int manager_push_to_network(Manager *m, int severity, int facility,
5158
const char *identifier, const char *message,
5259
const char *hostname, const char *pid,
5360
const struct timeval *tv);
61+
62+
const char *protocol_to_string(int v) _const_;
63+
int protocol_from_string(const char *s) _pure_;

src/netlog/netlog-network.c

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,14 @@ int manager_push_to_network(Manager *m,
153153
/* Ninth: message */
154154
IOVEC_SET_STRING(iov[n++], message);
155155

156-
/* Tenth: Newline message separator, if not implicitly terminated by end of UDP frame */
157-
if (m->protocol == SOCK_DGRAM)
156+
/* Tenth: Optional newline message separator, if not implicitly terminated by end of UDP frame */
157+
if (SYSLOG_TRANSMISSION_PROTOCOL_TCP == m->protocol)
158+
/* De facto standard: separate messages by a newline */
158159
IOVEC_SET_STRING(iov[n++], "\n");
160+
else if (SYSLOG_TRANSMISSION_PROTOCOL_UDP == m->protocol) {
161+
/* Message is implicitly terminated by end of UDP packet */
162+
} else
163+
return -EPROTONOSUPPORT;
159164

160165
return network_send(m, iov, n);
161166
}
@@ -175,7 +180,16 @@ int manager_open_network_socket(Manager *m) {
175180
if (!IN_SET(m->address.sockaddr.sa.sa_family, AF_INET, AF_INET6))
176181
return -EAFNOSUPPORT;
177182

178-
m->socket = socket(m->address.sockaddr.sa.sa_family, m->protocol|SOCK_CLOEXEC, 0);
183+
switch (m->protocol) {
184+
case SYSLOG_TRANSMISSION_PROTOCOL_UDP:
185+
m->socket = socket(m->address.sockaddr.sa.sa_family, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
186+
break;
187+
case SYSLOG_TRANSMISSION_PROTOCOL_TCP:
188+
m->socket = socket(m->address.sockaddr.sa.sa_family, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
189+
break;
190+
default:
191+
return -EINVAL;
192+
}
179193
if (m->socket < 0)
180194
return -errno;
181195

@@ -185,7 +199,7 @@ int manager_open_network_socket(Manager *m) {
185199
goto fail;
186200
}
187201

188-
if (SOCK_STREAM == m->protocol) {
202+
if (SYSLOG_TRANSMISSION_PROTOCOL_TCP == m->protocol) {
189203
union sockaddr_union sa;
190204
socklen_t salen;
191205
switch (m->address.sockaddr.sa.sa_family) {

0 commit comments

Comments
 (0)