How to add the mDNS is RTOS SDK?
Posted: Tue Nov 03, 2015 3:43 am
Hi ,all
does anybody success adding mdns in ESP8266 RTOS SDK???
how could we achieve it ????
i have found a linux example which using a socket。
but i don't know how to change it as a task in ESP8266
does anybody success adding mdns in ESP8266 RTOS SDK???
how could we achieve it ????
i have found a linux example which using a socket。
but i don't know how to change it as a task in ESP8266
Code: Select all
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#define PORT 53
#define MSG_SIZE 512
void usage(const char* prog)
{
fprintf(stderr,"Usage: %s IP1 IP2 IP3 IP4\n",prog);
fprintf(stderr,"Example:\n %s 192 168 1 1\n",prog);
fprintf(stderr,"Fake DNS returning the same IP address for any DNS request.\n");
}
int main(int argc, char *argv[])
{
struct sockaddr_in addr, server;
char msg[MSG_SIZE];
if(argc!=5)
{
usage(argv[0]);
return 1;
}
int ip1=atoi(argv[1]);
int ip2=atoi(argv[2]);
int ip3=atoi(argv[3]);
int ip4=atoi(argv[4]);
if(ip1<0||ip1>255||ip2<0||ip2>255||
ip3<0||ip3>255||ip4<0||ip4>255) {
usage(argv[0]);
return 1;
}
// socket creation
int sd=socket(AF_INET, SOCK_DGRAM, 0);
if(sd<0) {
fprintf(stderr,"%s: cannot open socket \n",argv[0]);
return 1;
}
// bind local server port
server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl(INADDR_ANY);
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;
}
int len = sizeof(addr);
int flags=0;
while(1) {
// receive message
int n = recvfrom(sd, msg, MSG_SIZE, flags,
(struct sockaddr *) &addr, &len);
if(n<0) {continue;}
// 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++]=ip1;msg[n++]=ip2;msg[n++]=ip3;msg[n++]=ip4; // IP
// Send the answer
sendto(sd,msg,n,flags,(struct sockaddr *)&addr,len);
}
ret