Subversion Repositories Kolibri OS

Rev

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

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                                 ;;
  3. ;; Copyright (C) KolibriOS team 2004-2013. 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.  
  104.         if TIMESTAMP
  105.         call    print_timestamp
  106.         end if
  107.         mov     esi, str_reconnect
  108.         call    print_text2
  109.  
  110.         mov     esi, quit_msg
  111.         call    cmd_usr_quit.with_message
  112.  
  113.         jmp     socket_connect
  114.  
  115.  
  116. socket_write_userinfo:
  117.  
  118. ; create packet in packetbuf
  119.         mov     edi, packetbuf
  120.  
  121.         mov     eax, 'NICK'
  122.         stosd
  123.         mov     al, ' '
  124.         stosb
  125.         mov     esi, user_nick
  126.         mov     ecx, MAX_NICK_LEN
  127.   .loop:
  128.         lodsb
  129.         test    al, al
  130.         jz      .done
  131.         stosb
  132.         dec     ecx
  133.         jnz     .loop
  134.   .done:
  135.         mov     ax, 0x0a0d
  136.         stosw
  137.  
  138.         mov     eax, 'USER'
  139.         stosd
  140.         mov     al, ' '
  141.         stosb
  142.         mov     esi, user_nick
  143.         mov     ecx, MAX_NICK_LEN
  144.   .loop2:
  145.         lodsb
  146.         test    al, al
  147.         jz      .done2
  148.         stosb
  149.         dec     ecx
  150.         jnz     .loop2
  151.   .done2:
  152.         mov     eax, ' 8 *'
  153.         stosd
  154.         mov     ax, ' :'
  155.         stosw
  156.         mov     al, ' '
  157.         stosb
  158.         mov     esi, user_real_name
  159.         mov     ecx, MAX_REAL_LEN
  160.   .loop3:
  161.         lodsb
  162.         test    al, al
  163.         jz      .done3
  164.         stosb
  165.         dec     ecx
  166.         jnz     .loop3
  167.   .done3:
  168.         mov     ax, 0x0a0d
  169.         stosw
  170.  
  171.         lea     esi, [edi - packetbuf]
  172.         mcall   send, [socketnum], packetbuf, , 0
  173.  
  174.         ret
  175.  
  176.  
  177.  
  178.  
  179. process_network_event:
  180. ; values for status: 0, 1, 2, 3
  181.         mov     eax, [status]
  182.         dec     eax
  183. ; 0 = STATUS_DISCONNECTED - do nothing
  184. ; (ignore network events if we are disconnected from network)
  185.         js      .nothing
  186. ; 1 = STATUS_RESOLVING
  187.         jz      .nothing
  188. ; 2 = STATUS_CONNECTING
  189.         dec     eax
  190.         jz      .connecting
  191. ; 3 = STATUS_CONNECTED
  192.         jmp     .connected
  193.  
  194.   .nothing:
  195.         ret
  196.  
  197.   .connecting:
  198.         call    socket_write_userinfo
  199.  
  200. ; The connection has been established, change status from "connecting" to "connected".
  201.         inc     [status]
  202.  
  203.   .connected:
  204.         call    socket_receive
  205.         ret
  206.  
  207.  
  208. socket_receive:
  209.  
  210.         pusha
  211.  
  212. ; TODO: read more data if we receive one full packet
  213.  
  214.   .nextpacket:
  215.         mcall   recv, [socketnum], packetbuf, 1024, MSG_DONTWAIT        ; read a packet
  216.         inc     eax                                                     ; check if we got one
  217.         jz      .done
  218.         dec     eax
  219.         jz      .done                                                   ; TODO: check for errors!
  220.  
  221. ; ok we have data, now feed it to the recoder
  222.  
  223.         lea     edx, [packetbuf + eax]                  ; edx = end pointer
  224.         mov     esi, packetbuf                          ; esi = start pointer
  225.   .nextcommand:
  226.         mov     edi, servercommand
  227.   .byteloop:
  228.         call    get_next_byte                           ; reads byte from [esi] to al
  229.         jnc     .nextpacket                             ; if CF is set, we need more data
  230.         cmp     al, 10
  231.         je      .got_command
  232.         cmp     al, 13
  233.         je      .got_command
  234.         stosb
  235.         jmp     .byteloop
  236.  
  237. ; we have a command, call the serverparser
  238.  
  239.   .got_command:
  240.         mov     byte[edi], 0                            ; mark the end of the command
  241.         push    esi edx
  242.         call    server_parser
  243.         pop     edx esi
  244.         jmp     .nextcommand
  245.  
  246.   .done:
  247.         popa
  248.  
  249.         ret