-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudprx.c
149 lines (120 loc) · 2.88 KB
/
udprx.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
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <assert.h>
#include <libdaemon/dlog.h>
#define BUFLEN 2048
#define PORT 8888
static int logfd=-1;
static int lasthour=-1;
//create path
int mkpath(char* file_path, mode_t mode) {
assert(file_path && *file_path);
char* p;
for (p=strchr(file_path+1, '/'); p; p=strchr(p+1, '/')) {
*p='\0';
if (mkdir(file_path, mode)==-1) {
if (errno!=EEXIST) { *p='/'; return -1; }
}
*p='/';
}
return 0;
}
static void openlogfile()
{
const char *dir="/var/log/udprx/raw/";
char timestr[80];
char filename[80];
int ret;
time_t t;
struct tm tmp;
//filename is YYYY:DD:MM:HH
t = time(NULL);
gmtime_r(&t, &tmp);
strftime(timestr, 80, "%Y/%m/%F-%H", &tmp);
//combine directory and filename
snprintf(filename, 80, "%s%s", dir, timestr);
//create directory
ret=mkpath(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if(ret<0) daemon_log(LOG_ERR, "mkpath failed: %s", strerror(errno));
//open file
logfd=open(filename,
O_CREAT | O_APPEND | O_WRONLY,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if(logfd<0) daemon_log(LOG_ERR, "open failed: %s", strerror(errno));
}
static void logmessage(char *buf, ssize_t buflen)
{
int i;
time_t t;
struct tm tmp;
char timestr[80];
char *datastr = malloc((buflen*2+1));
char logstr[2048];
ssize_t written;
//create time string
t = time(NULL);
gmtime_r(&t, &tmp);
strftime(timestr, 80, "%a, %d %b %Y %T %z", &tmp);
//create string representation of data bytes
for(i=0; i<buflen; i++) {
sprintf(datastr+(i*2), "%02x", buf[i] & 0xff);
}
//combine time and data strings
snprintf(logstr, 2048, "%s %s\n", timestr, datastr);
//check if hour has rolled
if(lasthour!=tmp.tm_hour) {
if(logfd > 0) {
close(logfd);
logfd=-1;
}
}
//write to log file
if(logfd < 0)
openlogfile();
if(logfd > 0) {
written=write(logfd, logstr, strlen(logstr));
if(written<0) daemon_log(LOG_ERR, "write failed: %s", strerror(errno));
}
lasthour=tmp.tm_hour;
free(datastr);
}
int opensocket()
{
int s;
int ret;
struct sockaddr_in si_me;
s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(s<0)
return s;
memset((char *) &si_me, 0, sizeof(si_me));
si_me.sin_family = AF_INET;
si_me.sin_port = htons(PORT);
si_me.sin_addr.s_addr = htonl(INADDR_ANY);
ret=bind(s, (const struct sockaddr *)&si_me, sizeof(si_me));
if(ret<0)
return ret;
return s;
}
int receive(int s)
{
char buf[BUFLEN];
ssize_t nbytes;
nbytes=recvfrom(s, buf, BUFLEN, 0, NULL, NULL);
if(nbytes<0)
{
daemon_log(LOG_ERR, "recvfrom failed: %s", strerror(errno));
return 1;
}
logmessage(buf, nbytes);
return 0;
}