Subversion Repositories Kolibri OS

Rev

Rev 2959 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                                 ;;
  3. ;; Copyright (C) KolibriOS team 2012. All rights reserved.         ;;
  4. ;; Distributed under terms of the GNU General Public License       ;;
  5. ;;                                                                 ;;
  6. ;;  pppoe.asm - PPPoE dialer for KolibriOS                         ;;
  7. ;;                                                                 ;;
  8. ;;  Written by hidnplayr@kolibrios.org                             ;;
  9. ;;                                                                 ;;
  10. ;;          GNU GENERAL PUBLIC LICENSE                             ;;
  11. ;;             Version 2, June 1991                                ;;
  12. ;;                                                                 ;;
  13. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  14.  
  15. format binary as ""
  16.  
  17. use32
  18.  
  19.         db      'MENUET01'      ; signature
  20.         dd      1               ; header version
  21.         dd      start           ; entry point
  22.         dd      i_end           ; initialized size
  23.         dd      mem             ; required memory
  24.         dd      mem             ; stack pointer
  25.         dd      0               ; parameters
  26.         dd      0               ; path
  27.  
  28. include '../macros.inc'
  29. purge mov,add,sub
  30. include '../proc32.inc'
  31. include '../dll.inc'
  32. include '../network.inc'
  33. include '../struct.inc'
  34.  
  35. ; PPP Active Discovery...
  36. PPPoE_PADI      = 0x09  ; .. Initiation
  37. PPPoE_PADO      = 0x07  ; .. Offer
  38. PPPoE_PADR      = 0x19  ; .. Request
  39. PPPoE_PADS      = 0x65  ; .. Session-confirmation
  40. PPPoE_PADT      = 0xa7  ; .. Terminate
  41.  
  42. TAG_EOL         = 0x0000
  43. TAG_SERVICE_NAME= 0x0101
  44. TAG_AC_NAME     = 0x0201
  45. TAG_HOST_UNIQ   = 0x0301
  46. TAG_AC_COOKIE   = 0x0401
  47.  
  48. struct  ETH_frame
  49.         DestMac         dp ?
  50.         SrcMac          dp ?
  51.         Type            dw ?
  52. ends
  53.  
  54.  
  55. struct  PPPoE_frame     ETH_frame
  56.         VersionAndType  db ?
  57.         Code            db ?
  58.         SessionID       dw ?
  59.         Length          dw ?            ; Length of payload, does NOT include the length PPPoE header.
  60.         Payload         rb 0
  61. ends
  62.  
  63. ; entry point
  64. start:
  65. ; load libraries
  66.         stdcall dll.Load, @IMPORT
  67.         test    eax, eax
  68.         jnz     exit
  69. ; initialize console
  70.         push    1
  71.         call    [con_start]
  72.         push    title
  73.         push    25
  74.         push    80
  75.         push    25
  76.         push    80
  77.         call    [con_init]
  78.  
  79. main:
  80.         mcall   40,  1 shl 7
  81.  
  82.         call    [con_cls]
  83. ; Welcome user
  84.         push    str1
  85.         call    [con_write_asciiz]
  86.  
  87.         mcall   socket, 777, 3, 666
  88.         mov     [socketnum], eax
  89.         mcall   send, [socketnum], PADI, PADI.length, 0
  90.  
  91. mainloop:
  92.         mcall   10
  93.  
  94.         call    [con_get_flags]
  95.         test    eax, 0x200                      ; con window closed?
  96.         jnz     close_conn
  97.  
  98.         mcall   recv, [socketnum], buffer, 4096
  99.         cmp     eax, sizeof.PPPoE_frame
  100.         jb      mainloop
  101.  
  102.         cmp     [buffer + PPPoE_frame.Code], PPPoE_PADO
  103.         je      pado
  104.  
  105.         cmp     [buffer + PPPoE_frame.Code], PPPoE_PADS
  106.         je      pads
  107.  
  108.         cmp     [buffer + PPPoE_frame.Code], PPPoE_PADT
  109.         je      padt
  110.  
  111.         jmp     mainloop
  112.  
  113. pado:
  114.  
  115.         push    str2
  116.         call    [con_write_asciiz]
  117.  
  118.         lea     esi, [buffer + ETH_frame.SrcMac]                ; source mac -> dest mac
  119.         lea     edi, [buffer + ETH_frame.DestMac]
  120.         movsw
  121.         movsd
  122.  
  123.         mov     byte [buffer + PPPoE_frame.Code], PPPoE_PADR    ; change packet type to PADR
  124.  
  125.         mov     al, byte [buffer + PPPoE_frame.Length + 1]      ; get packet size
  126.         mov     ah, byte [buffer + PPPoE_frame.Length + 0]
  127.         movzx   esi, ax
  128.         add     esi, sizeof.PPPoE_frame
  129.  
  130.         mcall   send, [socketnum], buffer, , 0  ; now send it!
  131.  
  132.         jmp     mainloop
  133.  
  134.  
  135. pads:
  136.  
  137.         push    str3
  138.         call    [con_write_asciiz]
  139.  
  140.         mov     edx, dword [buffer + ETH_frame.SrcMac]                ; source mac -> dest mac
  141.         mov      si, word [buffer + ETH_frame.SrcMac + 4]
  142.         mov     dword [PADT.mac], edx
  143.         mov     word [PADT.mac + 4], si
  144.  
  145.         mov     cx, word [buffer + PPPoE_frame.SessionID]       ; and Session ID
  146.         mov     [PADT.sid], cx
  147.  
  148.         mcall   76, API_PPPOE + 0               ; Start PPPoE session
  149.  
  150.         jmp     mainloop
  151.  
  152. padt:
  153.  
  154.         push    str4
  155.         call    [con_write_asciiz]
  156.  
  157.         mcall   76, API_PPPOE + 1
  158.  
  159. exit:
  160.         mcall   close, [socketnum]
  161.         mcall   -1
  162.  
  163.  
  164. close_conn:
  165.  
  166.         mcall   send, [socketnum], PADT, 14 + 6, 0
  167.         jmp     exit
  168.  
  169. ; data
  170. title   db      'PPPoE',0
  171. str1    db      'Sending PADI',13,10,0
  172. str2    db      'Got PADO',13,10,'Sending PADR',13,10,0
  173. str3    db      'Got PADS',13,10,'starting PPPoE session',13,10,0
  174. str4    db      'Got PADT - connection terminated by Access Concentrator',13,10,0
  175.  
  176.  
  177. PADI:
  178.         dp      -1              ; dest mac
  179.         dp      0               ; source mac (overwritten by kernel)
  180.         dw      0               ; type       (overwritten by kernel)
  181.  
  182.         db      0x11
  183.         db      PPPoE_PADI
  184.         dw      0               ; session ID
  185.         dw      20 shl 8
  186.  
  187.         dw      TAG_SERVICE_NAME
  188.         dw      0x0000
  189.  
  190.         dw      TAG_HOST_UNIQ
  191.         dw      0x0c00          ; 12 bytes long
  192.         dd      0xdead          ; some random id
  193.         dd      0xbeef
  194.         dd      0x1337
  195.  
  196.         .length = $ - PADI
  197.  
  198. PADT:
  199.  
  200.   .mac  dp      0
  201.         dp      0
  202.         dw      0
  203.  
  204.         db      0x11
  205.         db      PPPoE_PADT
  206.   .sid  dw      0
  207.         dw      0
  208.  
  209.  
  210. ; import
  211. align 4
  212. @IMPORT:
  213.  
  214. library console, 'console.obj'
  215. import  console,        \
  216.         con_start,      'START',        \
  217.         con_init,       'con_init',     \
  218.         con_write_asciiz,       'con_write_asciiz',     \
  219.         con_exit,       'con_exit',     \
  220.         con_gets,       'con_gets',\
  221.         con_cls,        'con_cls',\
  222.         con_getch2,     'con_getch2',\
  223.         con_set_cursor_pos, 'con_set_cursor_pos',\
  224.         con_write_string, 'con_write_string',\
  225.         con_get_flags,  'con_get_flags'
  226.  
  227.  
  228. i_end:
  229.  
  230. socketnum       dd ?
  231. buffer          rb 4096
  232.                 rb 4096    ; stack
  233. mem:
  234.