-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
178 lines (163 loc) · 3.53 KB
/
main.cpp
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <iostream>
#include <sstream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <unistd.h>
using namespace std;
// make && echo "hello world" 2>&1 | ./bin/dogcat 127.0.0.1 8124 -d -o "#domain:live,service:hello" -
int main(int argc, char *argv[])
{
if (argc < 3)
{
cout << "USAGE: dogcat HOST PORT OPTIONS... FILES..." << endl;
cout << "EXAMPLE: echo \"hello world\" 2>&1 | ./dogcat 127.0.0.1 8124 -d -o \"#domain:live,service:hello\" -" << endl;
return EXIT_FAILURE;
}
const char *const hostArg = argv[1];
const char *const portArg = argv[2];
int sock;
if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
{
cerr << "UDP socket failed" << endl;
return EXIT_FAILURE;
}
sockaddr_in sockAddr = { 0 };
sockAddr.sin_family = AF_INET;
sockAddr.sin_port = htons(atoi(portArg));
if (inet_aton(hostArg, &sockAddr.sin_addr) == 0)
{
cerr << "Host address invalid" << endl;
return EXIT_FAILURE;
}
const char *title = "";
bool wantTimestampAdded = false;
const char *optionalOptions = "";
int i;
for (i = 3; i < argc; ++i)
{
if (strcmp(argv[i], "-d") == 0)
{
wantTimestampAdded = true;
}
else if ((strcmp(argv[i], "-o") == 0) && (i + 1) < argc)
{
++i;
optionalOptions = argv[i];
}
else if ((strcmp(argv[i], "-t") == 0) && (i + 1) < argc)
{
++i;
title = argv[i];
}
else
{
break;
}
}
int titleLength = strlen(title);
string messageStart;
; {
stringstream ss;
ss << "_e{" << titleLength << ",";
messageStart = ss.str();
}
for (; i < argc; ++i)
{
const char *const fileName = argv[i];
int fd;
if (strcmp(fileName, "-") == 0) fd = 0;
else fd = open(fileName, O_RDONLY);
if (fd < 0)
{
cerr << "Cannot open " << fileName << endl;
continue;
}
char buf[256];
buf[255] = '\x00';
int bufFill = 0;
int bufAdd;
int bufLen;
for (;;)
{
bufAdd = read(fd, &buf[bufFill], 255 - bufFill);
if (bufAdd == 0)
{
bufAdd = -1;
}
bool endOfFile = bufAdd < 0;
for (;;)
{
bool marker = false;
if (bufAdd > 0)
{
bufLen = bufFill + bufAdd;
for (int i = bufFill; i < bufLen; ++i)
{
if (buf[i] < ' ' || buf[i] == '\r' || buf[i] == '\n' /* || buf[i] == 'l' */)
{
bufLen = i + 1;
marker = true;
break;
}
}
bufFill += bufAdd;
// buf[bufFill] = 0; // TEST
}
else
{
marker = true;
bufLen = bufFill + 1;
bufFill = 0;
}
if (marker && (bufLen > 1) || bufLen >= 255)
{
stringstream ss;
ss << messageStart << (bufLen - 1) << "}:" << title;
ss << "|";
ss.write(buf, bufLen - 1);
if (wantTimestampAdded)
ss << "|d:" << time(NULL);
if (optionalOptions[0])
ss << "|" << optionalOptions;
ss << "\n";
string s = ss.str();
sendto(sock, s.c_str(), s.size(), 0 , (sockaddr *)&sockAddr, sizeof(sockaddr_in));
// cout << s.c_str();
}
if (bufLen >= 255)
{
bufFill = 0;
break;
}
else if (bufFill && bufLen && (bufLen != bufFill))
{
bufAdd = bufFill - bufLen;
bufFill = 0;
// cout << "buf STR >>>" << buf << "<<<" << endl;
memcpy(buf, &buf[bufLen], bufAdd /* + 1 */);
// cout << "buf END >>>" << buf << "<<<" << endl;
}
else
{
// cout << "buf RESET" << endl;
if (marker)
{
bufFill = 0;
}
break;
}
}
if (bufAdd < 0)
{
break;
}
}
close(fd);
}
return EXIT_SUCCESS;
}
/* end of file */