//---------------------------------------------------------------------------- //MagicPacket versenden (immer 102 Byte) void wol_request () { unsigned int byte_count = 0; //sync stream erstellen (sechs mal FF) for (; byte_count < 6; byte_count++) { eth_buffer[UDP_DATA_START + byte_count] = 0xFF; } //16 mal die MAC-Adresse anhängen / darauf achten, dass nicht zu viel auf dem buffer landet for (; byte_count < 102 && byte_count < (MTU_SIZE-(UDP_DATA_START));) { for (unsigned char i=0; i < 6; i++) { eth_buffer[UDP_DATA_START + byte_count] = wol_mac[i]; byte_count++; } } create_new_udp_packet(byte_count,WOL_PORT,WOL_PORT,(*(unsigned long*)&wol_bcast_ip[0])); WOL_DEBUG("** MagicPacket gesendet an: %1i.%1i.%1i.%1i (0x%2x-0x%2x-0x%2x-0x%2x-0x%2x-0x%2x) **\r\n", wol_bcast_ip[0],wol_bcast_ip[1],wol_bcast_ip[2],wol_bcast_ip[3], wol_mac[0],wol_mac[1],wol_mac[2],wol_mac[3],wol_mac[4],wol_mac[5]); return; }
//----------------------------------------------------------------------------------- // sending msg to syslog server defined in EEPROM void syslog_send ( char *msg ) { char *packet = (char *)(eth_buffer+UDP_DATA_START); char *pp = packet; int i = 0; SYSLOG_DEBUG("SYSLOG send %s\r\n", msg); (*((unsigned long*)&syslog_server_ip[0])) = para_getip(SYSLOG_IP_EEPROM_STORE,SYSLOG_IP); SYSLOG_DEBUG("Server: %1i.%1i.%1i.%1i\r\n",syslog_server_ip[0],syslog_server_ip[1],syslog_server_ip[2],syslog_server_ip[3]); //Arp-Request senden unsigned long tmp_ip = (*(unsigned long*)&syslog_server_ip[0]); if ( syslog_server_ip[3] != 0xFF ) // quickhack for broadcast address if ( arp_entry_search(tmp_ip) >= MAX_ARP_ENTRY ) //Arp-Request senden? for ( i=0; i<SYSLOG_ARPRETRIES && (arp_request(tmp_ip) != 1); i++ ); if (i>=SYSLOG_ARPRETRIES) { SYSLOG_ERROR("No SYSLOG server!\r\n"); } // building the packet structure *pp++ = '<'; *pp++ = '0'; *pp++ = '>'; strcpy ( pp, msg ); create_new_udp_packet(strlen(packet),SYSLOG_CLIENT_PORT,SYSLOG_SERVER_PORT,tmp_ip); }
// ---------------------------------------------------------------------------- // send an ArtDmx packet void artnet_sendDmxPacket(void) { static unsigned char sequence = 1; struct artnet_dmx *msg; // clear packet buffer for (unsigned int i = 0; i < sizeof(struct artnet_dmx) + artnet_dmxChannels; i++) { //clear eth_buffer to 0 eth_buffer[UDP_DATA_START + i] = 0; } msg = (struct artnet_dmx *)ð_buffer[UDP_DATA_START]; strcpy_P((char*)msg->id,PSTR("Art-Net\0")); msg->opcode = OP_OUTPUT; msg->versionH = 0; msg->version = PROTOCOL_VERSION; msg->sequence = sequence++; if (sequence == 0) { sequence = 1; } msg->physical = 1; msg->universe = ((artnet_subNet << 4) | artnet_inputUniverse1); msg->lengthHi = (artnet_dmxChannels >> 8) & 0xFF; msg->length = artnet_dmxChannels & 0xFF; memcpy(&(msg->dataStart), (unsigned char *)&artnet_dmxUniverse[0], artnet_dmxChannels); create_new_udp_packet(sizeof(struct artnet_dmx) + artnet_dmxChannels, artnet_port, artnet_port, (unsigned long)0xffffffff); }
//---------------------------------------------------------------------------- //Empfang der IP void udp_cmd_get (unsigned char index) { //UDP_CMD_DEBUG("** CMD DATA GET Bytes: %i **\r\n",((UDP_DATA_END_VAR)-(UDP_DATA_START))); struct IP_Header *ip; ip = (struct IP_Header *)ð_buffer[IP_OFFSET]; udp_cmd_pollReplyTarget = ip->IP_Srcaddr; if(strcasestr_P((char*)ð_buffer[UDP_DATA_START],PSTR("CMD IP ")) != 0){ if ((eth_buffer[UDP_DATA_START+11] == myip[0] && eth_buffer[UDP_DATA_START+12] == myip[1] && eth_buffer[UDP_DATA_START+13] == myip[2] && eth_buffer[UDP_DATA_START+14] == myip[3])|| (eth_buffer[UDP_DATA_START+11] == 255 && eth_buffer[UDP_DATA_START+12] == 255 && eth_buffer[UDP_DATA_START+13] == 255 && eth_buffer[UDP_DATA_START+14] == 255)) { //write default port unsigned short tmp = 0x1936; eeprom_write_block(&tmp,(void *)64, 2); //write_eeprom_ip(IP_EEPROM_STORE); if (*((unsigned int*)ð_buffer[UDP_DATA_START+7]) != 0x00000000) { //value ins EEPROM schreiben for (unsigned char count = 0; count<4;count++) { eeprom_busy_wait (); eeprom_write_byte((unsigned char *)(IP_EEPROM_STORE + count),eth_buffer[UDP_DATA_START+7+count]); } } //write_eeprom_netmask(NETMASK_EEPROM_STORE); if (*((unsigned int*)ð_buffer[UDP_DATA_START+15]) != 0x00000000) { //value ins EEPROM schreiben for (unsigned char count = 0; count<4;count++) { eeprom_busy_wait (); eeprom_write_byte((unsigned char *)(NETMASK_EEPROM_STORE + count),eth_buffer[UDP_DATA_START+15+count]); } } (*((unsigned long*)&myip[0])) = MYIP; (*((unsigned long*)&netmask[0])) = NETMASK; //UDP_CMD_DEBUG("My IP: %1i.%1i.%1i.%1i\r\n",myip[0],myip[1],myip[2],myip[3]); //UDP_CMD_DEBUG("MASK %1i.%1i.%1i.%1i\r\n", netmask[0] , netmask[1] , netmask[2] , netmask[3]); //send OK eth_buffer[UDP_DATA_START] = 'O'; eth_buffer[UDP_DATA_START+1] = 'K'; //create_new_udp_packet(2,UDP_CMD_PORT_TX+1392,UDP_CMD_PORT_TX,udp_cmd_pollReplyTarget); create_new_udp_packet(2,UDP_CMD_PORT_TX+3456,UDP_CMD_PORT_TX,(unsigned long)0xffffffff); } } }
// ---------------------------------------------------------------------------- // send an ArtIpProgReply packet void artnet_sendIpProgReply(unsigned long target) { struct artnet_ipprogreply *msg; // clear packet buffer for (unsigned int i = 0; i < sizeof(struct artnet_ipprogreply); i++) { //clear eth_buffer to 0 eth_buffer[UDP_DATA_START + i] = 0; } msg = (struct artnet_ipprogreply *)ð_buffer[UDP_DATA_START]; strcpy_P((char*)msg->id,PSTR("Art-Net\0")); msg->opcode = OP_IPPROGREPLY; msg->versionH = 0; msg->version = PROTOCOL_VERSION; for (unsigned char i = 0; i < 4; i++) { msg->progIp[i] = myip[i]; msg->progSm[i] = netmask[i]; } msg->progPort[0] = (artnet_port >> 8) & 0xff; msg->progPort[1] = artnet_port & 0xff; create_new_udp_packet(sizeof(struct artnet_ipprogreply), artnet_port, artnet_port, target); }
// ---------------------------------------------------------------------------- // send an ArtPollReply packet void artnet_sendPollReply(void) { struct artnet_pollreply *msg; char buffer[LONG_NAME_LENGTH]; // clear packet buffer for (unsigned int i = 0; i < sizeof(struct artnet_pollreply); i++) { //clear eth_buffer to 0 eth_buffer[UDP_DATA_START + i] = 0; } msg = (struct artnet_pollreply *)ð_buffer[UDP_DATA_START]; strcpy_P((char*)msg->id,PSTR("Art-Net\0")); msg->opcode = OP_POLLREPLY; msg->addr.ip[0] = myip[0]; msg->addr.ip[1] = myip[1]; msg->addr.ip[2] = myip[2]; msg->addr.ip[3] = myip[3]; msg->addr.port = artnet_port; msg->versionInfoH = (FIRMWARE_VERSION >> 8) & 0xFF; msg->versionInfo = FIRMWARE_VERSION & 0xFF; msg->subSwitchH = 0; msg->subSwitch = artnet_subNet & 15; msg->oem = OEM_ID; msg->ubeaVersion = 0; msg->status = 0; msg->estaMan = 'S' * 256 + 'K'; strcpy(msg->shortName, artnet_shortName); strcpy(msg->longName, artnet_longName); sprintf(buffer, "#%04X [%04u] AvrArtNode is ready", artnet_status, artnet_pollReplyCounter); strcpy(msg->nodeReport, buffer); msg->numPortsH = 0; msg->numPorts = 1; if (artnet_dmxDirection == 1) { msg->portTypes[0] = PORT_TYPE_DMX_INPUT; } else { msg->portTypes[0] = PORT_TYPE_DMX_OUTPUT; } if (artnet_dmxDirection != 1) { msg->goodInput[0] = (1 << 3); } else { if (artnet_dmxChannels > 0) { msg->goodInput[0] |= (1 << 7); } } msg->goodOutput[0] = (1 << 1); if (artnet_dmxTransmitting == TRUE) { msg->goodOutput[0] |= (1 << 7); } msg->swin[0] = (artnet_subNet & 15) * 16 | (artnet_inputUniverse1 & 15); msg->swout[0] = (artnet_subNet & 15) * 16 | (artnet_outputUniverse1 & 15); msg->style = STYLE_NODE; msg->mac[0] = MYMAC1; msg->mac[1] = MYMAC2; msg->mac[2] = MYMAC3; msg->mac[3] = MYMAC4; msg->mac[4] = MYMAC5; msg->mac[5] = MYMAC6; create_new_udp_packet(sizeof(struct artnet_pollreply)+20, artnet_port, artnet_port, artnet_pollReplyTarget); }
//---------------------------------------------------------------------------- //Sendet DHCP messages an Broadcast void dhcp_message (unsigned char type) { struct dhcp_msg *msg; unsigned char *options; for (unsigned int i=0; i < sizeof (struct dhcp_msg); i++) //clear eth_buffer to 0 { eth_buffer[UDP_DATA_START+i] = 0; } msg = (struct dhcp_msg *)ð_buffer[UDP_DATA_START]; msg->op = 1; // BOOTREQUEST msg->htype = 1; // Ethernet msg->hlen = 6; // Ethernet MAC msg->xid[0] = MYMAC6; //use the MAC as the ID to be unique in the LAN msg->xid[1] = MYMAC5; msg->xid[2] = MYMAC4; msg->xid[3] = MYMAC3; msg->flags = HTONS(0x8000); msg->chaddr[0] = MYMAC1; msg->chaddr[1] = MYMAC2; msg->chaddr[2] = MYMAC3; msg->chaddr[3] = MYMAC4; msg->chaddr[4] = MYMAC5; msg->chaddr[5] = MYMAC6; options = &msg->options[0]; //options *options++ = 99; //magic cookie *options++ = 130; *options++ = 83; *options++ = 99; *options++ = 53; // Option 53: DHCP message type DHCP Discover *options++ = 1; // len = 1 *options++ = type; // 1 = DHCP Discover *options++ = 55; // Option 55: parameter request list *options++ = 3; // len = 3 *options++ = 1; // netmask *options++ = 3; // router *options++ = 6; // dns *options++ = 50; // Option 54: requested IP *options++ = 4; // len = 4 *options++ = myip[0]; *options++ = myip[1]; *options++ = myip[2]; *options++ = myip[3]; switch (type) { case DHCPDISCOVER: dhcp_state = DHCP_STATE_DISCOVER_SENT; DHCP_DEBUG("DISCOVER sent\r\n"); break; case DHCPREQUEST: *options++ = 54; // Option 54: server ID *options++ = 4; // len = 4 *options++ = cache.serv_id[0]; *options++ = cache.serv_id[1]; *options++ = cache.serv_id[2]; *options++ = cache.serv_id[3]; dhcp_state = DHCP_STATE_REQUEST_SENT; DHCP_DEBUG("REQUEST sent\r\n"); break; default: DHCP_DEBUG("Wrong DHCP msg type\r\n"); break; } *options++ = 12; // Option 12: host name *options++ = 8; // len = 8 *options++ = 'M'; *options++ = 'i'; *options++ = 'n'; *options++ = 'i'; *options++ = '-'; *options++ = 'A'; *options++ = 'V'; *options++ = 'R'; *options = 0xff; //end option create_new_udp_packet(sizeof (struct dhcp_msg),DHCP_CLIENT_PORT,DHCP_SERVER_PORT,(unsigned long)0xffffffff); }
//---------------------------------------------------------------------------- //send query to DNS server void dns_query(char *name) { unsigned char *nptr; unsigned char n; unsigned int byte_count; unsigned long tmp_ip; tmp_ip = (*(unsigned long*)&dns_server_ip[0]); if (arp_request(tmp_ip) == 1) // if DNS server is answering to ARP { nptr = ð_buffer[UDP_DATA_START]; *nptr++ = 0x12; //ID h *nptr++ = 0x34; //ID l *nptr++ = 0x01; //Flags h FLAG_RD ->Standard query *nptr++ = 0x00; //Flags l *nptr++ = 0x00; //QDCOUNT h *nptr++ = 0x01; //QDCOUNT l *nptr++ = 0x00; //ANCOUNT h *nptr++ = 0x00; //ANCOUNT l *nptr++ = 0x00; //NSCOUNT h *nptr++ = 0x00; //NSCOUNT l *nptr++ = 0x00; //ARCOUNT h *nptr++ = 0x00; //ARCOUNT l byte_count = 12; --name; /* Convert hostname to query format. */ do { ++name; nptr = ð_buffer[UDP_DATA_START+byte_count]; //remember the first byte byte_count++; for(n = 0; *name != '.' && *name != 0; ++name) //copy the name { eth_buffer[UDP_DATA_START+byte_count] = *name; byte_count++; n++; } *nptr = n; //store the length in the first byte } while(*name != 0); //until name string is finished //end of query: eth_buffer[UDP_DATA_START+byte_count] = 0; //zero string byte_count++; eth_buffer[UDP_DATA_START+byte_count] = 0; //2 bytes QTYPE 1=a host address byte_count++; eth_buffer[UDP_DATA_START+byte_count] = 1; // byte_count++; eth_buffer[UDP_DATA_START+byte_count] = 0; //2 bytes QCLASS 1=Internet byte_count++; eth_buffer[UDP_DATA_START+byte_count] = 1; // byte_count++; create_new_udp_packet(byte_count,DNS_CLIENT_PORT,DNS_SERVER_PORT,tmp_ip); dns_state = DNS_STATE_REQ_SENT; return; } dns_state = DNS_STATE_REQ_ERR; DNS_DEBUG("No DNS server!!\r\n"); }