summaryrefslogtreecommitdiffstats
path: root/cacheserver/utmpserver2.c
blob: 12c3a2998ccd14e6f62a83658fa88c75062935eb (plain) (blame)
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/* $Id$ */
#include <stdio.h>
#include <sys/time.h>

#include "bbs.h"

extern void utmplogin(int uid, int index, const int like[MAX_FRIEND], const int hate[MAX_REJECT]);
extern int genfriendlist(int uid, int index, ocfs_t *fs, int maxfs);
extern void utmplogoutall(void);
#ifdef FAKEDATA
FILE *fp;
#endif
#ifdef UTMPLOG
FILE *logfp;
#endif

clock_t begin_clock;
time_t begin_time;
int count_flooding, count_login;

#ifdef NOFLOODING
/* 0 ok, 1 delay action, 2 reject */
int action_frequently(int uid)
{
    int i;
    time_t now = time(NULL);
    time_t minute = now/60;
    time_t hour = minute/60;

    static time_t flood_base_minute;
    static time_t flood_base_hour;
    static struct {
    unsigned short lastlogin; // truncated time_t
    unsigned char minute_count;
    unsigned char hour_count;
    } flooding[MAX_USERS];

    if(minute!=flood_base_minute) {
    for(i=0; i<MAX_USERS; i++)
        flooding[i].minute_count=0;
    flood_base_minute=minute;
    }
    if(hour!=flood_base_hour) {
    for(i=0; i<MAX_USERS; i++)
        flooding[i].hour_count=0;
    flood_base_hour=hour;
    }

    if(abs(flooding[uid].lastlogin-(unsigned short)now)<=3 ||
        flooding[uid].minute_count>30 ||
        flooding[uid].hour_count>60) {
    count_flooding++;
    return 2;
    }

    flooding[uid].minute_count++;
    flooding[uid].hour_count++;
    flooding[uid].lastlogin=now;

    if(flooding[uid].minute_count>5 ||
        flooding[uid].hour_count>20) {
    count_flooding++;
    return 1;
    }
    return 0;
}
#endif /* NOFLOODING */

void syncutmp(int cfd) {
    int i;
    int like[MAX_FRIEND];
    int hate[MAX_REJECT];

#ifdef UTMPLOG
    int x=-1;
    if(logfp && ftell(logfp)> 500*(1<<20)) {
    fclose(logfp);
    logfp=NULL;
    }
    if(logfp) fwrite(&x, sizeof(x), 1, logfp);
#endif

    printf("logout all\n");
    utmplogoutall();
    fprintf(stderr,"sync begin\n");
    for(i=0; i<USHM_SIZE; i++) {
    int uid;
#ifdef FAKEDATA
    fread(&uid, sizeof(uid), 1, fp);
    if(uid==-2)
        break;
    fread(like, sizeof(like), 1, fp);
    fread(hate, sizeof(hate), 1, fp);
#else
    if( toread(cfd, &uid, sizeof(uid)) <= 0 ||
        toread(cfd, like, sizeof(like)) <= 0 ||
        toread(cfd, hate, sizeof(hate)) <= 0)
        break;
#endif
#ifdef UTMPLOG
    if(logfp) {
        fwrite(&uid, sizeof(uid), 1, logfp);
        fwrite(like, sizeof(like), 1, logfp);
        fwrite(hate, sizeof(hate), 1, logfp);
    }
#endif

    if(uid != 0)
        utmplogin(uid, i, like, hate);
    }
    if(i<USHM_SIZE) {
#ifdef UTMPLOG
    int x=-2;
    if(logfp) fwrite(&x, sizeof(x), 1, logfp);
#endif
    }

    fprintf(stderr,"sync end\n");
}

