|
Line
Link Here
|
| 0 |
-- a/src/shared/iengine.h |
0 |
++ b/src/shared/iengine.h |
|
Lines 415-437
Link Here
|
| 415 |
extern int getservermtu(); |
415 |
extern int getservermtu(); |
| 416 |
extern int getnumclients(); |
416 |
extern int getnumclients(); |
| 417 |
extern uint getclientip(int n); |
417 |
extern uint getclientip(int n); |
| 418 |
extern void putint(ucharbuf &p, int n); |
|
|
| 419 |
extern void putint(packetbuf &p, int n); |
| 420 |
extern void putint(vector<uchar> &p, int n); |
| 421 |
extern int getint(ucharbuf &p); |
| 422 |
extern void putuint(ucharbuf &p, int n); |
| 423 |
extern void putuint(packetbuf &p, int n); |
| 424 |
extern void putuint(vector<uchar> &p, int n); |
| 425 |
extern int getuint(ucharbuf &p); |
| 426 |
extern void putfloat(ucharbuf &p, float f); |
| 427 |
extern void putfloat(packetbuf &p, float f); |
| 428 |
extern void putfloat(vector<uchar> &p, float f); |
| 429 |
extern float getfloat(ucharbuf &p); |
| 430 |
extern void sendstring(const char *t, ucharbuf &p); |
| 431 |
extern void sendstring(const char *t, packetbuf &p); |
| 432 |
extern void sendstring(const char *t, vector<uchar> &p); |
| 433 |
extern void getstring(char *t, ucharbuf &p, int len = MAXTRANS); |
| 434 |
extern void filtertext(char *dst, const char *src, bool whitespace = true, int len = sizeof(string)-1); |
| 435 |
extern void localconnect(); |
418 |
extern void localconnect(); |
| 436 |
extern const char *disconnectreason(int reason); |
419 |
extern const char *disconnectreason(int reason); |
| 437 |
extern void disconnect_client(int n, int reason); |
420 |
extern void disconnect_client(int n, int reason); |
| 438 |
-- a/src/engine/server.cpp |
421 |
++ b/src/engine/server.cpp |
|
Lines 99-223
Link Here
|
| 99 |
va_end(args); |
99 |
va_end(args); |
| 100 |
} |
100 |
} |
| 101 |
#endif |
101 |
#endif |
| 102 |
|
|
|
| 103 |
// all network traffic is in 32bit ints, which are then compressed using the following simple scheme (assumes that most values are small). |
| 104 |
|
| 105 |
template<class T> |
| 106 |
static inline void putint_(T &p, int n) |
| 107 |
{ |
| 108 |
if(n<128 && n>-127) p.put(n); |
| 109 |
else if(n<0x8000 && n>=-0x8000) { p.put(0x80); p.put(n); p.put(n>>8); } |
| 110 |
else { p.put(0x81); p.put(n); p.put(n>>8); p.put(n>>16); p.put(n>>24); } |
| 111 |
} |
| 112 |
void putint(ucharbuf &p, int n) { putint_(p, n); } |
| 113 |
void putint(packetbuf &p, int n) { putint_(p, n); } |
| 114 |
void putint(vector<uchar> &p, int n) { putint_(p, n); } |
| 115 |
|
| 116 |
int getint(ucharbuf &p) |
| 117 |
{ |
| 118 |
int c = (char)p.get(); |
| 119 |
if(c==-128) { int n = p.get(); n |= char(p.get())<<8; return n; } |
| 120 |
else if(c==-127) { int n = p.get(); n |= p.get()<<8; n |= p.get()<<16; return n|(p.get()<<24); } |
| 121 |
else return c; |
| 122 |
} |
| 123 |
|
| 124 |
// much smaller encoding for unsigned integers up to 28 bits, but can handle signed |
| 125 |
template<class T> |
| 126 |
static inline void putuint_(T &p, int n) |
| 127 |
{ |
| 128 |
if(n < 0 || n >= (1<<21)) |
| 129 |
{ |
| 130 |
p.put(0x80 | (n & 0x7F)); |
| 131 |
p.put(0x80 | ((n >> 7) & 0x7F)); |
| 132 |
p.put(0x80 | ((n >> 14) & 0x7F)); |
| 133 |
p.put(n >> 21); |
| 134 |
} |
| 135 |
else if(n < (1<<7)) p.put(n); |
| 136 |
else if(n < (1<<14)) |
| 137 |
{ |
| 138 |
p.put(0x80 | (n & 0x7F)); |
| 139 |
p.put(n >> 7); |
| 140 |
} |
| 141 |
else |
| 142 |
{ |
| 143 |
p.put(0x80 | (n & 0x7F)); |
| 144 |
p.put(0x80 | ((n >> 7) & 0x7F)); |
| 145 |
p.put(n >> 14); |
| 146 |
} |
| 147 |
} |
| 148 |
void putuint(ucharbuf &p, int n) { putuint_(p, n); } |
| 149 |
void putuint(packetbuf &p, int n) { putuint_(p, n); } |
| 150 |
void putuint(vector<uchar> &p, int n) { putuint_(p, n); } |
| 151 |
|
| 152 |
int getuint(ucharbuf &p) |
| 153 |
{ |
| 154 |
int n = p.get(); |
| 155 |
if(n & 0x80) |
| 156 |
{ |
| 157 |
n += (p.get() << 7) - 0x80; |
| 158 |
if(n & (1<<14)) n += (p.get() << 14) - (1<<14); |
| 159 |
if(n & (1<<21)) n += (p.get() << 21) - (1<<21); |
| 160 |
if(n & (1<<28)) n |= -1<<28; |
| 161 |
} |
| 162 |
return n; |
| 163 |
} |
| 164 |
|
| 165 |
template<class T> |
| 166 |
static inline void putfloat_(T &p, float f) |
| 167 |
{ |
| 168 |
lilswap(&f, 1); |
| 169 |
p.put((uchar *)&f, sizeof(float)); |
| 170 |
} |
| 171 |
void putfloat(ucharbuf &p, float f) { putfloat_(p, f); } |
| 172 |
void putfloat(packetbuf &p, float f) { putfloat_(p, f); } |
| 173 |
void putfloat(vector<uchar> &p, float f) { putfloat_(p, f); } |
| 174 |
|
| 175 |
float getfloat(ucharbuf &p) |
| 176 |
{ |
| 177 |
float f; |
| 178 |
p.get((uchar *)&f, sizeof(float)); |
| 179 |
return lilswap(f); |
| 180 |
} |
| 181 |
|
| 182 |
template<class T> |
| 183 |
static inline void sendstring_(const char *t, T &p) |
| 184 |
{ |
| 185 |
while(*t) putint(p, *t++); |
| 186 |
putint(p, 0); |
| 187 |
} |
| 188 |
void sendstring(const char *t, ucharbuf &p) { sendstring_(t, p); } |
| 189 |
void sendstring(const char *t, packetbuf &p) { sendstring_(t, p); } |
| 190 |
void sendstring(const char *t, vector<uchar> &p) { sendstring_(t, p); } |
| 191 |
|
| 192 |
void getstring(char *text, ucharbuf &p, int len) |
| 193 |
{ |
| 194 |
char *t = text; |
| 195 |
do |
| 196 |
{ |
| 197 |
if(t>=&text[len]) { text[len-1] = 0; return; } |
| 198 |
if(!p.remaining()) { *t = 0; return; } |
| 199 |
*t = getint(p); |
| 200 |
} |
| 201 |
while(*t++); |
| 202 |
} |
| 203 |
|
| 204 |
void filtertext(char *dst, const char *src, bool whitespace, int len) |
| 205 |
{ |
| 206 |
for(int c = uchar(*src); c; c = uchar(*++src)) |
| 207 |
{ |
| 208 |
if(c == '\f') |
| 209 |
{ |
| 210 |
if(!*++src) break; |
| 211 |
continue; |
| 212 |
} |
| 213 |
if(iscubeprint(c) || (iscubespace(c) && whitespace)) |
| 214 |
{ |
| 215 |
*dst++ = c; |
| 216 |
if(!--len) break; |
| 217 |
} |
| 218 |
} |
| 219 |
*dst = '\0'; |
| 220 |
} |
| 221 |
|
102 |
|
| 222 |
enum { ST_EMPTY, ST_LOCAL, ST_TCPIP }; |
103 |
enum { ST_EMPTY, ST_LOCAL, ST_TCPIP }; |
| 223 |
|
104 |
|
| 224 |
-- a/src/shared/tools.h |
105 |
++ b/src/shared/tools.h |
|
Lines 1178-1182
Link Here
|
| 1178 |
extern uint randomMT(); |
1178 |
extern uint randomMT(); |
| 1179 |
extern int guessnumcpus(); |
1179 |
extern int guessnumcpus(); |
| 1180 |
|
1180 |
|
| 1181 |
#endif |
1181 |
extern void putint(ucharbuf &p, int n); |
| 1182 |
|
1182 |
extern void putint(packetbuf &p, int n); |
|
|
1183 |
extern void putint(vector<uchar> &p, int n); |
| 1184 |
extern int getint(ucharbuf &p); |
| 1185 |
extern void putuint(ucharbuf &p, int n); |
| 1186 |
extern void putuint(packetbuf &p, int n); |
| 1187 |
extern void putuint(vector<uchar> &p, int n); |
| 1188 |
extern int getuint(ucharbuf &p); |
| 1189 |
extern void putfloat(ucharbuf &p, float f); |
| 1190 |
extern void putfloat(packetbuf &p, float f); |
| 1191 |
extern void putfloat(vector<uchar> &p, float f); |
| 1192 |
extern float getfloat(ucharbuf &p); |
| 1193 |
extern void sendstring(const char *t, ucharbuf &p); |
| 1194 |
extern void sendstring(const char *t, packetbuf &p); |
| 1195 |
extern void sendstring(const char *t, vector<uchar> &p); |
| 1196 |
extern void getstring(char *t, ucharbuf &p, int len); |
| 1197 |
template<class T, size_t N> static inline void getstring(T (&t)[N], ucharbuf &p) { getstring(t, p, N); } |
| 1198 |
extern void filtertext(char *dst, const char *src, bool whitespace = true, int len = sizeof(string)-1); |
| 1199 |
|
| 1200 |
#endif |
| 1201 |
|
| 1183 |
-- a/src/engine/master.cpp |
1202 |
++ b/src/engine/master.cpp |
|
Lines 514-520
Link Here
|
| 514 |
authreq &a = c.authreqs.add(); |
514 |
authreq &a = c.authreqs.add(); |
| 515 |
a.reqtime = servtime; |
515 |
a.reqtime = servtime; |
| 516 |
a.id = id; |
516 |
a.id = id; |
| 517 |
uint seed[3] = { starttime, servtime, randomMT() }; |
517 |
uint seed[3] = { uint(starttime), servtime, randomMT() }; |
| 518 |
static vector<char> buf; |
518 |
static vector<char> buf; |
| 519 |
buf.setsize(0); |
519 |
buf.setsize(0); |
| 520 |
a.answer = genchallenge(u->pubkey, seed, sizeof(seed), buf); |
520 |
a.answer = genchallenge(u->pubkey, seed, sizeof(seed), buf); |
| 521 |
-- a/src/shared/tools.cpp |
521 |
++ b/src/shared/tools.cpp |
|
Lines 53-55
Link Here
|
| 53 |
return y; |
53 |
return y; |
| 54 |
} |
54 |
} |
| 55 |
|
55 |
|
|
|
56 |
///////////////////////// network /////////////////////// |
| 57 |
|
| 58 |
// all network traffic is in 32bit ints, which are then compressed using the following simple scheme (assumes that most values are small). |
| 59 |
|
| 60 |
template<class T> |
| 61 |
static inline void putint_(T &p, int n) |
| 62 |
{ |
| 63 |
if(n<128 && n>-127) p.put(n); |
| 64 |
else if(n<0x8000 && n>=-0x8000) { p.put(0x80); p.put(n); p.put(n>>8); } |
| 65 |
else { p.put(0x81); p.put(n); p.put(n>>8); p.put(n>>16); p.put(n>>24); } |
| 66 |
} |
| 67 |
void putint(ucharbuf &p, int n) { putint_(p, n); } |
| 68 |
void putint(packetbuf &p, int n) { putint_(p, n); } |
| 69 |
void putint(vector<uchar> &p, int n) { putint_(p, n); } |
| 70 |
|
| 71 |
int getint(ucharbuf &p) |
| 72 |
{ |
| 73 |
int c = (char)p.get(); |
| 74 |
if(c==-128) { int n = p.get(); n |= char(p.get())<<8; return n; } |
| 75 |
else if(c==-127) { int n = p.get(); n |= p.get()<<8; n |= p.get()<<16; return n|(p.get()<<24); } |
| 76 |
else return c; |
| 77 |
} |
| 78 |
|
| 79 |
// much smaller encoding for unsigned integers up to 28 bits, but can handle signed |
| 80 |
template<class T> |
| 81 |
static inline void putuint_(T &p, int n) |
| 82 |
{ |
| 83 |
if(n < 0 || n >= (1<<21)) |
| 84 |
{ |
| 85 |
p.put(0x80 | (n & 0x7F)); |
| 86 |
p.put(0x80 | ((n >> 7) & 0x7F)); |
| 87 |
p.put(0x80 | ((n >> 14) & 0x7F)); |
| 88 |
p.put(n >> 21); |
| 89 |
} |
| 90 |
else if(n < (1<<7)) p.put(n); |
| 91 |
else if(n < (1<<14)) |
| 92 |
{ |
| 93 |
p.put(0x80 | (n & 0x7F)); |
| 94 |
p.put(n >> 7); |
| 95 |
} |
| 96 |
else |
| 97 |
{ |
| 98 |
p.put(0x80 | (n & 0x7F)); |
| 99 |
p.put(0x80 | ((n >> 7) & 0x7F)); |
| 100 |
p.put(n >> 14); |
| 101 |
} |
| 102 |
} |
| 103 |
void putuint(ucharbuf &p, int n) { putuint_(p, n); } |
| 104 |
void putuint(packetbuf &p, int n) { putuint_(p, n); } |
| 105 |
void putuint(vector<uchar> &p, int n) { putuint_(p, n); } |
| 106 |
|
| 107 |
int getuint(ucharbuf &p) |
| 108 |
{ |
| 109 |
int n = p.get(); |
| 110 |
if(n & 0x80) |
| 111 |
{ |
| 112 |
n += (p.get() << 7) - 0x80; |
| 113 |
if(n & (1<<14)) n += (p.get() << 14) - (1<<14); |
| 114 |
if(n & (1<<21)) n += (p.get() << 21) - (1<<21); |
| 115 |
if(n & (1<<28)) n |= -1<<28; |
| 116 |
} |
| 117 |
return n; |
| 118 |
} |
| 119 |
|
| 120 |
template<class T> |
| 121 |
static inline void putfloat_(T &p, float f) |
| 122 |
{ |
| 123 |
lilswap(&f, 1); |
| 124 |
p.put((uchar *)&f, sizeof(float)); |
| 125 |
} |
| 126 |
void putfloat(ucharbuf &p, float f) { putfloat_(p, f); } |
| 127 |
void putfloat(packetbuf &p, float f) { putfloat_(p, f); } |
| 128 |
void putfloat(vector<uchar> &p, float f) { putfloat_(p, f); } |
| 129 |
|
| 130 |
float getfloat(ucharbuf &p) |
| 131 |
{ |
| 132 |
float f; |
| 133 |
p.get((uchar *)&f, sizeof(float)); |
| 134 |
return lilswap(f); |
| 135 |
} |
| 136 |
|
| 137 |
template<class T> |
| 138 |
static inline void sendstring_(const char *t, T &p) |
| 139 |
{ |
| 140 |
while(*t) putint(p, *t++); |
| 141 |
putint(p, 0); |
| 142 |
} |
| 143 |
void sendstring(const char *t, ucharbuf &p) { sendstring_(t, p); } |
| 144 |
void sendstring(const char *t, packetbuf &p) { sendstring_(t, p); } |
| 145 |
void sendstring(const char *t, vector<uchar> &p) { sendstring_(t, p); } |
| 146 |
|
| 147 |
void getstring(char *text, ucharbuf &p, int len) |
| 148 |
{ |
| 149 |
char *t = text; |
| 150 |
do |
| 151 |
{ |
| 152 |
if(t>=&text[len]) { text[len-1] = 0; return; } |
| 153 |
if(!p.remaining()) { *t = 0; return; } |
| 154 |
*t = getint(p); |
| 155 |
} |
| 156 |
while(*t++); |
| 157 |
} |
| 158 |
|
| 159 |
void filtertext(char *dst, const char *src, bool whitespace, int len) |
| 160 |
{ |
| 161 |
for(int c = uchar(*src); c; c = uchar(*++src)) |
| 162 |
{ |
| 163 |
if(c == '\f') |
| 164 |
{ |
| 165 |
if(!*++src) break; |
| 166 |
continue; |
| 167 |
} |
| 168 |
if(iscubeprint(c) || (iscubespace(c) && whitespace)) |
| 169 |
{ |
| 170 |
*dst++ = c; |
| 171 |
if(!--len) break; |
| 172 |
} |
| 173 |
} |
| 174 |
*dst = '\0'; |
| 175 |
} |
| 176 |
|