• Stars
    star
    368
  • Rank 115,958 (Top 3 %)
  • Language
    Objective-C
  • License
    MIT License
  • Created over 13 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

SNTP implementation for iOS

ios-ntp

A network time protocol client (and an application testbed for iOS). This project is no longer being worked on, but issues will be responded to.

Created by Gavin Eadie on Oct 17, 2010

News

June 4, 2018: (version 1.1.9) There was a conflict when using cocoasyncsocket and ios-ntp; added pod dependency and removed asyncsocket files from network-lib. The necessary files can be found at https://github.com/robbiehanson/CocoaAsyncSocket

January 24, 2018: (version 1.1.6) improvements have been made in a few areas:

  • a podspec target for tvOS has been added.
  • the files from CocoaAsyncSocket have been replaced with the most recent versions.

December 20, 2016: (version 1.1.4) improvements have been made in a few areas:

  • the use of pool ntp server host names is strongly discouraged so they have been removed from this code and documentation. Read the NTP Pool Project page at http://www.pool.ntp.org/vendors.html for context. NOTE: The library will query NO servers in its new default state .. now a ntp.hosts file MUST be provided.

February 1, 2016: (version 1.1.3) improvements have been made in a few areas:

  • arithmetic operating on an NTP 64-bit time has been improved slightly.
  • the delegate callback from NetAssocation now runs on the main thread, which allows it to modify any UI component (illegal from a background thread).
  • a "receive packet filter" has been added, but not yet invoked. This will used to drop UPD packets that don't pass validation.
  • [NetAssociation finish] now invalidates its timer so that association plays no further part in time derivation.
  • upgrade sources to most recent Objective-C conventions.

Getting a Quick Timecheck

ios-ntp is often (mostly?) used to make sure someone hasn't fiddled with the system clock. The complications involved in using multiple servers and averaging time offsets is overkill for this purpose. The following skeleton code is all that is needed to check the time. If you want some more assurance of accuracy, repeat the operation a few time; if you want to continue to watch the clock, you might invoke this at infrequent intervals:

	#import "ios-ntp.h"

	@interface ntpViewController : UIViewController <NetAssociationDelegate>

	@end

	@implementation ntpViewController
	- (void)viewDidLoad {
		[super viewDidLoad];

		netAssociation = [[NetAssociation alloc]
		initWithServerName:[NetAssociation ipAddrFromName:@"time.apple.com"]];
		netAssociation.delegate = self;
		[netAssociation sendTimeQuery];
	}

	- (void) reportFromDelegate {
		printf("time offset: %5.3f mSec", netAssociation.offset * 1000.0];
	}

January 17, 2016: (version 1.1.2) minor cleanup for Xcode 7.x

July 22, 2015: (version 1.1.1) ios-ntp has contained a resource file (called ntp.hosts) which contained a list of time server hosts to be used for querying the time. That file has been removed in this release.

The logic is that, since ios-ntp can now be added to a project via CocoaPods, any local changes made to that file will be overwritten the next time the ios-ntp pod is updated and, since ios-ntp already contains a built-in list of time servers, removing this file from the pod should not impact the behavior of the ios-ntp code.

If you want to use your own list of time servers, you need to create a file containing time host names, one per line, name it ntp.hosts and place it in the main bundle of your application (the sample app ios-ntp-app does this to use the server at time.apple.com).

June 10, 2015: (version 1.1) I recently discovered a re-entrancy bug when John Grismore brought my attention to inaccuracies in reported network time offsets. When a NetAssociation notified the NetClock that it had a new time offset, that event might interrupt the offset averaging that NetClock does causing the averaging to break.

In fact, this mechanism isn't optimal anyway! The notifications that cause offset averaging arrive at NetClock constantly whether the result is used or not. We're keeping the NetClock network time offset property up to date whether we need it or not. Better would be to perform the averaging only when the offset NetClock property is called for, and that is how ios-ntp now works.

The API is not changed, so your application should require no changes.

February 22, 2015: Several important changes have been made including one that will be helpful for those who want to get a quick one-time value of the difference between system time and network time.

Before this change, ios-ntp would use time estimates from a set of NetAssociations (one per time servers), constantly determining the best time by sampling these values. This is the model for computers which have a continuous low level task monitoring the time. Application in iOS often have a different need; they are more likely to want an fast estimate of the time on demand. To provide the ability to the developer, access has been provided to use a NetAssociations directly

An NetAssociation can now be asked for one measure of the time from one time server so an iOS app can create an NetAssociation, use it to get the time, and be done.

This code operates on 32-bit and 64-bit iOS devices.

This code requires iOS 7, or higher.


About

The clock on the oldest iPhone, iTouch or iPad is not closely synchronized to the correct time. In the case of a device which is obtaining its time from the telephone system, there is a setting to enable synchronizing to the phone company time, but that time has been known to be over a minute different from the correct time.

