Subversion Repositories NedoOS

Rev

Rev 73 | Rev 235 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download

  1. httphostname=DISKBUF+0x400
  2. dnsbuf=DISKBUF+0x0600
  3.  
  4. IPPROTO_TCP EQU 6
  5. IPPROTO_UDP EQU 17
  6.  
  7. AF_UNSPEC EQU 0
  8. AF_INET EQU 2
  9. AF_INET6 EQU 23
  10.  
  11. SOCK_STREAM EQU 0x01    ;tcp/ip
  12. SOCK_DGRAM      EQU 0x03                ;udp/ip
  13.  
  14. SHUT_RDWR               EQU 2
  15. ERR_INTR                EQU 4
  16. ERR_NFILE               EQU 23
  17. ERR_ALREADY     EQU 37
  18. ERR_NOTSOCK     EQU 38
  19. ERR_EMSGSIZE    EQU 40    ;/* Message too long */
  20. ERR_PROTOTYPE   EQU 41
  21. ERR_AFNOSUPPORT EQU 47
  22. ERR_HOSTUNREACH EQU 65
  23. ERR_CONNRESET   EQU 54
  24. ERR_NOTCONN     EQU 57
  25. ;struct sockaddr_in {short sin_family;unsigned short sin_port;
  26. ;       struct in_addr sin_addr;char sin_zero[8];};
  27.  
  28. ;/***************************************/
  29. ;/* The bit of Sn_MR regsiter defintion */
  30. ;/***************************************/
  31. Sn_MR_ALIGN         EQU (1 << 8)             ;< Alignment bit of Sn_MR. */
  32. Sn_MR_MULTI         EQU (1 << 7)             ;< Multicasting bit of Sn_MR. */
  33. Sn_MR_MF            EQU (1 << 6)             ;< MAC filter bit of Sn_MR. */
  34. Sn_MR_IGMPv         EQU (1 << 5)             ;< IGMP version bit of Sn_MR. */
  35. Sn_MR_ND            EQU (1 << 5)             ;< No delayed ack bit of Sn_MR. */
  36. Sn_MR_CLOSE         EQU 0x00                 ;< Protocol bits of Sn_MR. */
  37. Sn_MR_TCP           EQU 0x01                 ;< Protocol bits of Sn_MR. */
  38. Sn_MR_UDP           EQU 0x02                 ;< Protocol bits of Sn_MR. */
  39. Sn_MR_IPRAW         EQU 0x03                 ;< Protocol bits of Sn_MR. */
  40. Sn_MR_MACRAW        EQU 0x04                 ;< Protocol bits of Sn_MR. */
  41. Sn_MR_PPPoE         EQU 0x05                 ;< Protocol bits of Sn_MR. */
  42.  
  43. ;/******************************/
  44. ;/* The values of CR defintion */
  45. ;/******************************/
  46.  
  47. Sn_CR_OPEN          EQU 0x01                 ;< OPEN command value of Sn_CR. */
  48. Sn_CR_LISTEN        EQU 0x02                 ;< LISTEN command value of Sn_CR. */
  49. Sn_CR_CONNECT       EQU 0x04                 ;< CONNECT command value of Sn_CR. */
  50. Sn_CR_DISCON        EQU 0x08                 ;< DISCONNECT command value of Sn_CR. */
  51. Sn_CR_CLOSE         EQU 0x10                 ;< CLOSE command value of Sn_CR. */
  52. Sn_CR_SEND          EQU 0x20                 ;< SEND command value of Sn_CR. */
  53. Sn_CR_SEND_MAC      EQU 0x21                 ;< SEND_MAC command value of Sn_CR. */
  54. Sn_CR_SEND_KEEP     EQU 0x22                 ;< SEND_KEEP command value of Sn_CR */
  55. Sn_CR_RECV          EQU 0x40                 ;< RECV command value of Sn_CR */
  56. Sn_CR_PCON          EQU 0x23                 ;< PCON command value of Sn_CR */
  57. Sn_CR_PDISCON       EQU 0x24                 ;< PDISCON command value of Sn_CR */
  58. Sn_CR_PCR           EQU 0x25                 ;< PCR command value of Sn_CR */
  59. Sn_CR_PCN           EQU 0x26                 ;< PCN command value of Sn_CR */
  60. Sn_CR_PCJ           EQU 0x27                 ;< PCJ command value of Sn_CR */
  61.  
  62. ;/**********************************/
  63. ;/* The values of Sn_SSR defintion */
  64. ;/**********************************/
  65. SOCK_CLOSED         EQU 0x00                 ;< SOCKETn is released */
  66. SOCK_ARP            EQU 0x01                 ;< ARP-request is transmitted in order to acquire destination hardware address. */
  67. SOCK_INIT           EQU 0x13                 ;< SOCKETn is open as TCP mode. */
  68. SOCK_LISTEN         EQU 0x14                 ;< SOCKETn operates as "TCP SERVER" and waits for connection-request (SYN packet) from "TCP CLIENT". */
  69. SOCK_SYNSENT        EQU 0x15                 ;< Connect-request(SYN packet) is transmitted to "TCP SERVER". */
  70. SOCK_SYNRECV        EQU 0x16                 ;< Connect-request(SYN packet) is received from "TCP CLIENT". */
  71. SOCK_ESTABLISHED    EQU 0x17                 ;< TCP connection is established. */
  72. SOCK_FIN_WAIT       EQU 0x18                 ;< SOCKETn is closing. */
  73. SOCK_CLOSING        EQU 0x1A                 ;< SOCKETn is closing. */
  74. SOCK_TIME_WAIT      EQU 0x1B                 ;< SOCKETn is closing. */
  75. SOCK_CLOSE_WAIT     EQU 0x1C                 ;< Disconnect-request(FIN packet) is received from the peer. */
  76. SOCK_LAST_ACK       EQU 0x1D                 ;< SOCKETn is closing. */
  77. SOCK_UDP            EQU 0x22                 ;< SOCKETn is open as UDP mode. */
  78. SOCK_IPRAW          EQU 0x32                 ;< SOCKETn is open as IPRAW mode. */
  79. SOCK_MACRAW         EQU 0x42                 ;< SOCKET0 is open as MACRAW mode. */
  80. SOCK_PPPoE          EQU 0x5F                 ;< SOCKET0 is open as PPPoE mode. */
  81.                
  82.  
  83. openstream_http
  84.         ;display $
  85. ;de=filename (without "http://"), slash always presents
  86. ;out: A!=0 => error
  87.         ex de,hl
  88. openstream_http_hl
  89.         ld de,httphostname
  90.         push de
  91.         call strcopy
  92.         pop hl
  93.         call findslash
  94.         ;call strlen_tobc_keephl
  95.         ;ld a,'/'
  96.         ;cpir ;TODO ser.ver:port
  97.          ;jr nz,$
  98.         push hl ;filename after ser.ver/
  99.         dec hl ;at slash
  100.         ld (hl),0 ;end of httphostname
  101.        
  102. ;httphostname=server name (filename before slash, not including slash)
  103. ;top of stack=filename after slash (after ser.ver/)
  104.        
  105.                 call dns_resolver
  106.                 ld a,l
  107.                 or h
  108.                 jp z,CONNECTIONERROR
  109.                 ld de,host_ia+3
  110.                 ld bc,4
  111.                 ldir
  112. ;create socket:
  113.                 ld de,SOCK_STREAM+(AF_INET<<8)
  114.                 OS_NETSOCKET
  115.                 ld a,l
  116.                 or a
  117.                 jp m,CONNECTIONERROR;?C_EXIT
  118.                 ld (soc1),a
  119.                 LD DE,host_ia
  120.                 OS_NETCONNECT
  121.                  ld a,l ;DimkaM 12.03.2019
  122.                 or a
  123.                 jp p,connect_ok
  124. createsoc_err
  125.                 ld a,(soc1)
  126.                 ld e,0
  127.                 OS_NETSHUTDOWN
  128.                 jp CONNECTIONERROR
  129. connect_ok
  130. ;form GET message in DISKBUF (will be deleted in readstream)
  131.         ld hl,tGET
  132.         ld de,DISKBUF
  133.         call strcopy
  134.         dec de
  135.        
  136.          pop hl ;filename
  137.         call strcopy
  138.         dec de
  139. ;amp.dascene.net: єсЁрЄ№ яЁюсхы т ъюэЎх url:
  140.         dec de
  141.         ld a,(de)
  142.         sub ' '
  143.         jr z,$+3
  144.         inc de
  145.        
  146.         ld hl,tHTTP_host
  147.         call strcopy
  148.         dec de
  149.         ld hl,httphostname
  150.         call strcopy
  151.         dec de
  152.         ld hl,tGETend
  153.         call strcopy ;with terminator
  154.          dec de ;no terminator?
  155.                 ex de,hl
  156.         ld de,0xffff&(-DISKBUF)
  157.         add hl,de
  158.  
  159. ;send message to server:
  160.                 LD      a,(soc1)
  161.                 LD      DE,DISKBUF
  162. ;de=message
  163. ;hl=message size      
  164.         ;jr $
  165.                 OS_WIZNETWRITE
  166.                 bit 7,h
  167.                 jr nz,createsoc_err
  168.        
  169.         ld a,1
  170.         ld (http_firstreadflag),a
  171.        
  172.         xor a ;OK
  173.         ret
  174.        
  175. ;hl = poi to filename in string
  176. ;out: de = after last dot or start
  177. findlastdot
  178.         ld d,h
  179.         ld e,l ;de = after last dot
  180. findlastdot0
  181.         ld a,[hl]
  182.         inc hl
  183.         or a
  184.         ret z
  185.         cp '.'
  186.         jr nz,findlastdot0
  187.         jr findlastdot
  188.  
  189. tlocation
  190.         db "Location: ",0
  191.  
  192. readstream_http
  193.         ;display $
  194.          ld (readstream_http_requestedsize),hl
  195.         add hl,de
  196.         push de ;эрўрыю сєЇхЁр
  197.        
  198. http_firstreadflag=$+1
  199.         ld a,1
  200.         dec a
  201.         jr nz,readstream_http_nofirstread
  202. ;read until cr,lf,cr,lf or EOF or endofbuf
  203.         push hl
  204.         push de
  205.         or a
  206.         sbc hl,de ;ЁрчьхЁ
  207.         jr readstream_http_headlines0
  208. readstream_http_headretry
  209.         push de
  210.         push hl
  211.         call yieldgetkeynolang
  212.         pop hl
  213.         pop de
  214.         cp key_esc
  215.         jp z,readstream_err
  216.  
  217. readstream_http_headlines0
  218.          ld (readstream_http_headlineaddr),de
  219. readstream_http_head0
  220.         push de
  221.         push hl ;ЁрчьхЁ
  222.         ld hl,1
  223.         ld a,(soc1)
  224.         OS_WIZNETREAD
  225.         bit 7,h
  226.          ld a,l
  227.         pop hl
  228.         pop de
  229.         jr nz,readstream_err
  230.          or a
  231.          jr z,readstream_http_head0 ;тфЁєу юЄтхЄ эх єёяхы яЁшщЄш
  232.         dec hl ;ЁрчьхЁ
  233.         ld a,h
  234.         or l
  235.         jr z,readstream_err
  236.         ld b,d
  237.         ld c,e
  238.         inc de
  239.         ld a,(bc)
  240.         cp 0x0a
  241.         jr nz,readstream_http_head0
  242.         dec bc
  243.         ld a,(bc)
  244.         cp 0x0d
  245.         jr nz,readstream_http_head0
  246.         ;dec bc
  247.         ;ld a,(bc)
  248.         ;cp 0x0a
  249.         ;jr nz,readstream_http_head0
  250.         ;dec bc
  251.         ;ld a,(bc)
  252.         ;cp 0x0d
  253.         ;jr nz,readstream_http_head0
  254.        
  255. ;хёыш ёЄЁюър яєёЄр , Єю readstream_http_headq
  256. readstream_http_headlineaddr=$+1
  257.         ld a,(0)
  258.         cp 0x0d
  259.         jr z,readstream_http_headq
  260.        
  261.         push de
  262.         push hl
  263.        
  264.         ;jr $
  265.         ld hl,(readstream_http_headlineaddr)
  266.         ld de,tlocation
  267. ;эрщЄш ёЄЁюъє Location: <url> (хёыш moved temporarily)
  268.         call strcp_tillde0
  269.         ld b,h
  270.         ld c,l
  271.         pop hl
  272.         pop de
  273.         jr nz,readstream_http_headlines0
  274. ;bc=url
  275.         push bc
  276.         call closestream_http
  277.         pop hl
  278.         ld bc,7 ;"http://"
  279.         add hl,bc
  280.         call openstream_http_hl
  281.        
  282.         pop de
  283.         pop hl
  284.        
  285.         pop de ;эрўрыю сєЇхЁр
  286. readstream_http_requestedsize=$+1
  287.         ld hl,0
  288.         ;jr $
  289.         xor a
  290.         ld (http_firstreadflag),a ;яюўхьє-Єю ёЄЁрэшўър, ъєфр яхЁхрфЁхёєхЄ amp.dascene.net, юЄфр╕Є Їрщы схч http чруюыютър!
  291.         jp readstream_http
  292.        
  293.        
  294. readstream_http_headq
  295.         pop de
  296.         pop hl
  297. ;TODO яхЁхфхырЄ№: ўшЄрЄ№ ъръ юс√ўэю, яюЄюь шёърЄ№ чруюыютюъ, юЄЁхчрЄ№ хую, ёфтшэєЄ№ юёЄрЄюъ т эрўрыю сєЇхЁр ш яЁюўшЄрЄ№ х∙╕ ёЄюы№ъю цх
  298.  
  299.         xor a
  300.         ld (http_firstreadflag),a
  301.        
  302. readstream_http_nofirstread
  303.  
  304.         ;jr $
  305. readstream_loop
  306.         push hl ;фюъєфр ўшЄрЄ№
  307.         push de ;Єхъє∙шщ ptr
  308.         or a
  309.         sbc hl,de ;ЁрчьхЁ
  310.         ;ld a,h
  311.         ;or l
  312.         jr z,readstream_err
  313.         LD      a,(soc1)
  314.         OS_WIZNETREAD
  315.         bit 7,h
  316.         jr nz,readstream_err
  317.         pop de ;de=ъєфр ўшЄрыш, hl=ёъюы№ъю яЁюўшЄрыш
  318.         add hl,de
  319.         ex de,hl ;de=Єхъє∙шщ ptr
  320.         pop hl ;фюъєфр ўшЄрЄ№
  321.         jr readstream_loop
  322. readstream_err:
  323.         pop hl
  324.         pop de
  325.        
  326.         pop de ;эрўрыю сєЇхЁр
  327.         or a
  328.         sbc hl,de
  329.          ;jr $
  330.         ret
  331.        
  332.                
  333. closestream_http
  334.                 ld e,0
  335.                 ld a,(soc1)
  336.                 OS_NETSHUTDOWN
  337.         ret
  338.  
  339. host_ia:
  340.         defb 0,0,80,8,8,8,8
  341. ;httpslashcurdir
  342.         db '/'
  343. httpcurdir
  344. ;server/path (without / in the end)
  345.         ds 128
  346.  
  347. soc1
  348.         db 0 ;current socket
  349.  
  350. tGET
  351.         db "GET /",0
  352. tHTTP_host
  353.         db " HTTP/1.0\r\n" ;1.0 DimkaM for nedopc.com
  354.         db "Connection: close\r\n"
  355.         db "Host: ",0
  356. tGETend
  357.         db "\r\n"
  358.         db "User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)\r\n"
  359.         db "\r\n",0
  360.  
  361. ;httpgetstr
  362.         ;defb 'GET /cspr/index.htm HTTP/1.1',13,10
  363.         ;defb 'Host: dimkam.ru',13,10
  364.         ;defb 13,10
  365. dns_resolver:           ;DE-domain name
  366.     ld a,25;3
  367.     ld (dns_err_count),a
  368. dns_err_loop
  369.         ;push de
  370.         ld hl,dns_head
  371.         ld de,dnsbuf
  372.         ld bc,6
  373.         ldir
  374.         ex de,hl
  375.         ld de,dnsbuf+7
  376.         ld (hl),b;0
  377.         ld  c,256-7
  378.         ldir
  379.         ld de,dnsbuf+12
  380.         ld h,d
  381.         ld l,e
  382.         ld bc, httphostname ;pop bc
  383. name_loop:
  384.         inc hl
  385.         ld a,(bc)
  386.         ld (hl),a
  387.         inc bc
  388.         cp '.'
  389.         jr z,is_dot
  390.         or a
  391.         jr nz,name_loop
  392. is_dot:
  393.         sbc hl,de
  394.         ex de,hl
  395.         dec e
  396.         ld (hl),e
  397.         inc e
  398.         add hl,de
  399.         ld d,h
  400.         ld e,l
  401.         or a
  402.         jr nz,name_loop
  403.         inc a
  404.         inc hl
  405.         inc hl
  406.         ld (hl),a
  407.         inc hl
  408.         inc hl
  409.         ld (hl),a
  410.         inc hl
  411.         push hl
  412.        
  413.         ld de,0x0203
  414.         OS_NETSOCKET
  415.         ld a,l
  416.         ld (soc1),a
  417.         or a
  418.         jp m,dns_exiterr
  419.         LD      DE,dns_ia
  420.         OS_NETCONNECT
  421.                  ld a,l ;DimkaM 12.03.2019
  422.         or a
  423.         jp m,dns_exiterr
  424.        
  425.         pop hl
  426.         push hl
  427.         ld de,0xffff&(-dnsbuf)
  428.         add hl,de
  429.         LD      a,(soc1)
  430.         LD      DE,dnsbuf
  431.         OS_WIZNETWRITE
  432.         bit 7,h
  433.         jr nz,dns_exitcode
  434. dns_err_count=$+1
  435.         ld b,25
  436.         jr recv_wait1
  437. recv_wait:
  438.         ;YIELD
  439.         push bc
  440.         call yieldgetkeynolang
  441.         pop bc
  442. recv_wait1:
  443.         push bc
  444.         ld hl,256
  445.         LD      a,(soc1)
  446.         LD      DE,dnsbuf
  447.         OS_WIZNETREAD
  448.         pop bc
  449.         ld a,h
  450.         or l
  451.         jr nz,recv_wait_end
  452.         djnz recv_wait
  453.         ;ld a,54        ;ERR_CONNRESET
  454.         ;ld (errno),a
  455.         jr dns_exiterr
  456. recv_wait_end:
  457.         bit 7,h
  458.         jr nz,dns_exitcode
  459.         ;ld a,65                ;ERR_HOSTUNREACH
  460.         ;ld (errno),a
  461.         ld a,(dnsbuf+3)
  462.         and 0x0f       
  463.         jr nz,dns_exiterr
  464. dns_exitcode:
  465.         LD      a,(soc1)
  466.         LD      E,0
  467.         OS_NETSHUTDOWN
  468.         pop hl
  469. reqpars_l
  470.         inc hl
  471.         inc hl
  472.         inc hl
  473.         ld a,(hl)
  474.         ld de,7
  475.         add hl,de
  476.         ld b,(hl)
  477.         inc hl
  478.         ld c,(hl)
  479.         inc hl
  480.         dec a
  481.         ret z
  482.         cp 4
  483.         jr nz,exiterr1
  484.         add hl,bc
  485.         jr reqpars_l
  486. dns_exiterr:
  487.         pop af
  488.         LD      a,(soc1)
  489.         LD      E,0
  490.         OS_NETSHUTDOWN
  491.     ld a,(dns_err_count)
  492.     add a,a
  493.     ld (dns_err_count),a
  494.     jp nc,dns_err_loop
  495. exiterr1:
  496.     ld hl,0
  497.         ret
  498. dns_head
  499.         defb 0x11,0x22,0x01,0x00,0x00,0x01
  500. dns_ia:
  501.         defb 0,0,53,8,8,8,8
  502.