Subversion Repositories Kolibri OS

Rev

Rev 4710 | Rev 7300 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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