socket2me.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /*
  2. * Redistribution and use in source and binary forms, with or without
  3. * modification, are permitted provided that the following conditions are
  4. * met:
  5. *
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. *
  9. * * Redistributions in binary form must reproduce the above
  10. * copyright notice, this list of conditions and the following
  11. * disclaimer in the documentation and/or other materials provided
  12. * with the distribution.
  13. *
  14. * * Neither the name of [original copyright holder] nor the names of
  15. * its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written
  17. * permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. *
  32. * Copyright (C) 2007-2014, Anthony Minessale II <anthm@freeswitch.org>
  33. */
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <sys/types.h>
  37. #include <sys/socket.h>
  38. #include <arpa/inet.h>
  39. #include <sys/wait.h>
  40. #include <string.h>
  41. #include <unistd.h>
  42. #include <spandsp.h>
  43. #define SOCKET2ME_DEBUG 0
  44. #define MAXPENDING 10000
  45. #define RCVBUFSIZE 4198
  46. #define PORT_MIN 9000
  47. #define PORT_MAX 10000
  48. static int use_port = PORT_MIN;
  49. static void phase_b_handler(t30_state_t *s, void *user_data, int result)
  50. {
  51. int session;
  52. session = (intptr_t) user_data;
  53. printf("Phase B handler on session %d - (0x%X) %s\n", session, result, t30_frametype(result));
  54. }
  55. static void phase_d_handler(t30_state_t *s, void *user_data, int result)
  56. {
  57. int session;
  58. char ident[21];
  59. t30_stats_t t;
  60. session = (intptr_t) user_data;
  61. printf("Phase D handler on session %d - (0x%X) %s\n", session, result, t30_frametype(result));
  62. t30_get_transfer_statistics(s, &t);
  63. printf( "Phase D: bit rate %d\n", t.bit_rate);
  64. printf( "Phase D: ECM %s\n", (t.error_correcting_mode) ? "on" : "off");
  65. printf( "Phase D: pages transferred %d\n", t.pages_transferred);
  66. printf( "Phase D: image size %d x %d\n", t.width, t.length);
  67. printf( "Phase D: image resolution %d x %d\n", t.x_resolution, t.y_resolution);
  68. printf( "Phase D: bad rows %d\n", t.bad_rows);
  69. printf( "Phase D: longest bad row run %d\n", t.longest_bad_row_run);
  70. printf( "Phase D: compression type %d\n", t.encoding);
  71. printf( "Phase D: image size %d\n", t.image_size);
  72. t30_get_local_ident(s, ident);
  73. printf( "Phase D: local ident '%s'\n", ident);
  74. t30_get_far_ident(s, ident);
  75. printf( "Phase D: remote ident '%s'\n", ident);
  76. }
  77. static void phase_e_handler(t30_state_t *s, void *user_data, int result)
  78. {
  79. int session;
  80. t30_stats_t t;
  81. const char *u;
  82. char ident[21];
  83. session = (intptr_t) user_data;
  84. printf("Phase E handler on session %d - (%d) %s\n", session, result, t30_completion_code_to_str(result));
  85. t30_get_transfer_statistics(s, &t);
  86. printf( "Phase E: bit rate %d\n", t.bit_rate);
  87. printf( "Phase E: ECM %s\n", (t.error_correcting_mode) ? "on" : "off");
  88. printf( "Phase E: pages transferred %d\n", t.pages_transferred);
  89. printf( "Phase E: image size %d x %d\n", t.width, t.length);
  90. printf( "Phase E: image resolution %d x %d\n", t.x_resolution, t.y_resolution);
  91. printf( "Phase E: bad rows %d\n", t.bad_rows);
  92. printf( "Phase E: longest bad row run %d\n", t.longest_bad_row_run);
  93. printf( "Phase E: coding method %s\n", t4_encoding_to_str(t.encoding));
  94. printf( "Phase E: image size %d bytes\n", t.image_size);
  95. t30_get_local_ident(s, ident);
  96. printf( "Phase E: local ident '%s'\n", ident);
  97. t30_get_far_ident(s, ident);
  98. printf( "Phase E: remote ident '%s'\n", ident);
  99. if ((u = t30_get_far_country(s)))
  100. printf( "Phase E: Remote was made in '%s'\n", u);
  101. if ((u = t30_get_far_vendor(s)))
  102. printf( "Phase E: Remote was made by '%s'\n", u);
  103. if ((u = t30_get_far_model(s)))
  104. printf( "Phase E: Remote is model '%s'\n", u);
  105. }
  106. static int document_handler(t30_state_t *s, void *user_data, int event)
  107. {
  108. int session;
  109. session = (intptr_t) user_data;
  110. printf("Document handler on session %d - event %d\n", session, event);
  111. return FALSE;
  112. }
  113. void die(char *error_str)
  114. {
  115. perror(error_str);
  116. exit(1);
  117. }
  118. static void set_vars(char *data)
  119. {
  120. char *start, *end, *p=malloc(strlen(data)+1);
  121. char name[8192],value[8192];
  122. if(!p) {
  123. perror("malloc");
  124. exit(1);
  125. }
  126. memcpy(p,data,strlen(data)+1);
  127. start=p;
  128. while(start != 0 && *start != '\0') {
  129. if(end = strchr(start,'\r')) {
  130. *end = '\0';
  131. if(*(end + 1) == '\n') {
  132. end+=2;
  133. } else {
  134. end++;
  135. }
  136. } else {
  137. return;
  138. }
  139. sscanf(start,"%s: %s",name,value);
  140. setenv(name,value,1);
  141. start = end;
  142. }
  143. free(p);
  144. }
  145. static int cheezy_get_var(char *data, char *name, char *buf, size_t buflen)
  146. {
  147. char *p=data;
  148. /* the old way didnt make sure that variable values were used for the name hunt
  149. * and didnt ensure that only a full match of the variable name was used
  150. */
  151. do {
  152. if(!strncmp(p,name,strlen(name)) && *(p+strlen(name))==':') break;
  153. } while((p = (strstr(p,"\n")+1))!=(char *)1);
  154. if (p != (char *)1 && *p!='\0') {
  155. char *v, *e;
  156. if ((v = strchr(p, ':'))) {
  157. v++;
  158. while(v && *v == ' ') {
  159. v++;
  160. }
  161. if (v) {
  162. if (!(e = strchr(v, '\r'))) {
  163. e = strchr(v, '\n');
  164. }
  165. }
  166. if (v && e) {
  167. int cplen;
  168. int len = e - v;
  169. if (len > buflen - 1) {
  170. cplen = buflen -1;
  171. } else {
  172. cplen = len;
  173. }
  174. strncpy(buf, v, cplen);
  175. *(buf+cplen) = '\0';
  176. return 1;
  177. }
  178. }
  179. }
  180. return 0;
  181. }
  182. void client_run(int client_socket, char *local_ip, int local_port, char *remote_ip, int remote_port)
  183. {
  184. char sendbuf[RCVBUFSIZE], recvbuf[RCVBUFSIZE], infobuf[RCVBUFSIZE];
  185. struct sockaddr_in addr = {0}, sendaddr = {0};
  186. int read_bytes;
  187. int usock;
  188. int reuse_addr = 1;
  189. fax_state_t fax;
  190. char tmp[512], fn[512], *file_name = "/tmp/test.tiff";
  191. int send_fax = FALSE;
  192. int g711 = 0;
  193. int pcmu = 0;
  194. snprintf(sendbuf, sizeof(sendbuf), "connect\n\n");
  195. send(client_socket, sendbuf, strlen(sendbuf), 0);
  196. if ((read_bytes = recv(client_socket, infobuf, sizeof(infobuf), 0)) < 0) {
  197. die("recv() failed");
  198. }
  199. #if SOCKET2ME_DEBUG
  200. printf("READ [%s]\n", infobuf);
  201. #endif
  202. if (cheezy_get_var(infobuf, "Channel-Read-Codec-Name", tmp, sizeof(tmp))) {
  203. if (!strcasecmp(tmp, "pcmu")) {
  204. g711 = 1;
  205. pcmu = 1;
  206. } else if (!strcasecmp(tmp, "pcma")) {
  207. g711 = 1;
  208. }
  209. }
  210. snprintf(sendbuf, sizeof(sendbuf), "sendmsg\n"
  211. "call-command: unicast\n"
  212. "local-ip: %s\n"
  213. "local-port: %d\n"
  214. "remote-ip: %s\n"
  215. "remote-port: %d\n"
  216. "transport: udp\n"
  217. "%s"
  218. "\n",
  219. local_ip, local_port,
  220. remote_ip, remote_port,
  221. g711 ? "flags: native\n" : ""
  222. );
  223. if (cheezy_get_var(infobuf, "variable_fax_file_name", fn, sizeof(fn))) {
  224. file_name = fn;
  225. }
  226. if (cheezy_get_var(infobuf, "variable_fax_mode", tmp, sizeof(tmp))) {
  227. if (!strcasecmp(tmp, "send")) {
  228. send_fax = TRUE;
  229. }
  230. }
  231. if (cheezy_get_var(infobuf, "variable_fax_preexec", tmp, sizeof(tmp))) {
  232. set_vars(infobuf);
  233. system(tmp);
  234. }
  235. #if SOCKET2ME_DEBUG
  236. printf("SEND: [%s]\n", sendbuf);
  237. #endif
  238. send(client_socket, sendbuf, strlen(sendbuf), 0);
  239. memset(recvbuf, 0, sizeof(recvbuf));
  240. if ((read_bytes = recv(client_socket, recvbuf, sizeof(recvbuf), 0)) < 0) {
  241. die("recv() failed");
  242. }
  243. #if SOCKET2ME_DEBUG
  244. printf("READ [%s]\n", recvbuf);
  245. #endif
  246. if ((usock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
  247. die("socket() failed");
  248. }
  249. setsockopt(usock, SOL_SOCKET, SO_REUSEADDR, &reuse_addr, sizeof(reuse_addr));
  250. addr.sin_family = AF_INET;
  251. addr.sin_addr.s_addr = htonl(INADDR_ANY);
  252. /*addr.sin_addr.s_addr = inet_addr(remote_ip);*/
  253. addr.sin_port = htons(remote_port);
  254. sendaddr.sin_family = AF_INET;
  255. sendaddr.sin_addr.s_addr = inet_addr(local_ip);
  256. sendaddr.sin_port = htons(local_port);
  257. if (bind(usock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  258. die("bind() failed");
  259. }
  260. printf("%s Fax filename: [%s] from %s:%d -> %s:%d\n", send_fax ? "Sending" : "Receiving", file_name, local_ip, local_port, remote_ip, remote_port);
  261. fax_init(&fax, send_fax);
  262. t30_set_local_ident(&fax.t30_state, "Socket 2 ME");
  263. t30_set_header_info(&fax.t30_state, "Socket 2 ME");
  264. if (send_fax) {
  265. t30_set_tx_file(&fax.t30_state, file_name, -1, -1);
  266. } else {
  267. t30_set_rx_file(&fax.t30_state, file_name, -1);
  268. }
  269. t30_set_phase_b_handler(&fax.t30_state, phase_b_handler, NULL);
  270. t30_set_phase_d_handler(&fax.t30_state, phase_d_handler, NULL);
  271. t30_set_phase_e_handler(&fax.t30_state, phase_e_handler, NULL);
  272. t30_set_document_handler(&fax.t30_state, document_handler, NULL);
  273. t30_set_ecm_capability(&fax.t30_state, TRUE);
  274. t30_set_supported_compressions(&fax.t30_state, T30_SUPPORT_T4_1D_COMPRESSION | T30_SUPPORT_T4_2D_COMPRESSION | T30_SUPPORT_T6_COMPRESSION);
  275. t30_set_supported_image_sizes(&fax.t30_state, T30_SUPPORT_US_LETTER_LENGTH | T30_SUPPORT_US_LEGAL_LENGTH | T30_SUPPORT_UNLIMITED_LENGTH
  276. | T30_SUPPORT_215MM_WIDTH | T30_SUPPORT_255MM_WIDTH | T30_SUPPORT_303MM_WIDTH);
  277. t30_set_supported_resolutions(&fax.t30_state, T30_SUPPORT_STANDARD_RESOLUTION | T30_SUPPORT_FINE_RESOLUTION | T30_SUPPORT_SUPERFINE_RESOLUTION
  278. | T30_SUPPORT_R8_RESOLUTION | T30_SUPPORT_R16_RESOLUTION);
  279. for (;;) {
  280. struct sockaddr_in local_addr = {0};
  281. size_t cliAddrLen = sizeof(local_addr);
  282. unsigned char audiobuf[1024], rawbuf[1024], outbuf[1024];
  283. short *usebuf = NULL;
  284. int tx, tx_bytes, bigger, sample_count;
  285. fd_set ready;
  286. FD_ZERO(&ready);
  287. FD_SET(usock, &ready);
  288. FD_SET(client_socket, &ready);
  289. bigger = usock > client_socket ? usock : client_socket;
  290. select(++bigger, &ready, NULL, NULL, NULL);
  291. if (FD_ISSET(client_socket, &ready)) {
  292. memset(recvbuf, 0, sizeof(recvbuf));
  293. if ((read_bytes = recv(client_socket, recvbuf, sizeof(recvbuf), 0)) < 0) {
  294. die("recv() failed");
  295. }
  296. if (read_bytes == 0) {
  297. break;
  298. }
  299. #if SOCKET2ME_DEBUG
  300. printf("READ [%s]\n", recvbuf);
  301. #endif
  302. }
  303. if (!FD_ISSET(usock, &ready)) {
  304. continue;
  305. }
  306. if ((read_bytes = recvfrom(usock, audiobuf, sizeof(audiobuf), 0, (struct sockaddr *) &local_addr, &cliAddrLen)) < 0) {
  307. die("recvfrom() failed");
  308. }
  309. if (g711) {
  310. int i;
  311. short *rp = (short *) rawbuf;
  312. for (i = 0; i < read_bytes; i++) {
  313. if (pcmu) {
  314. rp[i] = ulaw_to_linear(audiobuf[i]);
  315. } else {
  316. rp[i] = alaw_to_linear(audiobuf[i]);
  317. }
  318. }
  319. usebuf = rp;
  320. sample_count = read_bytes;
  321. } else {
  322. usebuf = (short *) audiobuf;
  323. sample_count = read_bytes / 2;
  324. }
  325. fax_rx(&fax, usebuf, sample_count);
  326. #if SOCKET2ME_DEBUG
  327. printf("Handling client %s:%d %d bytes\n", inet_ntoa(local_addr.sin_addr), ntohs(local_addr.sin_port), read_bytes);
  328. #endif
  329. if ((tx = fax_tx(&fax, (short *)outbuf, sample_count)) < 0) {
  330. printf("Fax Error\n");
  331. break;
  332. } else if (!tx) {
  333. continue;
  334. }
  335. if (g711) {
  336. int i;
  337. short *bp = (short *) outbuf;
  338. for (i = 0; i < tx; i++) {
  339. if (pcmu) {
  340. rawbuf[i] = linear_to_ulaw(bp[i]);
  341. } else {
  342. rawbuf[i] = linear_to_alaw(bp[i]);
  343. }
  344. }
  345. usebuf = (short *) rawbuf;
  346. tx_bytes = tx;
  347. } else {
  348. usebuf = (short *)outbuf;
  349. tx_bytes = tx * 2;
  350. }
  351. cliAddrLen = sizeof(sendaddr);
  352. if (sendto(usock, usebuf, tx_bytes, 0, (struct sockaddr *) &sendaddr, sizeof(sendaddr)) != tx_bytes) {
  353. die("sendto() sent a different number of bytes than expected");
  354. }
  355. }
  356. close(client_socket);
  357. close(usock);
  358. t30_terminate(&fax.t30_state);
  359. fax_release(&fax);
  360. if (cheezy_get_var(infobuf, "variable_fax_postexec", tmp, sizeof(tmp))) {
  361. set_vars(infobuf);
  362. system(tmp);
  363. }
  364. printf("Done\n");
  365. snprintf(sendbuf, sizeof(sendbuf), "hangup\n\n");
  366. send(client_socket, sendbuf, strlen(sendbuf), 0);
  367. }
  368. int client_accept(int servSock)
  369. {
  370. int client_sock;
  371. struct sockaddr_in echoClntAddr;
  372. unsigned int clntLen;
  373. clntLen = sizeof(echoClntAddr);
  374. if ((client_sock = accept(servSock, (struct sockaddr *) &echoClntAddr, &clntLen)) < 0) {
  375. die("accept() failed");
  376. }
  377. printf("Client Connect: [%s]\n", inet_ntoa(echoClntAddr.sin_addr));
  378. return client_sock;
  379. }
  380. int main(int argc, char *argv[])
  381. {
  382. int servSock, client_sock;
  383. int port = 8084;
  384. struct sockaddr_in addr;
  385. pid_t pid;
  386. unsigned int process_count = 0;
  387. int reuse_addr = 1;
  388. char *local_ip = NULL;
  389. char *remote_ip = NULL;
  390. char *signal_port_name = NULL;
  391. if (argc > 0) {
  392. local_ip = argv[1];
  393. }
  394. if (argc > 1) {
  395. remote_ip = argv[2];
  396. }
  397. if (argc > 2) {
  398. signal_port_name = argv[3];
  399. }
  400. if (!local_ip) {
  401. local_ip = "127.0.0.1";
  402. }
  403. if (!remote_ip) {
  404. remote_ip = "127.0.0.1";
  405. }
  406. if (signal_port_name) {
  407. port = atoi(signal_port_name);
  408. }
  409. if ((servSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
  410. die("Socket Error!\n");
  411. }
  412. setsockopt(servSock, SOL_SOCKET, SO_REUSEADDR, &reuse_addr, sizeof(reuse_addr));
  413. memset(&addr, 0, sizeof(addr));
  414. addr.sin_family = AF_INET;
  415. addr.sin_addr.s_addr = htonl(INADDR_ANY);
  416. addr.sin_port = htons(port);
  417. if (bind(servSock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  418. die("Bind Error!\n");
  419. return -1;
  420. }
  421. if (listen(servSock, MAXPENDING) < 0) {
  422. die("Listen error\n");
  423. return -1;
  424. }
  425. for (;;) {
  426. int local_port = use_port++;
  427. int remote_port = use_port++;
  428. if (use_port++ >= PORT_MAX) {
  429. use_port = PORT_MIN;
  430. }
  431. client_sock = client_accept(servSock);
  432. if ((pid = fork()) < 0) {
  433. die("fork() failed");
  434. } else if (pid == 0) {
  435. close(servSock);
  436. client_run(client_sock, local_ip, local_port, remote_ip, remote_port);
  437. exit(0);
  438. }
  439. #if SOCKET2ME_DEBUG
  440. printf("with child process: %d\n", (int) pid);
  441. #endif
  442. close(client_sock);
  443. process_count++;
  444. while (process_count) {
  445. pid = waitpid((pid_t) -1, NULL, WNOHANG);
  446. if (pid < 0) {
  447. die("waitpid() failed");
  448. } else if (pid == 0) {
  449. break;
  450. } else {
  451. process_count--;
  452. }
  453. }
  454. }
  455. }