forked from TelegramMessenger/MTProxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpid.c
87 lines (72 loc) · 2.24 KB
/
pid.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
/*
This file is part of Mtproto-proxy Library.
Mtproto-proxy Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
Mtproto-proxy Library 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with Mtproto-proxy Library. If not, see <http://www.gnu.org/licenses/>.
Copyright 2011-2013 Vkontakte Ltd
2011-2013 Nikolai Durov
2011-2013 Andrey Lopatin
Copyright 2014 Telegram Messenger Inc
2014 Vitaly Valtman
*/
#define _FILE_OFFSET_BITS 64
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <sys/types.h>
#include "common/pid.h"
npid_t PID;
void init_common_PID (void) {
if (!PID.pid) {
int p = getpid ();
assert (!(p & 0x80000000));
PID.pid = p;
}
if (!PID.utime) {
PID.utime = time (0);
}
}
void init_client_PID (unsigned ip) {
if (ip && ip != 0x7f000001) {
PID.ip = ip;
}
// PID.port = 0;
init_common_PID ();
};
void init_server_PID (unsigned ip, int port) {
if (ip && ip != 0x7f000001) {
PID.ip = ip;
}
if (!PID.port) {
PID.port = port;
}
init_common_PID ();
};
/* returns 1 if X is a special case of Y, 2 if they match completely */
int matches_pid (npid_t *X, npid_t *Y) {
if (!memcmp (X, Y, sizeof (struct process_id))) {
return 2;
} else if ((!Y->ip || X->ip == Y->ip) && (!Y->port || X->port == Y->port) && (!Y->pid || X->pid == Y->pid) && (!Y->utime || X->utime == Y->utime)) {
return 1;
} else {
return 0;
}
}
int process_id_is_newer (struct process_id *a, struct process_id *b) {
assert (!memcmp (a, b, 6));
if (a->utime < b->utime) { return 0; }
if (a->utime > b->utime) { return 1; }
int x = (a->pid - b->pid) & 0x7fff;
if (x && x <= 0x3fff) { return 1; }
return 0;
}