Subversion Repositories HomeAutomation

Rev

Blame | Last modification | View Log | SVN | RSS feed

  1. /*********************************************
  2.  * vim:sw=8:ts=8:si:et
  3.  * To use the above modeline in vim you must have "set modeline" in your .vimrc
  4.  *
  5.  * Author: Guido Socher
  6.  * Copyright: GPL V2
  7.  * See http://www.gnu.org/licenses/gpl.html
  8.  *
  9.  * IP, Arp, UDP and TCP functions.
  10.  *
  11.  * The TCP implementation uses some size optimisations which are valid
  12.  * only if all data can be sent in one single packet. This is however
  13.  * not a big limitation for a microcontroller as you will anyhow use
  14.  * small web-pages. The TCP stack is therefore a SDP-TCP stack (single data packet TCP).
  15.  *
  16.  * Chip type           : ATMEGA88 with ENC28J60
  17.  *********************************************/
  18. #include <avr/io.h>
  19. #include <avr/pgmspace.h>
  20. #include "avr_compat.h"
  21. #include "net.h"
  22. #include "enc28j60.h"
  23.  
  24. static uint8_t wwwport=80;
  25. static uint8_t macaddr[6];
  26. static uint8_t ipaddr[4];
  27. static int16_t info_hdr_len=0;
  28. static int16_t info_data_len=0;
  29. static uint8_t seqnum=0xa; // my initial tcp sequence number
  30.  
  31. // The Ip checksum is calculated over the ip header only starting
  32. // with the header length field and a total length of 20 bytes
  33. // unitl ip.dst
  34. // You must set the IP checksum field to zero before you start
  35. // the calculation.
  36. // len for ip is 20.
  37. //
  38. // For UDP/TCP we do not make up the required pseudo header. Instead we
  39. // use the ip.src and ip.dst fields of the real packet:
  40. // The udp checksum calculation starts with the ip.src field
  41. // Ip.src=4bytes,Ip.dst=4 bytes,Udp header=8bytes + data length=16+len
  42. // In other words the len here is 8 + length over which you actually
  43. // want to calculate the checksum.
  44. // You must set the checksum field to zero before you start
  45. // the calculation.
  46. // len for udp is: 8 + 8 + data length
  47. // len for tcp is: 4+4 + 20 + option len + data length
  48. //
  49. // For more information on how this algorithm works see:
  50. // http://www.netfor2.com/checksum.html
  51. // http://www.msc.uky.edu/ken/cs471/notes/chap3.htm
  52. // The RFC has also a C code example: http://www.faqs.org/rfcs/rfc1071.html
  53. uint16_t checksum(uint8_t *buf, uint16_t len,uint8_t type){
  54.         // type 0=ip
  55.         //      1=udp
  56.         //      2=tcp
  57.         uint32_t sum = 0;
  58.  
  59.         //if(type==0){
  60.         //        // do not add anything
  61.         //}
  62.         if(type==1){
  63.                 sum+=IP_PROTO_UDP_V; // protocol udp
  64.                 // the length here is the length of udp (data+header len)
  65.                 // =length given to this function - (IP.scr+IP.dst length)
  66.                 sum+=len-8; // = real tcp len
  67.         }
  68.         if(type==2){
  69.                 sum+=IP_PROTO_TCP_V;
  70.                 // the length here is the length of tcp (data+header len)
  71.                 // =length given to this function - (IP.scr+IP.dst length)
  72.                 sum+=len-8; // = real tcp len
  73.         }
  74.         // build the sum of 16bit words
  75.         while(len >1){
  76.                 sum += 0xFFFF & (*buf<<8|*(buf+1));
  77.                 buf+=2;
  78.                 len-=2;
  79.         }
  80.         // if there is a byte left then add it (padded with zero)
  81.         if (len){
  82.                 sum += (0xFF & *buf)<<8;
  83.         }
  84.         // now calculate the sum over the bytes in the sum
  85.         // until the result is only 16bit long
  86.         while (sum>>16){
  87.                 sum = (sum & 0xFFFF)+(sum >> 16);
  88.         }
  89.         // build 1's complement:
  90.         return( (uint16_t) sum ^ 0xFFFF);
  91. }
  92.  
  93. // you must call this function once before you use any of the other functions:
  94. void init_ip_arp_udp_tcp(uint8_t *mymac,uint8_t *myip,uint8_t wwwp){
  95.         uint8_t i=0;
  96.         wwwport=wwwp;
  97.         while(i<4){
  98.                 ipaddr[i]=myip[i];
  99.                 i++;
  100.         }
  101.         i=0;
  102.         while(i<6){
  103.                 macaddr[i]=mymac[i];
  104.                 i++;
  105.         }
  106. }
  107.  
  108. uint8_t eth_type_is_arp_and_my_ip(uint8_t *buf,uint16_t len){
  109.         uint8_t i=0;
  110.         //  
  111.         if (len<41){
  112.                 return(0);
  113.         }
  114.         if(buf[ETH_TYPE_H_P] != ETHTYPE_ARP_H_V ||
  115.            buf[ETH_TYPE_L_P] != ETHTYPE_ARP_L_V){
  116.                 return(0);
  117.         }
  118.         while(i<4){
  119.                 if(buf[ETH_ARP_DST_IP_P+i] != ipaddr[i]){
  120.                         return(0);
  121.                 }
  122.                 i++;
  123.         }
  124.         return(1);
  125. }
  126.  
  127. uint8_t eth_type_is_ip_and_my_ip(uint8_t *buf,uint16_t len){
  128.         uint8_t i=0;
  129.         //eth+ip+udp header is 42
  130.         if (len<42){
  131.                 return(0);
  132.         }
  133.         if(buf[ETH_TYPE_H_P]!=ETHTYPE_IP_H_V ||
  134.            buf[ETH_TYPE_L_P]!=ETHTYPE_IP_L_V){
  135.                 return(0);
  136.         }
  137.         if (buf[IP_HEADER_LEN_VER_P]!=0x45){
  138.                 // must be IP V4 and 20 byte header
  139.                 return(0);
  140.         }
  141.         while(i<4){
  142.                 if(buf[IP_DST_P+i]!=ipaddr[i]){
  143.                         return(0);
  144.                 }
  145.                 i++;
  146.         }
  147.         return(1);
  148. }
  149. // make a return eth header from a received eth packet
  150. void make_eth(uint8_t *buf)
  151. {
  152.         uint8_t i=0;
  153.         //
  154.         //copy the destination mac from the source and fill my mac into src
  155.         while(i<6){
  156.                 buf[ETH_DST_MAC +i]=buf[ETH_SRC_MAC +i];
  157.                 buf[ETH_SRC_MAC +i]=macaddr[i];
  158.                 i++;
  159.         }
  160. }
  161. void fill_ip_hdr_checksum(uint8_t *buf)
  162. {
  163.         uint16_t ck;
  164.         // clear the 2 byte checksum
  165.         buf[IP_CHECKSUM_P]=0;
  166.         buf[IP_CHECKSUM_P+1]=0;
  167.         buf[IP_FLAGS_P]=0x40; // don't fragment
  168.         buf[IP_FLAGS_P+1]=0;  // fragement offset
  169.         buf[IP_TTL_P]=64; // ttl
  170.         // calculate the checksum:
  171.         ck=checksum(&buf[IP_P], IP_HEADER_LEN,0);
  172.         buf[IP_CHECKSUM_P]=ck>>8;
  173.         buf[IP_CHECKSUM_P+1]=ck& 0xff;
  174. }
  175.  
  176. // make a return ip header from a received ip packet
  177. void make_ip(uint8_t *buf)
  178. {
  179.         uint8_t i=0;
  180.         while(i<4){
  181.                 buf[IP_DST_P+i]=buf[IP_SRC_P+i];
  182.                 buf[IP_SRC_P+i]=ipaddr[i];
  183.                 i++;
  184.         }
  185.         fill_ip_hdr_checksum(buf);
  186. }
  187.  
  188. // make a return tcp header from a received tcp packet
  189. // rel_ack_num is how much we must step the seq number received from the
  190. // other side. We do not send more than 255 bytes of text (=data) in the tcp packet.
  191. // If mss=1 then mss is included in the options list
  192. //
  193. // After calling this function you can fill in the first data byte at TCP_OPTIONS_P+4
  194. // If cp_seq=0 then an initial sequence number is used (should be use in synack)
  195. // otherwise it is copied from the packet we received
  196. void make_tcphead(uint8_t *buf,uint16_t rel_ack_num,uint8_t mss,uint8_t cp_seq)
  197. {
  198.         uint8_t i=0;
  199.         uint8_t tseq;
  200.         while(i<2){
  201.                 buf[TCP_DST_PORT_H_P+i]=buf[TCP_SRC_PORT_H_P+i];
  202.                 buf[TCP_SRC_PORT_H_P+i]=0; // clear source port
  203.                 i++;
  204.         }
  205.         // set source port  (http):
  206.         buf[TCP_SRC_PORT_L_P]=wwwport;
  207.         i=4;
  208.         // sequence numbers:
  209.         // add the rel ack num to SEQACK
  210.         while(i>0){
  211.                 rel_ack_num=buf[TCP_SEQ_H_P+i-1]+rel_ack_num;
  212.                 tseq=buf[TCP_SEQACK_H_P+i-1];
  213.                 buf[TCP_SEQACK_H_P+i-1]=0xff&rel_ack_num;
  214.                 if (cp_seq){
  215.                         // copy the acknum sent to us into the sequence number
  216.                         buf[TCP_SEQ_H_P+i-1]=tseq;
  217.                 }else{
  218.                         buf[TCP_SEQ_H_P+i-1]= 0; // some preset vallue
  219.                 }
  220.                 rel_ack_num=rel_ack_num>>8;
  221.                 i--;
  222.         }
  223.         if (cp_seq==0){
  224.                 // put inital seq number
  225.                 buf[TCP_SEQ_H_P+0]= 0;
  226.                 buf[TCP_SEQ_H_P+1]= 0;
  227.                 // we step only the second byte, this allows us to send packts
  228.                 // with 255 bytes or 512 (if we step the initial seqnum by 2)
  229.                 buf[TCP_SEQ_H_P+2]= seqnum;
  230.                 buf[TCP_SEQ_H_P+3]= 0;
  231.                 // step the inititial seq num by something we will not use
  232.                 // during this tcp session:
  233.                 seqnum+=2;
  234.         }
  235.         // zero the checksum
  236.         buf[TCP_CHECKSUM_H_P]=0;
  237.         buf[TCP_CHECKSUM_L_P]=0;
  238.  
  239.         // The tcp header length is only a 4 bit field (the upper 4 bits).
  240.         // It is calculated in units of 4 bytes.
  241.         // E.g 24 bytes: 24/4=6 => 0x60=header len field
  242.         //buf[TCP_HEADER_LEN_P]=(((TCP_HEADER_LEN_PLAIN+4)/4)) <<4; // 0x60
  243.         if (mss){
  244.                 // the only option we set is MSS to 1408:
  245.                 // 1408 in hex is 0x580
  246.                 buf[TCP_OPTIONS_P]=2;
  247.                 buf[TCP_OPTIONS_P+1]=4;
  248.                 buf[TCP_OPTIONS_P+2]=0x05;
  249.                 buf[TCP_OPTIONS_P+3]=0x80;
  250.                 // 24 bytes:
  251.                 buf[TCP_HEADER_LEN_P]=0x60;
  252.         }else{
  253.                 // no options:
  254.                 // 20 bytes:
  255.                 buf[TCP_HEADER_LEN_P]=0x50;
  256.         }
  257. }
  258.  
  259. void make_arp_answer_from_request(uint8_t *buf)
  260. {
  261.         uint8_t i=0;
  262.         //
  263.         make_eth(buf);
  264.         buf[ETH_ARP_OPCODE_H_P]=ETH_ARP_OPCODE_REPLY_H_V;
  265.         buf[ETH_ARP_OPCODE_L_P]=ETH_ARP_OPCODE_REPLY_L_V;
  266.         // fill the mac addresses:
  267.         while(i<6){
  268.                 buf[ETH_ARP_DST_MAC_P+i]=buf[ETH_ARP_SRC_MAC_P+i];
  269.                 buf[ETH_ARP_SRC_MAC_P+i]=macaddr[i];
  270.                 i++;
  271.         }
  272.         i=0;
  273.         while(i<4){
  274.                 buf[ETH_ARP_DST_IP_P+i]=buf[ETH_ARP_SRC_IP_P+i];
  275.                 buf[ETH_ARP_SRC_IP_P+i]=ipaddr[i];
  276.                 i++;
  277.         }
  278.         // eth+arp is 42 bytes:
  279.         enc28j60PacketSend(42,buf);
  280. }
  281.  
  282. void make_echo_reply_from_request(uint8_t *buf,uint16_t len)
  283. {
  284.         make_eth(buf);
  285.         make_ip(buf);
  286.         buf[ICMP_TYPE_P]=ICMP_TYPE_ECHOREPLY_V;
  287.         // we changed only the icmp.type field from request(=8) to reply(=0).
  288.         // we can therefore easily correct the checksum:
  289.         if (buf[ICMP_CHECKSUM_P] > (0xff-0x08)){
  290.                 buf[ICMP_CHECKSUM_P+1]++;
  291.         }
  292.         buf[ICMP_CHECKSUM_P]+=0x08;
  293.         //
  294.         enc28j60PacketSend(len,buf);
  295. }
  296.  
  297. // you can send a max of 220 bytes of data
  298. void make_udp_reply_from_request(uint8_t *buf,char *data,uint8_t datalen,uint16_t port)
  299. {
  300.         uint8_t i=0;
  301.         uint16_t ck;
  302.         make_eth(buf);
  303.         if (datalen>220){
  304.                 datalen=220;
  305.         }
  306.         // total length field in the IP header must be set:
  307.         buf[IP_TOTLEN_H_P]=0;
  308.         buf[IP_TOTLEN_L_P]=IP_HEADER_LEN+UDP_HEADER_LEN+datalen;
  309.         make_ip(buf);
  310.         buf[UDP_DST_PORT_H_P]=port>>8;
  311.         buf[UDP_DST_PORT_L_P]=port & 0xff;
  312.         // source port does not matter and is what the sender used.
  313.         // calculte the udp length:
  314.         buf[UDP_LEN_H_P]=0;
  315.         buf[UDP_LEN_L_P]=UDP_HEADER_LEN+datalen;
  316.         // zero the checksum
  317.         buf[UDP_CHECKSUM_H_P]=0;
  318.         buf[UDP_CHECKSUM_L_P]=0;
  319.         // copy the data:
  320.         while(i<datalen){
  321.                 buf[UDP_DATA_P+i]=data[i];
  322.                 i++;
  323.         }
  324.         ck=checksum(&buf[IP_SRC_P], 16 + datalen,1);
  325.         buf[UDP_CHECKSUM_H_P]=ck>>8;
  326.         buf[UDP_CHECKSUM_L_P]=ck& 0xff;
  327.         enc28j60PacketSend(UDP_HEADER_LEN+IP_HEADER_LEN+ETH_HEADER_LEN+datalen,buf);
  328. }
  329.  
  330. void make_tcp_synack_from_syn(uint8_t *buf)
  331. {
  332.         uint16_t ck;
  333.         make_eth(buf);
  334.         // total length field in the IP header must be set:
  335.         // 20 bytes IP + 24 bytes (20tcp+4tcp options)
  336.         buf[IP_TOTLEN_H_P]=0;
  337.         buf[IP_TOTLEN_L_P]=IP_HEADER_LEN+TCP_HEADER_LEN_PLAIN+4;
  338.         make_ip(buf);
  339.         buf[TCP_FLAGS_P]=TCP_FLAGS_SYNACK_V;
  340.         make_tcphead(buf,1,1,0);
  341.         // calculate the checksum, len=8 (start from ip.src) + TCP_HEADER_LEN_PLAIN + 4 (one option: mss)
  342.         ck=checksum(&buf[IP_SRC_P], 8+TCP_HEADER_LEN_PLAIN+4,2);
  343.         buf[TCP_CHECKSUM_H_P]=ck>>8;
  344.         buf[TCP_CHECKSUM_L_P]=ck& 0xff;
  345.         // add 4 for option mss:
  346.         enc28j60PacketSend(IP_HEADER_LEN+TCP_HEADER_LEN_PLAIN+4+ETH_HEADER_LEN,buf);
  347. }
  348.  
  349. // get a pointer to the start of tcp data in buf
  350. // Returns 0 if there is no data
  351. // You must call init_len_info once before calling this function
  352. uint16_t get_tcp_data_pointer(void)
  353. {
  354.         if (info_data_len){
  355.                 return((uint16_t)TCP_SRC_PORT_H_P+info_hdr_len);
  356.         }else{
  357.                 return(0);
  358.         }
  359. }
  360.  
  361. // do some basic length calculations and store the result in static varibales
  362. void init_len_info(uint8_t *buf)
  363. {
  364.         info_data_len=(buf[IP_TOTLEN_H_P]<<8)|(buf[IP_TOTLEN_L_P]&0xff);
  365.         info_data_len-=IP_HEADER_LEN;
  366.         info_hdr_len=(buf[TCP_HEADER_LEN_P]>>4)*4; // generate len in bytes;
  367.         info_data_len-=info_hdr_len;
  368.         if (info_data_len<=0){
  369.                 info_data_len=0;
  370.         }
  371. }
  372.  
  373. // fill in tcp data at position pos. pos=0 means start of
  374. // tcp data. Returns the position at which the string after
  375. // this string could be filled.
  376. uint16_t fill_tcp_data_p(uint8_t *buf,uint16_t pos, const prog_char *progmem_s)
  377. {
  378.         char c;
  379.         // fill in tcp data at position pos
  380.         //
  381.         // with no options the data starts after the checksum + 2 more bytes (urgent ptr)
  382.         while ((c = pgm_read_byte(progmem_s++))) {
  383.                 buf[TCP_CHECKSUM_L_P+3+pos]=c;
  384.                 pos++;
  385.         }
  386.         return(pos);
  387. }
  388.  
  389. // fill in tcp data at position pos. pos=0 means start of
  390. // tcp data. Returns the position at which the string after
  391. // this string could be filled.
  392. uint16_t fill_tcp_data(uint8_t *buf,uint16_t pos, const char *s)
  393. {
  394.         // fill in tcp data at position pos
  395.         //
  396.         // with no options the data starts after the checksum + 2 more bytes (urgent ptr)
  397.         while (*s) {
  398.                 buf[TCP_CHECKSUM_L_P+3+pos]=*s;
  399.                 pos++;
  400.                 s++;
  401.         }
  402.         return(pos);
  403. }
  404.  
  405. // Make just an ack packet with no tcp data inside
  406. // This will modify the eth/ip/tcp header
  407. void make_tcp_ack_from_any(uint8_t *buf)
  408. {
  409.         uint16_t j;
  410.         make_eth(buf);
  411.         // fill the header:
  412.         buf[TCP_FLAGS_P]=TCP_FLAGS_ACK_V;
  413.         if (info_data_len==0){
  414.                 // if there is no data then we must still acknoledge one packet
  415.                 make_tcphead(buf,1,0,1); // no options
  416.         }else{
  417.                 make_tcphead(buf,info_data_len,0,1); // no options
  418.         }
  419.  
  420.         // total length field in the IP header must be set:
  421.         // 20 bytes IP + 20 bytes tcp (when no options)
  422.         j=IP_HEADER_LEN+TCP_HEADER_LEN_PLAIN;
  423.         buf[IP_TOTLEN_H_P]=j>>8;
  424.         buf[IP_TOTLEN_L_P]=j& 0xff;
  425.         make_ip(buf);
  426.         // calculate the checksum, len=8 (start from ip.src) + TCP_HEADER_LEN_PLAIN + data len
  427.         j=checksum(&buf[IP_SRC_P], 8+TCP_HEADER_LEN_PLAIN,2);
  428.         buf[TCP_CHECKSUM_H_P]=j>>8;
  429.         buf[TCP_CHECKSUM_L_P]=j& 0xff;
  430.         enc28j60PacketSend(IP_HEADER_LEN+TCP_HEADER_LEN_PLAIN+ETH_HEADER_LEN,buf);
  431. }
  432.  
  433. // you must have called init_len_info at some time before calling this function
  434. // dlen is the amount of tcp data (http data) we send in this packet
  435. // You can use this function only immediately after make_tcp_ack_from_any
  436. // This is because this function will NOT modify the eth/ip/tcp header except for
  437. // length and checksum
  438. void make_tcp_ack_with_data(uint8_t *buf,uint16_t dlen)
  439. {
  440.         uint16_t j;
  441.         // fill the header:
  442.         // This code requires that we send only one data packet
  443.         // because we keep no state information. We must therefore set
  444.         // the fin here:
  445.         buf[TCP_FLAGS_P]=TCP_FLAGS_ACK_V|TCP_FLAGS_PUSH_V|TCP_FLAGS_FIN_V;
  446.  
  447.         // total length field in the IP header must be set:
  448.         // 20 bytes IP + 20 bytes tcp (when no options) + len of data
  449.         j=IP_HEADER_LEN+TCP_HEADER_LEN_PLAIN+dlen;
  450.         buf[IP_TOTLEN_H_P]=j>>8;
  451.         buf[IP_TOTLEN_L_P]=j& 0xff;
  452.         fill_ip_hdr_checksum(buf);
  453.         // zero the checksum
  454.         buf[TCP_CHECKSUM_H_P]=0;
  455.         buf[TCP_CHECKSUM_L_P]=0;
  456.         // calculate the checksum, len=8 (start from ip.src) + TCP_HEADER_LEN_PLAIN + data len
  457.         j=checksum(&buf[IP_SRC_P], 8+TCP_HEADER_LEN_PLAIN+dlen,2);
  458.         buf[TCP_CHECKSUM_H_P]=j>>8;
  459.         buf[TCP_CHECKSUM_L_P]=j& 0xff;
  460.         enc28j60PacketSend(IP_HEADER_LEN+TCP_HEADER_LEN_PLAIN+dlen+ETH_HEADER_LEN,buf);
  461. }
  462.  
  463. /* end of ip_arp_udp.c */
  464.