[9] | 1 | /* proto.c Copyright Michael Temari 08/01/1996 All Rights Reserved */
|
---|
| 2 |
|
---|
| 3 | #include <sys/types.h>
|
---|
| 4 | #include <sys/stat.h>
|
---|
| 5 | #include <stdio.h>
|
---|
| 6 | #include <string.h>
|
---|
| 7 | #include <unistd.h>
|
---|
| 8 | #include <net/hton.h>
|
---|
| 9 | #include <net/gen/socket.h>
|
---|
| 10 | #include <net/gen/in.h>
|
---|
| 11 | #include <net/gen/inet.h>
|
---|
| 12 | #include <net/gen/tcp.h>
|
---|
| 13 | #include <net/gen/udp.h>
|
---|
| 14 |
|
---|
| 15 | #include "talk.h"
|
---|
| 16 | #include "proto.h"
|
---|
| 17 | #include "net.h"
|
---|
| 18 | #include "screen.h"
|
---|
| 19 |
|
---|
| 20 | _PROTOTYPE(static int TalkChk, (int gotreply, struct talk_reply *reply, char *msg));
|
---|
| 21 | _PROTOTYPE(static int TalkTrans, (int type, long id, struct talk_reply *reply, int here));
|
---|
| 22 |
|
---|
| 23 | static char *AnswerMsgs[] = {
|
---|
| 24 | "Success",
|
---|
| 25 | "User Not Logged In",
|
---|
| 26 | "Failure",
|
---|
| 27 | "Remote Does Not Know who we are",
|
---|
| 28 | "User is not accepting calls",
|
---|
| 29 | "Are request was not know",
|
---|
| 30 | "Incorrect Version",
|
---|
| 31 | "Bad Address",
|
---|
| 32 | "Bad Control Address"
|
---|
| 33 | };
|
---|
| 34 |
|
---|
| 35 | static int TalkChk(gotreply, reply, msg)
|
---|
| 36 | int gotreply;
|
---|
| 37 | struct talk_reply *reply;
|
---|
| 38 | char *msg;
|
---|
| 39 | {
|
---|
| 40 | if(!gotreply) {
|
---|
| 41 | ScreenMsg(msg);
|
---|
| 42 | return(-1);
|
---|
| 43 | }
|
---|
| 44 | if(reply->answer == SUCCESS) return(0);
|
---|
| 45 | if(reply->answer < (sizeof(AnswerMsgs) / sizeof(AnswerMsgs[0])))
|
---|
| 46 | ScreenMsg(AnswerMsgs[reply->answer]);
|
---|
| 47 | else
|
---|
| 48 | ScreenMsg("Bad Answer");
|
---|
| 49 |
|
---|
| 50 | return(-1);
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | static int TalkTrans(type, id, reply, here)
|
---|
| 54 | int type;
|
---|
| 55 | long id;
|
---|
| 56 | struct talk_reply *reply;
|
---|
| 57 | int here;
|
---|
| 58 | {
|
---|
| 59 | struct talk_request request;
|
---|
| 60 | int tries;
|
---|
| 61 | int gotreply;
|
---|
| 62 |
|
---|
| 63 | memset(&request, 0, sizeof(request));
|
---|
| 64 |
|
---|
| 65 | request.version = TALK_VERSION;
|
---|
| 66 | request.type = type;
|
---|
| 67 | request.id = id;
|
---|
| 68 | request.addr.sa_family = htons(AF_INET);
|
---|
| 69 | request.addr.sin_port = dataport;
|
---|
| 70 | request.addr.sin_addr = laddr;
|
---|
| 71 | request.ctl_addr.sa_family = htons(AF_INET);
|
---|
| 72 | request.ctl_addr.sin_port = ctlport;
|
---|
| 73 | request.ctl_addr.sin_addr = laddr;
|
---|
| 74 | request.pid = getpid();
|
---|
| 75 | strncpy(request.luser, luser, USER_SIZE);
|
---|
| 76 | strncpy(request.ruser, ruser, USER_SIZE);
|
---|
| 77 | strncpy(request.rtty, rtty, TTY_SIZE);
|
---|
| 78 |
|
---|
| 79 | tries = 0;
|
---|
| 80 | gotreply = 0;
|
---|
| 81 | while(!ScreenDone && tries++ < 3 && !gotreply) {
|
---|
| 82 | if(!sendrequest(&request, here))
|
---|
| 83 | if(!getreply(reply, 5))
|
---|
| 84 | gotreply = 1;
|
---|
| 85 | if(!gotreply) continue;
|
---|
| 86 | if(reply->version != request.version ||
|
---|
| 87 | reply->type != request.type)
|
---|
| 88 | gotreply = 0;
|
---|
| 89 | }
|
---|
| 90 | return(gotreply);
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | int TalkInit()
|
---|
| 94 | {
|
---|
| 95 | struct talk_reply reply;
|
---|
| 96 | long id = 0;
|
---|
| 97 | long rid;
|
---|
| 98 | int s;
|
---|
| 99 | int ring;
|
---|
| 100 | char buff[32];
|
---|
| 101 |
|
---|
| 102 | /* Check if someone was calling us */
|
---|
| 103 | ScreenMsg("Initiating Talk Protocol");
|
---|
| 104 |
|
---|
| 105 | /* Check is someone was calling us */
|
---|
| 106 | s = TalkTrans(LOOK_UP, ++id, &reply, 0);
|
---|
| 107 |
|
---|
| 108 | /* Someone was calling us */
|
---|
| 109 | if(s && reply.answer == SUCCESS) {
|
---|
| 110 | s = NetConnect(reply.addr.sin_port);
|
---|
| 111 | if(s == 1) {
|
---|
| 112 | ScreenMsg("Your party has hung up");
|
---|
| 113 | TalkTrans(DELETE, reply.id, &reply, 0);
|
---|
| 114 | }
|
---|
| 115 | return(s == 0 ? 0 : -1);
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | ScreenMsg("Ringing User");
|
---|
| 119 |
|
---|
| 120 | ring = 0;
|
---|
| 121 | while(!ScreenDone && ring++ < 5) {
|
---|
| 122 | if(TalkChk(TalkTrans(ANNOUNCE, -1, &reply, 0),
|
---|
| 123 | &reply, "No response to are ring"))
|
---|
| 124 | return(-1);
|
---|
| 125 | rid = reply.id;
|
---|
| 126 | sprintf(buff, "Ring #%d", ring);
|
---|
| 127 | ScreenMsg(buff);
|
---|
| 128 | if(ring == 1) {
|
---|
| 129 | if(TalkChk(TalkTrans(LEAVE_INVITE, ++id, &reply, 1),
|
---|
| 130 | &reply, "Could not leave are invitaion locally"))
|
---|
| 131 | return(-1);
|
---|
| 132 | }
|
---|
| 133 | s = NetListen(RING_WAIT);
|
---|
| 134 | if(s <= 0) {
|
---|
| 135 | TalkTrans(DELETE, reply.id, &reply, 1);
|
---|
| 136 | TalkTrans(DELETE, rid, &reply, 0);
|
---|
| 137 | return(s);
|
---|
| 138 | }
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | return(-1);
|
---|
| 142 | }
|
---|