Peer.hpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2025-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_PEER_HPP
  14. #define ZT_PEER_HPP
  15. #include <vector>
  16. #include "../include/ZeroTierOne.h"
  17. #include "Constants.hpp"
  18. #include "RuntimeEnvironment.hpp"
  19. #include "Node.hpp"
  20. #include "Path.hpp"
  21. #include "Address.hpp"
  22. #include "Utils.hpp"
  23. #include "Identity.hpp"
  24. #include "InetAddress.hpp"
  25. #include "Packet.hpp"
  26. #include "SharedPtr.hpp"
  27. #include "AtomicCounter.hpp"
  28. #include "Hashtable.hpp"
  29. #include "Mutex.hpp"
  30. #include "Bond.hpp"
  31. #include "BondController.hpp"
  32. #include "AES.hpp"
  33. #define ZT_PEER_MAX_SERIALIZED_STATE_SIZE (sizeof(Peer) + 32 + (sizeof(Path) * 2))
  34. namespace ZeroTier {
  35. /**
  36. * Peer on P2P Network (virtual layer 1)
  37. */
  38. class Peer
  39. {
  40. friend class SharedPtr<Peer>;
  41. friend class SharedPtr<Bond>;
  42. friend class Switch;
  43. friend class Bond;
  44. private:
  45. Peer() {} // disabled to prevent bugs -- should not be constructed uninitialized
  46. public:
  47. ~Peer() { Utils::burn(_key,sizeof(_key)); }
  48. /**
  49. * Construct a new peer
  50. *
  51. * @param renv Runtime environment
  52. * @param myIdentity Identity of THIS node (for key agreement)
  53. * @param peerIdentity Identity of peer
  54. * @throws std::runtime_error Key agreement with peer's identity failed
  55. */
  56. Peer(const RuntimeEnvironment *renv,const Identity &myIdentity,const Identity &peerIdentity);
  57. /**
  58. * @return This peer's ZT address (short for identity().address())
  59. */
  60. inline const Address &address() const { return _id.address(); }
  61. /**
  62. * @return This peer's identity
  63. */
  64. inline const Identity &identity() const { return _id; }
  65. /**
  66. * Log receipt of an authenticated packet
  67. *
  68. * This is called by the decode pipe when a packet is proven to be authentic
  69. * and appears to be valid.
  70. *
  71. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  72. * @param path Path over which packet was received
  73. * @param hops ZeroTier (not IP) hops
  74. * @param packetId Packet ID
  75. * @param verb Packet verb
  76. * @param inRePacketId Packet ID in reply to (default: none)
  77. * @param inReVerb Verb in reply to (for OK/ERROR, default: VERB_NOP)
  78. * @param trustEstablished If true, some form of non-trivial trust (like allowed in network) has been established
  79. * @param networkId Network ID if this pertains to a network, or 0 otherwise
  80. */
  81. void received(
  82. void *tPtr,
  83. const SharedPtr<Path> &path,
  84. const unsigned int hops,
  85. const uint64_t packetId,
  86. const unsigned int payloadLength,
  87. const Packet::Verb verb,
  88. const uint64_t inRePacketId,
  89. const Packet::Verb inReVerb,
  90. const bool trustEstablished,
  91. const uint64_t networkId,
  92. const int32_t flowId);
  93. /**
  94. * Check whether we have an active path to this peer via the given address
  95. *
  96. * @param now Current time
  97. * @param addr Remote address
  98. * @return True if we have an active path to this destination
  99. */
  100. inline bool hasActivePathTo(int64_t now,const InetAddress &addr) const
  101. {
  102. Mutex::Lock _l(_paths_m);
  103. for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  104. if (_paths[i].p) {
  105. if (((now - _paths[i].lr) < ZT_PEER_PATH_EXPIRATION)&&(_paths[i].p->address() == addr))
  106. return true;
  107. } else break;
  108. }
  109. return false;
  110. }
  111. /**
  112. * Send via best direct path
  113. *
  114. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  115. * @param data Packet data
  116. * @param len Packet length
  117. * @param now Current time
  118. * @param force If true, send even if path is not alive
  119. * @return True if we actually sent something
  120. */
  121. inline bool sendDirect(void *tPtr,const void *data,unsigned int len,int64_t now,bool force)
  122. {
  123. SharedPtr<Path> bp(getAppropriatePath(now,force));
  124. if (bp)
  125. return bp->send(RR,tPtr,data,len,now);
  126. return false;
  127. }
  128. /**
  129. * Record incoming packets to
  130. *
  131. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  132. * @param path Path over which packet was received
  133. * @param packetId Packet ID
  134. * @param payloadLength Length of packet data payload
  135. * @param verb Packet verb
  136. * @param flowId Flow ID
  137. * @param now Current time
  138. */
  139. void recordIncomingPacket(const SharedPtr<Path> &path, const uint64_t packetId,
  140. uint16_t payloadLength, const Packet::Verb verb, const int32_t flowId, int64_t now);
  141. /**
  142. *
  143. * @param path Path over which packet is being sent
  144. * @param packetId Packet ID
  145. * @param payloadLength Length of packet data payload
  146. * @param verb Packet verb
  147. * @param flowId Flow ID
  148. * @param now Current time
  149. */
  150. void recordOutgoingPacket(const SharedPtr<Path> &path, const uint64_t packetId,
  151. uint16_t payloadLength, const Packet::Verb verb, const int32_t flowId, int64_t now);
  152. /**
  153. * Record an invalid incoming packet. This packet failed
  154. * MAC/compression/cipher checks and will now contribute to a
  155. * Packet Error Ratio (PER).
  156. *
  157. * @param path Path over which packet was received
  158. */
  159. void recordIncomingInvalidPacket(const SharedPtr<Path>& path);
  160. /**
  161. * Get the most appropriate direct path based on current multipath and QoS configuration
  162. *
  163. * @param now Current time
  164. * @param includeExpired If true, include even expired paths
  165. * @return Best current path or NULL if none
  166. */
  167. SharedPtr<Path> getAppropriatePath(int64_t now, bool includeExpired, int32_t flowId = -1);
  168. /**
  169. * Send VERB_RENDEZVOUS to this and another peer via the best common IP scope and path
  170. */
  171. void introduce(void *const tPtr,const int64_t now,const SharedPtr<Peer> &other) const;
  172. /**
  173. * Send a HELLO to this peer at a specified physical address
  174. *
  175. * No statistics or sent times are updated here.
  176. *
  177. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  178. * @param localSocket Local source socket
  179. * @param atAddress Destination address
  180. * @param now Current time
  181. */
  182. void sendHELLO(void *tPtr,const int64_t localSocket,const InetAddress &atAddress,int64_t now);
  183. /**
  184. * Send ECHO (or HELLO for older peers) to this peer at the given address
  185. *
  186. * No statistics or sent times are updated here.
  187. *
  188. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  189. * @param localSocket Local source socket
  190. * @param atAddress Destination address
  191. * @param now Current time
  192. * @param sendFullHello If true, always send a full HELLO instead of just an ECHO
  193. */
  194. void attemptToContactAt(void *tPtr,const int64_t localSocket,const InetAddress &atAddress,int64_t now,bool sendFullHello);
  195. /**
  196. * Try a memorized or statically defined path if any are known
  197. *
  198. * Under the hood this is done periodically based on ZT_TRY_MEMORIZED_PATH_INTERVAL.
  199. *
  200. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  201. * @param now Current time
  202. */
  203. void tryMemorizedPath(void *tPtr,int64_t now);
  204. /**
  205. * A check to be performed periodically which determines whether multipath communication is
  206. * possible with this peer. This check should be performed early in the life-cycle of the peer
  207. * as well as during the process of learning new paths.
  208. */
  209. void performMultipathStateCheck(void *tPtr, int64_t now);
  210. /**
  211. * Send pings or keepalives depending on configured timeouts
  212. *
  213. * This also cleans up some internal data structures. It's called periodically from Node.
  214. *
  215. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  216. * @param now Current time
  217. * @param inetAddressFamily Keep this address family alive, or -1 for any
  218. * @return 0 if nothing sent or bit mask: bit 0x1 if IPv4 sent, bit 0x2 if IPv6 sent (0x3 means both sent)
  219. */
  220. unsigned int doPingAndKeepalive(void *tPtr,int64_t now);
  221. /**
  222. * Process a cluster redirect sent by this peer
  223. *
  224. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  225. * @param originatingPath Path from which redirect originated
  226. * @param remoteAddress Remote address
  227. * @param now Current time
  228. */
  229. void clusterRedirect(void *tPtr,const SharedPtr<Path> &originatingPath,const InetAddress &remoteAddress,const int64_t now);
  230. /**
  231. * Reset paths within a given IP scope and address family
  232. *
  233. * Resetting a path involves sending an ECHO to it and then deactivating
  234. * it until or unless it responds. This is done when we detect a change
  235. * to our external IP or another system change that might invalidate
  236. * many or all current paths.
  237. *
  238. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  239. * @param scope IP scope
  240. * @param inetAddressFamily Family e.g. AF_INET
  241. * @param now Current time
  242. */
  243. void resetWithinScope(void *tPtr,InetAddress::IpScope scope,int inetAddressFamily,int64_t now);
  244. /**
  245. * @param now Current time
  246. * @return All known paths to this peer
  247. */
  248. inline std::vector< SharedPtr<Path> > paths(const int64_t now) const
  249. {
  250. std::vector< SharedPtr<Path> > pp;
  251. Mutex::Lock _l(_paths_m);
  252. for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  253. if (!_paths[i].p) break;
  254. pp.push_back(_paths[i].p);
  255. }
  256. return pp;
  257. }
  258. /**
  259. * @return Time of last receive of anything, whether direct or relayed
  260. */
  261. inline int64_t lastReceive() const { return _lastReceive; }
  262. /**
  263. * @return True if we've heard from this peer in less than ZT_PEER_ACTIVITY_TIMEOUT
  264. */
  265. inline bool isAlive(const int64_t now) const { return ((now - _lastReceive) < ZT_PEER_ACTIVITY_TIMEOUT); }
  266. /**
  267. * @return True if this peer has sent us real network traffic recently
  268. */
  269. inline int64_t isActive(int64_t now) const { return ((now - _lastNontrivialReceive) < ZT_PEER_ACTIVITY_TIMEOUT); }
  270. /**
  271. * @return Latency in milliseconds of best/aggregate path or 0xffff if unknown / no paths
  272. */
  273. inline unsigned int latency(const int64_t now)
  274. {
  275. if (_canUseMultipath) {
  276. return (int)_lastComputedAggregateMeanLatency;
  277. } else {
  278. SharedPtr<Path> bp(getAppropriatePath(now,false));
  279. if (bp)
  280. return bp->latency();
  281. return 0xffff;
  282. }
  283. }
  284. /**
  285. * This computes a quality score for relays and root servers
  286. *
  287. * If we haven't heard anything from these in ZT_PEER_ACTIVITY_TIMEOUT, they
  288. * receive the worst possible quality (max unsigned int). Otherwise the
  289. * quality is a product of latency and the number of potential missed
  290. * pings. This causes roots and relays to switch over a bit faster if they
  291. * fail.
  292. *
  293. * @return Relay quality score computed from latency and other factors, lower is better
  294. */
  295. inline unsigned int relayQuality(const int64_t now)
  296. {
  297. const uint64_t tsr = now - _lastReceive;
  298. if (tsr >= ZT_PEER_ACTIVITY_TIMEOUT)
  299. return (~(unsigned int)0);
  300. unsigned int l = latency(now);
  301. if (!l)
  302. l = 0xffff;
  303. return (l * (((unsigned int)tsr / (ZT_PEER_PING_PERIOD + 1000)) + 1));
  304. }
  305. /**
  306. * @return 256-bit secret symmetric encryption key
  307. */
  308. inline const unsigned char *key() const { return _key; }
  309. /**
  310. * Set the currently known remote version of this peer's client
  311. *
  312. * @param vproto Protocol version
  313. * @param vmaj Major version
  314. * @param vmin Minor version
  315. * @param vrev Revision
  316. */
  317. inline void setRemoteVersion(unsigned int vproto,unsigned int vmaj,unsigned int vmin,unsigned int vrev)
  318. {
  319. _vProto = (uint16_t)vproto;
  320. _vMajor = (uint16_t)vmaj;
  321. _vMinor = (uint16_t)vmin;
  322. _vRevision = (uint16_t)vrev;
  323. }
  324. inline unsigned int remoteVersionProtocol() const { return _vProto; }
  325. inline unsigned int remoteVersionMajor() const { return _vMajor; }
  326. inline unsigned int remoteVersionMinor() const { return _vMinor; }
  327. inline unsigned int remoteVersionRevision() const { return _vRevision; }
  328. inline bool remoteVersionKnown() const { return ((_vMajor > 0)||(_vMinor > 0)||(_vRevision > 0)); }
  329. /**
  330. * @return True if peer has received a trust established packet (e.g. common network membership) in the past ZT_TRUST_EXPIRATION ms
  331. */
  332. inline bool trustEstablished(const int64_t now) const { return ((now - _lastTrustEstablishedPacketReceived) < ZT_TRUST_EXPIRATION); }
  333. /**
  334. * Rate limit gate for VERB_PUSH_DIRECT_PATHS
  335. */
  336. inline bool rateGatePushDirectPaths(const int64_t now)
  337. {
  338. if ((now - _lastDirectPathPushReceive) <= ZT_PUSH_DIRECT_PATHS_CUTOFF_TIME)
  339. ++_directPathPushCutoffCount;
  340. else _directPathPushCutoffCount = 0;
  341. _lastDirectPathPushReceive = now;
  342. return (_directPathPushCutoffCount < ZT_PUSH_DIRECT_PATHS_CUTOFF_LIMIT);
  343. }
  344. /**
  345. * Rate limit gate for VERB_NETWORK_CREDENTIALS
  346. */
  347. inline bool rateGateCredentialsReceived(const int64_t now)
  348. {
  349. if ((now - _lastCredentialsReceived) <= ZT_PEER_CREDENTIALS_CUTOFF_TIME)
  350. ++_credentialsCutoffCount;
  351. else _credentialsCutoffCount = 0;
  352. _lastCredentialsReceived = now;
  353. return (_directPathPushCutoffCount < ZT_PEER_CREDEITIALS_CUTOFF_LIMIT);
  354. }
  355. /**
  356. * Rate limit gate for sending of ERROR_NEED_MEMBERSHIP_CERTIFICATE
  357. */
  358. inline bool rateGateRequestCredentials(const int64_t now)
  359. {
  360. if ((now - _lastCredentialRequestSent) >= ZT_PEER_GENERAL_RATE_LIMIT) {
  361. _lastCredentialRequestSent = now;
  362. return true;
  363. }
  364. return false;
  365. }
  366. /**
  367. * Rate limit gate for inbound WHOIS requests
  368. */
  369. inline bool rateGateInboundWhoisRequest(const int64_t now)
  370. {
  371. if ((now - _lastWhoisRequestReceived) >= ZT_PEER_WHOIS_RATE_LIMIT) {
  372. _lastWhoisRequestReceived = now;
  373. return true;
  374. }
  375. return false;
  376. }
  377. /**
  378. * Rate limit gate for inbound ECHO requests. This rate limiter works
  379. * by draining a certain number of requests per unit time. Each peer may
  380. * theoretically receive up to ZT_ECHO_CUTOFF_LIMIT requests per second.
  381. */
  382. inline bool rateGateEchoRequest(const int64_t now)
  383. {
  384. /*
  385. // TODO: Rethink this
  386. if (_canUseMultipath) {
  387. _echoRequestCutoffCount++;
  388. int numToDrain = (now - _lastEchoCheck) / ZT_ECHO_DRAINAGE_DIVISOR;
  389. _lastEchoCheck = now;
  390. fprintf(stderr, "ZT_ECHO_CUTOFF_LIMIT=%d, (now - _lastEchoCheck)=%d, numToDrain=%d, ZT_ECHO_DRAINAGE_DIVISOR=%d\n", ZT_ECHO_CUTOFF_LIMIT, (now - _lastEchoCheck), numToDrain, ZT_ECHO_DRAINAGE_DIVISOR);
  391. if (_echoRequestCutoffCount > numToDrain) {
  392. _echoRequestCutoffCount-=numToDrain;
  393. }
  394. else {
  395. _echoRequestCutoffCount = 0;
  396. }
  397. return (_echoRequestCutoffCount < ZT_ECHO_CUTOFF_LIMIT);
  398. } else {
  399. if ((now - _lastEchoRequestReceived) >= (ZT_PEER_GENERAL_RATE_LIMIT)) {
  400. _lastEchoRequestReceived = now;
  401. return true;
  402. }
  403. return false;
  404. }
  405. */
  406. return true;
  407. }
  408. /**
  409. * Serialize a peer for storage in local cache
  410. *
  411. * This does not serialize everything, just non-ephemeral information.
  412. */
  413. template<unsigned int C>
  414. inline void serializeForCache(Buffer<C> &b) const
  415. {
  416. b.append((uint8_t)1);
  417. _id.serialize(b);
  418. b.append((uint16_t)_vProto);
  419. b.append((uint16_t)_vMajor);
  420. b.append((uint16_t)_vMinor);
  421. b.append((uint16_t)_vRevision);
  422. {
  423. Mutex::Lock _l(_paths_m);
  424. unsigned int pc = 0;
  425. for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
  426. if (_paths[i].p)
  427. ++pc;
  428. else break;
  429. }
  430. b.append((uint16_t)pc);
  431. for(unsigned int i=0;i<pc;++i)
  432. _paths[i].p->address().serialize(b);
  433. }
  434. }
  435. template<unsigned int C>
  436. inline static SharedPtr<Peer> deserializeFromCache(int64_t now,void *tPtr,Buffer<C> &b,const RuntimeEnvironment *renv)
  437. {
  438. try {
  439. unsigned int ptr = 0;
  440. if (b[ptr++] != 1)
  441. return SharedPtr<Peer>();
  442. Identity id;
  443. ptr += id.deserialize(b,ptr);
  444. if (!id)
  445. return SharedPtr<Peer>();
  446. SharedPtr<Peer> p(new Peer(renv,renv->identity,id));
  447. p->_vProto = b.template at<uint16_t>(ptr); ptr += 2;
  448. p->_vMajor = b.template at<uint16_t>(ptr); ptr += 2;
  449. p->_vMinor = b.template at<uint16_t>(ptr); ptr += 2;
  450. p->_vRevision = b.template at<uint16_t>(ptr); ptr += 2;
  451. // When we deserialize from the cache we don't actually restore paths. We
  452. // just try them and then re-learn them if they happen to still be up.
  453. // Paths are fairly ephemeral in the real world in most cases.
  454. const unsigned int tryPathCount = b.template at<uint16_t>(ptr); ptr += 2;
  455. for(unsigned int i=0;i<tryPathCount;++i) {
  456. InetAddress inaddr;
  457. try {
  458. ptr += inaddr.deserialize(b,ptr);
  459. if (inaddr)
  460. p->attemptToContactAt(tPtr,-1,inaddr,now,true);
  461. } catch ( ... ) {
  462. break;
  463. }
  464. }
  465. return p;
  466. } catch ( ... ) {
  467. return SharedPtr<Peer>();
  468. }
  469. }
  470. /**
  471. *
  472. * @return
  473. */
  474. SharedPtr<Bond> bond() { return _bondToPeer; }
  475. /**
  476. *
  477. * @return
  478. */
  479. inline int8_t bondingPolicy() { return _bondingPolicy; }
  480. //const AES *aesKeysIfSupported() const
  481. //{ return (const AES *)0; }
  482. const AES *aesKeysIfSupported() const
  483. { return (_vProto >= 12) ? _aesKeys : (const AES *)0; }
  484. private:
  485. struct _PeerPath
  486. {
  487. _PeerPath() : lr(0),p(),priority(1) {}
  488. int64_t lr; // time of last valid ZeroTier packet
  489. SharedPtr<Path> p;
  490. long priority; // >= 1, higher is better
  491. };
  492. uint8_t _key[ZT_SYMMETRIC_KEY_SIZE];
  493. AES _aesKeys[2];
  494. const RuntimeEnvironment *RR;
  495. int64_t _lastReceive; // direct or indirect
  496. int64_t _lastNontrivialReceive; // frames, things like netconf, etc.
  497. int64_t _lastTriedMemorizedPath;
  498. int64_t _lastDirectPathPushSent;
  499. int64_t _lastDirectPathPushReceive;
  500. int64_t _lastEchoRequestReceived;
  501. int64_t _lastCredentialRequestSent;
  502. int64_t _lastWhoisRequestReceived;
  503. int64_t _lastCredentialsReceived;
  504. int64_t _lastTrustEstablishedPacketReceived;
  505. int64_t _lastSentFullHello;
  506. int64_t _lastEchoCheck;
  507. unsigned char _freeRandomByte;
  508. uint16_t _vProto;
  509. uint16_t _vMajor;
  510. uint16_t _vMinor;
  511. uint16_t _vRevision;
  512. _PeerPath _paths[ZT_MAX_PEER_NETWORK_PATHS];
  513. Mutex _paths_m;
  514. Identity _id;
  515. unsigned int _directPathPushCutoffCount;
  516. unsigned int _credentialsCutoffCount;
  517. unsigned int _echoRequestCutoffCount;
  518. AtomicCounter __refCount;
  519. bool _remotePeerMultipathEnabled;
  520. int _uniqueAlivePathCount;
  521. bool _localMultipathSupported;
  522. bool _remoteMultipathSupported;
  523. bool _canUseMultipath;
  524. volatile bool _shouldCollectPathStatistics;
  525. volatile int8_t _bondingPolicy;
  526. int32_t _lastComputedAggregateMeanLatency;
  527. SharedPtr<Bond> _bondToPeer;
  528. };
  529. } // namespace ZeroTier
  530. // Add a swap() for shared ptr's to peers to speed up peer sorts
  531. namespace std {
  532. template<>
  533. inline void swap(ZeroTier::SharedPtr<ZeroTier::Peer> &a,ZeroTier::SharedPtr<ZeroTier::Peer> &b)
  534. {
  535. a.swap(b);
  536. }
  537. }
  538. #endif