summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonas Suhr Christensen <jsc@umbraculum.org>2014-02-12 15:48:20 +0100
committerJonas Suhr Christensen <jsc@umbraculum.org>2014-02-12 15:48:20 +0100
commit0b293f1f2c1ac8823efba78c3ce62102db9bab59 (patch)
treedcda52cfd6319c13c99e1ef36c1439c686d97671
parent708c973d837c8ddfd7e56c0c118b0a606363f731 (diff)
DNS lookup before trying to connect to host.
Cropping of to long messages.
-rw-r--r--hugin_syslog.c90
-rw-r--r--main_syslog.c21
2 files changed, 55 insertions, 56 deletions
diff --git a/hugin_syslog.c b/hugin_syslog.c
index b4e5ceb..5981395 100644
--- a/hugin_syslog.c
+++ b/hugin_syslog.c
@@ -57,13 +57,8 @@ typedef int socket_t;
#define DO_NOT_USE_REMOTE -2
-#define SYSLOG_MSGLEN 1024
-#define SYSLOG_PRILEN 5
-#define SYSLOG_TIMELEN 32
-#define SYSLOG_TAGLEN 32
-#define SYSLOG_CONTENTLEN SYSLOG_MSGLEN - SYSLOG_PRILEN - SYSLOG_TIMELEN - SYSLOG_TAGLEN
-//#define SYSLOG_CONTENTLEN SYSLOG_MSGLEN - SYSLOG_PRILEN - SYSLOG_TAGLEN - SYSLOG_HEADERLEN -1
-
+#define SYSLOG_MAXMSGLEN 1024
+#define SYSLOG_MAXTIMELEN 32
#define SYSLOG_LENOFEXECNAME 256
static int hug_syslog_sock;
@@ -94,21 +89,22 @@ void hug_syslog_init(const char* host, int port)
wsastartup();
#endif
+
+ struct hostent *hp = gethostbyname(host);
+ if(!hp) {
+ fprintf(stderr, "Failed to get host by name\n");
+ return;
+ }
+
// printf("Initializing syslog module remote %s:%d\n", host, port);
if ( (hug_syslog_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
fprintf(stderr, "Failed to create socket\n");
return;
}
+
memset(&hug_syslog_sockaddr, 0, sizeof(hug_syslog_sockaddr));
hug_syslog_sockaddr.sin_family = AF_INET;
// hug_syslog_sockaddr.sin_addr.s_addr = inet_addr(host);
- struct hostent *hp = gethostbyname(host);
-
- if(!hp) {
- fprintf(stderr, "Failed to get host by name\n");
- return;
- }
-
memcpy(&(hug_syslog_sockaddr.sin_addr),*(hp->h_addr_list),sizeof(struct in_addr));
hug_syslog_sockaddr.sin_port = htons(port);
@@ -122,7 +118,11 @@ void hug_syslog_init(const char* host, int port)
FILE* f = fopen("/proc/self/cmdline", "r");
if(f) {
char* s = NULL;
+
+ // This will not include commandline parameters
+ // as they are separated by \0
s = fgets(buf, SYSLOG_LENOFEXECNAME, f);
+
(void)s;
fclose(f);
}
@@ -242,30 +242,30 @@ void hug_syslog_output(char* msg)
const time_t rawtime = time(NULL);
struct tm time;
hug_localtime(&rawtime, &time);
- char buftime[SYSLOG_TIMELEN];
- strftime(buftime, SYSLOG_TIMELEN, "%b %e %H:%M:%S ", &time);
+ char buftime[SYSLOG_MAXTIMELEN+1];
+ strftime(buftime, SYSLOG_MAXTIMELEN, "%b %e %H:%M:%S", &time);
// Currently everything is mapped to local facility 4 as debug
- char bufpri[SYSLOG_PRILEN];
- strncpy(bufpri, "<167>", SYSLOG_PRILEN);
-
- char buftag[SYSLOG_TAGLEN];
- snprintf(buftag, SYSLOG_TAGLEN, "%s[%d]: ", execname, pid);
-
- char buf[SYSLOG_MSGLEN];
- memset(buf, 0, sizeof(buf));
- strncat(buf, bufpri, SYSLOG_PRILEN);
- strncat(buf, buftime, SYSLOG_TIMELEN);
- strncat(buf, buftag, SYSLOG_TAGLEN);
- strncat(buf, msg, SYSLOG_CONTENTLEN);
- strcat(buf, "\n");
-
-
-// printf("Sending to syslog: %s\n", buf);
- int buf_len = strlen(buf);
- if((sendto(hug_syslog_sock, buf, buf_len, 0, (struct sockaddr *) &hug_syslog_sockaddr,
- sizeof(hug_syslog_sockaddr))) != buf_len) {
- fprintf(stderr, "Failed to send message successfully: %s\n", strerror(errno));
+ int prio = 167;
+
+ char buf[SYSLOG_MAXMSGLEN+1];
+ buf[0] = '\0';
+ snprintf(buf, SYSLOG_MAXMSGLEN, "<%d>%s %s[%d]: ", prio, buftime, execname, pid);
+ int headerlen = strlen(buf);
+
+ int msglen = strlen(msg);
+ char* msgptr = msg;
+ while( (msgptr - msg) < msglen ) {
+ buf[headerlen] = '\0';
+ strncat(buf, msgptr, SYSLOG_MAXMSGLEN - headerlen);
+ int buflen = strlen(buf);
+
+ msgptr += buflen - headerlen;
+
+ if((sendto(hug_syslog_sock, buf, buflen, 0, (struct sockaddr *) &hug_syslog_sockaddr,
+ sizeof(hug_syslog_sockaddr))) != buflen) {
+ fprintf(stderr, "Failed to send message successfully: %s\n", strerror(errno));
+ }
}
}
@@ -282,21 +282,3 @@ void hug_syslog_close() {
}
#endif/*DISABLE_HUGIN*/
-
-#ifdef TEST_HUGIN_SYSLOG
-//Additional dependency files
-//deps:
-//Required cflags (autoconf vars may be used)
-//cflags:
-//Required link options (autoconf vars may be used)
-//libs:
-#include "test.h"
-
-TEST_BEGIN;
-
-// TODO: Put some testcode here (see test.h for usable macros).
-TEST_TRUE(false, "No tests yet!");
-
-TEST_END;
-
-#endif/*TEST_HUGIN_SYSLOG*/
diff --git a/main_syslog.c b/main_syslog.c
index 2ac99be..b187db8 100644
--- a/main_syslog.c
+++ b/main_syslog.c
@@ -33,9 +33,14 @@
#include <stdio.h>
+#define MULTMSG \
+"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean sed mauris id mauris commodo faucibus eu at dui. Duis lorem est, auctor sed tellus nec, aliquam blandit sapien. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris mattis dolor elit, in vulputate est mattis sed. Morbi vel ligula ut dui hendrerit congue id at dolor. Suspendisse vitae arcu aliquam, laoreet lectus a, faucibus libero. Nunc consectetur turpis a suscipit vulputate. Fusce quis libero id mi scelerisque ultricies. Nullam consectetur lectus non augue mollis consequat. Donec in ipsum imperdiet, blandit eros vitae, pretium erat. Suspendisse dolor neque, sodales a condimentum vitae, placerat vel lorem. Aliquam bibendum tempus metus, ac tincidunt ipsum placerat in. Phasellus blandit, ligula nec imperdiet eleifend, justo tortor volutpat lectus, sed lacinia lectus orci suscipit erat. Aliquam sollicitudin consequat justo, sit amet malesuada diam luctus quis. Donec imperdiet tortor nec egestas euismod. Etiam rhoncus pellentesque dolor, quis luctus felis mollis sit amet." \
+"Nullam auctor erat metus, eget venenatis leo sagittis ac. Suspendisse sit amet velit sodales, gravida felis nec, ornare felis. Aliquam erat volutpat. Morbi a vehicula nunc, non aliquam augue. Nunc quis ligula et nunc malesuada fringilla non at tortor. Nam pretium molestie sapien, eget ultrices nunc imperdiet vitae. Sed sed odio id diam scelerisque vestibulum in a neque. Maecenas aliquam placerat ornare. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Interdum et malesuada fames ac ante ipsum primis in faucibus. Proin accumsan sodales augue. Pellentesque sagittis tincidunt lorem, ac sagittis lorem posuere non. In tincidunt nisi lacinia nisi aliquam auctor et ut felis. In eget consequat leo. Donec at faucibus ligula." \
+"Aliquam non libero rutrum, euismod magna vitae, auctor tortor. Quisque accumsan fringilla neque. Nulla facilisi. Curabitur ante risus, interdum vitae mi in, suscipit commodo mi. Etiam in mi volutpat orci tincidunt posuere."
+
int main(int argc, char *argv[])
{
- hug_status_t status = hug_init(HUG_FLAG_OUTPUT_TO_SYSLOG,
+ hug_status_t status = hug_init(HUG_FLAG_OUTPUT_TO_SYSLOG,
HUG_OPTION_SYSLOG_HOST, "localhost",
HUG_OPTION_SYSLOG_PORT, 514,
HUG_OPTION_END);
@@ -50,8 +55,20 @@ int main(int argc, char *argv[])
DEBUG(example, "Or are we %d?", 42);
DEBUG(foo, "Or are we %d?", 42);
-
+
+ DEBUG(crop, "%s", MULTMSG);
+
hug_close();
+ status = hug_init(HUG_FLAG_OUTPUT_TO_SYSLOG,
+ HUG_OPTION_SYSLOG_HOST, "notahost",
+ HUG_OPTION_SYSLOG_PORT, 514,
+ HUG_OPTION_END);
+
+ if(status != HUG_STATUS_OK) {
+ printf("Error: %d\n", status);
+ return 1;
+ }
+
return 0;
}