O’Reilly has a very good book on Asterisk that should get you going. You can actually download a PDF version from Digium: asterisk.org/support
Nevertheless, here is roughly what I’ve got working on my machine:
sip.conf
In the general part I have the register commands for the external sip gateways. Then you also need a section [sipWhatever] with macthing credentials for each gateway. In this section, you define a context, for example context=from_GW1. We’ll need this later in the dialplan.
Also in sip.conf are sections for each phone, for example [sipPhone1]. Again, they need a context, for example context=PrivatePhones
zapata.conf
You need this to define your cards that connect to PSTN. There might be other config files for the drivers, but that depends on your hardware. Again, we need a context, for example context=from_PSTN
extensions.conf
This is the dial plan that ties everything together. For each of the contexts that we mentioned earlier, we now need a section.
[PrivatePhones]
; here we can dial our internal SIP phones that reference this context
exten => 101,1,Dial(SIP/sipPhone1,r)
exten => 102,1,Dial(SIP/sipPhone2,r)
; here we also tell it how to do outgoing calls, now we need some prefixes,
; for example 8xxxxx sends xxxxx to the SIP gateway, 9 goes to PSTN
exten => _8.,1,Dial(SIP/${EXTEN:1}@cheapsipgateway.com,60,trg))
exten => _9.,1,Dial(ZAP/1/${EXTEN:1},r)
; Incoming calls:
[from_GW1]
; our phone number at GW1 is 87654321
exten => 87654321,1,Dial(SIP/sipPhone1,20,r)
exten => 87654321,2,VoiceMail(u101@default)
exten => 87654321,102,VoiceMail(b101@default)
[from_PSTN]
; since this is hardware, we accept anything
exten => s,1,Dial(SIP/sipPhone1,20,r)
exten => s,2,VoiceMail(u101@default)
exten => s,102,VoiceMail(b101@default)
The voice mail bit is another story, leave those lines out in the beginning.
What I wrote up here is really just an example that should explain the principle how the configurations files are tied together. I have not copied them verbatim from a working system. Have a look at the book I mentioned in the first line. They explain all these things much better.