… | |
… | |
35 | using namespace std; |
35 | using namespace std; |
36 | |
36 | |
37 | #include <global.h> |
37 | #include <global.h> |
38 | #include <newclient.h> |
38 | #include <newclient.h> |
39 | #include <sproto.h> |
39 | #include <sproto.h> |
|
|
40 | #include <cstdarg> |
40 | |
41 | |
41 | #ifdef __linux__ |
42 | #ifdef __linux__ |
42 | # include <sys/types.h> |
43 | # include <sys/types.h> |
43 | # include <sys/socket.h> |
44 | # include <sys/socket.h> |
44 | # include <netinet/in.h> |
45 | # include <netinet/in.h> |
… | |
… | |
83 | * |
84 | * |
84 | * SockList functions/utilities |
85 | * SockList functions/utilities |
85 | * |
86 | * |
86 | **********************************************************************/ |
87 | **********************************************************************/ |
87 | |
88 | |
|
|
89 | SockList &SockList::operator <<(const data &v) |
|
|
90 | { |
|
|
91 | if (v.len) |
|
|
92 | { |
|
|
93 | memcpy (buf + len, v.ptr, v.len); |
|
|
94 | len += v.len; |
|
|
95 | } |
|
|
96 | |
|
|
97 | return *this; |
|
|
98 | } |
|
|
99 | |
88 | SockList &SockList::operator <<(const data8 &v) |
100 | SockList &SockList::operator <<(const data8 &v) |
89 | { |
101 | { |
90 | *this << uint8 (v.len); |
102 | unsigned int len = min (v.len, 0x00FF); |
91 | |
103 | return *this << uint8 (len) << data (v.ptr, len); |
92 | memcpy (buf + len, v.data, v.len); |
|
|
93 | len += v.len; |
|
|
94 | |
|
|
95 | return *this; |
|
|
96 | } |
104 | } |
97 | |
105 | |
98 | SockList &SockList::operator <<(const data16 &v) |
106 | SockList &SockList::operator <<(const data16 &v) |
99 | { |
107 | { |
100 | *this << uint16 (v.len); |
108 | unsigned int len = min (v.len, 0xFFFF); |
101 | |
109 | return *this << uint16 (len) << data (v.ptr, len); |
102 | memcpy (buf + len, v.data, v.len); |
|
|
103 | len += v.len; |
|
|
104 | |
|
|
105 | return *this; |
|
|
106 | } |
110 | } |
107 | |
111 | |
108 | SockList &SockList::operator <<(const char *v) |
112 | SockList &SockList::operator <<(const char *v) |
109 | { |
113 | { |
110 | if (v) |
114 | return *this << data (v, strlen (v ? v : 0)); |
111 | { |
115 | } |
112 | int l = strlen (v); |
|
|
113 | memcpy (buf + len, v, l); |
|
|
114 | len += l; |
|
|
115 | } |
|
|
116 | |
116 | |
117 | return *this; |
117 | void |
|
|
118 | SockList::printf (const char *format, ...) |
|
|
119 | { |
|
|
120 | va_list ap; |
|
|
121 | va_start (ap, format); |
|
|
122 | |
|
|
123 | len += vsprintf ((char *)buf + len, format, ap); |
|
|
124 | |
|
|
125 | va_end (ap); |
118 | } |
126 | } |
119 | |
127 | |
120 | /* Basically does the reverse of SockList_AddInt, but on |
128 | /* Basically does the reverse of SockList_AddInt, but on |
121 | * strings instead. Same for the GetShort, but for 16 bits. |
129 | * strings instead. Same for the GetShort, but for 16 bits. |
122 | */ |
130 | */ |
… | |
… | |
145 | * buffer allocated in the socklist. We make the assumption the buffer is |
153 | * buffer allocated in the socklist. We make the assumption the buffer is |
146 | * at least 2 bytes long. |
154 | * at least 2 bytes long. |
147 | */ |
155 | */ |
148 | |
156 | |
149 | int |
157 | int |
150 | SockList_ReadPacket (int fd, SockList * sl, int len) |
158 | SockList_ReadPacket (int fd, SockList *sl, int len) |
151 | { |
159 | { |
152 | int stat, toread; |
160 | int stat, toread; |
153 | |
161 | |
154 | /* Sanity check - shouldn't happen */ |
162 | /* Sanity check - shouldn't happen */ |
155 | if (sl->len < 0) |
163 | if (sl->len < 0) |
… | |
… | |
420 | /** |
428 | /** |
421 | * Takes a string of data, and writes it out to the socket. A very handy |
429 | * Takes a string of data, and writes it out to the socket. A very handy |
422 | * shortcut function. |
430 | * shortcut function. |
423 | */ |
431 | */ |
424 | void |
432 | void |
425 | cs_write_string (NewSocket * ns, const char *buf, int len) |
433 | cs_write_string (NewSocket *ns, const char *buf, int len) |
426 | { |
434 | { |
427 | SockList sl; |
435 | SockList sl; |
428 | |
436 | |
429 | sl.len = len; |
437 | sl.len = len; |
430 | sl.buf = (unsigned char *) buf; |
438 | sl.buf = (unsigned char *) buf; |
… | |
… | |
437 | * |
445 | * |
438 | * The only difference in this function is that we take a SockList |
446 | * The only difference in this function is that we take a SockList |
439 | *, and we prepend the length information. |
447 | *, and we prepend the length information. |
440 | */ |
448 | */ |
441 | void |
449 | void |
442 | Send_With_Handling (NewSocket * ns, SockList * msg) |
450 | Send_With_Handling (NewSocket *ns, SockList *msg) |
443 | { |
451 | { |
444 | unsigned char sbuf[4]; |
452 | unsigned char sbuf[4]; |
445 | |
453 | |
446 | if (ns->status == Ns_Dead || !msg) |
454 | if (ns->status == Ns_Dead || !msg) |
447 | return; |
455 | return; |
… | |
… | |
464 | /** |
472 | /** |
465 | * Takes a string of data, and writes it out to the socket. A very handy |
473 | * Takes a string of data, and writes it out to the socket. A very handy |
466 | * shortcut function. |
474 | * shortcut function. |
467 | */ |
475 | */ |
468 | void |
476 | void |
469 | Write_String_To_Socket (NewSocket * ns, char *buf, int len) |
477 | Write_String_To_Socket (NewSocket *ns, char *buf, int len) |
470 | { |
478 | { |
471 | SockList sl; |
479 | SockList sl; |
472 | |
480 | |
473 | sl.len = len; |
481 | sl.len = len; |
474 | sl.buf = (unsigned char *) buf; |
482 | sl.buf = (unsigned char *) buf; |