Commit Graph

233 Commits

Author SHA1 Message Date
Sarah Jamie Lewis 1a2fb40d91 Refactoring Application to remove channel handler duplications
Also simplfies application a lot, still not complete, but i like this approach much more
2018-01-07 16:51:46 -08:00
Sarah Jamie Lewis be62634c46 Fixing go vet issues with errorf params 2018-01-06 16:37:07 -08:00
Sarah Jamie Lewis 05e8675ed5 Fixing a few golint issues 2018-01-05 14:16:52 -08:00
Sarah Jamie Lewis f6cc472c6e Removing Erroneous SendMessage & Adding SupportChannelTypes Test 2018-01-04 15:51:32 -08:00
Sarah Jamie Lewis 617ca019f8 Adding stub tests for MessageBuilder 2018-01-04 15:32:37 -08:00
Sarah Jamie Lewis 7f215e86c4 Adding Open Connection Failed Test 2018-01-03 10:20:53 -08:00
Sarah Jamie Lewis 84d7336602 Adding successful version negotiation tests.
This change also simplifies ricochet_test.go to only exercise the Open()
function.
2018-01-03 10:12:59 -08:00
Sarah Jamie Lewis 049a0ea15f Stubbing OutboundVersionNegotiationTest
Actually committing enable features work!
2018-01-02 09:23:20 -08:00
Sarah Jamie Lewis 6d449e230f Improving Testing Documentation & Coverage Starting With Inbound Version Negotiation 2018-01-01 10:55:59 -08:00
Sarah Jamie Lewis f537fb4f76 Adding Simple Application Broadcast & Features Enabled 2018-01-01 10:06:58 -08:00
Sarah Jamie Lewis 1433b31e6f Change inbound/outbound handlers to use Identity.
Add Inbound Version Negotiation Test
2017-12-13 11:42:54 -08:00
Sarah Jamie Lewis 43b357fdb6 First cut of minimizing private_key exposure in the code base
Minor formatting
2017-12-05 11:00:04 -08:00
Sarah Jamie Lewis e1031861a2 Adding basic tests for crypto_utils 2017-11-04 13:31:37 -07:00
Sarah Jamie Lewis 958e07bf66 Fixing minor govet / misspell issue 2017-11-04 08:56:20 -07:00
Sarah Jamie Lewis 5057dd68ee Formatting + Checking Connection Error in Echobot 2017-11-02 16:45:27 -07:00
Sarah Jamie Lewis 9d2e898157
Update README.md
Fixing the examples & adding removing untrue statements.
2017-11-02 16:15:12 -07:00
Sarah Jamie Lewis 3e6dc80670 Fixup Application to align with new Connection API 2017-11-02 16:05:01 -07:00
Sarah Jamie Lewis dc285b18a9 Merge branch 'master' of https://github.com/dballard/go-ricochet into dballard-master 2017-11-02 15:53:01 -07:00
Sarah Jamie Lewis 8fe7b84fc9 Merge branch 'fix/chatchannel-api' of https://github.com/special/go-ricochet-protocol into special-fix/chatchannel-api 2017-11-02 15:45:09 -07:00
John Brooks 9a65aeed77 Improve ContactRequestChannel's API
After the RequestOpenChannel changes, it's now possible to specify the
name and message of an outbound request as variables of the channel handler,
instead implementing an interface method to return them.

Also added the SendResponse method, which is necessary to respond to an
inbound request that was in the Pending state.
2017-11-02 15:43:07 -07:00
John Brooks b2c87b1b72 Fix concurrency issues in ProcessAuthAsClient/Server
There were a few related issues with ProcessAuthAsClient/Server that
could cause deadlocks or leak goroutines:

Break() could end up being called more than once, which is always a
deadlock. This is fixed by using a sync.Once.

RequestOpenChannel was called without Do, and was called before
Process in the same goroutine, which would have deadlocked if Do was
used.

Timing out the authentication attempt wouldn't directly abort
Process(); it would only exit if the connection was closed somewhere
else.

It may be a good idea to change some of this to guarantee that
ProcessAuthAsClient returns either an authenticated connection or closes
the connection, but I'll leave that as a separate task for the moment.
2017-11-02 15:41:35 -07:00
John Brooks e459a56286 Prevent deadlocks with Do or Break and closing connections
After the last round of fixes in Do, there was still one major issue
making it impossible to use Do or Break safely: closing connections.

