client_id.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include "common/hadoop_err.h"
  19. #include "rpc/client_id.h"
  20. #include <errno.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. void hrpc_client_id_generate_rand(struct hrpc_client_id *id)
  25. {
  26. int i;
  27. for (i = 0; i <= HRPC_CLIENT_ID_LEN - 3; i += 3) {
  28. long int r = lrand48();
  29. id->buf[i + 0] = r & 0xff;
  30. id->buf[i + 1] = (r >> 8) & 0xff;
  31. id->buf[i + 2] = (r >> 16) & 0xff;
  32. }
  33. id->buf[HRPC_CLIENT_ID_LEN - 2] = lrand48() & 0xff;
  34. id->buf[HRPC_CLIENT_ID_LEN - 1] = lrand48() & 0xff;
  35. }
  36. struct hadoop_err *hrpc_client_id_from_bytes(void *bytes, int len,
  37. struct hrpc_client_id *out)
  38. {
  39. if (len != HRPC_CLIENT_ID_LEN) {
  40. return hadoop_lerr_alloc(EINVAL, "hrpc_client_id_from_bytes: "
  41. "invalid client id length of %d (expected %d)",
  42. len, HRPC_CLIENT_ID_LEN);
  43. }
  44. memcpy(out->buf, bytes, len);
  45. return NULL;
  46. }
  47. const char *hrpc_client_id_to_str(const struct hrpc_client_id *id,
  48. char *str, size_t str_len)
  49. {
  50. snprintf(str, str_len,
  51. "%02x%02x%02x%02x-%02x%02x%02x%02x-%02x%02x%02x%02x-%02x%02x%02x%02x",
  52. id->buf[0], id->buf[1], id->buf[2], id->buf[3],
  53. id->buf[4], id->buf[5], id->buf[6], id->buf[7],
  54. id->buf[8], id->buf[9], id->buf[10], id->buf[11],
  55. id->buf[12], id->buf[13], id->buf[14], id->buf[15]);
  56. return str;
  57. }
  58. int hrpc_client_id_compare(const struct hrpc_client_id *a,
  59. const struct hrpc_client_id *b)
  60. {
  61. return memcmp(a->buf, b->buf, HRPC_CLIENT_ID_LEN);
  62. }
  63. // vim: ts=4:sw=4:tw=79:et