summaryrefslogtreecommitdiff
path: root/src/mongo-utils.c
blob: 6676aa92c98110c02f64f92a1e9cbc579cbbda2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/* mongo-utils.c - libmongo-client utility functions
 * Copyright 2011, 2012 Gergely Nagy <algernon@balabit.hu>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/** @file src/mongo-utils.c
 * Implementation for various libmongo-client helper functions.
 */

#include <glib.h>
#include <glib/gprintf.h>

#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <errno.h>

#include "mongo-client.h"

static guint32 machine_id = 0;
static gint16 pid = 0;

void
mongo_util_oid_init (gint32 mid)
{
  pid_t p = getpid ();

  if (mid == 0)
    {
      srand (time (NULL));
      machine_id = rand ();
    }
  else
    machine_id = mid;

  /*
   * If our pid has more than 16 bits, let half the bits modulate the
   * machine_id.
   */
  if (sizeof (pid_t) > 2)
    {
      machine_id ^= pid >> 16;
    }
  pid = (gint16)p;
}

guint8 *
mongo_util_oid_new_with_time (gint32 ts, gint32 seq)
{
  guint8 *oid;
  gint32 t = GINT32_TO_BE (ts);
  gint32 tmp = GINT32_TO_BE (seq);

  if (machine_id == 0 || pid == 0)
    return NULL;

  oid = (guint8 *)g_new0 (guint8, 12);

  /* Sequence number, last 3 bytes
   * For simplicity's sake, we put this in first, and overwrite the
   * first byte later.
   */
  memcpy (oid + 4 + 2 + 2, &tmp, 4);
  /* First four bytes: the time, BE byte order */
  memcpy (oid, &t, 4);
  /* Machine ID, byte order doesn't matter, 3 bytes */
  memcpy (oid + 4, &machine_id, 3);
  /* PID, byte order doesn't matter, 2 bytes */
  memcpy (oid + 4 + 3, &pid, 2);

  return oid;
}

guint8 *
mongo_util_oid_new (gint32 seq)
{
  return mongo_util_oid_new_with_time (time (NULL), seq);
}

gchar *
mongo_util_oid_as_string (const guint8 *oid)
{
  gchar *str;
  gint j;

  if (!oid)
    return NULL;

  str = g_new (gchar, 26);
  for (j = 0; j < 12; j++)
    g_sprintf (&str[j * 2], "%02x", oid[j]);
  str[25] = 0;
  return str;
}

gboolean
mongo_util_parse_addr (const gchar *addr, gchar **host, gint *port)
{
  gchar *port_s, *ep;
  glong p;

  if (!addr || !host || !port)
    {
      if (host)
        *host = NULL;
      if (port)
        *port = -1;
      errno = EINVAL;
      return FALSE;
    }

  /* Check for IPv6 literal */
  if (addr[0] == '[')
    {
      /* Host is everything between [] */
      port_s = strchr (addr + 1, ']');
      if (!port_s || port_s - addr == 1)
        {
          *host = NULL;
          *port = -1;
          errno = EINVAL;
          return FALSE;
        }
      *host = g_strndup (addr + 1, port_s - addr - 1);

      port_s += 2;
      if (port_s - addr >= (glong)strlen (addr))
        {
          *port = -1;
          return TRUE;
        }
    }
  else
    {
      /* Dealing with something that's not an IPv6 literal */

      /* Split up to host:port */
      port_s = g_strrstr (addr, ":");
      if (!port_s)
        {
          *host = g_strdup (addr);
          *port = -1;
          return TRUE;
        }
      if (port_s == addr)
        {
          *host = NULL;
          *port = -1;
          errno = EINVAL;
          return FALSE;
        }
      port_s++;
      *host = g_strndup (addr, port_s - addr - 1);
    }

  p = strtol (port_s, &ep, 10);
  if (p == LONG_MIN || p == LONG_MAX)
    {
      g_free (*host);
      *host = NULL;
      *port = -1;
      errno = ERANGE;
      return FALSE;
    }
  if ((p != MONGO_CONN_LOCAL) && (p < 0 || p > INT_MAX))
    {
      g_free (*host);
      *host = NULL;
      *port = -1;
      errno = ERANGE;
      return FALSE;
    }
  *port = (gint)p;

  if (ep && *ep)
    {
      g_free (*host);
      *host = NULL;
      *port = -1;
      errno = EINVAL;
      return FALSE;
    }
  return TRUE;
}