how to add dns in ESP8266 ( RTOS SDK)
Posted: Fri Nov 06, 2015 7:10 am
hi all
i am want to adding dns server in ESP8266 (RTOS SDK)
my dns socket code is like as below
and i adding TinydnsTask() funtion as a Task in user_init();
but when i runing the code , it was always shut down when some one join in it
xTaskCreate(TinydnsTask, (signed char *)"Server", 256, NULL, 2, NULL);
Can you give me a hand !!!!!
BR!
i am want to adding dns server in ESP8266 (RTOS SDK)
my dns socket code is like as below
Code: Select all
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include "Tinydns.h"
#include <lwip/sockets.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#define PORT 53
#define MSG_SIZE 20
char *msg = NULL;
/*
void usage(const char* prog)
{
printf(stderr, "Usage: %s IP1 IP2 IP3 IP4\n", prog);
printf(stderr, "Example:\n %s 192 168 1 1\n", prog);
printf(stderr, "Fake DNS returning the same IP address for any DNS request.\n");
}
*/
void TinydnsTask(void *pvParameters)
{
struct sockaddr_in addr, server;
// socket creation
int sd = socket(AF_INET, SOCK_DGRAM, 0);
if (sd < 0) {
//printf(stderr, "%s: cannot open socket \n", argv[0]);
//return 1;
asm("break 1,1");
}
printf("TinyDns open socket \n" );
// bind local server port
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;//htonl()
server.sin_port = htons(PORT);
int rc = bind(sd, (struct sockaddr *) &server, sizeof(server));
if (rc < 0) {
//fprintf(stderr,"%s: cannot bind port number %d \n", argv[0],PORT);
//return 1;
asm("break 1,1");
}
printf("TinyDns Bind socket \n");
socklen_t len = sizeof(addr);
int flags = 0;
int RxTimeout = 50;
while (1) {
if (msg == NULL)
{
msg = (char *)malloc(MSG_SIZE);
}
//printf("==========TinyDns wait for receiving msg from socket==========");
int ret = setsockopt(sd, SOL_SOCKET, SO_RCVTIMEO, &RxTimeout, sizeof(int));
if (ret <= 0) {printf("TinyDns set socket failed \n");}
// receive message
int n = recvfrom(sd, msg, MSG_SIZE, flags, (struct sockaddr*)&addr, &len);
//int n = -1;
//vTaskDelay(1000);
if (n <= 0) {continue;}
printf("TinyDns recive msg : %d byte from socket \n",n);
// Same Id
msg[2] = 0x81; msg[3] = 0x80; // Change Opcode and flags
msg[6] = 0; msg[7] = 1; // One answer
msg[8] = 0; msg[9] = 0; // NSCOUNT
msg[10] = 0; msg[11] = 0; // ARCOUNT
// Keep request in message and add answer
msg[n++] = 0xC0; msg[n++] = 0x0C; // Offset to the domain name
msg[n++] = 0x00; msg[n++] = 0x01; // Type 1
msg[n++] = 0x00; msg[n++] = 0x01; // Class 1
msg[n++] = 0x00; msg[n++] = 0x00; msg[n++] = 0x00; msg[n++] = 0x3c; // TTL
msg[n++] = 0x00; msg[n++] = 0x04; // Size --> 4
msg[n++] = 0x12; msg[n++] = 0x34; msg[n++] = 0x56; msg[n++] = 0x78; // IP
// Send the answer
sendto(sd, msg, n, flags, (struct sockaddr *)&addr, len);
if (msg){
free(msg);
msg = NULL;
printf("free msg \n");
}
}
//return 0;
}
and i adding TinydnsTask() funtion as a Task in user_init();
but when i runing the code , it was always shut down when some one join in it
xTaskCreate(TinydnsTask, (signed char *)"Server", 256, NULL, 2, NULL);
Can you give me a hand !!!!!
BR!