~ubuntu-branches/ubuntu/trusty/zendframework/trusty

« back to all changes in this revision

Viewing changes to library/Zend/TimeSync/Ntp.php

  • Committer: Bazaar Package Importer
  • Author(s): Frank Habermann
  • Date: 2010-04-28 20:10:00 UTC
  • mfrom: (1.3.1 upstream) (9.1.4 sid)
  • Revision ID: james.westby@ubuntu.com-20100428201000-o347bj5qb5i3tpot
Tags: 1.10.4-1
new upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 * @package   Zend_TimeSync
17
17
 * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
18
18
 * @license   http://framework.zend.com/license/new-bsd     New BSD License
19
 
 * @version   $Id: Ntp.php 20096 2010-01-06 02:05:09Z bkarwin $
 
19
 * @version   $Id: Ntp.php 21481 2010-03-13 22:09:50Z thomas $
20
20
 */
21
21
 
22
22
/**
139
139
    }
140
140
 
141
141
    /**
 
142
     * Calculates a 32bit integer
 
143
     *
 
144
     * @param string $input
 
145
     * @return integer
 
146
     */
 
147
    protected function _getInteger($input)
 
148
    {
 
149
        $f1  = str_pad(ord($input[0]), 2, '0', STR_PAD_LEFT);
 
150
        $f1 .= str_pad(ord($input[1]), 2, '0', STR_PAD_LEFT);
 
151
        $f1 .= str_pad(ord($input[2]), 2, '0', STR_PAD_LEFT);
 
152
        $f1 .= str_pad(ord($input[3]), 2, '0', STR_PAD_LEFT);
 
153
        return (int) $f1;
 
154
    }
 
155
 
 
156
    /**
 
157
     * Calculates a 32bit signed fixed point number
 
158
     *
 
159
     * @param string $input
 
160
     * @return float
 
161
     */
 
162
    protected function _getFloat($input)
 
163
    {
 
164
        $f1  = str_pad(ord($input[0]), 2, '0', STR_PAD_LEFT);
 
165
        $f1 .= str_pad(ord($input[1]), 2, '0', STR_PAD_LEFT);
 
166
        $f1 .= str_pad(ord($input[2]), 2, '0', STR_PAD_LEFT);
 
167
        $f1 .= str_pad(ord($input[3]), 2, '0', STR_PAD_LEFT);
 
168
        $f2  = $f1 >> 17;
 
169
        $f3  = ($f1 & 0x0001FFFF);
 
170
        $f1  = $f2 . '.' . $f3;
 
171
        return (float) $f1;
 
172
    }
 
173
 
 
174
    /**
 
175
     * Calculates a 64bit timestamp
 
176
     *
 
177
     * @param string $input
 
178
     * @return float
 
179
     */
 
180
    protected function _getTimestamp($input)
 
181
    {
 
182
        $f1  = (ord($input[0]) * pow(256, 3));
 
183
        $f1 += (ord($input[1]) * pow(256, 2));
 
184
        $f1 += (ord($input[2]) * pow(256, 1));
 
185
        $f1 += (ord($input[3]));
 
186
        $f1 -= 2208988800;
 
187
 
 
188
        $f2  = (ord($input[4]) * pow(256, 3));
 
189
        $f2 += (ord($input[5]) * pow(256, 2));
 
190
        $f2 += (ord($input[6]) * pow(256, 1));
 
191
        $f2 += (ord($input[7]));
 
192
 
 
193
        return (float) ($f1 . "." . $f2);
 
194
    }
 
