00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
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
00085 #define NUT_THREAD_DISTSTACK 224
00086 #else
00087
00088 #define NUT_THREAD_DISTSTACK 320
00089 #endif
00090 #endif
00091
00092 #ifndef DISCOVERY_PORT
00093 #define DISCOVERY_PORT 9806
00094 #endif
00095
00096 typedef struct {
00097 uint32_t disopt_ipmask;
00098 uint16_t disopt_port;
00099 unsigned int disopt_flags;
00100 } DISCOVERY_OPTIONS;
00101
00102 static DISCOVERY_OPTIONS disopt;
00103 static uint32_t xid;
00104
00105 static int NutDiscoveryHandler(uint32_t ip, uint16_t port, DISCOVERY_TELE * dist, int len);
00106 static NutDiscoveryCallback discovery_callback = NutDiscoveryHandler;
00107
00115 int NutDiscoveryAnnTele(DISCOVERY_TELE * dist)
00116 {
00117 memset(dist, 0, sizeof(DISCOVERY_TELE));
00118 dist->dist_xid = xid;
00119 dist->dist_type = DIST_ANNOUNCE;
00120 dist->dist_ver = DISCOVERY_VERSION;
00121 memcpy(dist->dist_mac, confnet.cdn_mac, sizeof(dist->dist_mac));
00122 dist->dist_ip_addr = confnet.cdn_ip_addr;
00123 dist->dist_ip_mask = confnet.cdn_ip_mask;
00124 dist->dist_gateway = confnet.cdn_gateway;
00125 dist->dist_cip_addr = confnet.cdn_cip_addr;
00126 #if DISCOVERY_VERSION <= 0x10
00127 memcpy(dist->dist_hostname, confos.hostname, sizeof(dist->dist_hostname));
00128 return sizeof(DISCOVERY_TELE) - sizeof(dist->dist_custom);
00129 #else
00130 dist->dist_appendix[0] = (uint8_t)strlen(confos.hostname);
00131 memcpy(&dist->dist_appendix[1], confos.hostname, dist->dist_appendix[0]);
00132 return sizeof(DISCOVERY_TELE) - sizeof(dist->dist_appendix) + dist->dist_appendix[0] + 1;
00133 #endif
00134 }
00135
00143 int NutDiscoveryAppConf(DISCOVERY_TELE * dist)
00144 {
00145 memset(confos.hostname, 0, sizeof(confos.hostname));
00146 #if DISCOVERY_VERSION <= 0x10
00147 strncpy(confos.hostname, (char *)dist->dist_hostname, sizeof(confos.hostname) - 1);
00148 #else
00149 memcpy(confos.hostname, &dist->dist_appendix[1], dist->dist_appendix[0]);
00150 #endif
00151 NutSaveConfig();
00152
00153 memcpy(confnet.cdn_mac, dist->dist_mac, sizeof(confnet.cdn_mac));
00154 confnet.cdn_ip_addr = dist->dist_ip_addr;
00155 confnet.cdn_ip_mask = dist->dist_ip_mask;
00156 confnet.cdn_gateway = dist->dist_gateway;
00157 confnet.cdn_cip_addr = dist->dist_cip_addr;
00158
00159 return NutNetSaveConfig();
00160 }
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170 static int NutDiscoveryHandler(uint32_t ip, uint16_t port, DISCOVERY_TELE * dist, int len)
00171 {
00172 int rc = -1;
00173 #if DISCOVERY_VERSION <= 0x10
00174 size_t minlen = sizeof(DISCOVERY_TELE) - sizeof(dist->dist_custom);
00175 #else
00176 size_t minlen = sizeof(DISCOVERY_TELE) - sizeof(dist->dist_appendix) + 1;
00177 #endif
00178
00179
00180 if (len >= minlen) {
00181
00182
00183
00184 if (dist->dist_type == DIST_REQUEST) {
00185
00186 rc = NutDiscoveryAnnTele(dist);
00187 }
00188
00189
00190
00191
00192 else if (dist->dist_type == DIST_APPLY
00193
00194 && dist->dist_xid == xid
00195
00196 && dist->dist_ver == DISCOVERY_VERSION) {
00197 xid += NutGetTickCount();
00198
00199 rc = NutDiscoveryAppConf(dist);
00200 }
00201 }
00202 return rc;
00203 }
00204
00205 THREAD(DiscoveryResponder, arg)
00206 {
00207 UDPSOCKET *sock;
00208 DISCOVERY_TELE *dist;
00209 uint32_t raddr;
00210 uint16_t rport;
00211 int len;
00212
00213
00214 while ((dist = malloc(sizeof(DISCOVERY_TELE))) == NULL) {
00215 NutSleep(1000);
00216 }
00217
00218
00219 while ((sock = NutUdpCreateSocket(disopt.disopt_port)) == NULL) {
00220 NutSleep(1000);
00221 }
00222
00223
00224 {
00225 uint16_t max_ms = sizeof(DISCOVERY_TELE) * 3;
00226
00227 NutUdpSetSockOpt(sock, SO_RCVBUF, &max_ms, sizeof(max_ms));
00228 }
00229
00230
00231 memcpy(&xid, &confnet.cdn_mac[2], sizeof(xid));
00232 xid += NutGetTickCount();
00233
00234
00235 if (disopt.disopt_flags & DISF_INITAL_ANN) {
00236
00237 NutSleep(500 + (xid & 0x7FF));
00238 if ((len = NutDiscoveryAnnTele(dist)) > 0) {
00239 NutUdpSendTo(sock, INADDR_BROADCAST, disopt.disopt_port, dist, len);
00240 }
00241 }
00242
00243
00244 for (;;) {
00245 len = NutUdpReceiveFrom(sock, &raddr, &rport, dist, sizeof(DISCOVERY_TELE), 0);
00246
00247 if ((raddr & disopt.disopt_ipmask) == raddr && len > 0 && discovery_callback) {
00248 if ((len = discovery_callback(raddr, rport, dist, len)) > 0) {
00249
00250 if ((raddr & confnet.cdn_ip_mask) != (confnet.cdn_ip_addr & confnet.cdn_ip_mask)) {
00251 raddr = INADDR_BROADCAST;
00252 }
00253 NutUdpSendTo(sock, raddr, disopt.disopt_port, dist, len);
00254 }
00255 }
00256
00257 NutSleep(100);
00258 }
00259 }
00260
00275 NutDiscoveryCallback NutRegisterDiscoveryCallback(NutDiscoveryCallback func)
00276 {
00277 NutDiscoveryCallback rc = discovery_callback;
00278
00279 discovery_callback = func;
00280
00281 return rc;
00282 }
00283
00301 int NutRegisterDiscovery(uint32_t ipmask, uint16_t port, unsigned int flags)
00302 {
00303 static HANDLE tid = NULL;
00304
00305 if (tid == NULL) {
00306 disopt.disopt_ipmask = ipmask;
00307 disopt.disopt_port = port ? port : DISCOVERY_PORT;
00308 disopt.disopt_flags = flags;
00309 tid = NutThreadCreate("udisc", DiscoveryResponder, NULL,
00310 (NUT_THREAD_DISTSTACK * NUT_THREAD_STACK_MULT) + NUT_THREAD_STACK_ADD);
00311 if (tid) {
00312 return 0;
00313 }
00314 }
00315 return -1;
00316 }
00317