Plugin Registration and Version Checking

If you need you make a plugin that will load against multiple versions of Traffic Server, you can check the API version at both compilation time and run time.

Use the following interfaces:

The plugin registers the plugin and ensures it’s running with a compatible version of Traffic Server.

  1. #include <stdio.h>
  2. #include <ts/ts.h>
  3. #define PLUGIN_NAME "version"
  4. void
  5. TSPluginInit(int argc, const char *argv[])
  6. {
  7. (void)argc; // unused
  8. (void)argv; // unused
  9. // Get the version:
  10. const char *ts_version = TSTrafficServerVersionGet();
  11. if (!ts_version) {
  12. TSError("[%s] Can't get Traffic Server version.", PLUGIN_NAME);
  13. return;
  14. }
  15. // Split it in major, minor, patch:
  16. int major_ts_version = 0;
  17. int minor_ts_version = 0;
  18. int patch_ts_version = 0;
  19. if (sscanf(ts_version, "%d.%d.%d", &major_ts_version, &minor_ts_version, &patch_ts_version) != 3) {
  20. TSError("[%s] Can't extract versions.", PLUGIN_NAME);
  21. return;
  22. }
  23. TSPluginRegistrationInfo info;
  24. info.plugin_name = PLUGIN_NAME;
  25. info.vendor_name = "Apache Software Foundation";
  26. info.support_email = "dev@trafficserver.apache.org";
  27. // partial compilation
  28. #if (TS_VERSION_NUMBER < 3000000)
  29. if (TSPluginRegister(TS_SDK_VERSION_2_0, &info) != TS_SUCCESS) {
  30. #elif (TS_VERSION_NUMBER < 6000000)
  31. if (TSPluginRegister(TS_SDK_VERSION_3_0, &info) != TS_SUCCESS) {
  32. #else
  33. if (TSPluginRegister(&info) != TS_SUCCESS) {
  34. #endif
  35. TSError("[%s] Plugin registration failed.", PLUGIN_NAME);
  36. }
  37. TSDebug(PLUGIN_NAME, "Running in Apache Traffic Server: v%d.%d.%d", major_ts_version, minor_ts_version, patch_ts_version);
  38. }