Connections can be closed spontaneously, and this causes Process to
return. At that moment any ongoing call to Do or Break has deadlocked;
nothing will ever read those channels again. To prevent this, we have to
ensure that Do or Break won't try to send to Process' channels once it
has stopped reading from them. Doing that without other races is tricky.

The solution here, briefly, is to acquire a mutex in Do and Break before
checking the `closed` boolean, and hold that mutex for the entire
operation (including any blocking channel operations). When Process is
closing down the connection, it uses a separate goroutine to acquire the
same mutex and change the boolean, while still handling channel reads.
Once the boolean has changed, the mutex guarantees that nothing will try
to send to these channels again.

I've tried to document the problems and solutions in the code, because
it is subtle in some places and this is definitely critical code.
2017-11-02 15:41:34 -07:00
John Brooks c24773809e Fix and document safety problems with Connection.Do
There were several issues with the Do function that made it nearly
impossible to write safe code.

First, Do cannot be called recursively -- it will deadlock. There is
actually no way to implement a safe and recursive Do (or mutex) in Go,
because there is no primitive that will identify the current goroutine.

RequestOpenChannel used Do internally, which made it impossible to open
channels safely in many circumstances. That has been removed, so all
calls to RequestOpenChannel must be changed to happen under Do now.

Do now has more documentation and a new rule: no code exposed through
API can use Do, unless it has sole custody of the connection (such as
ProcessAuthAsClient).

Related to that problem, Do was impossible to call from inside handlers
(or anything else on the process goroutine) -- it would again just
deadlock. This is resolved by wrapping calls into user code to continue
handling invocations of Do (and only those) while the handler is
executing.

There is a third issue with connection close, but it will be addressed
in a separate commit

And finally, because it's impossible to timeout or interrupt a call to
Do, I also added a DoContext method that takes a go Context, which is
also passed through to the called function.
2017-11-02 15:41:30 -07:00
John Brooks 0f47f62465 Return the new channel from RequestOpenChannel
This fixes a quirk where it would've been difficult to tell which of
several channels of the same type+direction is the one you just created.

It's also a fairly common pattern to want to interact with a channel
right after opening it; for example, a chat channel is opened and can
immediately send messages before getting the peer response. It's
convenient to not have to do a separate lookup.
2017-11-02 15:40:02 -07:00
John Brooks d19102b257 Pass channel handler directly to RequestOpenChannel
RequestOpenChannel is the primary API to open a new outbound channel. It
was written to take a connection.Handler and use OnOpenChannelRequest
to get a channels.Handler to represent the new channel, which is the
same path that inbound channels will take.

Going through the global OnOpenChannelRequest method makes this much
less flexible and prevents passing parameters to the new channel handler
during creation.  This also requires users of the API to know/find the
connection handler, or worse, to boilerplate one into existence for their
channel creation.

Instead, I think this function should take a channels.Handler directly,
so that the caller gets full control over the handler for their new
channel.

As part of that change, I've also moved the authentication logic in
AutoConnectionHandler to be contained entirely within
{In,Out}boundConnectionHandler.
2017-11-02 15:40:02 -07:00
Sarah Jamie Lewis 1ed9265866
Merge pull request #31 from special/fix/pointers-to-interfaces
Don't use pointers to interfaces
2017-11-02 14:32:55 -07:00
Sarah Jamie Lewis ec16eee2aa
Merge pull request #29 from special/fix/create-outbound-conn
Make NegotiateVersionOutbound a public function
2017-11-02 14:31:42 -07:00
John Brooks a62d1bbcc9 Improve ChatChannel API for message acknowledgement
ChatChannel didn't return the message ID for sent messages, which made
using the returned ACKs impossible. The SendMessage method now returns
the uin32 messageID.

Also, SendMessage didn't support the TimeDelta field for messages, which
is used for queued or resent messages. This is now available as
SendMessageWithTime.