Note: Recent versions of iOS have NTP time derivation built in so that the system clock now varies from 'real time' by less than one second (it's usually accurate to about 10mS).

In addition, users may change their device time and severely affect applications that rely on correct times to enforce functionality, or may set their devices clock into the past in an attempt to dodge an expiry date.

This project contains code to provide time obtained from standard time servers using the simple network time protocol (SNTP: RFC 5905). The implementation is not a rigorous as described in that document since the goal was to improve time accuracy to tens of milliSeconds, not to microseconds.

Computers using the NTP protocol usually employ it in a continuous low level task to keep track of the time on a continuous basis. A background application uses occasional time estimates from a set of time servers to determine the best time by sampling these values over time. iOS applications are different, being more likely to want a one-time, quick estimate of the time.

ios-ntp provides both the continuous and on-demand modes of operation. The continuous mode uses multiple 'associations' with time servers which use timers to repeatedly obtain time estimates. These associations can, however, be used by the developer to get one time from one server.

Usage

The code can be incorporated as source code or as a framework in an Xcode project. The framework usage is temporarily unavailable but will be restored soon.

More to come about using a framework.

Download the ios-ntp project, add the necessary to your project, build and run. You will need:

	#import "ios-ntp.h"

where ios-ntp is referenced.

Continuous Mode

Simply create a NetworkClock. As soon as you create it, the NTP process will begin polling the time servers in the "ntp.hosts" file. You may wish to start it when the application starts, so that the time is well synchronized by the time you actually want to use it, just call it in your AppDelegate's didFinishLaunching method.:

	NetworkClock * nc = [NetworkClock sharedNetworkClock];

then wait at least ten seconds for some time servers to respond before calling:

	NSDate * nt = nc.networkTime;

It will take about one minute before untrustworthy servers start to get dropped from the pool.

It would probably be better if NetworkClock called back to a delegate method, like NetAssociation does below, when it had a good time but that's not how it works, yet, so you have to wait till things settle down.

On Demand Mode

This usage is slightly more complicated. The developer must create an NetAssociation (with some specified time server), and then tell it get the time from that server. The association uses a delegate method to return itself with time information.

	netAssociation = [[NetAssociation alloc] initWithServerName:@"time.apple.com"];
	netAssociation.delegate = self;
	[netAssociation sendTimeQuery];

	...

	- (void) reportFromDelegate {
	   double timeOffset = netAssociation.offset;
	}

Performance

iOS is an event driven system with an emphasis on rapid response to gestures at the cost of other activity. This encourages the extensive use of design patterns like notification and delegation so, I think, the calculation of small time differences in this environment suffers as a result.

License

The MIT License Copyright (c) 2010-2019, Ramsay Consulting

History

November 19, 2014: A large update was made today to bring ios-ntp into the modern world. The changes do include one bug fix, but are mostly related to making the code comply with the recent Xcode changes and requirements.

Finally, note that this code was first written when there were only 32-bit iOS devices. As I write this there are still 32-bit devices which run the latest version of iOS (iPhone 4S, for example), but all newer iOS devices have a 64-bit architecture (iPhone 6, for example), and Apple requires that this be supported.

More Repositories

1

hashpipe

hashpipe - pipe iff the hash matches
Go
405
star
2

random-ideas

random ideas
322
star
3

bson-cpp

Standalone repository for mongodb's BSON C++ Implementation
C++
154
star
4

transformer

transformer - multiformat data conversion
JavaScript
132
star
5

compress-pdf

comptess-pdf - cli util to compress a pdf
Shell
94
star
6

goprocess

goprocess - like Context, but with good close semantics.
Go
71
star
7

data

package manager for datasets
Go
59
star
8

go-peerstream

p2p stream multi-multiplexing in Go (with https://github.com/docker/spdystream)
Go
55
star
9

depviz

dependency visualizer for the web
JavaScript
49
star
10

ipfs-screencap

Capture screenshots, publish them to IPFS, and copy the link to the clipboard.
Shell
49
star
11

nanotime

nanosecond-precision 64bit time type
C
44
star
12

http2ipfs-web

JavaScript
43
star
13

ipfs-paste

paste stdin and clipboard to ipfs
Shell
32
star
14

asciinema-selfhost

asciinema-selfhost
CSS
28
star
15

yt2ipfs

Shell
26
star
16

go-netcatnat

netcat accross nats! (udp)
Go
24
star
17

datadex

a dataset index
Go
23
star
18

go-base58

simple base58 codec
Go
20
star
19

go-is-domain

Checks whether a string is a domain
Go
16
star
20

saucer

coffeescript boilerplate
CoffeeScript
16
star
21

peer-chess

JavaScript
15
star
22

go-msgio

simple package to r/w length-delimited slices.
Go
14
star
23

go-context

context.Context extentions
Go
13
star
24

go-random-files

create a random filesystem hierarchy
Go
13
star
25

latex

LaTeX styles and things
11
star
26

TeXchat

simple web chat service that renders TeX math
JavaScript
11
star
27

py-dronestore

Python implementation of dronestore library. Version control application objects.
Python
9
star
28

node-screencap-stream

SCaaS - Screen Capture as a Stream
JavaScript
8
star
29

dgram-stream

dgram-stream - udp as a duplex stream
JavaScript
8
star
30

node-bsdash

go-ipfs bitswap dashboard
JavaScript
8
star
31

ipfs-vbox

boot VMs in virtual box with IPFS
Shell
7
star
32

CocoaWeather

Cocoa bindings to weather APIs
Objective-C
7
star
33

quests

In which jbenet asks others for help.
6
star
34

go-simple-encrypt

simple encrypt
Go
6
star
35

hang-fds

Go
6
star
36

git-push-each

git push-reach -- pushes each commit to its own branch
Shell
6
star
37

http2ipfs

http2ipfs
Shell
6
star
38

winporep

Go
5
star
39

go-fuse-version

go-fuse-version
Go
5
star
40

BsonNetwork

networking as easy as making a dictionary
Objective-C
5
star
41

macos-ssh-proxy

simple macos ssh http proxy
Shell
4
star
42

js-senc

JavaScript
4
star
43

node-multi-dgram-stream

multi-dgram-stream
JavaScript
4
star
44

issue-dep-graph

JavaScript
4
star
45

transformer-pkg

transformer-pkg -- automate writing transformer modules
JavaScript
4
star
46

go-json2cbor

json2cbor
Go
4
star
47

go-libutp

utp.go -- libutp bindings in Go.
Go
4
star
48

cs106b-section

examples for cs106b section
C++
4
star
49

go-detect-race

detect if compiled with race
Go
4
star
50

ios-dronestore

dronestore ios implementation
Objective-C
4
star
51

node-subcomandante

taaaaake ooooooon meeee
JavaScript
4
star
52

go-os-rename

atomic os rename for go, even on windows
Go
4
star
53

node-ubjson-stream

streaming ubjson
Shell
3
star
54

go-utp

ฮผTP (Micro Transport Protocol) implementation in Golang
Go
3
star
55

ipfs-dockertest-logs

Shell
3
star
56

juan.benet.ai

website
HTML
3
star
57

bash-timeout3

timeout3 - execute a command with a timeout
Shell
3
star
58

ipfs-dist-install

Shell
3
star
59

node-scoped-transform-stream

scoped-transform-stream
JavaScript
3
star
60

protobufjs-stream

protobufjs-stream
JavaScript
3
star
61

go-feedback

go get feedback
Go
2
star
62

node-pipe-segment

pipe-segment makes non-trivial pipe units
JavaScript
2
star
63

dht-simple-tests

Go
2
star
64

node-vinyl-multipart-stream

JavaScript
2
star
65

gob-js-test

JavaScript
2
star
66

ipfs-diag-net-d3-vis

Shell
2
star
67

bohrium

Tagged image repository
Python
2
star
68

node-random-json-stream

random json output
JavaScript
2
star
69

go-temp-err-catcher

Go
2
star
70

go-chunk

Go
2
star
71

go-ctxgroup

Context Group
Go
2
star
72

node-ipfs-protobuf-codec

JavaScript
2
star
73

http-client-stream

JavaScript
2
star
74

go-test-timesort

sort go test output by duration
Go
2
star
75

go-router

Go
2
star
76

mdown

JavaScript
1
star
77

go-net-reuse

Go
1
star
78

ipfs-app-examples

JavaScript
1
star
79

propeller

Simple extensions for Google's App Engine
Python
1
star
80

Dusk-TextMate-Theme

A blue TextMate theme based on Twilight.
1
star
81

resume

My resume
1
star
82

node-pipe-segment-filter

simple pipe segment from a filter function
JavaScript
1
star
83

go-ipld-api-exp

Go
1
star
84

transformer.us-zipcode

JavaScript
1
star
85

py-bsonnetwork

BSON based RPC-like messaging library on gevent
Python
1
star
86

go-twotime

get the same time twice in go
Go
1
star
87

json2toml-webtool

JavaScript
1
star
88

ipfs-bench-add-opts

Go
1
star
89

prog-fs

Shell
1
star
90

experiment-binfs

Shell
1
star
91

transformer-conversion

conversions in transformer
JavaScript
1
star
92

transformer.buffer-to-base64

JavaScript
1
star
93

node-ipfs-msgproto

ipfs-msgproto
JavaScript
1
star
94

transformer.us-zipcode-to-us-city

JavaScript
1
star
95

transformer.js-date-to-unix-time

JavaScript
1
star
96

node-go-platform

JavaScript
1
star
97

node-ipfs-msgproto-network

node-ipfs-msgproto-network
JavaScript
1
star
98

browser-terminal-js

browser-terminal-js
JavaScript
1
star