Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
M
mongoose
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
esp
mongoose
Commits
36405545
Commit
36405545
authored
Sep 02, 2016
by
Sergey Lyubka
Committed by
Cesanta Bot
Sep 02, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add DNS server example
PUBLISHED_FROM=0d71533fd475c152c29fe304ca95559512a6059d
parent
12437fd7
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
71 additions
and
1 deletion
+71
-1
server_example.md
docs/dns/server_example.md
+69
-1
server_example.md
docs/http/server_example.md
+2
-0
No files found.
docs/dns/server_example.md
View file @
36405545
...
...
@@ -2,4 +2,72 @@
title
:
DNS server example
---
TBD
To create a DNS server, follow this pattern:
1.
Create a listening UDP connection by calling
`mg_bind()`
or
`mg_bind_opt()`
2.
Call
`mg_set_protocol_dns()`
for that listening connection.
That attaches a built-in DNS event handler which parses incoming
data and triggers DNS-specific events.
3.
Create an event handler function.
Here is an example of a simpe DNS server. It is a captive DNS server, meaning
that it replies with the same IP address on all queries, regardless of what
exactly host name is being resolved. Error checking is omitted for
the sake of clarity:
```
c
#include "mongoose.h"
#include <stdio.h>
static
const
char
*
s_listening_addr
=
"udp://:5353"
;
static
void
ev_handler
(
struct
mg_connection
*
nc
,
int
ev
,
void
*
ev_data
)
{
struct
mg_dns_message
*
msg
;
struct
mg_dns_resource_record
*
rr
;
struct
mg_dns_reply
reply
;
int
i
;
switch
(
ev
)
{
case
MG_DNS_MESSAGE
:
{
struct
mbuf
reply_buf
;
mbuf_init
(
&
reply_buf
,
512
);
msg
=
(
struct
mg_dns_message
*
)
ev_data
;
reply
=
mg_dns_create_reply
(
&
reply_buf
,
msg
);
for
(
i
=
0
;
i
<
msg
->
num_questions
;
i
++
)
{
char
rname
[
256
];
rr
=
&
msg
->
questions
[
i
];
mg_dns_uncompress_name
(
msg
,
&
rr
->
name
,
rname
,
sizeof
(
rname
)
-
1
);
fprintf
(
stdout
,
"Q type %d name %s
\n
"
,
rr
->
rtype
,
rname
);
if
(
rr
->
rtype
==
MG_DNS_A_RECORD
)
{
in_addr_t
addr
=
inet_addr
(
"127.0.0.1"
);
mg_dns_reply_record
(
&
reply
,
rr
,
NULL
,
rr
->
rtype
,
10
,
&
addr
,
4
);
}
}
mg_dns_send_reply
(
nc
,
&
reply
);
nc
->
flags
|=
MG_F_SEND_AND_CLOSE
;
mbuf_free
(
&
reply_buf
);
break
;
}
}
}
int
main
(
int
argc
,
char
*
argv
[])
{
struct
mg_mgr
mgr
;
struct
mg_connection
*
c
;
mg_mgr_init
(
&
mgr
,
NULL
);
c
=
mg_bind
(
&
mgr
,
s_listening_addr
,
ev_handler
);
mg_set_protocol_dns
(
c
);
for
(;;)
{
mg_mgr_poll
(
&
mgr
,
1000
);
}
mg_mgr_free
(
&
mgr
);
return
0
;
}
```
See full
[
Captive DNS server example
](
https://github.com/cesanta/mongoose/tree/master/examples/captive_dns_server
)
.
docs/http/server_example.md
View file @
36405545
...
...
@@ -51,3 +51,5 @@ int main(void) {
return
0
;
}
```
See full
[
HTTP server example
](
https://github.com/cesanta/mongoose/tree/master/examples/simplest_web_server
)
.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment