1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
#include "journal_commit.h"
#include <config.h>
#include "debug.h"
#include <string>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <unistd.h>
#include <endian.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdarg.h>
#include <fcntl.h>
#include <time.h>
#include <errno.h>
#include "template.h"
#include "templateparser.h"
static int mwrite(int sock, const char *fmt, ...)
{
int l = 0;
va_list args;
char *buffer;
buffer = (char*)calloc(64*1024, 1);
va_start(args, fmt);
l = vsnprintf(buffer, 64*1024, fmt, args);
va_end(args);
if(sock != -1 && write(sock, buffer, l) != l) {
ERR_LOG(journal, "write did not write all the bytes in the buffer.\n");
}
DEBUG(journal, "%s", buffer);
free(buffer);
return l;
}
int journal_commit(const char *cpr, const char *user,
const char *addr, unsigned short int port,
const char *buf, size_t size)
{
int sock = -1;
#ifndef WITHOUT_UPLOADSERVER
struct sockaddr_in sin;
char *ip;
struct in_addr **addr_list;
struct hostent *he;
he = gethostbyname(addr);
if(!he || !he->h_length) {
ERR_LOG(journal, "gethostbyname(%s) failed (errno=%d)!\n", addr, errno);
return -1;
}
addr_list = (struct in_addr **)he->h_addr_list;
ip = inet_ntoa(*addr_list[0]);
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = inet_addr(ip);
sin.sin_port = htons(port);
if( (sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
ERR_LOG(journal, "Socket() failed!\n");
return -1;
}
if(connect(sock, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
ERR_LOG(journal, "Connect() failed!\n");
perror(":");
return -1;
}
#else
sock = open("/tmp/pracro_journal.log", O_CREAT | O_WRONLY | O_TRUNC);
#endif
mwrite(sock, "PUT JOURNAL PROTO1.0 \r\n");
mwrite(sock, "size : %i\r\n", size);
mwrite(sock, "user: %s\r\n", user);
mwrite(sock, "generator: pracro\r\n");
mwrite(sock, "cpr: %s\r\n", cpr);
mwrite(sock, "date: %i\r\n", time(NULL));
mwrite(sock, "charset: utf8\r\n");
mwrite(sock, "\r\n");
std::string resume = buf;
if(sock != -1 && write(sock, resume.c_str(), resume.size()) != (ssize_t)resume.size()) {
ERR_LOG(journal, "write did not write all the bytes in the buffer.\n");
}
DEBUG(journal, "%s\n", buf);
#ifndef WITHOUT_UPLOADSERVER
close(sock);
#endif
return 0;
}
#ifdef TEST_JOURNAL_COMMIT
int main()
{
return 0;
}
#endif
|