Subversion Repositories Kolibri OS

Rev

Rev 4710 | Rev 7300 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3545 hidnplayr 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                                 ;;
6026 hidnplayr 3
;; Copyright (C) KolibriOS team 2004-2016. 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
 
6026 hidnplayr 213
; FIXME: make this a proper stream!
3545 hidnplayr 214
 
215
  .nextpacket:
3704 hidnplayr 216
        mcall   recv, [socketnum], packetbuf, 1024, MSG_DONTWAIT        ; read a packet
4477 hidnplayr 217
        inc     eax                                                     ; check if we got any data
218
        jz      .done                                                   ; TODO: handle errors!
3545 hidnplayr 219
        dec     eax
4477 hidnplayr 220
        jz      .disconnected
3545 hidnplayr 221
 
6026 hidnplayr 222
; ok we have data, now feed it to the command splicer
3545 hidnplayr 223
 
6026 hidnplayr 224
        mov     ecx, eax
3545 hidnplayr 225
        mov     esi, packetbuf                          ; esi = start pointer
226
  .nextcommand:
227
        mov     edi, servercommand
228
  .byteloop:
6026 hidnplayr 229
        lodsb
3545 hidnplayr 230
        cmp     al, 10
231
        je      .got_command
232
        cmp     al, 13
233
        je      .got_command
234
        stosb
6026 hidnplayr 235
        dec     ecx
236
        jnz     .byteloop
237
        ;;; FIXME
238
        jmp     .nextpacket
3545 hidnplayr 239
 
240
; we have a command, call the serverparser
241
 
242
  .got_command:
243
        mov     byte[edi], 0                            ; mark the end of the command
6026 hidnplayr 244
        push    esi ecx
3545 hidnplayr 245
        call    server_parser
6026 hidnplayr 246
        pop     ecx esi
247
        test    ecx, ecx
248
        jnz     .nextcommand
3545 hidnplayr 249
 
250
  .done:
251
        popa
4477 hidnplayr 252
        ret
3545 hidnplayr 253
 
4477 hidnplayr 254
 
255
  .disconnected:
4710 hidnplayr 256
        if TIMESTAMP
257
        call    print_timestamp
258
        end if
259
        mov     esi, str_srv_disconnected
4623 hidnplayr 260
        call    print_asciiz
4477 hidnplayr 261
 
262
        mov     [status], STATUS_DISCONNECTED
263
        mcall   close, [socketnum]
264
 
265
        popa
3545 hidnplayr 266
        ret