And finally, the ChatMessageAck callback didn't indicate if mesasges
were accepted or not, which is part of the protocol. That was added as a
field, which is unfortunately a breaking API change, but I've made
enough of those lately to not feel guilty about it.
2017-09-25 13:04:21 -07:00
John Brooks ea788d58ef Don't use pointers to interfaces
There are few situations where a pointer to an interface is useful in
Go, and this isn't one. Interfaces can hold types by value or pointer,
so long as that type fulfills the interface.
2017-09-23 16:44:12 -06:00
John Brooks 41d9401ca4 Make NegotiateVersionOutbound a public function
This is needed to allow creating a Connection for an arbitrary
application-provided io.ReadWriteCloser, instead of doing network logic
inside of go-ricochet. ricochet-go does its own connection management.

I would rather rework this API so that Connection has two construct
methods, inbound and outbound, that do version negotiation and are
always used. The methods that do networking and construct a Connection
would then be a separate (and non-root) package building on top of that.
2017-09-19 14:19:06 -06:00
Dan Ballard f04239c885 make echobot use new SetupOnion 2017-09-04 20:33:21 -07:00
Sarah Jamie Lewis d2dceef028 Merge pull request #26 from dballard/crypto-utils
Add more crypto/utils and extend SetupOnion to support unix sockets
2017-08-15 11:34:26 -07:00
Dan Ballard 5937ceee73 Add more crypto/utils and extend SetupOnion to support unix sockets 2017-08-14 08:43:33 -07:00
Sarah Jamie Lewis 93baafc2f7 Adding bulb to godep 2017-08-05 12:48:25 -07:00
Sarah Jamie Lewis 22cbf5d738 First Cut of Applications + Bugs, Formatting 2017-07-04 11:29:11 -07:00
Sarah Jamie Lewis 1cf7c2b7c7 Adding a Trace log to Connection and removing all other logging directives 2017-06-27 12:48:35 -07:00
Sarah Jamie Lewis f4ed1c244b Adding Inbound Version Negotiation
+ Error handling for missing private key setting
2017-06-27 10:39:33 -07:00
Sarah Jamie Lewis a2fa40492a Merge pull request #22 from dballard/patch-1
fix typo
2017-06-10 20:18:11 -07:00
Sarah Jamie Lewis 4f1a2f82cc Merge pull request #21 from dballard/new_api-fix_app
fix syntax errors
2017-06-10 20:17:58 -07:00
Dan Ballard d895b46a03 fix typo 2017-06-10 16:19:56 -07:00
Dan Ballard 6f07cff0bc fix syntax errors 2017-06-10 15:20:41 -07:00
Sarah Jamie Lewis 5d767174b1 Brand new API v0.2 2017-05-02 16:33:51 -07:00
Sarah Jamie Lewis 5a720a08d0 Merge pull request #17 from special/api-handlers
Rework the API around connection events
2017-01-15 15:49:09 -08:00
John Brooks 860ae9a024 Rework the API around connection events
This is a rework of some parts of the API to make connection management
for applications more sane and reliable.

- The RicochetService interface is split into the ServiceHandler and
  ConnectionHandler interfaces. ServiceHandler is implemented by the
  application to handle inbound connections to a listener.
  ConnectionHandler is implemented to handle events for a single
  OpenConnection. Handler instances should no longer be shared for
  different listeners or connections.

- Instead of automatically starting a processConnection goroutine, the
  application is now responsible for calling OpenConnection.Process in a
  goroutine to act on the connection. This function blocks until the
  connection is closed. This change allows a better application pattern
  for setting the handler of a connection and reacting to connection
  loss.

- It is no longer necessary to have started a listener in order to make
  outbound connections.

- The Ricochet type is removed, because it no longer served any purpose,
  and this avoids having any shared state between different listeners or
  connections.
2016-12-03 16:53:13 -08:00
Sarah Jamie Lewis 630efa186e Fixing up EchoBot for golint 2016-11-08 15:12:50 -08:00
Sarah Jamie Lewis 9d6592e1e4 Fixing up some comments 2016-11-08 15:05:05 -08:00
Sarah Jamie Lewis c37f9008b6 gofmt simplify 2016-11-08 14:55:17 -08:00
Sarah Jamie Lewis 985e7d3c5c Update README.md 2016-11-08 14:45:51 -08:00
Sarah Jamie Lewis 13b2f9111d Counting main package in tests 2016-11-08 14:42:58 -08:00
Sarah Jamie Lewis 779dabed9c Fixing spelling 2016-11-08 14:40:38 -08:00