-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvlogsrv.c
161 lines (137 loc) · 3.4 KB
/
vlogsrv.c
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
/*
* Simple vlogger server
*
* Copyright (C) 2002-2003 RD <rd@thc.org>
*
* Please check http://www.thc.org for update
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* See NOTICE for additional copyright notices.
*/
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sched.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "vlogger.h"
#define BUFFER_LEN 8192
char *logdir = NULL;
void
print_usage(void)
{
fprintf(stdout,
"Usage: vlogsrv [-w logdir] [-h] port\n"
"\n"
" -w logdir Write the log data to files in logdir directory\n"
" instead of display the results on standard output\n"
" -h Print this help screen\n\n");
}
#define MAX_FNAME 255
void
logging(struct sockaddr_in *addr, int offset, const char *ttyname,
const char *data)
{
if (logdir == NULL) {
printf("%s:%s:%s%s", inet_ntoa(addr->sin_addr), ttyname,
data, data[strlen(data)-1]==10?"":"\n");
fflush(stdout);
} else {
char fname[MAX_FNAME];
FILE *f;
snprintf(fname, MAX_FNAME - 1, "%s/%s", logdir,
inet_ntoa(addr->sin_addr));
f = fopen(fname, "a");
if (f == NULL) {
printf("Unable to open %s\n", fname);
return;
}
fprintf(f, "%s* %s", ttyname, data);
fclose(f);
}
}
int
main(int argc, char **argv)
{
struct sockaddr_in addr, caddr;
unsigned char buf[BUFFER_LEN];
int udp_socket, len, port, c;
memset(&addr, 0, sizeof(addr));
memset(&caddr, 0, sizeof(caddr));
while ((c = getopt(argc, argv, "w:h")) != EOF) {
switch (c) {
case 'w':
logdir = strdup(optarg);
break;
case 'c':
case 'h':
default:
print_usage();
exit(EXIT_SUCCESS);
}
}
if (optind < argc)
port = atol(argv[optind++]);
else {
print_usage();
exit(EXIT_SUCCESS);
}
if (optind < argc) {
print_usage();
exit(EXIT_FAILURE);
}
printf("Listening UDP port %d\n", port);
udp_socket = socket(PF_INET, SOCK_DGRAM, 0);
caddr.sin_family = AF_INET;
caddr.sin_port = htons(port);
caddr.sin_addr.s_addr = INADDR_ANY;
if (bind(udp_socket, (struct sockaddr *) &caddr, sizeof(caddr))) {
perror("Could not bind UDP socket!\n");
exit(-1);
}
while (1) {
char *ttyname, *data;
unsigned int offset, fromlen = sizeof(addr);
len = recvfrom(udp_socket, buf, BUFFER_LEN, 0,
(struct sockaddr *) &addr, &fromlen);
if (len <= 0) {
perror("UDP recv error!\n");
exit(-1);
}
buf[len] = 0;
if (buf[0] != SEND_CMD) {
printf("Wrong command %d\n", buf[0]);
continue;
}
offset = buf[1];
offset = (offset << 8) | buf[2];
offset = (offset << 8) | buf[3];
offset = (offset << 8) | buf[4];
ttyname = buf + OFFSET_LEN + 1;
data = buf + HEADER_LEN;
logging(&addr, offset, ttyname, data);
}
return 0;
}