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. void
  4. TSPluginInit(int argc, const char *argv[])
  5. {
  6. (void)argc; // unused
  7. (void)argv; // unused
  8. // Get the version:
  9. const char *ts_version = TSTrafficServerVersionGet();
  10. if (!ts_version) {
  11. TSError("[version] Can't get Traffic Server verion.\n");
  12. return;
  13. }
  14. // Split it in major, minor, patch:
  15. int major_ts_version = 0;
  16. int minor_ts_version = 0;
  17. int patch_ts_version = 0;
  18. if (sscanf(ts_version, "%d.%d.%d", &major_ts_version, &minor_ts_version, &patch_ts_version) != 3) {
  19. TSError("[version] Can't extract verions.\n");
  20. return;
  21. }
  22. TSPluginRegistrationInfo info;
  23. info.plugin_name = "version-plugin";
  24. info.vendor_name = "MyCompany";
  25. info.support_email = "ts-api-support@MyCompany.com";
  26. // partial compilation
  27. #if (TS_VERSION_NUMBER < 3000000)
  28. if (TSPluginRegister(TS_SDK_VERSION_2_0, &info) != TS_SUCCESS) {
  29. #elif (TS_VERSION_NUMBER < 6000000)
  30. if (TSPluginRegister(TS_SDK_VERSION_3_0, &info) != TS_SUCCESS) {
  31. #else
  32. if (TSPluginRegister(&info) != TS_SUCCESS) {
  33. #endif
  34. TSError("[version] Plugin registration failed. \n");
  35. }
  36. TSDebug("debug-version-plugin", "Running in Apache Traffic Server: v%d.%d.%d", major_ts_version, minor_ts_version,
  37. patch_ts_version);
  38. }