Skip to content

Commit b49b271

Browse files
authored
Merge pull request syslog-ng#4964 from MrAnno/python-srcip
python: add `LogMessage::set_source_ipaddress()`
2 parents 0f763e1 + fc92734 commit b49b271

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

modules/python/python-logmsg.c

+24
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "msg-format.h"
3636
#include "scratch-buffers.h"
3737
#include "cfg.h"
38+
#include "gsockaddr.h"
3839

3940
#include <datetime.h>
4041

@@ -352,6 +353,28 @@ py_log_message_get_pri(PyLogMessage *self, PyObject *args, PyObject *kwrds)
352353
return PyLong_FromLong(self->msg->pri);
353354
}
354355

356+
static PyObject *
357+
py_log_message_set_source_ipaddress(PyLogMessage *self, PyObject *args, PyObject *kwrds)
358+
{
359+
const gchar *ip;
360+
Py_ssize_t ip_length;
361+
guint port = 0;
362+
363+
static const gchar *kwlist[] = {"ip", "port", NULL};
364+
if (!PyArg_ParseTupleAndKeywords(args, kwrds, "z#|I", (gchar **) kwlist, &ip, &ip_length, &port))
365+
return NULL;
366+
367+
if (!ip)
368+
Py_RETURN_FALSE;
369+
370+
GSockAddr *saddr = g_sockaddr_inet_or_inet6_new(ip, (guint16) port);
371+
if (!saddr)
372+
Py_RETURN_FALSE;
373+
374+
log_msg_set_saddr(self->msg, saddr);
375+
Py_RETURN_TRUE;
376+
}
377+
355378
static PyObject *
356379
py_log_message_set_timestamp(PyLogMessage *self, PyObject *args, PyObject *kwrds)
357380
{
@@ -442,6 +465,7 @@ static PyMethodDef py_log_message_methods[] =
442465
{ "get_as_str", (PyCFunction)py_log_message_get_as_str, METH_VARARGS | METH_KEYWORDS, "Get value as string" },
443466
{ "set_pri", (PyCFunction)py_log_message_set_pri, METH_VARARGS | METH_KEYWORDS, "Set syslog priority" },
444467
{ "get_pri", (PyCFunction)py_log_message_get_pri, METH_VARARGS | METH_KEYWORDS, "Get syslog priority" },
468+
{ "set_source_ipaddress", (PyCFunction)py_log_message_set_source_ipaddress, METH_VARARGS | METH_KEYWORDS, "Set source address" },
445469
{ "set_timestamp", (PyCFunction)py_log_message_set_timestamp, METH_VARARGS | METH_KEYWORDS, "Set timestamp" },
446470
{ "get_timestamp", (PyCFunction)py_log_message_get_timestamp, METH_VARARGS | METH_KEYWORDS, "Get timestamp" },
447471
{ "set_bookmark", (PyCFunction)py_log_message_set_bookmark, METH_VARARGS | METH_KEYWORDS, "Set bookmark" },

0 commit comments

Comments
 (0)