Alexandre Lision | 7fd5d3d | 2013-12-04 13:06:40 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Test program for tivi interface |
| 3 | */ |
| 4 | #include <stdio.h> |
| 5 | #include <unistd.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <errno.h> |
| 8 | #include <string.h> |
| 9 | #include <sys/types.h> |
| 10 | #include <sys/socket.h> |
| 11 | #include <netinet/in.h> |
| 12 | #include <arpa/inet.h> |
| 13 | |
| 14 | #include <CtZrtpSession.h> |
| 15 | #include <CtZrtpCallback.h> |
| 16 | |
| 17 | struct sockaddr_in adr_inet; |
| 18 | struct sockaddr_in adr_clnt; |
| 19 | socklen_t lenClnt; // length |
| 20 | int s; // Socket |
| 21 | |
| 22 | static void hexdump(const char* title, const unsigned char *s, int l) |
| 23 | { |
| 24 | int n=0; |
| 25 | |
| 26 | if (s == NULL) return; |
| 27 | |
| 28 | fprintf(stderr, "%s",title); |
| 29 | for( ; n < l ; ++n) { |
| 30 | if((n%16) == 0) |
| 31 | fprintf(stderr, "\n%04x",n); |
| 32 | fprintf(stderr, " %02x",s[n]); |
| 33 | } |
| 34 | fprintf(stderr, "\n"); |
| 35 | } |
| 36 | |
| 37 | static void displayError(const char *what) { |
| 38 | fprintf(stderr, "Error: %s: %s\n", strerror(errno), what); |
| 39 | exit(1); |
| 40 | } |
| 41 | |
| 42 | static void sendData(uint8_t *buffer, unsigned int length) |
| 43 | { |
| 44 | int z = sendto(s, buffer, length, 0, (struct sockaddr *)&adr_clnt, lenClnt); |
| 45 | if ( z < 0 ) { |
| 46 | displayError("sendto(2)"); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | // This is the callback that we use for audio stream |
| 51 | class TestCallbackAudio: public CtZrtpCb { |
| 52 | void onNewZrtpStatus(CtZrtpSession *session, char *p, CtZrtpSession::streamName streamNm) { |
| 53 | uint8_t buffer[20]; |
| 54 | fprintf(stderr, "new status: %s\n", p == NULL ? "NULL" : p); |
| 55 | session->getInfo("sec_state", buffer, 19); |
| 56 | printf("secState: %s\n", buffer); |
| 57 | } |
| 58 | |
| 59 | void onNeedEnroll(CtZrtpSession *session, CtZrtpSession::streamName streamNm, int32_t info) { |
| 60 | fprintf(stderr, "Need enroll\n"); |
| 61 | } |
| 62 | |
| 63 | void onPeer(CtZrtpSession *session, char *name, int iIsVerified, CtZrtpSession::streamName streamNm) { |
| 64 | fprintf(stderr, "onPeer: %s", name == NULL || strlen(name) == 0 ? "NULL" : name); |
| 65 | fprintf(stderr, ", verified: %s\n", iIsVerified ? "YES" : "NO"); |
| 66 | uint8_t buffer[20]; |
| 67 | |
| 68 | session->getInfo("rs1", buffer, 19); |
| 69 | printf("RS1: %s ", buffer); |
| 70 | |
| 71 | session->getInfo("rs2", buffer, 19); |
| 72 | printf("RS2: %s ", buffer); |
| 73 | |
| 74 | session->getInfo("pbx", buffer, 19); |
| 75 | printf("PBX: %s ", buffer); |
| 76 | |
| 77 | session->getInfo("aux", buffer, 9); |
| 78 | printf("AUX: %s\n", buffer); |
| 79 | |
| 80 | session->getInfo("lbClient", buffer, 19); |
| 81 | printf("Client: %s ", buffer); |
| 82 | |
| 83 | session->getInfo("lbVersion", buffer, 19); |
| 84 | printf("Version: %s ", buffer); |
| 85 | |
| 86 | session->getInfo("lbChiper", buffer, 19); |
| 87 | printf("cipher: %s ", buffer); |
| 88 | |
| 89 | session->getInfo("lbHash", buffer, 19); |
| 90 | printf("hash: %s ", buffer); |
| 91 | |
| 92 | session->getInfo("lbAuthTag", buffer, 19); |
| 93 | printf("auth: %s ", buffer); |
| 94 | |
| 95 | session->getInfo("lbKeyExchange", buffer, 19); |
| 96 | printf("KeyEx: %s ", buffer); |
| 97 | |
| 98 | session->getInfo("sc_secure", buffer, 19); |
| 99 | printf("SC secure: %s ", buffer); |
| 100 | |
| 101 | session->getInfo("sdp_hash", buffer, 19); |
| 102 | printf("zrtp-hash: %s\n", buffer); |
| 103 | |
| 104 | session->setLastPeerNameVerify("TestName", 0); |
| 105 | } |
| 106 | |
| 107 | void onZrtpWarning(CtZrtpSession *session, char *p, CtZrtpSession::streamName streamNm) { |
| 108 | fprintf(stderr, "Warning: %s\n", p == NULL ? "NULL" : p); |
| 109 | } |
| 110 | |
| 111 | }; |
| 112 | |
| 113 | class TestSendCallbackAudio: public CtZrtpSendCb { |
| 114 | void sendRtp(CtZrtpSession const *session, uint8_t* packet, size_t length, CtZrtpSession::streamName streamNm) { |
| 115 | // hexdump("ZRTP packet", packet, length); |
| 116 | fprintf(stderr, "ZRTP send packet, length: %lu, %.8s\n", length, packet+16); |
| 117 | sendData(packet, length); |
| 118 | } |
| 119 | }; |
| 120 | |
| 121 | extern char zrtpBuildInfo[]; |
| 122 | static unsigned char recvAuxSecret[] = {1,2,3,4,5,6,7,8,9,0}; |
| 123 | |
| 124 | |
| 125 | int main(int argc,char **argv) { |
| 126 | int z; |
| 127 | ssize_t length; |
| 128 | socklen_t len_inet; |
| 129 | const char *srvr_addr = "127.0.0.1"; |
| 130 | uint8_t buffer[1300]; // Recv buffer |
| 131 | uint32_t uiSSRC = 0xfeedbacc; |
| 132 | |
| 133 | fprintf(stderr, "Config info: %s\n", getZrtpBuildInfo()); |
| 134 | |
| 135 | CtZrtpSession::initCache("testzid.dat"); // initialize cache file |
| 136 | |
| 137 | CtZrtpSession *session = new CtZrtpSession(); |
| 138 | TestCallbackAudio *callback = new TestCallbackAudio(); |
| 139 | TestSendCallbackAudio *sendCallback = new TestSendCallbackAudio(); |
| 140 | |
| 141 | session->init(true, true); // audio and video |
| 142 | |
| 143 | session->setUserCallback(callback, CtZrtpSession::AudioStream); |
| 144 | session->setSendCallback(sendCallback, CtZrtpSession::AudioStream); |
| 145 | session->getSignalingHelloHash((char*)buffer, CtZrtpSession::AudioStream, 0); |
| 146 | session->setAuxSecret(recvAuxSecret, sizeof(recvAuxSecret)); |
| 147 | |
| 148 | fprintf(stderr, "Our Hello hash: %s\n", buffer); |
| 149 | |
| 150 | // Set a bogous peer hello hash to force a warning |
| 151 | // session->setSignalingHelloHash("950ff29288587ca0115948f386a89aa98d41b56089f267e62d5cbd42c997aa7c", CtZrtpSession::AudioStream); |
| 152 | |
| 153 | s = socket(AF_INET,SOCK_DGRAM,0); |
| 154 | if ( s == -1 ) { |
| 155 | displayError("socket()"); |
| 156 | } |
| 157 | memset(&adr_inet,0,sizeof adr_inet); |
| 158 | adr_inet.sin_family = AF_INET; |
| 159 | adr_inet.sin_port = htons(5002); |
| 160 | adr_inet.sin_addr.s_addr = inet_addr(srvr_addr); |
| 161 | |
| 162 | if (adr_inet.sin_addr.s_addr == INADDR_NONE ) { |
| 163 | displayError("bad address listener."); |
| 164 | } |
| 165 | len_inet = sizeof(adr_inet); |
| 166 | |
| 167 | z = bind(s, (struct sockaddr *)&adr_inet, len_inet); |
| 168 | if ( z == -1 ) { |
| 169 | displayError("bind()"); |
| 170 | } |
| 171 | |
| 172 | memset(&adr_inet,0,sizeof adr_inet); |
| 173 | adr_clnt.sin_family = AF_INET; |
| 174 | adr_clnt.sin_port = htons(5004); |
| 175 | adr_clnt.sin_addr.s_addr = inet_addr(srvr_addr); |
| 176 | |
| 177 | if (adr_clnt.sin_addr.s_addr == INADDR_NONE ) { |
| 178 | displayError("bad address listener."); |
| 179 | } |
| 180 | lenClnt = sizeof(adr_clnt); |
| 181 | |
| 182 | if (!session->isStarted(CtZrtpSession::AudioStream)) |
| 183 | session->start(uiSSRC, CtZrtpSession::AudioStream); |
| 184 | |
| 185 | // Now wait for requests: |
| 186 | for (;;) { |
| 187 | |
| 188 | len_inet = sizeof(adr_clnt); |
| 189 | length = recvfrom(s, buffer, sizeof(buffer), 0, NULL, NULL); |
| 190 | if (length < 0) { |
| 191 | displayError("recvfrom(2)"); |
| 192 | } |
| 193 | // hexdump("Data before processing", buffer, length); |
| 194 | |
| 195 | // if (!session->isStarted(CtZrtpSession::AudioStream)) |
| 196 | // session->start(uiSSRC, CtZrtpSession::AudioStream); |
| 197 | |
| 198 | /* |
| 199 | * process incoming data |
| 200 | */ |
| 201 | size_t newLength; |
| 202 | int rc = session->processIncomingRtp(buffer, length, &newLength, CtZrtpSession::AudioStream); |
| 203 | fprintf(stderr, "processing returns: %d\n", rc); |
| 204 | // hexdump("Data after processing", buffer, newLength); |
| 205 | if (rc == 0) |
| 206 | continue; // drop packet |
| 207 | |
| 208 | fprintf(stderr, "Received data: %s\n", &buffer[12]); // assume normal RTP packet for debug printout |
| 209 | } |
| 210 | /* |
| 211 | * Close the socket and exit: |
| 212 | */ |
| 213 | close(s); |
| 214 | return 0; |
| 215 | } |