void processlogin(int cfd, int uid, int index)
{
    int like[MAX_FRIEND];
    int hate[MAX_REJECT];
    /*  logout €銝虫€ utmpserver , 航賣亙唬€鈭
       撌脩 logout 撣唾€€隞乩賢芸 MAX_FRIEND €閬憭銝€鈭 */
#define MAX_FS   (2 * MAX_FRIEND)
    int res;
    int nfs;
    ocfs_t fs[MAX_FS];

#ifdef FAKEDATA
    fread(like, sizeof(like), 1, fp);
    fread(hate, sizeof(hate), 1, fp);
#else
    if(toread(cfd, like, sizeof(like)) <= 0 ||
        toread(cfd, hate, sizeof(hate)) <= 0)
    return;
#endif
#ifdef UTMPLOG
    if(logfp) {
    int x=-3;
    fwrite(&x, sizeof(x), 1, logfp);
    fwrite(&uid, sizeof(uid), 1, logfp);
    fwrite(&index, sizeof(index), 1, logfp);
    fwrite(like, sizeof(like), 1, logfp);
    fwrite(hate, sizeof(hate), 1, logfp);
    }
#endif

    utmplogin(uid, index, like, hate);
    nfs=genfriendlist(uid, index, fs, MAX_FS);
#ifndef FAKEDATA
    res=0;
#ifdef NOFLOODING
    res=action_frequently(uid);
#endif
    towrite(cfd, &res, sizeof(res));
    towrite(cfd, &nfs, sizeof(nfs));
    towrite(cfd, fs, sizeof(ocfs_t) * nfs);
#endif
}

void showstat(void)
{
    clock_t now_clock=clock();
    time_t now_time=time(0);

    time_t used_time=now_time-begin_time;
    clock_t used_clock=now_clock-begin_clock;

    printf("%.24s : real %.0f cpu %.2f : %d login %d flood, %.2f login/sec, %.2f%% load.\n", 
        ctime(&now_time), (double)used_time, (double)used_clock/CLOCKS_PER_SEC, 
        count_login, count_flooding,
        (double)count_login/used_time, (double)used_clock/CLOCKS_PER_SEC/used_time*100);

    begin_time=now_time;
    begin_clock=now_clock;
    count_login=0;
    count_flooding=0;
}

int main(int argc, char *argv[])
{
    struct  sockaddr_in     clientaddr;
    int     ch, port = 5120, sfd, cfd, len;
    char   *iface_ip = NULL;
    int cmd;
    int uid,index;
    int fail;
    int firstsync=0;

#ifdef UTMPLOG
    logfp = fopen("utmp.log","a");
    if(logfp && ftell(logfp)> 500*(1<<20)) {
    fclose(logfp);
    logfp=NULL;
    }
#endif

    Signal(SIGPIPE, SIG_IGN);
    while( (ch = getopt(argc, argv, "p:i:h")) != -1 )
    switch( ch ){
    case 'p':
        port = atoi(optarg);
        break;
    case 'i':
        iface_ip = optarg;
        break;
    case 'h':
    default:
        fprintf(stderr, "usage: utmpserver [-i interface_ip] [-p port]\n");
        return 1;
    }

#ifdef FAKEDATA
    fp=fopen("utmp.data","rb");
#else
    if( (sfd = tobind(iface_ip, port)) < 0 )
    return 1;
#endif
    while(1) {
#ifdef FAKEDATA
    if(fread(&cmd, sizeof(cmd), 1, fp)==0) break;
#else
    len = sizeof(clientaddr);
    if( (cfd = accept(sfd, (struct sockaddr *)&clientaddr, &len)) < 0 ){
        if( errno != EINTR )
        sleep(1);
        continue;
    }
    toread(cfd, &cmd, sizeof(cmd));
#endif

    if(cmd==-1) {
        syncutmp(cfd);
#ifndef FAKEDATA
        close(cfd);
#endif
        firstsync=1;
        continue;
    }
    if(!firstsync) {
        // don't accept client before first sync, to prevent incorrect friend data
        close(cfd);
        continue;
    }

    fail=0;
#ifdef FAKEDATA
    fread(&uid, sizeof(uid), 1, fp);
    fread(&index, sizeof(index), 1, fp);
#else
    if(cmd==-2) {
        if(toread(cfd, &index, sizeof(index)) <= 0)
        fail=1;
        if(toread(cfd, &uid, sizeof(uid)) <= 0)
        fail=1;
    } else if(cmd>=0) {
        // old client
        fail=1;
    } else {
        printf("unknown cmd=%d\n",cmd);
    }
#endif
    if(index>=USHM_SIZE) {
        fprintf(stderr, "bad index=%d\n",index);
        fail=1;
    }

    if(fail) {
#ifndef FAKEDATA
        close(cfd);
#endif
        continue;
    }

    count_login++;
    processlogin(cfd, uid, index);
    if(count_login>=4000 || time(NULL)-begin_time>30*60)
        showstat();
#ifndef FAKEDATA
    close(cfd);
#endif
    }
#ifdef FAKEDATA
    fclose(fp);
#endif
    return 0;
}