Rev 9979 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3545 | hidnplayr | 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
2 | ;; ;; |
||
9979 | hidnplayr | 3 | ;; Copyright (C) KolibriOS team 2004-2024. All rights reserved. ;; |
3545 | hidnplayr | 4 | ;; Distributed under terms of the GNU General Public License ;; |
5 | ;; ;; |
||
4143 | hidnplayr | 6 | ;; Written by hidnplayr@kolibrios.org ;; |
3545 | hidnplayr | 7 | ;; ;; |
8 | ;; GNU GENERAL PUBLIC LICENSE ;; |
||
9 | ;; Version 2, June 1991 ;; |
||
10 | ;; ;; |
||
11 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
||
12 | |||
13 | |||
14 | socket_connect: |
||
15 | |||
16 | ; ignore if status is not "disconnected" |
||
17 | cmp [status], STATUS_DISCONNECTED |
||
4143 | hidnplayr | 18 | jne .reconnect |
3545 | hidnplayr | 19 | |
4143 | hidnplayr | 20 | if TIMESTAMP |
21 | call print_timestamp |
||
22 | end if |
||
3545 | hidnplayr | 23 | mov esi, str_connecting |
4623 | hidnplayr | 24 | call print_asciiz |
25 | |||
3545 | hidnplayr | 26 | mov esi, irc_server_name |
4623 | hidnplayr | 27 | call print_asciiz |
3545 | hidnplayr | 28 | |
4623 | hidnplayr | 29 | mov al, 10 |
30 | call print_char |
||
31 | |||
3545 | hidnplayr | 32 | ; update status |
33 | inc [status] ; was STATUS_DISCONNECTED, now STATUS_RESOLVING |
||
34 | |||
35 | ; resolve name |
||
36 | push esp ; reserve stack place |
||
37 | push esp ; fourth parameter |
||
38 | push 0 ; third parameter |
||
39 | push 0 ; second parameter |
||
40 | push irc_server_name |
||
41 | call [getaddrinfo] |
||
42 | pop esi |
||
43 | ; test for error |
||
44 | test eax, eax |
||
45 | jnz .fail_dns |
||
46 | |||
47 | ; fill in ip in sockstruct |
||
48 | mov eax, [esi + addrinfo.ai_addr] |
||
49 | mov eax, [eax + sockaddr_in.sin_addr] |
||
50 | mov [sockaddr1.ip], eax |
||
51 | |||
52 | ; free allocated memory |
||
53 | push esi |
||
54 | call [freeaddrinfo] |
||
55 | |||
56 | ; update status |
||
57 | inc [status] |
||
58 | |||
59 | ; connect |
||
60 | mcall socket, AF_INET4, SOCK_STREAM, 0 |
||
61 | cmp eax, -1 |
||
62 | jz .fail |
||
63 | mov [socketnum], eax |
||
64 | |||
65 | mcall connect, [socketnum], sockaddr1, 18 |
||
66 | cmp eax, -1 |
||
67 | jz .fail_refused |
||
68 | |||
69 | ret |
||
70 | |||
71 | .fail: |
||
72 | mov [status], STATUS_DISCONNECTED |
||
73 | |||
4143 | hidnplayr | 74 | if TIMESTAMP |
75 | call print_timestamp |
||
76 | end if |
||
3545 | hidnplayr | 77 | mov esi, str_sockerr |
4623 | hidnplayr | 78 | call print_asciiz |
3545 | hidnplayr | 79 | |
80 | ret |
||
81 | |||
82 | .fail_dns: |
||
83 | mov [status], STATUS_DISCONNECTED |
||
84 | |||
4143 | hidnplayr | 85 | if TIMESTAMP |
86 | call print_timestamp |
||
87 | end if |
||
3545 | hidnplayr | 88 | mov esi, str_dnserr |
4623 | hidnplayr | 89 | call print_asciiz |
3545 | hidnplayr | 90 | |
91 | ret |
||
92 | |||
93 | .fail_refused: |
||
94 | mov [status], STATUS_DISCONNECTED |
||
95 | |||
4143 | hidnplayr | 96 | if TIMESTAMP |
97 | call print_timestamp |
||
98 | end if |
||
3545 | hidnplayr | 99 | mov esi, str_refused |
4623 | hidnplayr | 100 | call print_asciiz |
3545 | hidnplayr | 101 | |
102 | ret |
||
103 | |||
4143 | hidnplayr | 104 | .reconnect: |
105 | if TIMESTAMP |
||
106 | call print_timestamp |
||
107 | end if |
||
108 | mov esi, str_reconnect |
||
4623 | hidnplayr | 109 | call print_asciiz |
3545 | hidnplayr | 110 | |
4143 | hidnplayr | 111 | mov esi, quit_msg |
4710 | hidnplayr | 112 | call quit_server |
4143 | hidnplayr | 113 | |
114 | jmp socket_connect |
||
115 | |||
116 | |||
3545 | hidnplayr | 117 | socket_write_userinfo: |
118 | |||
119 | ; create packet in packetbuf |
||
120 | mov edi, packetbuf |
||
121 | |||
122 | mov eax, 'NICK' |
||
123 | stosd |
||
124 | mov al, ' ' |
||
125 | stosb |
||
126 | mov esi, user_nick |
||
127 | mov ecx, MAX_NICK_LEN |
||
128 | .loop: |
||
129 | lodsb |
||
130 | test al, al |
||
131 | jz .done |
||
132 | stosb |
||
133 | dec ecx |
||
134 | jnz .loop |
||
135 | .done: |
||
3981 | hidnplayr | 136 | mov ax, 0x0a0d |
3545 | hidnplayr | 137 | stosw |
138 | |||
139 | mov eax, 'USER' |
||
140 | stosd |
||
141 | mov al, ' ' |
||
142 | stosb |
||
143 | mov esi, user_nick |
||
144 | mov ecx, MAX_NICK_LEN |
||
145 | .loop2: |
||
146 | lodsb |
||
147 | test al, al |
||
148 | jz .done2 |
||
149 | stosb |
||
150 | dec ecx |
||
151 | jnz .loop2 |
||
152 | .done2: |
||
153 | mov eax, ' 8 *' |
||
154 | stosd |
||
155 | mov ax, ' :' |
||
156 | stosw |
||
157 | mov al, ' ' |
||
158 | stosb |
||
159 | mov esi, user_real_name |
||
160 | mov ecx, MAX_REAL_LEN |
||
161 | .loop3: |
||
162 | lodsb |
||
163 | test al, al |
||
164 | jz .done3 |
||
165 | stosb |
||
166 | dec ecx |
||
167 | jnz .loop3 |
||
168 | .done3: |
||
3981 | hidnplayr | 169 | mov ax, 0x0a0d |
3545 | hidnplayr | 170 | stosw |
171 | |||
172 | lea esi, [edi - packetbuf] |
||
173 | mcall send, [socketnum], packetbuf, , 0 |
||
174 | |||
175 | ret |
||
176 | |||
177 | |||
178 | |||
179 | |||
180 | process_network_event: |
||
181 | ; values for status: 0, 1, 2, 3 |
||
182 | mov eax, [status] |
||
183 | dec eax |
||
184 | ; 0 = STATUS_DISCONNECTED - do nothing |
||
185 | ; (ignore network events if we are disconnected from network) |
||
186 | js .nothing |
||
187 | ; 1 = STATUS_RESOLVING |
||
188 | jz .nothing |
||
189 | ; 2 = STATUS_CONNECTING |
||
190 | dec eax |
||
191 | jz .connecting |
||
192 | ; 3 = STATUS_CONNECTED |
||
193 | jmp .connected |
||
194 | |||
195 | .nothing: |
||
196 | ret |
||
197 | |||
198 | .connecting: |
||
199 | call socket_write_userinfo |
||
200 | |||
201 | ; The connection has been established, change status from "connecting" to "connected". |
||
202 | inc [status] |
||
203 | |||
204 | .connected: |
||
4143 | hidnplayr | 205 | call socket_receive |
3545 | hidnplayr | 206 | ret |
207 | |||
208 | |||
4143 | hidnplayr | 209 | socket_receive: |
3545 | hidnplayr | 210 | |
211 | pusha |
||
212 | |||
9984 | hidnplayr | 213 | .recv_more: |
214 | mcall recv, [socketnum], packetbuf , PACKETBUF_SIZE, MSG_DONTWAIT ; read a packet |
||
7300 | hidnplayr | 215 | inc eax ; check if we got any data |
216 | jz .done ; TODO: handle errors! |
||
3545 | hidnplayr | 217 | dec eax |
4477 | hidnplayr | 218 | jz .disconnected |
3545 | hidnplayr | 219 | |
6026 | hidnplayr | 220 | ; ok we have data, now feed it to the command splicer |
3545 | hidnplayr | 221 | |
6026 | hidnplayr | 222 | mov ecx, eax |
3545 | hidnplayr | 223 | mov esi, packetbuf ; esi = start pointer |
224 | mov edi, servercommand |
||
9984 | hidnplayr | 225 | add edi, [cmd_remaining] |
3545 | hidnplayr | 226 | .byteloop: |
9979 | hidnplayr | 227 | test ecx, ecx |
9984 | hidnplayr | 228 | jz .recv_more |
6026 | hidnplayr | 229 | lodsb |
9979 | hidnplayr | 230 | dec ecx |
3545 | hidnplayr | 231 | cmp al, 10 |
232 | je .got_command |
||
233 | cmp al, 13 |
||
234 | je .got_command |
||
9984 | hidnplayr | 235 | cmp edi, servercommand + SERVERCOMMAND_SIZE ; Prevent command buffer overflow |
236 | jae .byteloop |
||
3545 | hidnplayr | 237 | stosb |
9979 | hidnplayr | 238 | jmp .byteloop |
3545 | hidnplayr | 239 | |
240 | ; we have a command, call the serverparser |
||
241 | |||
242 | .got_command: |
||
9979 | hidnplayr | 243 | cmp edi, servercommand + 4 |
244 | jb .nextcommand |
||
245 | |||
3545 | hidnplayr | 246 | mov byte[edi], 0 ; mark the end of the command |
6026 | hidnplayr | 247 | push esi ecx |
9979 | hidnplayr | 248 | mov ecx, edi |
249 | sub ecx, servercommand ; put length in ecx |
||
3545 | hidnplayr | 250 | call server_parser |
6026 | hidnplayr | 251 | pop ecx esi |
3545 | hidnplayr | 252 | |
9984 | hidnplayr | 253 | .nextcommand: |
254 | mov edi, servercommand |
||
255 | mov [cmd_remaining], ecx ; remaining bytes since last EOL |
||
256 | jmp .byteloop |
||
257 | |||
3545 | hidnplayr | 258 | .done: |
259 | popa |
||
4477 | hidnplayr | 260 | ret |
3545 | hidnplayr | 261 | |
4477 | hidnplayr | 262 | |
263 | .disconnected: |
||
4710 | hidnplayr | 264 | if TIMESTAMP |
265 | call print_timestamp |
||
266 | end if |
||
267 | mov esi, str_srv_disconnected |
||
4623 | hidnplayr | 268 | call print_asciiz |
4477 | hidnplayr | 269 | |
270 | mov [status], STATUS_DISCONNECTED |
||
271 | mcall close, [socketnum] |
||
272 | |||
273 | popa |
||
3545 | hidnplayr | 274 | ret |