Subversion Repositories Kolibri OS

Rev

Rev 4623 | Rev 7300 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                                 ;;
  3. ;; Copyright (C) KolibriOS team 2004-2014. All rights reserved.    ;;
  4. ;; Distributed under terms of the GNU General Public License       ;;
  5. ;;                                                                 ;;
  6. ;;   Written by hidnplayr@kolibrios.org                            ;;
  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
  18.         jne     .reconnect
  19.  
  20.         if TIMESTAMP
  21.         call    print_timestamp
  22.         end if
  23.         mov     esi, str_connecting
  24.         call    print_asciiz
  25.  
  26.         mov     esi, irc_server_name
  27.         call    print_asciiz
  28.  
  29.         mov     al, 10
  30.         call    print_char
  31.  
  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.  
  74.         if TIMESTAMP
  75.         call    print_timestamp
  76.         end if
  77.         mov     esi, str_sockerr
  78.         call    print_asciiz
  79.  
  80.         ret
  81.  
  82.   .fail_dns:
  83.         mov     [status], STATUS_DISCONNECTED
  84.  
  85.         if TIMESTAMP
  86.         call    print_timestamp
  87.         end if
  88.         mov     esi, str_dnserr
  89.         call    print_asciiz
  90.  
  91.         ret
  92.  
  93.   .fail_refused:
  94.         mov     [status], STATUS_DISCONNECTED
  95.  
  96.         if TIMESTAMP
  97.         call    print_timestamp
  98.         end if
  99.         mov     esi, str_refused
  100.         call    print_asciiz
  101.  
  102.         ret
  103.  
  104.   .reconnect:
  105.         if TIMESTAMP
  106.         call    print_timestamp
  107.         end if
  108.         mov     esi, str_reconnect
  109.         call    print_asciiz
  110.  
  111.         mov     esi, quit_msg
  112.         call    quit_server
  113.  
  114.         jmp     socket_connect
  115.  
  116.  
  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:
  136.         mov     ax, 0x0a0d
  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:
  169.         mov     ax, 0x0a0d
  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:
  205.         call    socket_receive
  206.         ret
  207.  
  208.  
  209. socket_receive:
  210.  
  211.         pusha
  212.  
  213. ; TODO: read more data if we receive one full packet
  214.  
  215.   .nextpacket:
  216.         mcall   recv, [socketnum], packetbuf, 1024, MSG_DONTWAIT        ; read a packet
  217.         inc     eax                                                     ; check if we got any data
  218.         jz      .done                                                   ; TODO: handle errors!
  219.         dec     eax
  220.         jz      .disconnected
  221.  
  222. ; ok we have data, now feed it to the recoder
  223.  
  224.         lea     edx, [packetbuf + eax]                  ; edx = end pointer
  225.         mov     esi, packetbuf                          ; esi = start pointer
  226.   .nextcommand:
  227.         mov     edi, servercommand
  228.   .byteloop:
  229.         call    get_next_byte                           ; reads byte from [esi] to al
  230.         jnc     .nextpacket                             ; if CF is set, we need more data (TODO: dont throw away old data!!!)
  231.         cmp     al, 10
  232.         je      .got_command
  233.         cmp     al, 13
  234.         je      .got_command
  235.         stosb
  236.         jmp     .byteloop
  237.  
  238. ; we have a command, call the serverparser
  239.  
  240.   .got_command:
  241.         mov     byte[edi], 0                            ; mark the end of the command
  242.         push    esi edx
  243.         call    server_parser
  244.         pop     edx esi
  245.         jmp     .nextcommand
  246.  
  247.   .done:
  248.         popa
  249.         ret
  250.  
  251.  
  252.   .disconnected:
  253.         if TIMESTAMP
  254.         call    print_timestamp
  255.         end if
  256.         mov     esi, str_srv_disconnected
  257.         call    print_asciiz
  258.  
  259.         mov     [status], STATUS_DISCONNECTED
  260.         mcall   close, [socketnum]
  261.  
  262.         popa
  263.         ret