Nut/OS  4.10.3
API Reference
discover.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2001-2005 by egnite Software GmbH. All rights reserved.
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions
00006  * are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the copyright holders nor the names of
00014  *    contributors may be used to endorse or promote products derived
00015  *    from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY EGNITE SOFTWARE GMBH AND CONTRIBUTORS
00018  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00019  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00020  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EGNITE
00021  * SOFTWARE GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00022  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00023  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00024  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00025  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00026  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00027  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00028  * SUCH DAMAGE.
00029  *
00030  * For additional information see http://www.ethernut.de/
00031  *
00032  */
00033 
00034 /*
00035  * $Log$
00036  * Revision 1.6  2009/02/13 14:52:05  haraldkipp
00037  * Include memdebug.h for heap management debugging support.
00038  *
00039  * Revision 1.5  2009/02/06 15:37:40  haraldkipp
00040  * Added stack space multiplier and addend. Adjusted stack space.
00041  *
00042  * Revision 1.4  2009/01/17 11:26:52  haraldkipp
00043  * Getting rid of two remaining BSD types in favor of stdint.
00044  * Replaced 'u_int' by 'unsinged int' and 'uptr_t' by 'uintptr_t'.
00045  *
00046  * Revision 1.3  2009/01/16 17:03:50  haraldkipp
00047  * Configurable discovery protocol version and port plus
00048  * configurable service thread stack size. The new version 1.1
00049  * allows host names up to 98 characters. Added some code
00050  * to make sure, that nothing is overwritten with version 1.0
00051  * protocol and too long host names. Protocol version 1.0
00052  * is still the default.
00053  *
00054  * Revision 1.2  2008/08/11 07:00:35  haraldkipp
00055  * BSD types replaced by stdint types (feature request #1282721).
00056  *
00057  * Revision 1.1  2006/09/07 09:06:17  haraldkipp
00058  * Discovery service added.
00059  *
00060  */
00061 
00062 #include <sys/confnet.h>
00063 #include <sys/confos.h>
00064 #include <sys/thread.h>
00065 #include <sys/timer.h>
00066 #include <sys/socket.h>
00067 
00068 #include <netinet/in.h>
00069 #include <net/if_var.h>
00070 
00071 #include <stdlib.h>
00072 #include <string.h>
00073 #include <memdebug.h>
00074 
00075 #include <pro/discover.h>
00076 
00081 
00082 #ifndef NUT_THREAD_DISTSTACK
00083 #if defined(__AVR__)
00084 #if defined(__GNUC__)
00085 /* avr-gcc size optimized code uses 124 bytes. */
00086 #define NUT_THREAD_DISTSTACK  224
00087 #else
00088 /* icc-avr v7.19 uses 244 bytes. */
00089 #define NUT_THREAD_DISTSTACK  384
00090 #endif
00091 #elif defined(__AVR32__)
00092 #define NUT_THREAD_DISTSTACK 512
00093 #else
00094 /* arm-elf-gcc used 232 bytes with size optimized, 476 bytes with debug code. */
00095 #define NUT_THREAD_DISTSTACK  320
00096 #endif
00097 #endif
00098 
00099 #ifndef DISCOVERY_PORT
00100 #define DISCOVERY_PORT  9806
00101 #endif
00102 
00103 typedef struct {
00104     uint32_t disopt_ipmask;
00105     uint16_t disopt_port;
00106     unsigned int disopt_flags;
00107 } DISCOVERY_OPTIONS;
00108 
00109 static DISCOVERY_OPTIONS disopt;
00110 static uint32_t xid;
00111 
00112 static int NutDiscoveryHandler(uint32_t ip, uint16_t port, DISCOVERY_TELE * dist, int len);
00113 static NutDiscoveryCallback discovery_callback = NutDiscoveryHandler;
00114 
00122 int NutDiscoveryAnnTele(DISCOVERY_TELE * dist)
00123 {
00124     memset(dist, 0, sizeof(DISCOVERY_TELE));
00125     dist->dist_xid = xid;
00126     dist->dist_type = DIST_ANNOUNCE;
00127     dist->dist_ver = DISCOVERY_VERSION;
00128     memcpy(dist->dist_mac, confnet.cdn_mac, sizeof(dist->dist_mac));
00129     dist->dist_ip_addr = confnet.cdn_ip_addr;
00130     dist->dist_ip_mask = confnet.cdn_ip_mask;
00131     dist->dist_gateway = confnet.cdn_gateway;
00132     dist->dist_cip_addr = confnet.cdn_cip_addr;
00133 #if DISCOVERY_VERSION <= 0x10
00134     memcpy(dist->dist_hostname, confos.hostname, sizeof(dist->dist_hostname));
00135     return sizeof(DISCOVERY_TELE) - sizeof(dist->dist_custom);
00136 #else
00137     dist->dist_appendix[0] = (uint8_t)strlen(confos.hostname);
00138     memcpy(&dist->dist_appendix[1], confos.hostname, dist->dist_appendix[0]);
00139     return sizeof(DISCOVERY_TELE) - sizeof(dist->dist_appendix) + dist->dist_appendix[0] + 1;
00140 #endif
00141 }
00142 
00150 int NutDiscoveryAppConf(DISCOVERY_TELE * dist)
00151 {
00152     memset(confos.hostname, 0, sizeof(confos.hostname));
00153 #if DISCOVERY_VERSION <= 0x10
00154     strncpy(confos.hostname, (char *)dist->dist_hostname, sizeof(confos.hostname) - 1);
00155 #else
00156     memcpy(confos.hostname, &dist->dist_appendix[1], dist->dist_appendix[0]);
00157 #endif
00158     NutSaveConfig();
00159 
00160     memcpy(confnet.cdn_mac, dist->dist_mac, sizeof(confnet.cdn_mac));
00161     confnet.cdn_ip_addr = dist->dist_ip_addr;
00162     confnet.cdn_ip_mask = dist->dist_ip_mask;
00163     confnet.cdn_gateway = dist->dist_gateway;
00164     confnet.cdn_cip_addr = dist->dist_cip_addr;
00165 
00166     return NutNetSaveConfig();
00167 }
00168 
00169 /*
00170  * \brief Default discovery datagram handler.
00171  *
00172  * \param ip    Sender's IP address.
00173  * \param port  Sender's UDP port.
00174  * \param dtel  Pointer to the UDP telegram buffer.
00175  * \param len   UDP telegram size.
00176  */
00177 static int NutDiscoveryHandler(uint32_t ip, uint16_t port, DISCOVERY_TELE * dist, int len)
00178 {
00179     int rc = -1;
00180 #if DISCOVERY_VERSION <= 0x10
00181     size_t minlen = sizeof(DISCOVERY_TELE) - sizeof(dist->dist_custom);
00182 #else
00183     size_t minlen = sizeof(DISCOVERY_TELE) - sizeof(dist->dist_appendix) + 1;
00184 #endif
00185 
00186     /* Check minimum datagram length. */
00187     if (len >= minlen) {
00188         /* 
00189          * Request telegram. 
00190          */
00191         if (dist->dist_type == DIST_REQUEST) {
00192             /* Respond to requests. */
00193             rc = NutDiscoveryAnnTele(dist);
00194         }
00195 
00196         /* 
00197          * Apply telegram. 
00198          */
00199         else if (dist->dist_type == DIST_APPLY
00200                    /* Check exchange ID. */
00201                    && dist->dist_xid == xid
00202                    /* Required protocol version. */
00203                    && dist->dist_ver == DISCOVERY_VERSION) {    
00204             xid += NutGetTickCount();
00205             /* Store configuration. */
00206             rc = NutDiscoveryAppConf(dist);
00207         }
00208     }
00209     return rc;
00210 }
00211 
00212 THREAD(DiscoveryResponder, arg)
00213 {
00214     UDPSOCKET *sock;
00215     DISCOVERY_TELE *dist;
00216     uint32_t raddr;
00217     uint16_t rport;
00218     int len;
00219 
00220     /* Insist on allocating a datagram buffer. */
00221     while ((dist = malloc(sizeof(DISCOVERY_TELE))) == NULL) {
00222         NutSleep(1000);
00223     }
00224 
00225     /* Insist on creating a socket. */
00226     while ((sock = NutUdpCreateSocket(disopt.disopt_port)) == NULL) {
00227         NutSleep(1000);
00228     }
00229 
00230     /* Nut/Net doesn't provide UDP datagram buffering by default. */
00231     {
00232         uint16_t max_ms = sizeof(DISCOVERY_TELE) * 3;
00233 
00234         NutUdpSetSockOpt(sock, SO_RCVBUF, &max_ms, sizeof(max_ms));
00235     }
00236 
00237     /* Create a pseudo random telegram identifier. */
00238     memcpy(&xid, &confnet.cdn_mac[2], sizeof(xid));
00239     xid += NutGetTickCount();
00240 
00241     /* Optionally send initial announce datagram. */
00242     if (disopt.disopt_flags & DISF_INITAL_ANN) {
00243         /* Variable sleep avoids broadcast storms in large installations. */
00244         NutSleep(500 + (xid & 0x7FF));
00245         if ((len = NutDiscoveryAnnTele(dist)) > 0) {
00246             NutUdpSendTo(sock, INADDR_BROADCAST, disopt.disopt_port, dist, len);
00247         }
00248     }
00249 
00250     /* Handle discovery telegrams in an endless loop. */
00251     for (;;) {
00252         len = NutUdpReceiveFrom(sock, &raddr, &rport, dist, sizeof(DISCOVERY_TELE), 0);
00253         /* Updates may be filtered by an IP mask. */
00254         if ((raddr & disopt.disopt_ipmask) == raddr && len > 0 && discovery_callback) {
00255             if ((len = discovery_callback(raddr, rport, dist, len)) > 0) {
00256                 /* Broadcast response if destination is unreachable. */
00257                 if ((raddr & confnet.cdn_ip_mask) != (confnet.cdn_ip_addr & confnet.cdn_ip_mask)) {
00258                     raddr = INADDR_BROADCAST;
00259                 }
00260                 NutUdpSendTo(sock, raddr, disopt.disopt_port, dist, len);
00261             }
00262         }
00263         /* Avoid system blocking by datagram storms. */
00264         NutSleep(100);
00265     }
00266 }
00267 
00282 NutDiscoveryCallback NutRegisterDiscoveryCallback(NutDiscoveryCallback func)
00283 {
00284     NutDiscoveryCallback rc = discovery_callback;
00285 
00286     discovery_callback = func;
00287 
00288     return rc;
00289 }
00290 
00308 int NutRegisterDiscovery(uint32_t ipmask, uint16_t port, unsigned int flags)
00309 {
00310     static HANDLE tid = NULL;
00311 
00312     if (tid == NULL) {
00313         disopt.disopt_ipmask = ipmask;
00314         disopt.disopt_port = port ? port : DISCOVERY_PORT;
00315         disopt.disopt_flags = flags;
00316         tid = NutThreadCreate("udisc", DiscoveryResponder, NULL, 
00317             (NUT_THREAD_DISTSTACK * NUT_THREAD_STACK_MULT) + NUT_THREAD_STACK_ADD);
00318         if (tid) {
00319             return 0;
00320         }
00321     }
00322     return -1;
00323 }
00324