llmnr_common.c
Go to the documentation of this file.
1 /**
2  * @file llmnr_common.c
3  * @brief Definitions common to LLMNR client and responder
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2019 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 1.9.2
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL LLMNR_TRACE_LEVEL
33 
34 //Dependencies
35 #include <stdlib.h>
36 #include <string.h>
37 #include <ctype.h>
38 #include "core/net.h"
39 #include "llmnr/llmnr_client.h"
40 #include "llmnr/llmnr_responder.h"
41 #include "llmnr/llmnr_common.h"
42 #include "dns/dns_debug.h"
43 #include "debug.h"
44 
45 //Check TCP/IP stack configuration
46 #if (LLMNR_CLIENT_SUPPORT == ENABLED || LLMNR_RESPONDER_SUPPORT == ENABLED)
47 
48 //LLMNR IPv6 multicast group (ff02::1:3)
50  IPV6_ADDR(0xFF02, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0001, 0x0003);
51 
52 
53 /**
54  * @brief LLMNR related initialization
55  * @param[in] interface Underlying network interface
56  * @return Error code
57  **/
58 
60 {
61  error_t error;
62 
63 #if (IPV4_SUPPORT == ENABLED)
64  //Join the LLMNR IPv4 multicast group
66  //Any error to report?
67  if(error)
68  return error;
69 #endif
70 
71 #if (IPV6_SUPPORT == ENABLED)
72  //Join the LLMNR IPv6 multicast group
74  //Any error to report?
75  if(error)
76  return error;
77 #endif
78 
79  //Callback function to be called when a LLMNR message is received
81  NULL);
82  //Any error to report?
83  if(error)
84  return error;
85 
86  //Successful initialization
87  return NO_ERROR;
88 }
89 
90 
91 /**
92  * @brief Process incoming LLMNR message
93  * @param[in] interface Underlying network interface
94  * @param[in] pseudoHeader UDP pseudo header
95  * @param[in] udpHeader UDP header
96  * @param[in] buffer Multi-part buffer containing the incoming LLMNR message
97  * @param[in] offset Offset to the first byte of the LLMNR message
98  * @param[in] param Callback function parameter (not used)
99  **/
100 
101 void llmnrProcessMessage(NetInterface *interface, const IpPseudoHeader *pseudoHeader,
102  const UdpHeader *udpHeader, const NetBuffer *buffer, size_t offset, void *param)
103 {
104  size_t length;
106 
107  //Retrieve the length of the LLMNR message
108  length = netBufferGetLength(buffer) - offset;
109 
110  //Ensure the LLMNR message is valid
111  if(length < sizeof(LlmnrHeader))
112  return;
114  return;
115 
116  //Point to the LLMNR message header
117  message = netBufferAt(buffer, offset);
118  //Sanity check
119  if(message == NULL)
120  return;
121 
122  //Debug message
123  TRACE_INFO("LLMNR message received (%" PRIuSIZE " bytes)...\r\n", length);
124  //Dump message
126 
127  //LLMNR messages received with an opcode other than zero must be silently
128  //ignored
129  if(message->opcode != DNS_OPCODE_QUERY)
130  return;
131 
132  //LLMNR messages received with non-zero response codes must be silently
133  //ignored
134  if(message->rcode != DNS_RCODE_NO_ERROR)
135  return;
136 
137  //LLMNR query received?
138  if(!message->qr)
139  {
140 #if (LLMNR_RESPONDER_SUPPORT == ENABLED)
141  //Process incoming LLMNR query message
142  llmnrProcessQuery(interface, pseudoHeader, udpHeader, message, length);
143 #endif
144  }
145  //LLMNR response received?
146  else
147  {
148 #if (LLMNR_CLIENT_SUPPORT == ENABLED)
149  //Process incoming LLMNR response message
150  llmnrProcessResponse(interface, pseudoHeader, udpHeader, message, length);
151 #endif
152  }
153 }
154 
155 #endif
TCP/IP stack core.
Debugging facilities.
size_t netBufferGetLength(const NetBuffer *buffer)
Get the actual length of a multi-part buffer.
Definition: net_mem.c:297
uint8_t message[]
Definition: chap.h:152
__start_packed struct @183 Ipv6Addr
IPv6 network address.
Data logging functions for debugging purpose (DNS)
void llmnrProcessQuery(NetInterface *interface, const IpPseudoHeader *pseudoHeader, const UdpHeader *udpHeader, const LlmnrHeader *message, size_t length)
Process LLMNR query message.
#define LLMNR_PORT
Definition: llmnr_common.h:46
__start_packed struct @150 DnsHeader
DNS message header.
error_t ipv4JoinMulticastGroup(NetInterface *interface, Ipv4Addr groupAddr)
Join the specified host group.
Definition: ipv4.c:1186
void * netBufferAt(const NetBuffer *buffer, size_t offset)
Returns a pointer to the data at the specified position.
Definition: net_mem.c:413
#define IPV6_ADDR(a, b, c, d, e, f, g, h)
Definition: ipv6.h:112
__start_packed struct @126 UdpHeader
UDP header.
error_t udpAttachRxCallback(NetInterface *interface, uint16_t port, UdpRxCallback callback, void *param)
Register user callback.
Definition: udp.c:751
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:88
LLMNR responder (Link-Local Multicast Name Resolution)
void llmnrProcessMessage(NetInterface *interface, const IpPseudoHeader *pseudoHeader, const UdpHeader *udpHeader, const NetBuffer *buffer, size_t offset, void *param)
Process incoming LLMNR message.
Definition: llmnr_common.c:101
#define LLMNR_IPV4_MULTICAST_ADDR
Definition: llmnr_common.h:51
void dnsDumpMessage(const DnsHeader *message, size_t length)
Dump DNS message for debugging purpose.
Definition: dns_debug.c:52
#define TRACE_INFO(...)
Definition: debug.h:94
Success.
Definition: error.h:44
#define DNS_MESSAGE_MAX_SIZE
Definition: dns_common.h:45
error_t
Error codes.
Definition: error.h:42
#define PRIuSIZE
Definition: compiler_port.h:78
#define NetInterface
Definition: net.h:36
__start_packed struct @208 LlmnrHeader
LLMNR message header.
IP pseudo header.
Definition: ip.h:78
void llmnrProcessResponse(NetInterface *interface, const IpPseudoHeader *pseudoHeader, const UdpHeader *udpHeader, const LlmnrHeader *message, size_t length)
Process LLMNR response message.
Definition: llmnr_client.c:277
Definitions common to LLMNR client and responder.
LLMNR client (Link-Local Multicast Name Resolution)
error_t llmnrInit(NetInterface *interface)
LLMNR related initialization.
Definition: llmnr_common.c:59
const Ipv6Addr LLMNR_IPV6_MULTICAST_ADDR
Definition: llmnr_common.c:49
uint8_t length
Definition: dtls_misc.h:142
error_t ipv6JoinMulticastGroup(NetInterface *interface, const Ipv6Addr *groupAddr)
Join an IPv6 multicast group.
Definition: ipv6.c:1927