Subversion Repositories Kolibri OS

Rev

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