195
 
 
196
    /**
142
197
     * Reads the data returned from the timeserver
143
198
     *
144
199
     * This will return an array with binary data listing:
162
217
            'stratum'        => ord(fread($this->_socket, 1)),
163
218
            'poll'           => ord(fread($this->_socket, 1)),
164
219
            'precision'      => ord(fread($this->_socket, 1)),
165
 
            'rootdelay'      => ord(fread($this->_socket, 4)),
166
 
            'rootdispersion' => ord(fread($this->_socket, 4)),
167
 
            'referenceid'    => ord(fread($this->_socket, 4)),
168
 
            'referencestamp' => ord(fread($this->_socket, 4)),
169
 
            'referencemicro' => ord(fread($this->_socket, 4)),
170
 
            'originatestamp' => ord(fread($this->_socket, 4)),
171
 
            'originatemicro' => ord(fread($this->_socket, 4)),
172
 
            'receivestamp'   => ord(fread($this->_socket, 4)),
173
 
            'receivemicro'   => ord(fread($this->_socket, 4)),
174
 
            'transmitstamp'  => ord(fread($this->_socket, 4)),
175
 
            'transmitmicro'  => ord(fread($this->_socket, 4)),
176
 
            'clientreceived' => 0
 
220
            'rootdelay'      => $this->_getFloat(fread($this->_socket, 4)),
 
221
            'rootdispersion' => $this->_getFloat(fread($this->_socket, 4)),
 
222
            'referenceid'    => fread($this->_socket, 4),
 
223
            'referencestamp' => $this->_getTimestamp(fread($this->_socket, 8)),
 
224
            'originatestamp' => $this->_getTimestamp(fread($this->_socket, 8)),
 
225
            'receivestamp'   => $this->_getTimestamp(fread($this->_socket, 8)),
 
226
            'transmitstamp'  => $this->_getTimestamp(fread($this->_socket, 8)),
 
227
            'clientreceived' => microtime(true)
177
228
        );
178
229
 
179
230
        $this->_disconnect();
180
 
 
181
231
        return $result;
182
232
    }
183
233
 
345
395
         * Both positive and negative values, depending on clock precision and skew, are
346
396
         * possible.
347
397
         */
348
 
        $this->_info['rootdelay']     = $binary['rootdelay'] >> 15;
349
 
        $this->_info['rootdelayfrac'] = ($binary['rootdelay'] << 17) >> 17;
 
398
        $this->_info['rootdelay'] = $binary['rootdelay'];
350
399
 
351
400
        /*
352
401
         * Indicates the maximum error relative to the primary reference source at the
354
403
         *
355
404
         * Only positive values greater than zero are possible.
356
405
         */
357
 
        $this->_info['rootdispersion']     = $binary['rootdispersion'] >> 15;
358
 
        $this->_info['rootdispersionfrac'] = ($binary['rootdispersion'] << 17) >> 17;
359
 
 
360
 
        /*
361
 
         * The local time, in timestamp format, at the peer
362
 
         * when its latest NTP message was sent.
363
 
         */
364
 
        $original  = (float) $binary['originatestamp'];
365
 
        $original += (float) ($binary['originatemicro'] / 4294967296);
366
 
 
367
 
        /*
368
 
         * The local time, in timestamp format, when the latest
369
 
         * NTP message from the peer arrived.
370
 
         */
371
 
        $received  = (float) $binary['receivestamp'];
372
 
        $received += (float) ($binary['receivemicro'] / 4294967296);
373
 
 
374
 
        /*
375
 
         * The local time, in timestamp format, at which the
376
 
         * NTP message departed the sender.
377
 
         */
378
 
        $transmit  = (float) $binary['transmitstamp'];
379
 
        $transmit += (float) ($binary['transmitmicro'] / 4294967296);
 
406
        $this->_info['rootdispersion'] = $binary['rootdispersion'];
380
407
 
381
408
        /*
382
409
         * The roundtrip delay of the peer clock relative to the local clock
385
412
         * Note that this variable can take on both positive and negative values,
386
413
         * depending on clock precision and skew-error accumulation.
387
414
         */
388
 
        $roundtrip                = ($binary['clientreceived'] - $original);
389
 
        $roundtrip               -= ($transmit - $received);
390
 
        $this->_info['roundtrip'] = ($roundtrip / 2);
 
415
        $this->_info['roundtrip']  = $binary['receivestamp'];
 
416
        $this->_info['roundtrip'] -= $binary['originatestamp'];
 
417
        $this->_info['roundtrip'] -= $binary['transmitstamp'];
 
418
        $this->_info['roundtrip'] += $binary['clientreceived'];
 
419
        $this->_info['roundtrip'] /= 2;
391
420
 
392
421
        // The offset of the peer clock relative to the local clock, in seconds.
393
 
        $offset                = ($received - $original + $transmit - $binary['clientreceived']);
394
 
        $this->_info['offset'] = ($offset / 2);
395
 
 
 
422
        $this->_info['offset']  = $binary['receivestamp'];
 
423
        $this->_info['offset'] -= $binary['originatestamp'];
 
424
        $this->_info['offset'] += $binary['transmitstamp'];
 
425
        $this->_info['offset'] -= $binary['clientreceived'];
 
426
        $this->_info['offset'] /= 2;
396
427
        $time = (time() - $this->_info['offset']);
397
428
 
398
429
        return $time;