Merge branch 'master' of https://github.com/philippe44/TinySVCmDNS into origin/philippe44
This commit is contained in:
commit
76a79c8c3d
|
@ -0,0 +1,17 @@
|
|||
# Auto detect text files and perform LF normalization
|
||||
* text=auto
|
||||
|
||||
# Custom for Visual Studio
|
||||
*.cs diff=csharp
|
||||
|
||||
# Standard to msysgit
|
||||
*.doc diff=astextplain
|
||||
*.DOC diff=astextplain
|
||||
*.docx diff=astextplain
|
||||
*.DOCX diff=astextplain
|
||||
*.dot diff=astextplain
|
||||
*.DOT diff=astextplain
|
||||
*.pdf diff=astextplain
|
||||
*.PDF diff=astextplain
|
||||
*.rtf diff=astextplain
|
||||
*.RTF diff=astextplain
|
|
@ -0,0 +1,57 @@
|
|||
# Windows image file caches
|
||||
Thumbs.db
|
||||
ehthumbs.db
|
||||
|
||||
# Folder config file
|
||||
Desktop.ini
|
||||
|
||||
# Recycle Bin used on file shares
|
||||
$RECYCLE.BIN/
|
||||
|
||||
# Windows Installer files
|
||||
*.cab
|
||||
*.msi
|
||||
*.msm
|
||||
*.msp
|
||||
|
||||
# Windows shortcuts
|
||||
*.lnk
|
||||
|
||||
# =========================
|
||||
# Operating System Files
|
||||
# =========================
|
||||
|
||||
# OSX
|
||||
# =========================
|
||||
|
||||
.DS_Store
|
||||
.AppleDouble
|
||||
.LSOverride
|
||||
|
||||
# Thumbnails
|
||||
._*
|
||||
|
||||
# Files that might appear in the root of a volume
|
||||
.DocumentRevisions-V100
|
||||
.fseventsd
|
||||
.Spotlight-V100
|
||||
.TemporaryItems
|
||||
.Trashes
|
||||
.VolumeIcon.icns
|
||||
|
||||
# Directories potentially created on remote AFP share
|
||||
.AppleDB
|
||||
.AppleDesktop
|
||||
Network Trash Folder
|
||||
Temporary Items
|
||||
.apdisk
|
||||
|
||||
__history/
|
||||
Win32/
|
||||
*.cb*
|
||||
*.o
|
||||
*.tds
|
||||
*.res
|
||||
*.map
|
||||
*.il*
|
||||
build
|
476
LICENSE.txt
476
LICENSE.txt
|
@ -1,457 +1,25 @@
|
|||
GNU LESSER GENERAL PUBLIC LICENSE
|
||||
Version 2.1, February 1999
|
||||
Copyright (c) 2011, Darell Tan
|
||||
All rights reserved.
|
||||
|
||||
Copyright (C) 1991, 1999 Free Software Foundation, Inc.
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
3. The name of the author may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
[This is the first released version of the Lesser GPL. It also counts
|
||||
as the successor of the GNU Library Public License, version 2, hence
|
||||
the version number 2.1.]
|
||||
|
||||
Preamble
|
||||
|
||||
The licenses for most software are designed to take away your
|
||||
freedom to share and change it. By contrast, the GNU General Public
|
||||
Licenses are intended to guarantee your freedom to share and change
|
||||
free software--to make sure the software is free for all its users.
|
||||
|
||||
This license, the Lesser General Public License, applies to some
|
||||
specially designated software packages--typically libraries--of the
|
||||
Free Software Foundation and other authors who decide to use it. You
|
||||
can use it too, but we suggest you first think carefully about whether
|
||||
this license or the ordinary General Public License is the better
|
||||
strategy to use in any particular case, based on the explanations below.
|
||||
|
||||
When we speak of free software, we are referring to freedom of use,
|
||||
not price. Our General Public Licenses are designed to make sure that
|
||||
you have the freedom to distribute copies of free software (and charge
|
||||
for this service if you wish); that you receive source code or can get
|
||||
it if you want it; that you can change the software and use pieces of
|
||||
it in new free programs; and that you are informed that you can do
|
||||
these things.
|
||||
|
||||
To protect your rights, we need to make restrictions that forbid
|
||||
distributors to deny you these rights or to ask you to surrender these
|
||||
rights. These restrictions translate to certain responsibilities for
|
||||
you if you distribute copies of the library or if you modify it.
|
||||
|
||||
For example, if you distribute copies of the library, whether gratis
|
||||
or for a fee, you must give the recipients all the rights that we gave
|
||||
you. You must make sure that they, too, receive or can get the source
|
||||
code. If you link other code with the library, you must provide
|
||||
complete object files to the recipients, so that they can relink them
|
||||
with the library after making changes to the library and recompiling
|
||||
it. And you must show them these terms so they know their rights.
|
||||
|
||||
We protect your rights with a two-step method: (1) we copyright the
|
||||
library, and (2) we offer you this license, which gives you legal
|
||||
permission to copy, distribute and/or modify the library.
|
||||
|
||||
To protect each distributor, we want to make it very clear that
|
||||
there is no warranty for the free library. Also, if the library is
|
||||
modified by someone else and passed on, the recipients should know
|
||||
that what they have is not the original version, so that the original
|
||||
author's reputation will not be affected by problems that might be
|
||||
introduced by others.
|
||||
|
||||
Finally, software patents pose a constant threat to the existence of
|
||||
any free program. We wish to make sure that a company cannot
|
||||
effectively restrict the users of a free program by obtaining a
|
||||
restrictive license from a patent holder. Therefore, we insist that
|
||||
any patent license obtained for a version of the library must be
|
||||
consistent with the full freedom of use specified in this license.
|
||||
|
||||
Most GNU software, including some libraries, is covered by the
|
||||
ordinary GNU General Public License. This license, the GNU Lesser
|
||||
General Public License, applies to certain designated libraries, and
|
||||
is quite different from the ordinary General Public License. We use
|
||||
this license for certain libraries in order to permit linking those
|
||||
libraries into non-free programs.
|
||||
|
||||
When a program is linked with a library, whether statically or using
|
||||
a shared library, the combination of the two is legally speaking a
|
||||
combined work, a derivative of the original library. The ordinary
|
||||
General Public License therefore permits such linking only if the
|
||||
entire combination fits its criteria of freedom. The Lesser General
|
||||
Public License permits more lax criteria for linking other code with
|
||||
the library.
|
||||
|
||||
We call this license the "Lesser" General Public License because it
|
||||
does Less to protect the user's freedom than the ordinary General
|
||||
Public License. It also provides other free software developers Less
|
||||
of an advantage over competing non-free programs. These disadvantages
|
||||
are the reason we use the ordinary General Public License for many
|
||||
libraries. However, the Lesser license provides advantages in certain
|
||||
special circumstances.
|
||||
|
||||
For example, on rare occasions, there may be a special need to
|
||||
encourage the widest possible use of a certain library, so that it becomes
|
||||
a de-facto standard. To achieve this, non-free programs must be
|
||||
allowed to use the library. A more frequent case is that a free
|
||||
library does the same job as widely used non-free libraries. In this
|
||||
case, there is little to gain by limiting the free library to free
|
||||
software only, so we use the Lesser General Public License.
|
||||
|
||||
In other cases, permission to use a particular library in non-free
|
||||
programs enables a greater number of people to use a large body of
|
||||
free software. For example, permission to use the GNU C Library in
|
||||
non-free programs enables many more people to use the whole GNU
|
||||
operating system, as well as its variant, the GNU/Linux operating
|
||||
system.
|
||||
|
||||
Although the Lesser General Public License is Less protective of the
|
||||
users' freedom, it does ensure that the user of a program that is
|
||||
linked with the Library has the freedom and the wherewithal to run
|
||||
that program using a modified version of the Library.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow. Pay close attention to the difference between a
|
||||
"work based on the library" and a "work that uses the library". The
|
||||
former contains code derived from the library, whereas the latter must
|
||||
be combined with the library in order to run.
|
||||
|
||||
GNU LESSER GENERAL PUBLIC LICENSE
|
||||
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
||||
|
||||
0. This License Agreement applies to any software library or other
|
||||
program which contains a notice placed by the copyright holder or
|
||||
other authorized party saying it may be distributed under the terms of
|
||||
this Lesser General Public License (also called "this License").
|
||||
Each licensee is addressed as "you".
|
||||
|
||||
A "library" means a collection of software functions and/or data
|
||||
prepared so as to be conveniently linked with application programs
|
||||
(which use some of those functions and data) to form executables.
|
||||
|
||||
The "Library", below, refers to any such software library or work
|
||||
which has been distributed under these terms. A "work based on the
|
||||
Library" means either the Library or any derivative work under
|
||||
copyright law: that is to say, a work containing the Library or a
|
||||
portion of it, either verbatim or with modifications and/or translated
|
||||
straightforwardly into another language. (Hereinafter, translation is
|
||||
included without limitation in the term "modification".)
|
||||
|
||||
"Source code" for a work means the preferred form of the work for
|
||||
making modifications to it. For a library, complete source code means
|
||||
all the source code for all modules it contains, plus any associated
|
||||
interface definition files, plus the scripts used to control compilation
|
||||
and installation of the library.
|
||||
|
||||
Activities other than copying, distribution and modification are not
|
||||
covered by this License; they are outside its scope. The act of
|
||||
running a program using the Library is not restricted, and output from
|
||||
such a program is covered only if its contents constitute a work based
|
||||
on the Library (independent of the use of the Library in a tool for
|
||||
writing it). Whether that is true depends on what the Library does
|
||||
and what the program that uses the Library does.
|
||||
|
||||
1. You may copy and distribute verbatim copies of the Library's
|
||||
complete source code as you receive it, in any medium, provided that
|
||||
you conspicuously and appropriately publish on each copy an
|
||||
appropriate copyright notice and disclaimer of warranty; keep intact
|
||||
all the notices that refer to this License and to the absence of any
|
||||
warranty; and distribute a copy of this License along with the
|
||||
Library.
|
||||
|
||||
You may charge a fee for the physical act of transferring a copy,
|
||||
and you may at your option offer warranty protection in exchange for a
|
||||
fee.
|
||||
|
||||
2. You may modify your copy or copies of the Library or any portion
|
||||
of it, thus forming a work based on the Library, and copy and
|
||||
distribute such modifications or work under the terms of Section 1
|
||||
above, provided that you also meet all of these conditions:
|
||||
|
||||
a) The modified work must itself be a software library.
|
||||
|
||||
b) You must cause the files modified to carry prominent notices
|
||||
stating that you changed the files and the date of any change.
|
||||
|
||||
c) You must cause the whole of the work to be licensed at no
|
||||
charge to all third parties under the terms of this License.
|
||||
|
||||
d) If a facility in the modified Library refers to a function or a
|
||||
table of data to be supplied by an application program that uses
|
||||
the facility, other than as an argument passed when the facility
|
||||
is invoked, then you must make a good faith effort to ensure that,
|
||||
in the event an application does not supply such function or
|
||||
table, the facility still operates, and performs whatever part of
|
||||
its purpose remains meaningful.
|
||||
|
||||
(For example, a function in a library to compute square roots has
|
||||
a purpose that is entirely well-defined independent of the
|
||||
application. Therefore, Subsection 2d requires that any
|
||||
application-supplied function or table used by this function must
|
||||
be optional: if the application does not supply it, the square
|
||||
root function must still compute square roots.)
|
||||
|
||||
These requirements apply to the modified work as a whole. If
|
||||
identifiable sections of that work are not derived from the Library,
|
||||
and can be reasonably considered independent and separate works in
|
||||
themselves, then this License, and its terms, do not apply to those
|
||||
sections when you distribute them as separate works. But when you
|
||||
distribute the same sections as part of a whole which is a work based
|
||||
on the Library, the distribution of the whole must be on the terms of
|
||||
this License, whose permissions for other licensees extend to the
|
||||
entire whole, and thus to each and every part regardless of who wrote
|
||||
it.
|
||||
|
||||
Thus, it is not the intent of this section to claim rights or contest
|
||||
your rights to work written entirely by you; rather, the intent is to
|
||||
exercise the right to control the distribution of derivative or
|
||||
collective works based on the Library.
|
||||
|
||||
In addition, mere aggregation of another work not based on the Library
|
||||
with the Library (or with a work based on the Library) on a volume of
|
||||
a storage or distribution medium does not bring the other work under
|
||||
the scope of this License.
|
||||
|
||||
3. You may opt to apply the terms of the ordinary GNU General Public
|
||||
License instead of this License to a given copy of the Library. To do
|
||||
this, you must alter all the notices that refer to this License, so
|
||||
that they refer to the ordinary GNU General Public License, version 2,
|
||||
instead of to this License. (If a newer version than version 2 of the
|
||||
ordinary GNU General Public License has appeared, then you can specify
|
||||
that version instead if you wish.) Do not make any other change in
|
||||
these notices.
|
||||
|
||||
Once this change is made in a given copy, it is irreversible for
|
||||
that copy, so the ordinary GNU General Public License applies to all
|
||||
subsequent copies and derivative works made from that copy.
|
||||
|
||||
This option is useful when you wish to copy part of the code of
|
||||
the Library into a program that is not a library.
|
||||
|
||||
4. You may copy and distribute the Library (or a portion or
|
||||
derivative of it, under Section 2) in object code or executable form
|
||||
under the terms of Sections 1 and 2 above provided that you accompany
|
||||
it with the complete corresponding machine-readable source code, which
|
||||
must be distributed under the terms of Sections 1 and 2 above on a
|
||||
medium customarily used for software interchange.
|
||||
|
||||
If distribution of object code is made by offering access to copy
|
||||
from a designated place, then offering equivalent access to copy the
|
||||
source code from the same place satisfies the requirement to
|
||||
distribute the source code, even though third parties are not
|
||||
compelled to copy the source along with the object code.
|
||||
|
||||
5. A program that contains no derivative of any portion of the
|
||||
Library, but is designed to work with the Library by being compiled or
|
||||
linked with it, is called a "work that uses the Library". Such a
|
||||
work, in isolation, is not a derivative work of the Library, and
|
||||
therefore falls outside the scope of this License.
|
||||
|
||||
However, linking a "work that uses the Library" with the Library
|
||||
creates an executable that is a derivative of the Library (because it
|
||||
contains portions of the Library), rather than a "work that uses the
|
||||
library". The executable is therefore covered by this License.
|
||||
Section 6 states terms for distribution of such executables.
|
||||
|
||||
When a "work that uses the Library" uses material from a header file
|
||||
that is part of the Library, the object code for the work may be a
|
||||
derivative work of the Library even though the source code is not.
|
||||
Whether this is true is especially significant if the work can be
|
||||
linked without the Library, or if the work is itself a library. The
|
||||
threshold for this to be true is not precisely defined by law.
|
||||
|
||||
If such an object file uses only numerical parameters, data
|
||||
structure layouts and accessors, and small macros and small inline
|
||||
functions (ten lines or less in length), then the use of the object
|
||||
file is unrestricted, regardless of whether it is legally a derivative
|
||||
work. (Executables containing this object code plus portions of the
|
||||
Library will still fall under Section 6.)
|
||||
|
||||
Otherwise, if the work is a derivative of the Library, you may
|
||||
distribute the object code for the work under the terms of Section 6.
|
||||
Any executables containing that work also fall under Section 6,
|
||||
whether or not they are linked directly with the Library itself.
|
||||
|
||||
6. As an exception to the Sections above, you may also combine or
|
||||
link a "work that uses the Library" with the Library to produce a
|
||||
work containing portions of the Library, and distribute that work
|
||||
under terms of your choice, provided that the terms permit
|
||||
modification of the work for the customer's own use and reverse
|
||||
engineering for debugging such modifications.
|
||||
|
||||
You must give prominent notice with each copy of the work that the
|
||||
Library is used in it and that the Library and its use are covered by
|
||||
this License. You must supply a copy of this License. If the work
|
||||
during execution displays copyright notices, you must include the
|
||||
copyright notice for the Library among them, as well as a reference
|
||||
directing the user to the copy of this License. Also, you must do one
|
||||
of these things:
|
||||
|
||||
a) Accompany the work with the complete corresponding
|
||||
machine-readable source code for the Library including whatever
|
||||
changes were used in the work (which must be distributed under
|
||||
Sections 1 and 2 above); and, if the work is an executable linked
|
||||
with the Library, with the complete machine-readable "work that
|
||||
uses the Library", as object code and/or source code, so that the
|
||||
user can modify the Library and then relink to produce a modified
|
||||
executable containing the modified Library. (It is understood
|
||||
that the user who changes the contents of definitions files in the
|
||||
Library will not necessarily be able to recompile the application
|
||||
to use the modified definitions.)
|
||||
|
||||
b) Use a suitable shared library mechanism for linking with the
|
||||
Library. A suitable mechanism is one that (1) uses at run time a
|
||||
copy of the library already present on the user's computer system,
|
||||
rather than copying library functions into the executable, and (2)
|
||||
will operate properly with a modified version of the library, if
|
||||
the user installs one, as long as the modified version is
|
||||
interface-compatible with the version that the work was made with.
|
||||
|
||||
c) Accompany the work with a written offer, valid for at
|
||||
least three years, to give the same user the materials
|
||||
specified in Subsection 6a, above, for a charge no more
|
||||
than the cost of performing this distribution.
|
||||
|
||||
d) If distribution of the work is made by offering access to copy
|
||||
from a designated place, offer equivalent access to copy the above
|
||||
specified materials from the same place.
|
||||
|
||||
e) Verify that the user has already received a copy of these
|
||||
materials or that you have already sent this user a copy.
|
||||
|
||||
For an executable, the required form of the "work that uses the
|
||||
Library" must include any data and utility programs needed for
|
||||
reproducing the executable from it. However, as a special exception,
|
||||
the materials to be distributed need not include anything that is
|
||||
normally distributed (in either source or binary form) with the major
|
||||
components (compiler, kernel, and so on) of the operating system on
|
||||
which the executable runs, unless that component itself accompanies
|
||||
the executable.
|
||||
|
||||
It may happen that this requirement contradicts the license
|
||||
restrictions of other proprietary libraries that do not normally
|
||||
accompany the operating system. Such a contradiction means you cannot
|
||||
use both them and the Library together in an executable that you
|
||||
distribute.
|
||||
|
||||
7. You may place library facilities that are a work based on the
|
||||
Library side-by-side in a single library together with other library
|
||||
facilities not covered by this License, and distribute such a combined
|
||||
library, provided that the separate distribution of the work based on
|
||||
the Library and of the other library facilities is otherwise
|
||||
permitted, and provided that you do these two things:
|
||||
|
||||
a) Accompany the combined library with a copy of the same work
|
||||
based on the Library, uncombined with any other library
|
||||
facilities. This must be distributed under the terms of the
|
||||
Sections above.
|
||||
|
||||
b) Give prominent notice with the combined library of the fact
|
||||
that part of it is a work based on the Library, and explaining
|
||||
where to find the accompanying uncombined form of the same work.
|
||||
|
||||
8. You may not copy, modify, sublicense, link with, or distribute
|
||||
the Library except as expressly provided under this License. Any
|
||||
attempt otherwise to copy, modify, sublicense, link with, or
|
||||
distribute the Library is void, and will automatically terminate your
|
||||
rights under this License. However, parties who have received copies,
|
||||
or rights, from you under this License will not have their licenses
|
||||
terminated so long as such parties remain in full compliance.
|
||||
|
||||
9. You are not required to accept this License, since you have not
|
||||
signed it. However, nothing else grants you permission to modify or
|
||||
distribute the Library or its derivative works. These actions are
|
||||
prohibited by law if you do not accept this License. Therefore, by
|
||||
modifying or distributing the Library (or any work based on the
|
||||
Library), you indicate your acceptance of this License to do so, and
|
||||
all its terms and conditions for copying, distributing or modifying
|
||||
the Library or works based on it.
|
||||
|
||||
10. Each time you redistribute the Library (or any work based on the
|
||||
Library), the recipient automatically receives a license from the
|
||||
original licensor to copy, distribute, link with or modify the Library
|
||||
subject to these terms and conditions. You may not impose any further
|
||||
restrictions on the recipients' exercise of the rights granted herein.
|
||||
You are not responsible for enforcing compliance by third parties with
|
||||
this License.
|
||||
|
||||
11. If, as a consequence of a court judgment or allegation of patent
|
||||
infringement or for any other reason (not limited to patent issues),
|
||||
conditions are imposed on you (whether by court order, agreement or
|
||||
otherwise) that contradict the conditions of this License, they do not
|
||||
excuse you from the conditions of this License. If you cannot
|
||||
distribute so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you
|
||||
may not distribute the Library at all. For example, if a patent
|
||||
license would not permit royalty-free redistribution of the Library by
|
||||
all those who receive copies directly or indirectly through you, then
|
||||
the only way you could satisfy both it and this License would be to
|
||||
refrain entirely from distribution of the Library.
|
||||
|
||||
If any portion of this section is held invalid or unenforceable under any
|
||||
particular circumstance, the balance of the section is intended to apply,
|
||||
and the section as a whole is intended to apply in other circumstances.
|
||||
|
||||
It is not the purpose of this section to induce you to infringe any
|
||||
patents or other property right claims or to contest validity of any
|
||||
such claims; this section has the sole purpose of protecting the
|
||||
integrity of the free software distribution system which is
|
||||
implemented by public license practices. Many people have made
|
||||
generous contributions to the wide range of software distributed
|
||||
through that system in reliance on consistent application of that
|
||||
system; it is up to the author/donor to decide if he or she is willing
|
||||
to distribute software through any other system and a licensee cannot
|
||||
impose that choice.
|
||||
|
||||
This section is intended to make thoroughly clear what is believed to
|
||||
be a consequence of the rest of this License.
|
||||
|
||||
12. If the distribution and/or use of the Library is restricted in
|
||||
certain countries either by patents or by copyrighted interfaces, the
|
||||
original copyright holder who places the Library under this License may add
|
||||
an explicit geographical distribution limitation excluding those countries,
|
||||
so that distribution is permitted only in or among countries not thus
|
||||
excluded. In such case, this License incorporates the limitation as if
|
||||
written in the body of this License.
|
||||
|
||||
13. The Free Software Foundation may publish revised and/or new
|
||||
versions of the Lesser General Public License from time to time.
|
||||
Such new versions will be similar in spirit to the present version,
|
||||
but may differ in detail to address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the Library
|
||||
specifies a version number of this License which applies to it and
|
||||
"any later version", you have the option of following the terms and
|
||||
conditions either of that version or of any later version published by
|
||||
the Free Software Foundation. If the Library does not specify a
|
||||
license version number, you may choose any version ever published by
|
||||
the Free Software Foundation.
|
||||
|
||||
14. If you wish to incorporate parts of the Library into other free
|
||||
programs whose distribution conditions are incompatible with these,
|
||||
write to the author to ask for permission. For software which is
|
||||
copyrighted by the Free Software Foundation, write to the Free
|
||||
Software Foundation; we sometimes make exceptions for this. Our
|
||||
decision will be guided by the two goals of preserving the free status
|
||||
of all derivatives of our free software and of promoting the sharing
|
||||
and reuse of software generally.
|
||||
|
||||
NO WARRANTY
|
||||
|
||||
15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO
|
||||
WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
|
||||
EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
|
||||
OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY
|
||||
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
|
||||
LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME
|
||||
THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||
|
||||
16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
|
||||
WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY
|
||||
AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU
|
||||
FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
|
||||
CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
|
||||
LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
|
||||
RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
|
||||
FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
|
||||
SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
|
||||
DAMAGES.
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
|
|
47
Makefile
47
Makefile
|
@ -1,32 +1,31 @@
|
|||
#
|
||||
# Makefile for tinysvcmdns
|
||||
#
|
||||
SRC = .
|
||||
LIBRARY =
|
||||
DEFINES = -DNDEBUG
|
||||
|
||||
CFLAGS += -Wall -pedantic -std=gnu99
|
||||
#CFLAGS += -g
|
||||
CFLAGS += -O2 -DNDEBUG
|
||||
LDLIBS = -lpthread
|
||||
vpath %.c $(SRC)
|
||||
|
||||
BIN=testmdnsd
|
||||
INCLUDE = -I$(SRC)
|
||||
|
||||
LIBTINYSVCMDNS_OBJS = mdns.o mdnsd.o
|
||||
SOURCES = mdns.c mdnsd.c tinysvcmdns.c
|
||||
|
||||
OBJECTS = $(patsubst %.c,$(OBJ)/%.o,$(SOURCES))
|
||||
|
||||
.PHONY: all clean
|
||||
all: $(EXECUTABLE)
|
||||
|
||||
all: $(BIN) libtinysvcmdns.a
|
||||
$(EXECUTABLE): $(OBJECTS)
|
||||
$(CC) $(OBJECTS) $(LIBRARY) $(LDFLAGS) -o $@
|
||||
|
||||
$(OBJECTS): | bin $(OBJ)
|
||||
|
||||
$(OBJ):
|
||||
@mkdir -p $@
|
||||
|
||||
bin:
|
||||
@mkdir -p bin
|
||||
|
||||
$(OBJ)/%.o : %.c
|
||||
$(CC) $(CFLAGS) $(CPPFLAGS) $(INCLUDE) $< -c -o $@
|
||||
|
||||
clean:
|
||||
-$(RM) *.o
|
||||
-$(RM) *.bin
|
||||
-$(RM) mdns
|
||||
-$(RM) $(BIN)
|
||||
-$(RM) libtinysvcmdns.a
|
||||
|
||||
mdns: mdns.o
|
||||
|
||||
mdnsd: mdns.o mdnsd.o
|
||||
|
||||
testmdnsd: testmdnsd.o libtinysvcmdns.a
|
||||
|
||||
libtinysvcmdns.a: $(patsubst %, libtinysvcmdns.a(%), $(LIBTINYSVCMDNS_OBJS))
|
||||
rm -f $(OBJECTS) $(EXECUTABLE)
|
||||
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
CC=armv5te-cross-linux-gnueabi-gcc
|
||||
CFLAGS ?= -Wall -fPIC -O2 $(OPTS) $(INCLUDE) $(DEFINES)
|
||||
LDFLAGS ?= -s -lpthread -lm -lrt -L.
|
||||
|
||||
OBJ = bin/arm
|
||||
EXECUTABLE = bin/tinysvcmdns-arm
|
||||
|
||||
include Makefile
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
CC = x86_64-cross-freebsd11-gcc
|
||||
CFLAGS ?= -Wall -fPIC -ggdb -O2 $(OPTS) $(INCLUDE) $(DEFINES)
|
||||
LDFLAGS ?= -s -lpthread -lcrypto -lm -lrt -lz -L.
|
||||
|
||||
OBJ = bin/bsd-x64
|
||||
EXECUTABLE = bin/tinysvcmdns-bsd-x64
|
||||
|
||||
include Makefile
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
CC = i386-cross-solaris2.10-gcc
|
||||
CFLAGS = -Wall -fPIC -O2 $(OPTS) $(INCLUDE) $(DEFINES)
|
||||
LDFLAGS = -s -lpthread -ldl -lm -lrt -lsocket -lnsl -lz -L.
|
||||
|
||||
OBJ = bin/i86pc-solaris
|
||||
EXECUTABLE = bin/tinysvcmdns-i86pc-solaris
|
||||
|
||||
include Makefile
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
CC = o64-gcc
|
||||
CFLAGS ?= -Wall -fPIC -O2 $(OPTS) $(INCLUDE) $(DEFINES) -arch x86_64
|
||||
LDFLAGS ?= -lpthread -ldl -lm -L. -arch x86_64 -mmacosx-version-min=10.10
|
||||
|
||||
OBJ = bin/osx
|
||||
EXECUTABLE = bin/tinysvcmdns-osx
|
||||
|
||||
include Makefile
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
CFLAGS ?= -Wall -fPIC -ggdb -O2 $(OPTS) $(INCLUDE) $(DEFINES)
|
||||
LDFLAGS ?= -s -lpthread -lcrypto -ldl -lm -lrt -lz -L.
|
||||
|
||||
OBJ = bin/x86
|
||||
EXECUTABLE = bin/tinysvcmdns-x86
|
||||
|
||||
include Makefile
|
||||
|
|
@ -0,0 +1,79 @@
|
|||
This is a fork from https://bitbucket.org/geekman/tinysvcmdns. It works under
|
||||
Windows, OSX, and Linux (x86 and ARM) and with Embarcadero C++ builder XE7
|
||||
which is not C11 compliant
|
||||
|
||||
I've added a function to stop a registered service, send bye-bye packet and
|
||||
properly release all allocated memory
|
||||
|
||||
I've also added a small real responder
|
||||
|
||||
----------------------------- original README -----------------------------
|
||||
|
||||
tinysvcmdns
|
||||
============
|
||||
tinysvcmdns is a tiny MDNS responder implementation for publishing services.
|
||||
|
||||
This implementation is only concerned with publishing services, without a
|
||||
system-wide daemon like Bonjour or Avahi. Its other goal is to be extremely
|
||||
small, embeddable, and have no external dependencies.
|
||||
|
||||
It only answers queries related to its own hostname (the A record), the
|
||||
service PTRs, and the "_services.dns-sd._udp.local" name, which advertises
|
||||
all services on a particular host.
|
||||
|
||||
Services consist of a single SRV and TXT record.
|
||||
|
||||
Decoding of MDNS packets is only done to retrieve the questions and answer RRs.
|
||||
The purpose for decoding answer RRs is to make sure the service PTR is not
|
||||
sent out if it is already included in the answer RRs.
|
||||
|
||||
It also only utilizes multicast packets, so no "QU" queries are accepted.
|
||||
|
||||
There is no name collision detection, so this means no queries are generated
|
||||
before publishing the services. However compliant responders will avoid using
|
||||
our names, since the implementation will respond to queries that match our
|
||||
name.
|
||||
|
||||
|
||||
TODO
|
||||
-----
|
||||
* better, more stable & complete API
|
||||
* name collision detection
|
||||
|
||||
|
||||
FILES
|
||||
------
|
||||
* mdns.c - provides data structures, parsing & encoding of MDNS packets
|
||||
* mdnsd.c - implements the server socket, communication and thread
|
||||
* testmdnsd.c - an example that creates an instance until terminated
|
||||
|
||||
|
||||
LICENSE
|
||||
--------
|
||||
tinysvcmdns is licensed under the 3-clause ("modified") BSD License.
|
||||
|
||||
Copyright (C) 2011 Darell Tan
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
3. The name of the author may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
351
mdns.c
351
mdns.c
|
@ -1,20 +1,29 @@
|
|||
/*
|
||||
* tinysvcmdns - a tiny MDNS implementation for publishing services
|
||||
* Copyright (C) 2011 Darell Tan
|
||||
* All rights reserved.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "mdns.h"
|
||||
|
@ -23,10 +32,13 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <winsock.h>
|
||||
#include <in6addr.h>
|
||||
#else
|
||||
#include <netinet/in.h>
|
||||
|
||||
|
||||
#define DEFAULT_TTL 120
|
||||
#endif
|
||||
|
||||
|
||||
struct name_comp {
|
||||
|
@ -38,13 +50,9 @@ struct name_comp {
|
|||
|
||||
// ----- label functions -----
|
||||
|
||||
// compares 2 names
|
||||
static inline int cmp_nlabel(const uint8_t *L1, const uint8_t *L2) {
|
||||
return strcmp((char *) L1, (char *) L2);
|
||||
}
|
||||
|
||||
// duplicates a name
|
||||
inline uint8_t *dup_nlabel(const uint8_t *n) {
|
||||
//inline uint8_t *dup_nlabel(const uint8_t *n) {
|
||||
uint8_t *dup_nlabel(const uint8_t *n) {
|
||||
assert(n[0] <= 63); // prevent mis-use
|
||||
return (uint8_t *) strdup((char *) n);
|
||||
}
|
||||
|
@ -52,9 +60,10 @@ inline uint8_t *dup_nlabel(const uint8_t *n) {
|
|||
// duplicates a label
|
||||
uint8_t *dup_label(const uint8_t *label) {
|
||||
int len = *label + 1;
|
||||
uint8_t *newlabel;
|
||||
if (len > 63)
|
||||
return NULL;
|
||||
uint8_t *newlabel = malloc(len + 1);
|
||||
newlabel = malloc(len + 1);
|
||||
strncpy((char *) newlabel, (char *) label, len);
|
||||
newlabel[len] = '\0';
|
||||
return newlabel;
|
||||
|
@ -162,8 +171,9 @@ uint8_t *create_nlabel(const char *name) {
|
|||
lenpos = p;
|
||||
|
||||
while (p < e) {
|
||||
*lenpos = 0;
|
||||
char *dot = memchr(p + 1, '.', e - p - 1);
|
||||
*lenpos = 0;
|
||||
|
||||
if (dot == NULL)
|
||||
dot = e + 1;
|
||||
*lenpos = dot - p - 1;
|
||||
|
@ -242,6 +252,19 @@ static uint8_t *uncompress_nlabel(uint8_t *pkt_buf, size_t pkt_len, size_t off)
|
|||
|
||||
// ----- RR list & group functions -----
|
||||
|
||||
const char *rr_get_type_name(enum rr_type type) {
|
||||
switch (type) {
|
||||
case RR_A: return "A";
|
||||
case RR_PTR: return "PTR";
|
||||
case RR_TXT: return "TXT";
|
||||
case RR_AAAA: return "AAAA";
|
||||
case RR_SRV: return "SRV";
|
||||
case RR_NSEC: return "NSEC";
|
||||
case RR_ANY: return "ANY";
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void rr_entry_destroy(struct rr_entry *rr) {
|
||||
struct rr_data_txt *txt_rec;
|
||||
assert(rr);
|
||||
|
@ -249,7 +272,7 @@ void rr_entry_destroy(struct rr_entry *rr) {
|
|||
// check rr_type and free data elements
|
||||
switch (rr->type) {
|
||||
case RR_PTR:
|
||||
if (rr->data.PTR.name)
|
||||
if (rr->data.PTR.name)
|
||||
free(rr->data.PTR.name);
|
||||
// don't free entry
|
||||
break;
|
||||
|
@ -258,7 +281,7 @@ void rr_entry_destroy(struct rr_entry *rr) {
|
|||
txt_rec = &rr->data.TXT;
|
||||
while (txt_rec) {
|
||||
struct rr_data_txt *next = txt_rec->next;
|
||||
if (txt_rec->txt)
|
||||
if (txt_rec->txt)
|
||||
free(txt_rec->txt);
|
||||
|
||||
// only free() if it wasn't part of the struct
|
||||
|
@ -274,6 +297,11 @@ void rr_entry_destroy(struct rr_entry *rr) {
|
|||
free(rr->data.SRV.target);
|
||||
break;
|
||||
|
||||
case RR_AAAA:
|
||||
if (rr->data.AAAA.addr)
|
||||
free(rr->data.AAAA.addr);
|
||||
break;
|
||||
|
||||
default:
|
||||
// nothing to free
|
||||
break;
|
||||
|
@ -301,6 +329,37 @@ int rr_list_count(struct rr_list *rr) {
|
|||
return i;
|
||||
}
|
||||
|
||||
struct rr_entry *rr_entry_remove(struct rr_group *group, struct rr_entry *entry, enum rr_type type) {
|
||||
struct rr_group *g;
|
||||
|
||||
for (g = group; g; g = g->next) {
|
||||
struct rr_list *lrr = g->rr, *prr= NULL;
|
||||
for (; lrr; lrr = lrr->next) {
|
||||
if (lrr->e->type == type) {
|
||||
switch (type) {
|
||||
case RR_PTR:
|
||||
if (lrr->e->data.PTR.entry == entry) {
|
||||
struct rr_entry *e = lrr->e;
|
||||
if (prr == NULL) {
|
||||
g->rr = lrr->next;
|
||||
} else {
|
||||
prr->next = lrr->next;
|
||||
}
|
||||
free(lrr);
|
||||
return e;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
prr = lrr;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct rr_entry *rr_list_remove(struct rr_list **rr_head, struct rr_entry *rr) {
|
||||
struct rr_list *le = *rr_head, *pe = NULL;
|
||||
for (; le; le = le->next) {
|
||||
|
@ -320,6 +379,28 @@ struct rr_entry *rr_list_remove(struct rr_list **rr_head, struct rr_entry *rr) {
|
|||
return NULL;
|
||||
}
|
||||
|
||||
void rr_group_clean(struct rr_group **head) {
|
||||
struct rr_group *le = *head, *pe = NULL;
|
||||
|
||||
while (le) {
|
||||
if (le->rr == NULL) {
|
||||
free(le->name);
|
||||
if (pe == NULL) {
|
||||
*head = le->next;
|
||||
free(le);
|
||||
le = *head;
|
||||
} else {
|
||||
pe->next = le->next;
|
||||
free(le);
|
||||
le = pe->next;
|
||||
}
|
||||
} else {
|
||||
pe = le;
|
||||
le = le->next;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// appends an rr_entry to an RR list
|
||||
// if the RR is already in the list, it will not be added
|
||||
// RRs are compared by memory location - not its contents
|
||||
|
@ -354,10 +435,19 @@ int rr_list_append(struct rr_list **rr_head, struct rr_entry *rr) {
|
|||
rr->cache_flush = 1; \
|
||||
rr->rr_class = 1;
|
||||
|
||||
struct rr_entry *rr_create_a(uint8_t *name, uint32_t addr) {
|
||||
struct rr_entry *rr_create_a(uint8_t *name, struct in_addr addr) {
|
||||
DECL_MALLOC_ZERO_STRUCT(rr, rr_entry);
|
||||
FILL_RR_ENTRY(rr, name, RR_A);
|
||||
rr->data.A.addr = addr;
|
||||
rr->data.A.addr = addr.s_addr;
|
||||
rr->ttl = DEFAULT_TTL_FOR_RECORD_WITH_HOSTNAME; // 120 seconds -- see RFC 6762 Section 10
|
||||
return rr;
|
||||
}
|
||||
|
||||
struct rr_entry *rr_create_aaaa(uint8_t *name, struct in6_addr *addr) {
|
||||
DECL_MALLOC_ZERO_STRUCT(rr, rr_entry);
|
||||
FILL_RR_ENTRY(rr, name, RR_AAAA);
|
||||
rr->data.AAAA.addr = addr;
|
||||
rr->ttl = DEFAULT_TTL_FOR_RECORD_WITH_HOSTNAME; // 120 seconds -- see RFC 6762 Section 10
|
||||
return rr;
|
||||
}
|
||||
|
||||
|
@ -366,13 +456,16 @@ struct rr_entry *rr_create_srv(uint8_t *name, uint16_t port, uint8_t *target) {
|
|||
FILL_RR_ENTRY(rr, name, RR_SRV);
|
||||
rr->data.SRV.port = port;
|
||||
rr->data.SRV.target = target;
|
||||
rr->ttl = DEFAULT_TTL_FOR_RECORD_WITH_HOSTNAME; // 120 seconds -- see RFC 6762 Section 10
|
||||
return rr;
|
||||
}
|
||||
|
||||
struct rr_entry *rr_create_ptr(uint8_t *name, struct rr_entry *d_rr) {
|
||||
DECL_MALLOC_ZERO_STRUCT(rr, rr_entry);
|
||||
FILL_RR_ENTRY(rr, name, RR_PTR);
|
||||
rr->cache_flush = 0; // PTRs shouldn't have their cache flush bit set
|
||||
rr->data.PTR.entry = d_rr;
|
||||
rr->ttl = DEFAULT_TTL_FOR_RECORD_WITH_HOSTNAME; // 120 seconds -- see RFC 6762 Section 10
|
||||
return rr;
|
||||
}
|
||||
|
||||
|
@ -383,7 +476,7 @@ struct rr_entry *rr_create(uint8_t *name, enum rr_type type) {
|
|||
}
|
||||
|
||||
void rr_set_nsec(struct rr_entry *rr_nsec, enum rr_type type) {
|
||||
assert(rr_nsec->type = RR_NSEC);
|
||||
assert(rr_nsec->type == RR_NSEC);
|
||||
assert((type / 8) < sizeof(rr_nsec->data.NSEC.bitmap));
|
||||
|
||||
rr_nsec->data.NSEC.bitmap[ type / 8 ] = 1 << (7 - (type % 8));
|
||||
|
@ -447,12 +540,29 @@ struct rr_group *rr_group_find(struct rr_group* g, uint8_t *name) {
|
|||
struct rr_entry *rr_entry_find(struct rr_list *rr_list, uint8_t *name, uint16_t type) {
|
||||
struct rr_list *rr = rr_list;
|
||||
for (; rr; rr = rr->next) {
|
||||
if (rr->e->type == type)
|
||||
if (rr->e->type == type && cmp_nlabel(rr->e->name, name) == 0)
|
||||
return rr->e;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// looks for a matching entry in rr_list
|
||||
// if entry is a PTR, we need to check if the PTR target also matches
|
||||
struct rr_entry *rr_entry_match(struct rr_list *rr_list, struct rr_entry *entry) {
|
||||
struct rr_list *rr = rr_list;
|
||||
for (; rr; rr = rr->next) {
|
||||
if (rr->e->type == entry->type && cmp_nlabel(rr->e->name, entry->name) == 0) {
|
||||
if (entry->type != RR_PTR) {
|
||||
return rr->e;
|
||||
} else if (cmp_nlabel(MDNS_RR_GET_PTR_NAME(entry), MDNS_RR_GET_PTR_NAME(rr->e)) == 0) {
|
||||
// if it's a PTR, we need to make sure PTR target also matches
|
||||
return rr->e;
|
||||
}
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void rr_group_destroy(struct rr_group *group) {
|
||||
struct rr_group *g = group;
|
||||
|
||||
|
@ -465,6 +575,32 @@ void rr_group_destroy(struct rr_group *group) {
|
|||
}
|
||||
}
|
||||
|
||||
uint8_t *mdns_write_u16(uint8_t *ptr, const uint16_t v) {
|
||||
*ptr++ = (uint8_t) (v >> 8) & 0xFF;
|
||||
*ptr++ = (uint8_t) (v >> 0) & 0xFF;
|
||||
return ptr;
|
||||
}
|
||||
|
||||
uint8_t *mdns_write_u32(uint8_t *ptr, const uint32_t v) {
|
||||
*ptr++ = (uint8_t) (v >> 24) & 0xFF;
|
||||
*ptr++ = (uint8_t) (v >> 16) & 0xFF;
|
||||
*ptr++ = (uint8_t) (v >> 8) & 0xFF;
|
||||
*ptr++ = (uint8_t) (v >> 0) & 0xFF;
|
||||
return ptr;
|
||||
}
|
||||
|
||||
uint16_t mdns_read_u16(const uint8_t *ptr) {
|
||||
return ((ptr[0] & 0xFF) << 8) |
|
||||
((ptr[1] & 0xFF) << 0);
|
||||
}
|
||||
|
||||
uint32_t mdns_read_u32(const uint8_t *ptr) {
|
||||
return ((ptr[0] & 0xFF) << 24) |
|
||||
((ptr[1] & 0xFF) << 16) |
|
||||
((ptr[2] & 0xFF) << 8) |
|
||||
((ptr[3] & 0xFF) << 0);
|
||||
}
|
||||
|
||||
// initialize the packet for reply
|
||||
// clears the packet of list structures but not its list items
|
||||
void mdns_init_reply(struct mdns_pkt *pkt, uint16_t id) {
|
||||
|
@ -518,11 +654,11 @@ static size_t mdns_parse_qn(uint8_t *pkt_buf, size_t pkt_len, size_t off,
|
|||
p += label_len(pkt_buf, pkt_len, off);
|
||||
rr->name = name;
|
||||
|
||||
rr->type = ntohs( * (uint16_t *) p );
|
||||
rr->type = mdns_read_u16(p);
|
||||
p += sizeof(uint16_t);
|
||||
|
||||
rr->unicast_query = (*p & 0x80) == 0x80;
|
||||
rr->rr_class = ntohs( * (uint16_t *) p) & ~0x80;
|
||||
rr->rr_class = mdns_read_u16(p) & ~0x80;
|
||||
p += sizeof(uint16_t);
|
||||
|
||||
rr_list_append(&pkt->rr_qn, rr);
|
||||
|
@ -540,6 +676,7 @@ static size_t mdns_parse_rr(uint8_t *pkt_buf, size_t pkt_len, size_t off,
|
|||
uint8_t *name;
|
||||
size_t rr_data_len = 0;
|
||||
struct rr_data_txt *txt_rec;
|
||||
int parse_error = 0;
|
||||
|
||||
assert(pkt != NULL);
|
||||
|
||||
|
@ -553,40 +690,85 @@ static size_t mdns_parse_rr(uint8_t *pkt_buf, size_t pkt_len, size_t off,
|
|||
p += label_len(pkt_buf, pkt_len, off);
|
||||
rr->name = name;
|
||||
|
||||
rr->type = ntohs( * (uint16_t *) p );
|
||||
rr->type = mdns_read_u16(p);
|
||||
p += sizeof(uint16_t);
|
||||
|
||||
rr->cache_flush = (*p & 0x80) == 0x80;
|
||||
rr->rr_class = ntohs( * (uint16_t *) p) & ~0x80;
|
||||
rr->rr_class = mdns_read_u16(p) & ~0x80;
|
||||
p += sizeof(uint16_t);
|
||||
|
||||
rr->ttl = ntohl( * (uint32_t *) p );
|
||||
rr->ttl = mdns_read_u32(p);
|
||||
p += sizeof(uint32_t);
|
||||
|
||||
// RR data
|
||||
rr_data_len = ntohs( * (uint16_t *) p );
|
||||
rr_data_len = mdns_read_u16(p);
|
||||
p += sizeof(uint16_t);
|
||||
|
||||
if (p + rr_data_len > e) {
|
||||
DEBUG_PRINTF("rr_data_len goes beyond packet buffer: %zu > %zu\n", rr_data_len, e - p);
|
||||
rr_entry_destroy(rr);
|
||||
return 0;
|
||||
}
|
||||
|
||||
e = p + rr_data_len;
|
||||
|
||||
// see if we can parse the RR data
|
||||
switch (rr->type) {
|
||||
case RR_A:
|
||||
rr->data.A.addr = ntohl( * (uint32_t *) p );
|
||||
if (rr_data_len < sizeof(uint32_t)) {
|
||||
DEBUG_PRINTF("invalid rr_data_len=%zu for A record\n", rr_data_len);
|
||||
parse_error = 1;
|
||||
break;
|
||||
}
|
||||
rr->data.A.addr = ntohl(mdns_read_u32(p)); /* addr already in net order */
|
||||
p += sizeof(uint32_t);
|
||||
break;
|
||||
|
||||
case RR_AAAA: {
|
||||
int i;
|
||||
if (rr_data_len < sizeof(struct in6_addr)) {
|
||||
DEBUG_PRINTF("invalid rr_data_len=%zu for AAAA record\n", rr_data_len);
|
||||
parse_error = 1;
|
||||
break;
|
||||
}
|
||||
rr->data.AAAA.addr = malloc(sizeof(struct in6_addr));
|
||||
for (i = 0; i < sizeof(struct in6_addr); i++)
|
||||
rr->data.AAAA.addr->s6_addr[i] = p[i];
|
||||
p += sizeof(struct in6_addr);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
case RR_PTR:
|
||||
rr->data.PTR.name = uncompress_nlabel(pkt_buf, pkt_len, p - pkt_buf);
|
||||
if (rr->data.PTR.name == NULL) {
|
||||
DEBUG_PRINTF("unable to parse/uncompress label for PTR name\n");
|
||||
parse_error = 1;
|
||||
break;
|
||||
}
|
||||
p += rr_data_len;
|
||||
break;
|
||||
|
||||
case RR_TXT:
|
||||
txt_rec = &rr->data.TXT;
|
||||
|
||||
// not supposed to happen, but we should handle it
|
||||
if (rr_data_len == 0) {
|
||||
DEBUG_PRINTF("WARN: rr_data_len for TXT is 0\n");
|
||||
txt_rec->txt = create_label("");
|
||||
break;
|
||||
}
|
||||
|
||||
while (1) {
|
||||
txt_rec->txt = copy_label(pkt_buf, pkt_len, p - pkt_buf);
|
||||
if (txt_rec->txt == NULL) {
|
||||
DEBUG_PRINTF("unable to copy label for TXT record\n");
|
||||
parse_error = 1;
|
||||
break;
|
||||
}
|
||||
p += txt_rec->txt[0] + 1;
|
||||
|
||||
if (p >= e)
|
||||
if (p >= e)
|
||||
break;
|
||||
|
||||
// allocate another record
|
||||
|
@ -601,6 +783,12 @@ static size_t mdns_parse_rr(uint8_t *pkt_buf, size_t pkt_len, size_t off,
|
|||
p = e;
|
||||
}
|
||||
|
||||
// if there was a parse error, destroy partial rr_entry
|
||||
if (parse_error) {
|
||||
rr_entry_destroy(rr);
|
||||
return 0;
|
||||
}
|
||||
|
||||
rr_list_append(&pkt->rr_ans, rr);
|
||||
|
||||
return p - (pkt_buf + off);
|
||||
|
@ -608,7 +796,7 @@ static size_t mdns_parse_rr(uint8_t *pkt_buf, size_t pkt_len, size_t off,
|
|||
|
||||
// parse a MDNS packet into an mdns_pkt struct
|
||||
struct mdns_pkt *mdns_parse_pkt(uint8_t *pkt_buf, size_t pkt_len) {
|
||||
uint16_t *p = (uint16_t *) pkt_buf;
|
||||
uint8_t *p = pkt_buf;
|
||||
size_t off;
|
||||
struct mdns_pkt *pkt;
|
||||
int i;
|
||||
|
@ -619,14 +807,14 @@ struct mdns_pkt *mdns_parse_pkt(uint8_t *pkt_buf, size_t pkt_len) {
|
|||
MALLOC_ZERO_STRUCT(pkt, mdns_pkt);
|
||||
|
||||
// parse header
|
||||
pkt->id = ntohs(*p); p++;
|
||||
pkt->flags = ntohs(*p); p++;
|
||||
pkt->num_qn = ntohs(*p); p++;
|
||||
pkt->num_ans_rr = ntohs(*p); p++;
|
||||
pkt->num_auth_rr = ntohs(*p); p++;
|
||||
pkt->num_add_rr = ntohs(*p); p++;
|
||||
pkt->id = mdns_read_u16(p); p += sizeof(uint16_t);
|
||||
pkt->flags = mdns_read_u16(p); p += sizeof(uint16_t);
|
||||
pkt->num_qn = mdns_read_u16(p); p += sizeof(uint16_t);
|
||||
pkt->num_ans_rr = mdns_read_u16(p); p += sizeof(uint16_t);
|
||||
pkt->num_auth_rr = mdns_read_u16(p); p += sizeof(uint16_t);
|
||||
pkt->num_add_rr = mdns_read_u16(p); p += sizeof(uint16_t);
|
||||
|
||||
off = (uint8_t *) p - pkt_buf;
|
||||
off = p - pkt_buf;
|
||||
|
||||
// parse questions
|
||||
for (i = 0; i < pkt->num_qn; i++) {
|
||||
|
@ -659,7 +847,7 @@ struct mdns_pkt *mdns_parse_pkt(uint8_t *pkt_buf, size_t pkt_len) {
|
|||
|
||||
// encodes a name (label) into a packet using the name compression scheme
|
||||
// encoded names will be added to the compression list for subsequent use
|
||||
static size_t mdns_encode_name(uint8_t *pkt_buf, size_t pkt_len, size_t off,
|
||||
static size_t mdns_encode_name(uint8_t *pkt_buf, size_t pkt_len, size_t off,
|
||||
const uint8_t *name, struct name_comp *comp) {
|
||||
struct name_comp *c, *c_tail = NULL;
|
||||
uint8_t *p = pkt_buf + off;
|
||||
|
@ -667,10 +855,14 @@ static size_t mdns_encode_name(uint8_t *pkt_buf, size_t pkt_len, size_t off,
|
|||
|
||||
if (name) {
|
||||
while (*name) {
|
||||
int segment_len;
|
||||
// cache the name for subsequent compression
|
||||
DECL_STRUCT(new_c, name_comp);
|
||||
|
||||
// find match for compression
|
||||
for (c = comp; c; c = c->next) {
|
||||
if (cmp_nlabel(name, c->label) == 0) {
|
||||
*(uint16_t *) p = htons(0xC000 | (c->pos & ~0xC000));
|
||||
mdns_write_u16(p, 0xC000 | (c->pos & ~0xC000));
|
||||
return len + sizeof(uint16_t);
|
||||
}
|
||||
|
||||
|
@ -679,11 +871,9 @@ static size_t mdns_encode_name(uint8_t *pkt_buf, size_t pkt_len, size_t off,
|
|||
}
|
||||
|
||||
// copy this segment
|
||||
int segment_len = *name + 1;
|
||||
segment_len = *name + 1;
|
||||
strncpy((char *) p, (char *) name, segment_len);
|
||||
|
||||
// cache the name for subsequent compression
|
||||
DECL_MALLOC_ZERO_STRUCT(new_c, name_comp);
|
||||
MALLOC_ZERO_STRUCT(new_c, name_comp);
|
||||
|
||||
new_c->label = (uint8_t *) name;
|
||||
new_c->pos = p - pkt_buf;
|
||||
|
@ -720,16 +910,13 @@ static size_t mdns_encode_rr(uint8_t *pkt_buf, size_t pkt_len, size_t off,
|
|||
p += l;
|
||||
|
||||
// type
|
||||
*(uint16_t *) p = htons(rr->type);
|
||||
p += sizeof(uint16_t);
|
||||
p = mdns_write_u16(p, rr->type);
|
||||
|
||||
// class & cache flush
|
||||
*(uint16_t *) p = htons((rr->rr_class & ~0x8000) | (rr->cache_flush << 15));
|
||||
p += sizeof(uint16_t);
|
||||
p = mdns_write_u16(p, (rr->rr_class & ~0x8000) | (rr->cache_flush << 15));
|
||||
|
||||
// TTL
|
||||
*(uint32_t *) p = htonl(rr->ttl);
|
||||
p += sizeof(uint32_t);
|
||||
p = mdns_write_u32(p, rr->ttl);
|
||||
|
||||
// data length (filled in later)
|
||||
p += sizeof(uint16_t);
|
||||
|
@ -739,8 +926,13 @@ static size_t mdns_encode_rr(uint8_t *pkt_buf, size_t pkt_len, size_t off,
|
|||
|
||||
switch (rr->type) {
|
||||
case RR_A:
|
||||
*(uint32_t *) p = (rr->data.A.addr);
|
||||
p += sizeof(uint32_t);
|
||||
/* htonl() needed coz addr already in net order */
|
||||
p = mdns_write_u32(p, htonl(rr->data.A.addr));
|
||||
break;
|
||||
|
||||
case RR_AAAA:
|
||||
for (i = 0; i < sizeof(struct in6_addr); i++)
|
||||
*p++ = rr->data.AAAA.addr->s6_addr[i];
|
||||
break;
|
||||
|
||||
case RR_PTR:
|
||||
|
@ -760,14 +952,11 @@ static size_t mdns_encode_rr(uint8_t *pkt_buf, size_t pkt_len, size_t off,
|
|||
break;
|
||||
|
||||
case RR_SRV:
|
||||
*(uint16_t *) p = htons(rr->data.SRV.priority);
|
||||
p += sizeof(uint16_t);
|
||||
p = mdns_write_u16(p, rr->data.SRV.priority);
|
||||
|
||||
*(uint16_t *) p = htons(rr->data.SRV.weight);
|
||||
p += sizeof(uint16_t);
|
||||
p = mdns_write_u16(p, rr->data.SRV.weight);
|
||||
|
||||
*(uint16_t *) p = htons(rr->data.SRV.port);
|
||||
p += sizeof(uint16_t);
|
||||
p = mdns_write_u16(p, rr->data.SRV.port);
|
||||
|
||||
p += mdns_encode_name(pkt_buf, pkt_len, p - pkt_buf,
|
||||
rr->data.SRV.target, comp);
|
||||
|
@ -794,7 +983,7 @@ static size_t mdns_encode_rr(uint8_t *pkt_buf, size_t pkt_len, size_t off,
|
|||
l = p - p_data;
|
||||
|
||||
// fill in the length
|
||||
*(uint16_t *) (p - l - sizeof(uint16_t)) = htons(l);
|
||||
mdns_write_u16(p - l - sizeof(uint16_t), l);
|
||||
|
||||
return p - pkt_buf - off;
|
||||
}
|
||||
|
@ -803,10 +992,11 @@ static size_t mdns_encode_rr(uint8_t *pkt_buf, size_t pkt_len, size_t off,
|
|||
// returns the size of the entire MDNS packet
|
||||
size_t mdns_encode_pkt(struct mdns_pkt *answer, uint8_t *pkt_buf, size_t pkt_len) {
|
||||
struct name_comp *comp;
|
||||
uint16_t *p = (uint16_t *) pkt_buf;
|
||||
uint8_t *p = pkt_buf;
|
||||
//uint8_t *e = pkt_buf + pkt_len;
|
||||
size_t off;
|
||||
int i;
|
||||
struct rr_list *rr_set[3];
|
||||
|
||||
assert(answer != NULL);
|
||||
assert(pkt_len >= 12);
|
||||
|
@ -817,18 +1007,18 @@ size_t mdns_encode_pkt(struct mdns_pkt *answer, uint8_t *pkt_buf, size_t pkt_len
|
|||
// this is an Answer - number of qns should be zero
|
||||
assert(answer->num_qn == 0);
|
||||
|
||||
*p++ = htons(answer->id);
|
||||
*p++ = htons(answer->flags);
|
||||
*p++ = htons(answer->num_qn);
|
||||
*p++ = htons(answer->num_ans_rr);
|
||||
*p++ = htons(answer->num_auth_rr);
|
||||
*p++ = htons(answer->num_add_rr);
|
||||
p = mdns_write_u16(p, answer->id);
|
||||
p = mdns_write_u16(p, answer->flags);
|
||||
p = mdns_write_u16(p, answer->num_qn);
|
||||
p = mdns_write_u16(p, answer->num_ans_rr);
|
||||
p = mdns_write_u16(p, answer->num_auth_rr);
|
||||
p = mdns_write_u16(p, answer->num_add_rr);
|
||||
|
||||
off = (uint8_t *) p - pkt_buf;
|
||||
off = p - pkt_buf;
|
||||
|
||||
// allocate list for name compression
|
||||
comp = malloc(sizeof(struct name_comp));
|
||||
if (comp == NULL)
|
||||
if (comp == NULL)
|
||||
return -1;
|
||||
memset(comp, 0, sizeof(struct name_comp));
|
||||
|
||||
|
@ -837,12 +1027,9 @@ size_t mdns_encode_pkt(struct mdns_pkt *answer, uint8_t *pkt_buf, size_t pkt_len
|
|||
comp->pos = 0;
|
||||
|
||||
// skip encoding of qn
|
||||
|
||||
struct rr_list *rr_set[] = {
|
||||
answer->rr_ans,
|
||||
answer->rr_auth,
|
||||
answer->rr_add
|
||||
};
|
||||
rr_set[0] = answer->rr_ans;
|
||||
rr_set[1] = answer->rr_auth;
|
||||
rr_set[2] = answer->rr_add;
|
||||
|
||||
// encode answer, authority and additional RRs
|
||||
for (i = 0; i < sizeof(rr_set) / sizeof(rr_set[0]); i++) {
|
||||
|
|
70
mdns.h
70
mdns.h
|
@ -1,20 +1,29 @@
|
|||
/*
|
||||
* tinysvcmdns - a tiny MDNS implementation for publishing services
|
||||
* Copyright (C) 2011 Darell Tan
|
||||
* All rights reserved.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef __MDNS_H__
|
||||
|
@ -22,6 +31,13 @@
|
|||
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <winsock.h>
|
||||
#else
|
||||
#include <arpa/inet.h>
|
||||
#endif
|
||||
|
||||
#define MALLOC_ZERO_STRUCT(x, type) \
|
||||
x = malloc(sizeof(struct type)); \
|
||||
|
@ -30,6 +46,12 @@
|
|||
#define DECL_MALLOC_ZERO_STRUCT(x, type) \
|
||||
struct type * MALLOC_ZERO_STRUCT(x, type)
|
||||
|
||||
#define DECL_STRUCT(x, type) \
|
||||
struct type * x;
|
||||
|
||||
#define DEFAULT_TTL_FOR_RECORD_WITH_HOSTNAME 120
|
||||
#define DEFAULT_TTL 4500
|
||||
|
||||
#ifndef NDEBUG
|
||||
#define DEBUG_PRINTF(...) printf(__VA_ARGS__)
|
||||
#else
|
||||
|
@ -66,6 +88,10 @@ struct rr_data_a {
|
|||
uint32_t addr;
|
||||
};
|
||||
|
||||
struct rr_data_aaaa {
|
||||
struct in6_addr *addr;
|
||||
};
|
||||
|
||||
struct rr_entry {
|
||||
uint8_t *name;
|
||||
|
||||
|
@ -96,6 +122,7 @@ struct rr_entry {
|
|||
struct rr_data_txt TXT;
|
||||
struct rr_data_ptr PTR;
|
||||
struct rr_data_a A;
|
||||
struct rr_data_aaaa AAAA;
|
||||
} data;
|
||||
};
|
||||
|
||||
|
@ -122,6 +149,9 @@ struct rr_group {
|
|||
#define MDNS_FLAG_GET_RCODE(x) (x & 0x0F)
|
||||
#define MDNS_FLAG_GET_OPCODE(x) ((x >> 11) & 0x0F)
|
||||
|
||||
// gets the PTR target name, either from "name" member or "entry" member
|
||||
#define MDNS_RR_GET_PTR_NAME(rr) (rr->data.PTR.name != NULL ? rr->data.PTR.name : rr->data.PTR.entry->name)
|
||||
|
||||
struct mdns_pkt {
|
||||
uint16_t id; // transaction ID
|
||||
uint16_t flags;
|
||||
|
@ -145,7 +175,11 @@ void mdns_pkt_destroy(struct mdns_pkt *p);
|
|||
void rr_group_destroy(struct rr_group *group);
|
||||
struct rr_group *rr_group_find(struct rr_group *g, uint8_t *name);
|
||||
struct rr_entry *rr_entry_find(struct rr_list *rr_list, uint8_t *name, uint16_t type);
|
||||
struct rr_entry *rr_entry_match(struct rr_list *rr_list, struct rr_entry *entry);
|
||||
void rr_entry_destroy(struct rr_entry *rr);
|
||||
struct rr_entry *rr_entry_remove(struct rr_group *group, struct rr_entry *entry, enum rr_type type);
|
||||
void rr_group_add(struct rr_group **group, struct rr_entry *rr);
|
||||
void rr_group_clean(struct rr_group **head);
|
||||
|
||||
int rr_list_count(struct rr_list *rr);
|
||||
int rr_list_append(struct rr_list **rr_head, struct rr_entry *rr);
|
||||
|
@ -154,11 +188,14 @@ void rr_list_destroy(struct rr_list *rr, char destroy_items);
|
|||
|
||||
struct rr_entry *rr_create_ptr(uint8_t *name, struct rr_entry *d_rr);
|
||||
struct rr_entry *rr_create_srv(uint8_t *name, uint16_t port, uint8_t *target);
|
||||
struct rr_entry *rr_create_a(uint8_t *name, uint32_t addr);
|
||||
struct rr_entry *rr_create_aaaa(uint8_t *name, struct in6_addr *addr);
|
||||
struct rr_entry *rr_create_a(uint8_t *name, struct in_addr addr);
|
||||
struct rr_entry *rr_create(uint8_t *name, enum rr_type type);
|
||||
void rr_set_nsec(struct rr_entry *rr_nsec, enum rr_type type);
|
||||
void rr_add_txt(struct rr_entry *rr_txt, const char *txt);
|
||||
|
||||
const char *rr_get_type_name(enum rr_type type);
|
||||
|
||||
uint8_t *create_label(const char *txt);
|
||||
uint8_t *create_nlabel(const char *name);
|
||||
char *nlabel_to_str(const uint8_t *name);
|
||||
|
@ -166,4 +203,9 @@ uint8_t *dup_label(const uint8_t *label);
|
|||
uint8_t *dup_nlabel(const uint8_t *n);
|
||||
uint8_t *join_nlabel(const uint8_t *n1, const uint8_t *n2);
|
||||
|
||||
// compares 2 names
|
||||
static inline int cmp_nlabel(const uint8_t *L1, const uint8_t *L2) {
|
||||
return strcmp((char *) L1, (char *) L2);
|
||||
}
|
||||
|
||||
#endif /*!__MDNS_H__*/
|
||||
|
|
434
mdnsd.c
434
mdnsd.c
|
@ -1,41 +1,66 @@
|
|||
/*
|
||||
* tinysvcmdns - a tiny MDNS implementation for publishing services
|
||||
* Copyright (C) 2011 Darell Tan
|
||||
* All rights reserved.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
#define LOG_ERR 3
|
||||
#else
|
||||
#include <sys/select.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <net/if.h>
|
||||
#include <syslog.h>
|
||||
#endif
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/select.h>
|
||||
#include <fcntl.h>
|
||||
#include <signal.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
#ifndef WIN32
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
#include <assert.h>
|
||||
#include <pthread.h>
|
||||
#include <syslog.h>
|
||||
|
||||
/*
|
||||
* Define a proper IP socket level if not already done.
|
||||
* Required to compile on OS X
|
||||
*/
|
||||
#ifndef SOL_IP
|
||||
#define SOL_IP IPPROTO_IP
|
||||
#endif
|
||||
|
||||
#include "mdns.h"
|
||||
#include "mdnsd.h"
|
||||
|
@ -57,6 +82,7 @@ struct mdnsd {
|
|||
struct rr_group *group;
|
||||
struct rr_list *announce;
|
||||
struct rr_list *services;
|
||||
struct rr_list *leave;
|
||||
uint8_t *hostname;
|
||||
};
|
||||
|
||||
|
@ -79,35 +105,58 @@ static void log_message(int loglevel, char *fmt_str, ...) {
|
|||
fprintf(stderr, "%s\n", buf);
|
||||
}
|
||||
|
||||
static int create_recv_sock() {
|
||||
static int create_recv_sock(uint32_t host) {
|
||||
int sd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
int r = -1;
|
||||
int on = 1;
|
||||
char onChar = 1;
|
||||
struct sockaddr_in serveraddr;
|
||||
struct ip_mreq mreq;
|
||||
socklen_t addrlen;
|
||||
unsigned char ttl = 255;
|
||||
|
||||
if (sd < 0) {
|
||||
log_message(LOG_ERR, "recv socket(): %m");
|
||||
return sd;
|
||||
}
|
||||
|
||||
int r = -1;
|
||||
|
||||
int on = 1;
|
||||
if ((r = setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof(on))) < 0) {
|
||||
log_message(LOG_ERR, "recv setsockopt(SO_REUSEADDR): %m");
|
||||
return r;
|
||||
}
|
||||
|
||||
#if !defined(WIN32)
|
||||
if (!getsockopt(sd, SOL_SOCKET, SO_REUSEPORT,(char*) &on, &addrlen)) {
|
||||
on = 1;
|
||||
if ((r = setsockopt(sd, SOL_SOCKET, SO_REUSEPORT,(char*) &on, sizeof(on))) < 0) {
|
||||
log_message(LOG_ERR, "recv setsockopt(SO_REUSEPORT): %m", r);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/* bind to an address */
|
||||
struct sockaddr_in serveraddr;
|
||||
memset(&serveraddr, 0, sizeof(serveraddr));
|
||||
serveraddr.sin_family = AF_INET;
|
||||
serveraddr.sin_port = htons(MDNS_PORT);
|
||||
serveraddr.sin_addr.s_addr = htonl(INADDR_ANY); /* receive multicast */
|
||||
if ((r = bind(sd, (struct sockaddr *)&serveraddr, sizeof(serveraddr))) < 0) {
|
||||
log_message(LOG_ERR, "recv bind(): %m");
|
||||
return r;
|
||||
}
|
||||
|
||||
memset(&mreq, 0, sizeof(struct ip_mreq));
|
||||
mreq.imr_interface.s_addr = host;
|
||||
if ((r = setsockopt(sd, IPPROTO_IP, IP_MULTICAST_IF, (char*) &mreq.imr_interface.s_addr, sizeof(mreq.imr_interface.s_addr))) < 0) {
|
||||
log_message(LOG_ERR, "recv setsockopt(IP_PROTO_IP): %m");
|
||||
return r;
|
||||
}
|
||||
|
||||
if ((r = setsockopt(sd, IPPROTO_IP, IP_MULTICAST_TTL, (void*) &ttl, sizeof(ttl))) < 0) {
|
||||
log_message(LOG_ERR, "recv setsockopt(IP_MULTICAST_IP): %m");
|
||||
return r;
|
||||
}
|
||||
|
||||
// add membership to receiving socket
|
||||
struct ip_mreq mreq;
|
||||
memset(&mreq, 0, sizeof(struct ip_mreq));
|
||||
mreq.imr_interface.s_addr = htonl(INADDR_ANY);
|
||||
mreq.imr_multiaddr.s_addr = inet_addr(MDNS_ADDR);
|
||||
if ((r = setsockopt(sd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *) &mreq, sizeof(mreq))) < 0) {
|
||||
log_message(LOG_ERR, "recv setsockopt(IP_ADD_MEMBERSHIP): %m");
|
||||
|
@ -115,13 +164,14 @@ static int create_recv_sock() {
|
|||
}
|
||||
|
||||
// enable loopback in case someone else needs the data
|
||||
if ((r = setsockopt(sd, IPPROTO_IP, IP_MULTICAST_LOOP, (char *) &on, sizeof(on))) < 0) {
|
||||
if ((r = setsockopt(sd, IPPROTO_IP, IP_MULTICAST_LOOP, (char *) &onChar, sizeof(onChar))) < 0) {
|
||||
log_message(LOG_ERR, "recv setsockopt(IP_MULTICAST_LOOP): %m");
|
||||
return r;
|
||||
}
|
||||
|
||||
|
||||
#ifdef IP_PKTINFO
|
||||
on = 1;
|
||||
if ((r = setsockopt(sd, SOL_IP, IP_PKTINFO, (char *) &on, sizeof(on))) < 0) {
|
||||
log_message(LOG_ERR, "recv setsockopt(IP_PKTINFO): %m");
|
||||
return r;
|
||||
|
@ -148,32 +198,29 @@ static ssize_t send_packet(int fd, const void *data, size_t len) {
|
|||
// type can be RR_ANY, which populates all entries EXCEPT RR_NSEC
|
||||
static int populate_answers(struct mdnsd *svr, struct rr_list **rr_head, uint8_t *name, enum rr_type type) {
|
||||
int num_ans = 0;
|
||||
struct rr_entry *rr;
|
||||
struct rr_group *ans_grp;
|
||||
struct rr_list *n;
|
||||
|
||||
// check if we have the records
|
||||
pthread_mutex_lock(&svr->data_lock);
|
||||
struct rr_group *ans_grp = rr_group_find(svr->group, name);
|
||||
ans_grp = rr_group_find(svr->group, name);
|
||||
if (ans_grp == NULL) {
|
||||
pthread_mutex_unlock(&svr->data_lock);
|
||||
return num_ans;
|
||||
}
|
||||
|
||||
// include all records?
|
||||
if (type == RR_ANY) {
|
||||
struct rr_list *n = ans_grp->rr;
|
||||
for (; n; n = n->next) {
|
||||
// exclude NSEC
|
||||
if (n->e->type == RR_NSEC)
|
||||
continue;
|
||||
// decide which records should go into answers
|
||||
n = ans_grp->rr;
|
||||
for (; n; n = n->next) {
|
||||
// exclude NSEC for RR_ANY
|
||||
if (type == RR_ANY && n->e->type == RR_NSEC)
|
||||
continue;
|
||||
|
||||
if ((type == n->e->type || type == RR_ANY) && cmp_nlabel(name, n->e->name) == 0) {
|
||||
num_ans += rr_list_append(rr_head, n->e);
|
||||
}
|
||||
} else {
|
||||
// match record type
|
||||
rr = rr_entry_find(ans_grp->rr, name, type);
|
||||
if (rr)
|
||||
num_ans += rr_list_append(rr_head, rr);
|
||||
}
|
||||
|
||||
pthread_mutex_unlock(&svr->data_lock);
|
||||
|
||||
return num_ans;
|
||||
|
@ -187,11 +234,8 @@ static void add_related_rr(struct mdnsd *svr, struct rr_list *list, struct mdns_
|
|||
switch (ans->type) {
|
||||
case RR_PTR:
|
||||
// target host A, AAAA records
|
||||
reply->num_add_rr += populate_answers(svr, &reply->rr_add,
|
||||
(ans->data.PTR.name ?
|
||||
ans->data.PTR.name :
|
||||
ans->data.PTR.entry->name),
|
||||
RR_ANY);
|
||||
reply->num_add_rr += populate_answers(svr, &reply->rr_add,
|
||||
MDNS_RR_GET_PTR_NAME(ans), RR_ANY);
|
||||
break;
|
||||
|
||||
case RR_SRV:
|
||||
|
@ -239,26 +283,30 @@ static void announce_srv(struct mdnsd *svr, struct mdns_pkt *reply, uint8_t *nam
|
|||
// returns >0 if processed, 0 otherwise
|
||||
static int process_mdns_pkt(struct mdnsd *svr, struct mdns_pkt *pkt, struct mdns_pkt *reply) {
|
||||
int i;
|
||||
struct rr_list *qnl;
|
||||
struct rr_list *ans, *prev_ans;
|
||||
|
||||
assert(pkt != NULL);
|
||||
|
||||
// is it standard query?
|
||||
if ((pkt->flags & MDNS_FLAG_RESP) == 0 &&
|
||||
if ((pkt->flags & MDNS_FLAG_RESP) == 0 &&
|
||||
MDNS_FLAG_GET_OPCODE(pkt->flags) == 0) {
|
||||
mdns_init_reply(reply, pkt->id);
|
||||
|
||||
DEBUG_PRINTF("flags = %04x, qn = %d, ans = %d, add = %d\n",
|
||||
DEBUG_PRINTF("flags = %04x, qn = %d, ans = %d, add = %d\n",
|
||||
pkt->flags,
|
||||
pkt->num_qn,
|
||||
pkt->num_ans_rr,
|
||||
pkt->num_add_rr);
|
||||
|
||||
// loop through questions
|
||||
struct rr_list *qnl = pkt->rr_qn;
|
||||
qnl = pkt->rr_qn;
|
||||
for (i = 0; i < pkt->num_qn; i++, qnl = qnl->next) {
|
||||
struct rr_entry *qn = qnl->e;
|
||||
int num_ans_added = 0;
|
||||
|
||||
char *namestr = nlabel_to_str(qn->name);
|
||||
DEBUG_PRINTF("qn #%d: type 0x%02x %s - ", i, qn->type, namestr);
|
||||
DEBUG_PRINTF("qn #%d: type %s (%02x) %s - ", i, rr_get_type_name(qn->type), qn->type, namestr);
|
||||
free(namestr);
|
||||
|
||||
// check if it's a unicast query - we ignore those
|
||||
|
@ -267,17 +315,42 @@ static int process_mdns_pkt(struct mdnsd *svr, struct mdns_pkt *pkt, struct mdns
|
|||
continue;
|
||||
}
|
||||
|
||||
// see if it is in the answers
|
||||
if (rr_entry_find(pkt->rr_ans, qn->name, qn->type)) {
|
||||
DEBUG_PRINTF("our record is already in their answers\n");
|
||||
continue;
|
||||
num_ans_added = populate_answers(svr, &reply->rr_ans, qn->name, qn->type);
|
||||
reply->num_ans_rr += num_ans_added;
|
||||
|
||||
DEBUG_PRINTF("added %d answers\n", num_ans_added);
|
||||
}
|
||||
|
||||
// remove our replies if they were already in their answers
|
||||
ans = NULL; prev_ans = NULL;
|
||||
for (ans = reply->rr_ans; ans; ) {
|
||||
struct rr_list *next_ans = ans->next;
|
||||
struct rr_entry *known_ans = rr_entry_match(pkt->rr_ans, ans->e);
|
||||
|
||||
// discard answers that have at least half of the actual TTL
|
||||
if (known_ans != NULL && known_ans->ttl >= ans->e->ttl / 2) {
|
||||
char *namestr = nlabel_to_str(ans->e->name);
|
||||
DEBUG_PRINTF("removing answer for %s\n", namestr);
|
||||
free(namestr);
|
||||
|
||||
// check if list item is head
|
||||
if (prev_ans == NULL)
|
||||
reply->rr_ans = ans->next;
|
||||
else
|
||||
prev_ans->next = ans->next;
|
||||
free(ans);
|
||||
|
||||
ans = prev_ans;
|
||||
|
||||
// adjust answer count
|
||||
reply->num_ans_rr--;
|
||||
}
|
||||
|
||||
reply->num_ans_rr += populate_answers(svr, &reply->rr_ans, qn->name, qn->type);
|
||||
|
||||
DEBUG_PRINTF("adding %d answers\n", reply->num_ans_rr);
|
||||
prev_ans = ans;
|
||||
ans = next_ans;
|
||||
}
|
||||
|
||||
|
||||
// see if we can match additional records for answers
|
||||
add_related_rr(svr, reply->rr_ans, reply);
|
||||
|
||||
|
@ -292,47 +365,127 @@ static int process_mdns_pkt(struct mdnsd *svr, struct mdns_pkt *pkt, struct mdns
|
|||
return 0;
|
||||
}
|
||||
|
||||
int create_pipe(int handles[2]) {
|
||||
#ifdef _WIN32
|
||||
SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||
struct sockaddr_in serv_addr;
|
||||
int len;
|
||||
|
||||
if (sock == INVALID_SOCKET) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(&serv_addr, 0, sizeof(serv_addr));
|
||||
serv_addr.sin_family = AF_INET;
|
||||
serv_addr.sin_port = htons(0);
|
||||
serv_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
||||
if (bind(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) == SOCKET_ERROR) {
|
||||
closesocket(sock);
|
||||
return -1;
|
||||
}
|
||||
if (listen(sock, 1) == SOCKET_ERROR) {
|
||||
closesocket(sock);
|
||||
return -1;
|
||||
}
|
||||
len = sizeof(serv_addr);
|
||||
if (getsockname(sock, (SOCKADDR*)&serv_addr, &len) == SOCKET_ERROR) {
|
||||
closesocket(sock);
|
||||
return -1;
|
||||
}
|
||||
if ((handles[1] = socket(PF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
|
||||
closesocket(sock);
|
||||
return -1;
|
||||
}
|
||||
if (connect(handles[1], (struct sockaddr*)&serv_addr, len) == SOCKET_ERROR) {
|
||||
closesocket(sock);
|
||||
return -1;
|
||||
}
|
||||
if ((handles[0] = accept(sock, (struct sockaddr*)&serv_addr, &len)) == INVALID_SOCKET) {
|
||||
closesocket((SOCKET)handles[1]);
|
||||
handles[1] = INVALID_SOCKET;
|
||||
closesocket(sock);
|
||||
return -1;
|
||||
}
|
||||
closesocket(sock);
|
||||
return 0;
|
||||
#else
|
||||
return pipe(handles);
|
||||
#endif
|
||||
}
|
||||
|
||||
int read_pipe(int s, char* buf, int len) {
|
||||
#ifdef _WIN32
|
||||
int ret = recv(s, buf, len, 0);
|
||||
if (ret < 0 && WSAGetLastError() == WSAECONNRESET) {
|
||||
ret = 0;
|
||||
}
|
||||
return ret;
|
||||
#else
|
||||
return read(s, buf, len);
|
||||
#endif
|
||||
}
|
||||
|
||||
int write_pipe(int s, char* buf, int len) {
|
||||
#ifdef _WIN32
|
||||
return send(s, buf, len, 0);
|
||||
#else
|
||||
return write(s, buf, len);
|
||||
#endif
|
||||
}
|
||||
|
||||
int close_pipe(int s) {
|
||||
#ifdef _WIN32
|
||||
return closesocket(s);
|
||||
#else
|
||||
return close(s);
|
||||
#endif
|
||||
}
|
||||
|
||||
// main loop to receive, process and send out MDNS replies
|
||||
// also handles MDNS service announces
|
||||
static void main_loop(struct mdnsd *svr) {
|
||||
fd_set sockfd_set;
|
||||
int max_fd = svr->sockfd;
|
||||
char notify_buf[2]; // buffer for reading of notify_pipe
|
||||
struct mdns_pkt *mdns_reply;
|
||||
struct mdns_pkt *mdns;
|
||||
struct rr_list *svc_le;
|
||||
|
||||
void *pkt_buffer = malloc(PACKET_SIZE);
|
||||
|
||||
if (svr->notify_pipe[0] > max_fd)
|
||||
max_fd = svr->notify_pipe[0];
|
||||
|
||||
struct mdns_pkt *mdns_reply = malloc(sizeof(struct mdns_pkt));
|
||||
mdns_reply = malloc(sizeof(struct mdns_pkt));
|
||||
memset(mdns_reply, 0, sizeof(struct mdns_pkt));
|
||||
|
||||
while (! svr->stop_flag) {
|
||||
struct timeval tv = {
|
||||
.tv_sec = 0,
|
||||
.tv_usec = 0,
|
||||
};
|
||||
|
||||
FD_ZERO(&sockfd_set);
|
||||
FD_SET(svr->sockfd, &sockfd_set);
|
||||
FD_SET(svr->notify_pipe[0], &sockfd_set);
|
||||
select(max_fd + 1, &sockfd_set, NULL, NULL, &tv);
|
||||
select(max_fd + 1, &sockfd_set, NULL, NULL, NULL);
|
||||
|
||||
if (FD_ISSET(svr->sockfd, &sockfd_set)) {
|
||||
if (FD_ISSET(svr->notify_pipe[0], &sockfd_set)) {
|
||||
// flush the notify_pipe
|
||||
read_pipe(svr->notify_pipe[0], (char*)¬ify_buf, 1);
|
||||
} else if (FD_ISSET(svr->sockfd, &sockfd_set)) {
|
||||
struct sockaddr_in fromaddr;
|
||||
socklen_t sockaddr_size = sizeof(struct sockaddr_in);
|
||||
|
||||
ssize_t recvsize = recvfrom(svr->sockfd, pkt_buffer, PACKET_SIZE, 0,
|
||||
ssize_t recvsize = recvfrom(svr->sockfd, pkt_buffer, PACKET_SIZE, 0,
|
||||
(struct sockaddr *) &fromaddr, &sockaddr_size);
|
||||
if (recvsize < 0) {
|
||||
log_message(LOG_ERR, "recv(): %m");
|
||||
}
|
||||
|
||||
DEBUG_PRINTF("data from=%s size=%ld\n", inet_ntoa(fromaddr.sin_addr), (long) recvsize);
|
||||
struct mdns_pkt *mdns = mdns_parse_pkt(pkt_buffer, recvsize);
|
||||
mdns = mdns_parse_pkt(pkt_buffer, recvsize);
|
||||
if (mdns != NULL) {
|
||||
if (process_mdns_pkt(svr, mdns, mdns_reply)) {
|
||||
size_t replylen = mdns_encode_pkt(mdns_reply, pkt_buffer, PACKET_SIZE);
|
||||
send_packet(svr->sockfd, pkt_buffer, replylen);
|
||||
} else if (mdns->num_qn == 0) {
|
||||
DEBUG_PRINTF("(no questions in packet)\n\n");
|
||||
}
|
||||
|
||||
mdns_pkt_destroy(mdns);
|
||||
|
@ -342,17 +495,18 @@ static void main_loop(struct mdnsd *svr) {
|
|||
// send out announces
|
||||
while (1) {
|
||||
struct rr_entry *ann_e = NULL;
|
||||
char *namestr;
|
||||
|
||||
// extract from head of list
|
||||
pthread_mutex_lock(&svr->data_lock);
|
||||
if (svr->announce)
|
||||
if (svr->announce)
|
||||
ann_e = rr_list_remove(&svr->announce, svr->announce->e);
|
||||
pthread_mutex_unlock(&svr->data_lock);
|
||||
|
||||
if (! ann_e)
|
||||
break;
|
||||
|
||||
char *namestr = nlabel_to_str(ann_e->name);
|
||||
namestr = nlabel_to_str(ann_e->name);
|
||||
DEBUG_PRINTF("sending announce for %s\n", namestr);
|
||||
free(namestr);
|
||||
|
||||
|
@ -363,13 +517,45 @@ static void main_loop(struct mdnsd *svr) {
|
|||
send_packet(svr->sockfd, pkt_buffer, replylen);
|
||||
}
|
||||
}
|
||||
|
||||
// send out bye-bye for terminating services
|
||||
while (1) {
|
||||
struct rr_entry *leave_e = NULL;
|
||||
char *namestr;
|
||||
|
||||
pthread_mutex_lock(&svr->data_lock);
|
||||
if (svr->leave)
|
||||
leave_e = rr_list_remove(&svr->leave, svr->leave->e);
|
||||
pthread_mutex_unlock(&svr->data_lock);
|
||||
|
||||
if (!leave_e)
|
||||
break;
|
||||
|
||||
mdns_init_reply(mdns_reply, 0);
|
||||
|
||||
namestr = nlabel_to_str(leave_e->name);
|
||||
DEBUG_PRINTF("sending bye-bye for %s\n", namestr);
|
||||
free(namestr);
|
||||
|
||||
leave_e->ttl = 0;
|
||||
mdns_reply->num_ans_rr += rr_list_append(&mdns_reply->rr_ans, leave_e);
|
||||
|
||||
// send out packet
|
||||
if (mdns_reply->num_ans_rr > 0) {
|
||||
size_t replylen = mdns_encode_pkt(mdns_reply, pkt_buffer, PACKET_SIZE);
|
||||
send_packet(svr->sockfd, pkt_buffer, replylen);
|
||||
}
|
||||
|
||||
rr_entry_destroy(leave_e->data.PTR.entry);
|
||||
rr_entry_destroy(leave_e);
|
||||
}
|
||||
}
|
||||
|
||||
// main thread terminating. send out "goodbye packets" for services
|
||||
mdns_init_reply(mdns_reply, 0);
|
||||
|
||||
pthread_mutex_lock(&svr->data_lock);
|
||||
struct rr_list *svc_le = svr->services;
|
||||
svc_le = svr->services;
|
||||
for (; svc_le; svc_le = svc_le->next) {
|
||||
// set TTL to zero
|
||||
svc_le->e->ttl = 0;
|
||||
|
@ -385,11 +571,12 @@ static void main_loop(struct mdnsd *svr) {
|
|||
|
||||
// destroy packet
|
||||
mdns_init_reply(mdns_reply, 0);
|
||||
|
||||
free(mdns_reply);
|
||||
|
||||
free(pkt_buffer);
|
||||
|
||||
close(svr->sockfd);
|
||||
close_pipe(svr->sockfd);
|
||||
|
||||
svr->stop_flag = 2;
|
||||
}
|
||||
|
@ -397,7 +584,7 @@ static void main_loop(struct mdnsd *svr) {
|
|||
/////////////////////////////////////////////////////
|
||||
|
||||
|
||||
void mdnsd_set_hostname(struct mdnsd *svr, const char *hostname, uint32_t ip) {
|
||||
void mdnsd_set_hostname(struct mdnsd *svr, const char *hostname, struct in_addr addr) {
|
||||
struct rr_entry *a_e = NULL,
|
||||
*nsec_e = NULL;
|
||||
|
||||
|
@ -405,9 +592,10 @@ void mdnsd_set_hostname(struct mdnsd *svr, const char *hostname, uint32_t ip) {
|
|||
// dont ask me what happens if the IP changes
|
||||
assert(svr->hostname == NULL);
|
||||
|
||||
a_e = rr_create_a(create_nlabel(hostname), ip);
|
||||
a_e = rr_create_a(create_nlabel(hostname), addr);
|
||||
|
||||
nsec_e = rr_create(create_nlabel(hostname), RR_NSEC);
|
||||
nsec_e->ttl = DEFAULT_TTL_FOR_RECORD_WITH_HOSTNAME;
|
||||
rr_set_nsec(nsec_e, RR_A);
|
||||
|
||||
pthread_mutex_lock(&svr->data_lock);
|
||||
|
@ -417,7 +605,33 @@ void mdnsd_set_hostname(struct mdnsd *svr, const char *hostname, uint32_t ip) {
|
|||
pthread_mutex_unlock(&svr->data_lock);
|
||||
}
|
||||
|
||||
struct mdns_service *mdnsd_register_svc(struct mdnsd *svr, const char *instance_name,
|
||||
void mdnsd_set_hostname_v6(struct mdnsd *svr, const char *hostname, struct in6_addr *addr) {
|
||||
struct rr_entry *aaaa_e = NULL, *nsec_e = NULL;
|
||||
|
||||
// currently can't be called twice
|
||||
// dont ask me what happens if the IP changes
|
||||
assert(svr->hostname == NULL);
|
||||
|
||||
aaaa_e = rr_create_aaaa(create_nlabel(hostname), addr); // 120 seconds automatically
|
||||
|
||||
nsec_e = rr_create(create_nlabel(hostname), RR_NSEC);
|
||||
nsec_e->ttl = DEFAULT_TTL_FOR_RECORD_WITH_HOSTNAME; // set to 120 seconds (default is 4500)
|
||||
rr_set_nsec(nsec_e, RR_AAAA);
|
||||
|
||||
pthread_mutex_lock(&svr->data_lock);
|
||||
svr->hostname = create_nlabel(hostname);
|
||||
rr_group_add(&svr->group, aaaa_e);
|
||||
rr_group_add(&svr->group, nsec_e);
|
||||
pthread_mutex_unlock(&svr->data_lock);
|
||||
}
|
||||
|
||||
void mdnsd_add_rr(struct mdnsd *svr, struct rr_entry *rr) {
|
||||
pthread_mutex_lock(&svr->data_lock);
|
||||
rr_group_add(&svr->group, rr);
|
||||
pthread_mutex_unlock(&svr->data_lock);
|
||||
}
|
||||
|
||||
struct mdns_service *mdnsd_register_svc(struct mdnsd *svr, const char *instance_name,
|
||||
const char *type, uint16_t port, const char *hostname, const char *txt[]) {
|
||||
struct rr_entry *txt_e = NULL,
|
||||
*srv_e = NULL,
|
||||
|
@ -430,7 +644,7 @@ struct mdns_service *mdnsd_register_svc(struct mdnsd *svr, const char *instance_
|
|||
|
||||
// combine service name
|
||||
type_nlabel = create_nlabel(type);
|
||||
inst_nlabel = create_nlabel(instance_name);
|
||||
inst_nlabel = create_label(instance_name);
|
||||
nlabel = join_nlabel(inst_nlabel, type_nlabel);
|
||||
|
||||
// create TXT record
|
||||
|
@ -479,31 +693,78 @@ struct mdns_service *mdnsd_register_svc(struct mdnsd *svr, const char *instance_
|
|||
free(inst_nlabel);
|
||||
|
||||
// notify server
|
||||
write(svr->notify_pipe[1], ".", 1);
|
||||
write_pipe(svr->notify_pipe[1], ".", 1);
|
||||
|
||||
return service;
|
||||
}
|
||||
|
||||
void mdns_service_remove(struct mdnsd *svr, struct mdns_service *svc) {
|
||||
struct rr_list *rr;
|
||||
|
||||
assert(svr != NULL && svc != NULL);
|
||||
|
||||
// modify lists here
|
||||
pthread_mutex_lock(&svr->data_lock);
|
||||
|
||||
for (rr = svc->entries; rr; rr = rr->next) {
|
||||
struct rr_group *g;
|
||||
struct rr_entry *ptr_e;
|
||||
|
||||
// remove entry from groups and destroy entries that are not PTR
|
||||
if ((g = rr_group_find(svr->group, rr->e->name)) != NULL) {
|
||||
rr_list_remove(&g->rr, rr->e);
|
||||
}
|
||||
|
||||
// remove PTR and BPTR related to this SVC
|
||||
if ((ptr_e = rr_entry_remove(svr->group, rr->e, RR_PTR)) != NULL) {
|
||||
struct rr_entry *bptr_e;
|
||||
|
||||
// remove PTR from announce and services
|
||||
rr_list_remove(&svr->announce, ptr_e);
|
||||
rr_list_remove(&svr->services, ptr_e);
|
||||
|
||||
// find BPTR and remove it from groups
|
||||
bptr_e = rr_entry_remove(svr->group, ptr_e, RR_PTR);
|
||||
rr_entry_destroy(bptr_e);
|
||||
|
||||
// add PTR to list of announces for leaving
|
||||
rr_list_append(&svr->leave, ptr_e);
|
||||
} else {
|
||||
// destroy entries not needed for sending "leave" packet
|
||||
rr_entry_destroy(rr->e);
|
||||
}
|
||||
}
|
||||
|
||||
// remove all empty groups
|
||||
rr_group_clean(&svr->group);
|
||||
|
||||
// destroy this service entries
|
||||
rr_list_destroy(svc->entries, 0);
|
||||
free(svc);
|
||||
|
||||
pthread_mutex_unlock(&svr->data_lock);
|
||||
}
|
||||
|
||||
void mdns_service_destroy(struct mdns_service *srv) {
|
||||
assert(srv != NULL);
|
||||
rr_list_destroy(srv->entries, 0);
|
||||
free(srv);
|
||||
}
|
||||
|
||||
struct mdnsd *mdnsd_start() {
|
||||
struct mdnsd *mdnsd_start(struct in_addr host) {
|
||||
pthread_t tid;
|
||||
pthread_attr_t attr;
|
||||
|
||||
struct mdnsd *server = malloc(sizeof(struct mdnsd));
|
||||
memset(server, 0, sizeof(struct mdnsd));
|
||||
|
||||
if (pipe(server->notify_pipe) != 0) {
|
||||
if (create_pipe(server->notify_pipe) != 0) {
|
||||
log_message(LOG_ERR, "pipe(): %m\n");
|
||||
free(server);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
server->sockfd = create_recv_sock();
|
||||
server->sockfd = create_recv_sock(host.s_addr);
|
||||
if (server->sockfd < 0) {
|
||||
log_message(LOG_ERR, "unable to create recv socket");
|
||||
free(server);
|
||||
|
@ -521,31 +782,34 @@ struct mdnsd *mdnsd_start() {
|
|||
free(server);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
return server;
|
||||
}
|
||||
|
||||
void mdnsd_stop(struct mdnsd *s) {
|
||||
struct timeval tv;
|
||||
|
||||
if (!s) return;
|
||||
|
||||
tv.tv_sec = 0;
|
||||
tv.tv_usec = 500*1000;
|
||||
|
||||
assert(s != NULL);
|
||||
|
||||
struct timeval tv = {
|
||||
.tv_sec = 0,
|
||||
.tv_usec = 500 * 1000,
|
||||
};
|
||||
|
||||
s->stop_flag = 1;
|
||||
write(s->notify_pipe[1], ".", 1);
|
||||
write_pipe(s->notify_pipe[1], ".", 1);
|
||||
|
||||
while (s->stop_flag != 2)
|
||||
select(0, NULL, NULL, NULL, &tv);
|
||||
|
||||
close(s->notify_pipe[0]);
|
||||
close(s->notify_pipe[1]);
|
||||
close_pipe(s->notify_pipe[0]);
|
||||
close_pipe(s->notify_pipe[1]);
|
||||
|
||||
pthread_mutex_destroy(&s->data_lock);
|
||||
rr_group_destroy(s->group);
|
||||
rr_list_destroy(s->announce, 0);
|
||||
rr_list_destroy(s->services, 0);
|
||||
rr_list_destroy(s->leave, 0);
|
||||
|
||||
if (s->hostname)
|
||||
free(s->hostname);
|
||||
|
|
48
mdnsd.h
48
mdnsd.h
|
@ -1,39 +1,54 @@
|
|||
/*
|
||||
* tinysvcmdns - a tiny MDNS implementation for publishing services
|
||||
* Copyright (C) 2011 Darell Tan
|
||||
* All rights reserved.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef __MDNSD_H__
|
||||
#define __MDNSD_H__
|
||||
|
||||
#include <stdint.h>
|
||||
#ifdef _WIN32
|
||||
#include <inaddr.h>
|
||||
#else
|
||||
#include <netinet/in.h>
|
||||
#endif
|
||||
|
||||
struct mdnsd;
|
||||
struct mdns_service;
|
||||
|
||||
|
||||
// starts a MDNS responder instance
|
||||
// returns NULL if unsuccessful
|
||||
struct mdnsd *mdnsd_start();
|
||||
struct mdnsd *mdnsd_start(struct in_addr host);
|
||||
|
||||
// stops the given MDNS responder instance
|
||||
void mdnsd_stop(struct mdnsd *s);
|
||||
|
||||
// sets the hostname for the given MDNS responder instance
|
||||
void mdnsd_set_hostname(struct mdnsd *svr, const char *hostname, uint32_t ip);
|
||||
void mdnsd_set_hostname(struct mdnsd *svr, const char *hostname, struct in_addr addr);
|
||||
|
||||
// registers a service with the MDNS responder instance
|
||||
struct mdns_service *mdnsd_register_svc(struct mdnsd *svr, const char *instance_name,
|
||||
|
@ -42,5 +57,8 @@ struct mdns_service *mdnsd_register_svc(struct mdnsd *svr, const char *instance_
|
|||
// destroys the mdns_service struct returned by mdnsd_register_svc()
|
||||
void mdns_service_destroy(struct mdns_service *srv);
|
||||
|
||||
// remove AND destroys the mdns_service struct returned by mdnsd_register_svc()
|
||||
void mdns_service_remove(struct mdnsd *svr, struct mdns_service *svc);
|
||||
|
||||
|
||||
#endif/*!__MDNSD_H__*/
|
||||
|
|
|
@ -0,0 +1,323 @@
|
|||
/*
|
||||
* tinysvcmdns - a tiny MDNS implementation for publishing services
|
||||
* Copyright (C) 2011 Darell Tan
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <signal.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <winsock2.h>
|
||||
#include <in6addr.h>
|
||||
#include <ws2tcpip.h>
|
||||
typedef uint32_t in_addr_t;
|
||||
#define strcasecmp stricmp
|
||||
#elif defined (linux) || defined (__FreeBSD__) || defined (sun)
|
||||
#include <unistd.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <net/if.h>
|
||||
#include <strings.h>
|
||||
#if defined (__FreeBSD__) || defined (sun)
|
||||
#include <ifaddrs.h>
|
||||
#include <net/if_dl.h>
|
||||
#include <net/if_types.h>
|
||||
#endif
|
||||
#if defined (sun)
|
||||
#include <sys/sockio.h>
|
||||
#endif
|
||||
#elif defined (__APPLE__)
|
||||
#include <unistd.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <net/if.h>
|
||||
#include <ifaddrs.h>
|
||||
#endif
|
||||
|
||||
#include "mdns.h"
|
||||
#include "mdnsd.h"
|
||||
|
||||
struct mdns_service *svc;
|
||||
struct mdnsd *svr;
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
#ifdef WIN32
|
||||
static void winsock_init(void) {
|
||||
WSADATA wsaData;
|
||||
WORD wVersionRequested = MAKEWORD(2, 2);
|
||||
int WSerr = WSAStartup(wVersionRequested, &wsaData);
|
||||
if (WSerr != 0) exit(1);
|
||||
}
|
||||
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void winsock_close(void) {
|
||||
WSACleanup();
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
#define MAX_INTERFACES 256
|
||||
#define DEFAULT_INTERFACE 1
|
||||
#if !defined(WIN32)
|
||||
#define INVALID_SOCKET (-1)
|
||||
#endif
|
||||
static in_addr_t get_localhost(char **name)
|
||||
{
|
||||
#ifdef WIN32
|
||||
char buf[256];
|
||||
struct hostent *h = NULL;
|
||||
struct sockaddr_in LocalAddr;
|
||||
|
||||
memset(&LocalAddr, 0, sizeof(LocalAddr));
|
||||
|
||||
gethostname(buf, 256);
|
||||
h = gethostbyname(buf);
|
||||
|
||||
if (name) *name = strdup(buf);
|
||||
|
||||
if (h != NULL) {
|
||||
memcpy(&LocalAddr.sin_addr, h->h_addr_list[0], 4);
|
||||
return LocalAddr.sin_addr.s_addr;
|
||||
}
|
||||
else return INADDR_ANY;
|
||||
#elif defined (__APPLE__) || defined(__FreeBSD__)
|
||||
struct ifaddrs *ifap, *ifa;
|
||||
|
||||
if (name) {
|
||||
*name = malloc(256);
|
||||
gethostname(*name, 256);
|
||||
}
|
||||
|
||||
if (getifaddrs(&ifap) != 0) return INADDR_ANY;
|
||||
|
||||
/* cycle through available interfaces */
|
||||
for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
|
||||
/* Skip loopback, point-to-point and down interfaces,
|
||||
* except don't skip down interfaces
|
||||
* if we're trying to get a list of configurable interfaces. */
|
||||
if ((ifa->ifa_flags & IFF_LOOPBACK) ||
|
||||
(!( ifa->ifa_flags & IFF_UP))) {
|
||||
continue;
|
||||
}
|
||||
if (ifa->ifa_addr->sa_family == AF_INET) {
|
||||
/* We don't want the loopback interface. */
|
||||
if (((struct sockaddr_in *)(ifa->ifa_addr))->sin_addr.s_addr ==
|
||||
htonl(INADDR_LOOPBACK)) {
|
||||
continue;
|
||||
}
|
||||
return ((struct sockaddr_in *)(ifa->ifa_addr))->sin_addr.s_addr;
|
||||
break;
|
||||
}
|
||||
}
|
||||
freeifaddrs(ifap);
|
||||
|
||||
return INADDR_ANY;
|
||||
#else
|
||||
char szBuffer[MAX_INTERFACES * sizeof (struct ifreq)];
|
||||
struct ifconf ifConf;
|
||||
struct ifreq ifReq;
|
||||
int nResult;
|
||||
long unsigned int i;
|
||||
int LocalSock;
|
||||
struct sockaddr_in LocalAddr;
|
||||
int j = 0;
|
||||
|
||||
if (name) {
|
||||
*name = malloc(256);
|
||||
gethostname(*name, 256);
|
||||
}
|
||||
|
||||
/* purify */
|
||||
memset(&ifConf, 0, sizeof(ifConf));
|
||||
memset(&ifReq, 0, sizeof(ifReq));
|
||||
memset(szBuffer, 0, sizeof(szBuffer));
|
||||
memset(&LocalAddr, 0, sizeof(LocalAddr));
|
||||
|
||||
/* Create an unbound datagram socket to do the SIOCGIFADDR ioctl on. */
|
||||
LocalSock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||
if (LocalSock == INVALID_SOCKET) return false;
|
||||
/* Get the interface configuration information... */
|
||||
ifConf.ifc_len = (int)sizeof szBuffer;
|
||||
ifConf.ifc_ifcu.ifcu_buf = (caddr_t) szBuffer;
|
||||
nResult = ioctl(LocalSock, SIOCGIFCONF, &ifConf);
|
||||
if (nResult < 0) {
|
||||
close(LocalSock);
|
||||
return INADDR_ANY;
|
||||
}
|
||||
|
||||
/* Cycle through the list of interfaces looking for IP addresses. */
|
||||
for (i = 0lu; i < (long unsigned int)ifConf.ifc_len && j < DEFAULT_INTERFACE; ) {
|
||||
struct ifreq *pifReq =
|
||||
(struct ifreq *)((caddr_t)ifConf.ifc_req + i);
|
||||
i += sizeof *pifReq;
|
||||
/* See if this is the sort of interface we want to deal with. */
|
||||
memset(ifReq.ifr_name, 0, sizeof(ifReq.ifr_name));
|
||||
strncpy(ifReq.ifr_name, pifReq->ifr_name,
|
||||
sizeof(ifReq.ifr_name) - 1);
|
||||
/* Skip loopback, point-to-point and down interfaces,
|
||||
* except don't skip down interfaces
|
||||
* if we're trying to get a list of configurable interfaces. */
|
||||
ioctl(LocalSock, SIOCGIFFLAGS, &ifReq);
|
||||
if ((ifReq.ifr_flags & IFF_LOOPBACK) ||
|
||||
(!(ifReq.ifr_flags & IFF_UP))) {
|
||||
continue;
|
||||
}
|
||||
if (pifReq->ifr_addr.sa_family == AF_INET) {
|
||||
/* Get a pointer to the address...*/
|
||||
memcpy(&LocalAddr, &pifReq->ifr_addr,
|
||||
sizeof pifReq->ifr_addr);
|
||||
/* We don't want the loopback interface. */
|
||||
if (LocalAddr.sin_addr.s_addr ==
|
||||
htonl(INADDR_LOOPBACK)) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
/* increment j if we found an address which is not loopback
|
||||
* and is up */
|
||||
j++;
|
||||
}
|
||||
close(LocalSock);
|
||||
|
||||
return LocalAddr.sin_addr.s_addr;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static int print_usage(void) {
|
||||
printf("[host <ip>] <identity> <type> <port> <txt> [txt] ... [txt]\n");
|
||||
#ifdef WIN32
|
||||
winsock_close();
|
||||
#endif
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void sighandler(int signum) {
|
||||
mdnsd_stop(svr);
|
||||
#ifdef WIN32
|
||||
winsock_close();
|
||||
#endif
|
||||
exit(0);
|
||||
}
|
||||
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/* */
|
||||
/*---------------------------------------------------------------------------*/
|
||||
#ifdef MDNS_SVC
|
||||
int mdns_server(int argc, char *argv[]) {
|
||||
#else
|
||||
int main(int argc, char *argv[]) {
|
||||
#endif
|
||||
char type[255];
|
||||
int port;
|
||||
const char **txt;
|
||||
struct in_addr host;
|
||||
char *hostname;
|
||||
int opt = 0;
|
||||
|
||||
signal(SIGINT, sighandler);
|
||||
signal(SIGTERM, sighandler);
|
||||
#if defined(SIGPIPE)
|
||||
signal(SIGPIPE, SIG_IGN);
|
||||
#endif
|
||||
#if defined(SIGQUIT)
|
||||
signal(SIGQUIT, sighandler);
|
||||
#endif
|
||||
#if defined(SIGHUP)
|
||||
signal(SIGHUP, sighandler);
|
||||
#endif
|
||||
|
||||
#ifdef WIN32
|
||||
winsock_init();
|
||||
#endif
|
||||
|
||||
host.s_addr = get_localhost(&hostname);
|
||||
hostname = realloc(hostname, strlen(hostname) + strlen(".local") + 1);
|
||||
strcat(hostname, ".local");
|
||||
|
||||
if (!strcasecmp(argv[1], "host")) {
|
||||
host.s_addr = inet_addr(argv[2]);
|
||||
opt = 2;
|
||||
}
|
||||
|
||||
if (host.s_addr == INADDR_ANY) {
|
||||
printf("cannot find host address\n");
|
||||
free(hostname);
|
||||
return print_usage();
|
||||
}
|
||||
|
||||
if (argc < 5+opt) return print_usage();
|
||||
|
||||
port = atoi(argv[3+opt]);
|
||||
|
||||
svr = mdnsd_start(host);
|
||||
if (svr == NULL) return print_usage();
|
||||
|
||||
txt = malloc((argc - 4 + 1 - opt) * sizeof(char**));
|
||||
memcpy(txt, argv + 4 + opt, (argc - 4 - opt) * sizeof(char**));
|
||||
txt[argc - 4 - opt] = NULL;
|
||||
|
||||
mdnsd_set_hostname(svr, hostname, host);
|
||||
|
||||
sprintf(type, "%s.local", argv[2 + opt]);
|
||||
|
||||
printf("host : %s\nidentity : %s\ntype : %s\n"
|
||||
"ip : %s\nport : %u\n",
|
||||
hostname, argv[1 + opt], type, inet_ntoa(host), port);
|
||||
|
||||
free(hostname);
|
||||
|
||||
svc = mdnsd_register_svc(svr, argv[1 + opt], type, port, NULL, txt);
|
||||
// or, to remove service call: mdns_service_remove(svr, svc);
|
||||
mdns_service_destroy(svc);
|
||||
|
||||
|
||||
#ifdef WIN32
|
||||
Sleep(INFINITE);
|
||||
#else
|
||||
pause();
|
||||
#endif
|
||||
|
||||
mdnsd_stop(svr);
|
||||
|
||||
#ifdef WIN32
|
||||
winsock_close();
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue