summaryrefslogtreecommitdiffstats
path: root/cacheserver/utmpserver.c
blob: 5826eb59d88c2421954b155568f895d2be4d5643 (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
/* $Id$ */
#include "bbs.h"
#include <err.h>

struct {
    int     uid;
    short   nFriends, nRejects;
    int     friend[MAX_FRIEND];
    int     reject[MAX_REJECT];
} utmp[MAX_ACTIVE];

inline void countarray(int *s, int max)
{
    int     i;
    for( i = 0 ; i < max && s[i] ; ++i )
    ;
    return i;
}

int main(int argc, char **argv)
{
    struct  sockaddr_in     clientaddr;
    int     ch, port = 5120, sfd, cfd, len, index, i, uid;

    while( (ch = getopt(argc, argv, "p:h")) != -1 )
    switch( ch ){
    case 'p':
        port = atoi(optarg);
        break;

    case 'h':
    default:
        fprintf(stderr, "usage: utmpserver [-p port]\n");
        return 1;
    }

    if( (sfd = tobind(port)) < 0 )
    return 1;

    while( 1 ){
    len = sizeof(clientaddr);
    if( (cfd = accept(sfd, (struct sockaddr *)&clientaddr, &len)) < 0 ){
        if( errno != EINTR )
        sleep(1);
        continue;
    }
    toread(cfd, &index, sizeof(index));
    if( index == -1 ){
        int     nSynced = 0;
        for( i = 0 ; i < MAX_ACTIVE ; ++i, ++nSynced )
        if( toread(cfd, &utmp[i].uid, sizeof(utmp[i].uid)) > 0      &&
            toread(cfd, utmp[i].friend, sizeof(utmp[i].friend)) > 0 &&
            toread(cfd, utmp[i].reject, sizeof(utmp[i].reject)) > 0 )
            ;
        else
            for( ; i < MAX_ACTIVE ; ++i )
            utmp[i].uid = 0;
        close(cfd);
        fprintf(stderr, "%d users synced\n", nSynced);
        continue;
    }

    if( toread(cfd, &uid, sizeof(uid)) > 0                              &&
        toread(cfd, utmp[index].friend, sizeof(utmp[index].friend)) > 0 &&
        toread(cfd, utmp[index].reject, sizeof(utmp[index].reject)) > 0 ){
        int     nFriends = 0, frarray[MAX_FRIEND];
        utmp[index].uid = uid;
        utmp[index].nFriends = countarray(utmp[index].friend, MAX_FRIEND);
        utmp[index].nRejects = countarray(utmp[index].reject, MAX_REJECT);

    }
    close(cfd);
    }
    return 0;
}