void dnslkup_request(byte *buf,const prog_char *progmem_hostname) { byte i,lenpos,lencnt; char c; haveDNSanswer=0; dns_ansError=0; dnstid_l++; // increment for next request, finally wrap send_udp_prepare(buf,(DNSCLIENT_SRC_PORT_H<<8)|(dnstid_l&0xff),dnsip,53); buf[UDP_DATA_P+1]=dnstid_l; buf[UDP_DATA_P+2]=1; // flags, standard recursive query i=3; while(i<10) buf[UDP_DATA_P+i++]=0; buf[UDP_DATA_P+5]=1; // 1 question lenpos=12; i=13; lencnt=0; while ((c = pgm_read_byte(progmem_hostname++))) { if (c=='.'){ buf[UDP_DATA_P+lenpos]=lencnt; lencnt=0; lenpos=i; } buf[UDP_DATA_P+i]=c; lencnt++; i++; } buf[UDP_DATA_P+lenpos]=lencnt-1; buf[UDP_DATA_P+i++]=0; // terminate with zero, means root domain. buf[UDP_DATA_P+i++]=0; buf[UDP_DATA_P+i++]=1; // type A buf[UDP_DATA_P+i++]=0; buf[UDP_DATA_P+i++]=1; // class IN buf[UDP_DATA_P]=i-12; send_udp_transmit(buf,i); }
int simple_client(eAdrGD* fADRGD,uint8_t* fSostEth,uint8_t* nBlock, uint8_t* fIPAddr,uint8_t* fMACAddr,uint8_t* fPORTNUMBER) { SPI1_Init(); pADRGD=fADRGD; EthSost=fSostEth; EthBlock=nBlock; PORTNUMBER=fPORTNUMBER; MACAddr=fMACAddr; IPAddr=fIPAddr; enc28j60Init(MACAddr); enc28j60PhyWrite(PHLCON,0x476); init_ip_arp_udp_tcp(MACAddr,fIPAddr,*PORTNUMBER); //dat_p=packetloop_arp_icmp_tcp(buf,enc28j60PacketReceive(BUFFER_SIZE, buf)); buf[UDP_DATA_P]='H'; buf[UDP_DATA_P+1]='E'; buf[UDP_DATA_P+2]='L'; buf[UDP_DATA_P+3]='L'; buf[UDP_DATA_P+4]='O'; buf[UDP_DATA_P+5]=' '; buf[UDP_DATA_P+6]='W'; buf[UDP_DATA_P+7]='O'; buf[UDP_DATA_P+8]='R'; buf[UDP_DATA_P+9]='L'; buf[UDP_DATA_P+10]='D'; //Sockets[0].IP_PHASE=0; //make_tcp_ack_from_any(fbuf); // send ack for http get //make_tcp_ack_with_data(fbuf,plen,0); // send data send_udp_prepare(buf,5001,dis_ip,5001,dis_mac); send_udp_transmit(buf,11); }
// send a DNS udp request packet // See http://www.ietf.org/rfc/rfc1034.txt // and http://www.ietf.org/rfc/rfc1035.txt // //void dnslkup_request(uint8_t *buf,const prog_char *progmem_hostname) void dnslkup_request(uint8_t *buf, uint8_t *hostname) { uint8_t i,lenpos,lencnt; char c; haveDNSanswer=0; dns_ansError=0; dnstid_l++; // increment for next request, finally wrap send_udp_prepare(buf,(DNSCLIENT_SRC_PORT_H<<8)|(dnstid_l&0xff),dnsip,53); // fill tid: //buf[UDP_DATA_P] see below buf[UDP_DATA_P+1]=dnstid_l; buf[UDP_DATA_P+2]=1; // flags, standard recursive query i=3; // most fields are zero, here we zero everything and fill later while(i<10){ buf[UDP_DATA_P+i]=0; i++; } buf[UDP_DATA_P+5]=1; // 1 question // the structure of the domain name // we ask for is always length, string, length, string, ... // for earch portion of the name. // www.twitter.com would become: 3www7twitter3com // // the first len starts at i=12 lenpos=12; i=13; lencnt=1; // while ((c = pgm_read_byte(progmem_hostname++))) { while ((c = *hostname++)) { if (c=='.'){ buf[UDP_DATA_P+lenpos]=lencnt-1; lencnt=0; lenpos=i; } buf[UDP_DATA_P+i]=c; lencnt++; i++; } buf[UDP_DATA_P+lenpos]=lencnt-1; buf[UDP_DATA_P+i]=0; // terminate with zero, means root domain. i++; buf[UDP_DATA_P+i]=0; i++; buf[UDP_DATA_P+i]=1; // type A i++; buf[UDP_DATA_P+i]=0; i++; buf[UDP_DATA_P+i]=1; // class IN i++; // we encode the length into the upper byte of the TID // this way we can easily jump over the query section // of the answer: buf[UDP_DATA_P]=i-12; send_udp_transmit(buf,i); }
// This function writes a basic message template into buf // It processes all fields excluding the options section. // Most fields are initialized with zero. void make_dhcp_message_template(uint8_t *buf,const uint8_t transactionID) { uint8_t i=0; uint8_t allxff[6]={0xff,0xff,0xff,0xff,0xff,0xff}; // all of it can be used as mac, the first 4 can be used as IP send_udp_prepare(buf,DHCP_SRV_DST_PORT,allxff,DHCP_SRV_SRC_PORT,allxff); // source IP is 0.0.0.0, a while loop produces smaller code than memset i=0; while(i<4){ buf[IP_SRC_P +i]=0; i++; } // now fill the bootstrap protocol layer starting at UDP_DATA_P buf[UDP_DATA_P]=1;// message type = boot request buf[UDP_DATA_P+1]=1;// hw type buf[UDP_DATA_P+2]=6;// mac len buf[UDP_DATA_P+i+3]=0;// hops // we use only one byte TIDs, we fill the first byte with 1 and // the rest with transactionID. The first byte is used to // distinguish inital requests from renew requests. buf[UDP_DATA_P+4]=1; i=0; while(i<3){ buf[UDP_DATA_P+i+5]=transactionID; i++; } // we set seconds always to zero // // set my own MAC the rest is empty: // a while loop produces smaller code than memset i=8; // start at 8 while(i<28){ buf[UDP_DATA_P +i]=0; i++; } // own mac (send_udp_prepare did fill it at eth level): i=0; while(i<6){ buf[UDP_DATA_P+i+28]=buf[ETH_SRC_MAC +i]; i++; } // now we need to write 202 bytes of zero // a while loop produces smaller code than memset, don't use memset i=34; // start at 8 while(i<236){ buf[UDP_DATA_P +i]=0; i++; } // the magic cookie has to be: // 99, 130, 83, 99 buf[UDP_DATA_P+MAGIC_COOKIE_P]=0x63; buf[UDP_DATA_P+MAGIC_COOKIE_P+1]=0x82; buf[UDP_DATA_P+MAGIC_COOKIE_P+2]=0x53; buf[UDP_DATA_P+MAGIC_COOKIE_P+3]=0x63; }
//typedef int (*comm_phy_tx_flush_fn)(comm *comm, comm_arg* tx); static int COMM_UDP_tx_flush(comm *comm, comm_arg* tx) { ecomm.tx_ix = UDP_DATA_P; if (ETH_SPI_tx_free()) { u16_t comm_len = tx->len + 4; if (tx->flags & COMM_STAT_ALERT_BIT) { send_udp_prepare(ecomm.tx_frame, 0xcafe, (u8_t*)COMM_UDP_IP_BROADCAST, 0xcafe); } else { send_udp_prepare(ecomm.tx_frame, 0xcafe, &ecomm.cur_server.ip[0], 0xcafe); } send_udp_finalize(ecomm.tx_frame, comm_len); if (ETH_SPI_send(ecomm.tx_frame, UDP_HEADER_LEN+IP_HEADER_LEN+ETH_HEADER_LEN + comm_len, 0)) { DBG(D_COMM, D_DEBUG, "COMM UDP sent frame %i bytes\n", UDP_HEADER_LEN+IP_HEADER_LEN+ETH_HEADER_LEN+comm_len); return R_COMM_OK; } else { DBG(D_COMM, D_DEBUG, "COMM UDP send frame %i bytes FAIL\n", UDP_HEADER_LEN+IP_HEADER_LEN+ETH_HEADER_LEN+comm_len); return R_COMM_PHY_FAIL; } } else { return R_COMM_PHY_FAIL; } }
/* UDP message length calculation */ xt = ((buf[UDP_LEN_H_P]<<8)+buf[UDP_LEN_L_P])-8; #if 1 if(coap_parse(&pkt,buf+UDP_DATA_P,xt) != 0) { dbg(PSTR("\r\n> Bad coap packet\r\n")); } else { coap_handle_req(&scratch_buf, &pkt, &rsppkt); xt = sizeof(response); coap_build(response, &xt, &rsppkt); make_udp_reply_from_request(buf,response,xt,COAPPORT); } #endif } clear_flag(new_packet,coap_server_flag); } PT_END(pt); }/*---------------------------------------------------------------------------*/ static PT_THREAD(udp_broadcast_thread(struct pt *pt)) { PT_BEGIN(pt); while(1) { /* TODO: Add debouncing ... */ PT_WAIT_UNTIL(pt,btn1_pressed()); PT_WAIT_UNTIL(pt,!btn1_pressed()); send_udp_prepare(buf,broadcastport,broadcastip,broadcastport,broadcastmac); buf[UDP_DATA_P+0] = 'h'; buf[UDP_DATA_P+1] = 'e'; buf[UDP_DATA_P+2] = 'l'; buf[UDP_DATA_P+3] = 'l'; buf[UDP_DATA_P+4] = 'o'; send_udp_transmit(buf,5); } PT_END(pt); }
// send a DNS udp request packet // See http://www.ietf.org/rfc/rfc1034.txt // and http://www.ietf.org/rfc/rfc1035.txt // gwmac is the internal mac addess of your router // because we use 8.8.8.8 as a DNS server uint8_t dnslkup_request(uint8_t *buf,const char *hostname,const uint8_t *gwmac) { uint8_t i,lenpos,lencnt; haveDNSanswer=0; if(!enc28j60linkup()){ dns_ansError=4; // could not send request, link down return(1); } dns_ansError=0; dnstid_l++; // increment for next request, finally wrap send_udp_prepare(buf,(DNSCLIENT_SRC_PORT_H<<8)|(dnstid_l&0xff),dnsip,53,gwmac); // fill tid: //buf[UDP_DATA_P] see below buf[UDP_DATA_P+1]=dnstid_l; buf[UDP_DATA_P+2]=1; // flags, standard recursive query i=3; // most fields are zero, here we zero everything and fill later while(i<12){ buf[UDP_DATA_P+i]=0; i++; } buf[UDP_DATA_P+5]=1; // 1 question // the structure of the domain name // we ask for is always length, string, length, string, ... // for earch portion of the name. // www.twitter.com would become: 3www7twitter3com // // the first len starts at i=12 lenpos=12; i=13; lencnt=1; // need to start with one as there is no dot before the domain name and the below algorithm assumes lencnt=0 at dot while(*hostname){ if (*hostname=='\0') break; if (*hostname=='.'){ buf[UDP_DATA_P+lenpos]=lencnt-1; // fill the length field lencnt=0; lenpos=i; } buf[UDP_DATA_P+i]=*hostname; lencnt++; i++; hostname++; } buf[UDP_DATA_P+lenpos]=lencnt-1; buf[UDP_DATA_P+i]=0; // terminate with zero, means root domain. i++; buf[UDP_DATA_P+i]=0; i++; buf[UDP_DATA_P+i]=1; // type A i++; buf[UDP_DATA_P+i]=0; i++; buf[UDP_DATA_P+i]=1; // class IN i++; // we encode the length into the upper byte of the TID // this way we can easily jump over the query section // of the answer: buf[UDP_DATA_P]=i-12; send_udp_transmit(buf,i); return(0); }
void EtherShield::ES_send_udp_data(uint8_t *buf,uint16_t dlen,uint16_t source_port, uint8_t *dest_ip, uint16_t dest_port) { send_udp_prepare(buf,source_port, dest_ip, dest_port); send_udp_transmit(buf,dlen); }
void EtherShield::ES_send_udp_data(uint8_t *buf, uint8_t *destmac,uint16_t dlen,uint16_t source_port, uint8_t *dest_ip, uint16_t dest_port) { send_udp_prepare(buf,source_port, dest_ip, dest_port); for(uint8_t i = 0; i< 6; i++ ) buf[ETH_DST_MAC+i] = destmac[i]; send_udp_transmit(buf,dlen); }