THREAD(DiscoveryResponder, arg) { UDPSOCKET *sock; DISCOVERY_TELE *dist; u_long raddr; u_short rport; int len; /* Insist on allocating a datagram buffer. */ while ((dist = malloc(sizeof(DISCOVERY_TELE))) == NULL) { NutSleep(1000); } /* Insist on creating a socket. */ while ((sock = NutUdpCreateSocket(disopt.disopt_port)) == NULL) { NutSleep(1000); } /* Nut/Net doesn't provide UDP datagram buffering by default. */ { u_short max_ms = sizeof(DISCOVERY_TELE) * 3; NutUdpSetSockOpt(sock, SO_RCVBUF, &max_ms, sizeof(max_ms)); } /* Create a pseudo random telegram identifier. */ memcpy(&xid, &confnet.cdn_mac[2], sizeof(xid)); xid += NutGetTickCount(); /* Optionally send initial announce datagram. */ if (disopt.disopt_flags & DISF_INITAL_ANN) { /* Variable sleep avoids broadcast storms in large installations. */ NutSleep(500 + (xid & 0x7FF)); if ((len = NutDiscoveryAnnTele(dist)) > 0) { NutUdpSendTo(sock, INADDR_BROADCAST, disopt.disopt_port, dist, len); } } /* Handle discovery telegrams in an endless loop. */ for (;;) { len = NutUdpReceiveFrom(sock, &raddr, &rport, dist, sizeof(DISCOVERY_TELE), 0); /* Updates may be filtered by an IP mask. */ if ((raddr & disopt.disopt_ipmask) == raddr && len > 0 && discovery_callback) { if ((len = discovery_callback(raddr, rport, dist, len)) > 0) { /* Broadcast response if destination is unreachable. */ if ((raddr & confnet.cdn_ip_mask) != (confnet.cdn_ip_addr & confnet.cdn_ip_mask)) { raddr = INADDR_BROADCAST; } NutUdpSendTo(sock, raddr, disopt.disopt_port, dist, len); } } /* Avoid system blocking by datagram storms. */ NutSleep(100); } }
THREAD(syslog_out, __arg) { while(1) // to satisfy no return { if( !syslog_sock ) NutSleep(5000); // Have some data to send if( (syslog_get_pos != put_pos) && syslog_sock ) { char buf[16]; uint16_t len = log_get_part( buf, sizeof(buf), &syslog_get_pos ); NutUdpSendTo( syslog_sock, syslog_server, syslog_port, buf, len ); NutSleep(10); // Don't overflow } NutSleep(100); } }
THREAD(udpReceiveThread, arg) { NH * nh = (NH*)arg; uint8_t data[256]; uint32_t addr; uint16_t port; int i; for(;;) { i = NutUdpReceiveFrom(nh->udpSock, &addr, &port, data, 256, 0); data[i] = '\0'; if (i > 0) { //printf("Udp connection\n"); //printf("%s:%d \n", inet_ntoa(addr), port); //fflush(stdout); //bool doCleanPacketIn = true; snmpPacket* packetIn = snmp_packetalloc(); snmpPacket* packetOut = snmp_packetalloc(); snmp_parse(packetIn, data, i); packetOut->command = SNMP_GET_RESP; packetOut->requestID = packetIn->requestID; packetOut->objectID = packetIn->objectID; long value; // --- vastataan ready to play -viestiin if(packetIn->objectID == 1 && packetIn->command == SNMP_GET_REQ) //READY-TO-PLAY -message { if(nh->readyToPlay == true) value = 1; else value = 0; packetOut->valueType = SNMP_INT; packetOut->valueLength = 1; packetOut->value = &value; packetOut->error = 0x00; snmp_construct(packetOut); NutUdpSendTo(nh->udpSock, addr, port, packetOut->packet, packetOut->length); } // --- odotetaan vastausta ready to play viestiin else if(nh->getOpponentList && packetIn->objectID == 1 && packetIn->command == SNMP_GET_RESP) { uint8_t tmp; tmp = *((uint8_t*)(packetIn->value)); if(tmp == 1) //fill the opponent list, if broadcast is sent { nh->opponentList[nh->numOpponents] = addr; nh->numOpponents++; } } // --- haaste vastaanotettu else if (nh->opponent == 0 && packetIn->objectID == 2 && packetIn->command == SNMP_SET_REQ) { nh->openRequest = true; nh->opponent = addr; nh->requestID = packetIn->requestID; } //haaste vastaanotettu, mutta meillä oli jo haaste päällä else if ( (nh->openRequest || addr != nh->opponent) && packetIn->objectID == 2 && packetIn->command == SNMP_SET_REQ) NH_sendSnmpError(nh, addr, SNMP_PORT, SNMP_GET_RESP, 2, packetOut->requestID); // --- odotetaan vastausta haasteeseen else if( packetIn->objectID == 2 && nh->challengeSend == true && nh->opponent == addr && packetIn->command == SNMP_GET_RESP ) { nh->challengeSend = false; if( packetIn->error != 0x00) nh->opponent = false; else nh->challengeAccepted = true; } // --- odotetaan vastustajan START-TURN else if(packetIn->objectID == 2 && nh->opponent == addr && packetIn->command == SNMP_SET_REQ ) { nh->enemyStartedTurn = true; //printf("NW: enemy started turn\n"); } // --- odotetaan vastustajalta OK-viestiä else if(nh->opponent == addr && packetIn->command == SNMP_GET_RESP && packetIn->objectID == nh->currentOID && packetIn->valueType == SNMP_INT) { if(packetIn->error == 0x00) { nh->enemyOK = true; //printf("NW: Received enemyOK!\n"); } } // --- odotetaan salvoja else if( packetIn->objectID == 3 && nh->opponent == addr && packetIn->command == SNMP_SET_REQ ) { nh->enemySalvoReceived = true; //printf("NW: SALVO received (%d):\n", nh->enemySalvoReceived); // debug uint8_t* ptr = (uint8_t*)packetIn->value; for(i = 0; i < 5; i++) { nh->lastEnemySalvo[i].x = ptr[i*2]; nh->lastEnemySalvo[i].y = ptr[i*2+1]; //printf("%d - %d\n", nh->lastEnemySalvo[i].x, nh->lastEnemySalvo[i].y); } // OK vastaus NH_sendBooleanResponse(nh, 3, true); } // --- Odotetaan salvon tuloksia else if( packetIn->objectID == 4 && nh->opponent == addr && packetIn->command == SNMP_GET_RESP) { //printf("NW: SALVO results received: \n"); // debug uint8_t* ptr = (uint8_t*)packetIn->value; for (i = 0; i < 5; i++) nh->salvoResults[i] = ptr[i]; //debug //for (i = 0; i < 5; i++) //printf("%d: %d\n", i, nh->salvoResults[i]); nh->salvoResultsReceived = true; } // --- odotetaan salvon tuloksien pyyntöä else if(packetIn->objectID == 4 && packetIn->command == SNMP_GET_REQ && nh->opponent == addr) { nh->resultsQueryReceived = true; //printf("NW: enemy asked for salvo results: %d\n", nh->resultsQueryReceived); } // --- chattiviesti: else if(packetIn->objectID == 5 && packetIn->command == SNMP_SET_REQ && packetIn->valueType == SNMP_OCT) { /* uint8_t* ptr = (uint8_t*)packetIn->value; //lisätään viestin alkuun lähettähän ip sprintf(nh->chatMsgs[nh->emptyMsg], inet_ntoa(addr)); strcat(nh->chatMsgs[nh->emptyMsg], ":\n"); strncat(nh->chatMsgs[nh->emptyMsg], message, MAX_MSG_LENGTH - 16); //lisätään viesti listaan sprintf(chatMsgs[nh->emptyMsg], ptr); nh->manageMessages(); */ } else if(packetIn->command == SNMP_GET_REQ) //debug { printf("NW: weird SNMP_GET_REQ received, OID: %d, opponent: %s", packetIn->objectID, inet_ntoa(nh->opponent)); } snmp_free(packetIn); snmp_free(packetOut); } else if (i == 0) { printf ("Udp timeout\n"); } else { printf ("Udp error\n"); } } }
/* * Main application routine. * */ int main(void) { uint32_t baud = 115200; UDPSOCKET *socket; uint32_t ip_addr; uint32_t ip_udp_echo; int rc; int error; int packet_nr; uint16_t length; /* * Initialize the uart device. */ NutRegisterDevice(&DEV_DEBUG, 0, 0); freopen(DEV_DEBUG_NAME, "w", stdout); _ioctl(_fileno(stdout), UART_SETSPEED, &baud); puts("Demo for ICMP support in UDP sockets...\r\n"); #ifdef DEV_ETHER #ifndef NUT_UDP_ICMP_SUPPORT #warning ICMP support for UDP sockets not enabled in the configurator, please enable NUT_UDP_ICMP_SUPPORT puts("ICMP support for UDP sockets not enabled in the configurator\r\n"); puts("Please enable NUT_UDP_ICMP_SUPPORT\r\n"); #endif /* * Register the network device. */ puts("Configuring Ethernet interface"); NutRegisterDevice(&DEV_ETHER, 0, 0); ip_addr = inet_addr(MY_IP); NutNetIfConfig("eth0", my_mac, ip_addr, inet_addr(MY_MASK)); NutIpRouteAdd(0, 0, inet_addr(MY_GATE), &DEV_ETHER); printf("%s ready\r\n", inet_ntoa(ip_addr)); socket = NutUdpCreateSocket(UDP_ECHO_PORT); if (socket == 0) { printf("Could not create UDP socket in port '%d'\r\n", UDP_ECHO_PORT); puts("Demo halted...\r\n"); while (1); } else { printf("Successfully created UDP socket on port '%d'\r\n", UDP_ECHO_PORT); length = UDP_BUFF_SIZE; if (NutUdpSetSockOpt(socket, SO_RCVBUF, &length, sizeof(length))) {; printf("Could not set UDP receive buffer size (%d)\r\n", length); puts("Demo halted...\r\n"); while (1); } } if (NutThreadCreate("RCV", UDPReceiver, (void*)socket, 1024) == 0) { puts("Could not start receiver thread\r\n"); puts("Demo halted...\r\n"); while (1); } else { puts("Receiver thread started\r\n"); } puts("Starting echo test (1 packet / second)\r\n"); ip_udp_echo = inet_addr(UDP_ECHO_IP); packet_nr = 0; while (1) { packet_nr ++; sprintf(send_buffer, "Packet: %d", packet_nr); rc = NutUdpSendTo(socket, ip_udp_echo, UDP_ECHO_PORT, send_buffer, length); printf("--> Sended packet: \"%s\", to %s, rc: %d\r\n", send_buffer, inet_ntoa(ip_udp_echo), rc); if (rc < 0) { uint32_t remote_ip; uint16_t remote_port; error = NutUdpError(socket, &remote_ip, &remote_port); print_udp_icmp_error(remote_ip, remote_port, error); } NutSleep(1000); } #endif /* DEV_ETHER */ return 0; }