lpdsend.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. #include <sys/types.h>
  2. #include <unistd.h>
  3. #include <sys/stat.h>
  4. #include <sys/param.h>
  5. #include <stdlib.h>
  6. #include <fcntl.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <signal.h>
  10. #define REDIALTIMEOUT 15
  11. #ifdef PLAN9
  12. #include <Plan9libnet.h>
  13. #endif
  14. #define TIMEOUT 600
  15. char tmpfilename[L_tmpnam+1];
  16. int alarmstate = 0;
  17. int debugflag = 0;
  18. int killflag = 0;
  19. int statflag = 0;
  20. void
  21. cleanup(void) {
  22. unlink(tmpfilename);
  23. }
  24. #define SBSIZE 8192
  25. unsigned char sendbuf[SBSIZE];
  26. void
  27. debug(char *str) {
  28. if (debugflag)
  29. fprintf(stderr, "%s", str);
  30. }
  31. void
  32. alarmhandler(int sig) {
  33. fprintf(stderr, "timeout occurred, check printer.\n");
  34. exit(2);
  35. }
  36. /* send a message after each WARNPC percent of data sent */
  37. #define WARNPC 5
  38. int
  39. copyfile(int in, int out, long tosend) {
  40. int n;
  41. int sent = 0;
  42. int percent = 0;
  43. if (debugflag)
  44. fprintf(stderr, "lpdsend: copyfile(%d,%d,%ld)\n",
  45. in, out, tosend);
  46. while ((n=read(in, sendbuf, SBSIZE)) > 0) {
  47. if (debugflag)
  48. fprintf(stderr, "lpdsend: copyfile read %d bytes from %d\n",
  49. n, in);
  50. alarm(TIMEOUT); alarmstate = 1;
  51. if (write(out, sendbuf, n) != n) {
  52. alarm(0);
  53. fprintf(stderr, "write to fd %d failed\n", out);
  54. return(0);
  55. }
  56. alarm(0);
  57. if (debugflag)
  58. fprintf(stderr, "lpdsend: copyfile wrote %d bytes to %d\n",
  59. n, out);
  60. sent += n;
  61. if (tosend && ((sent*100/tosend)>=(percent+WARNPC))) {
  62. percent += WARNPC;
  63. fprintf(stderr, ": %5.2f%% sent\n", sent*100.0/tosend);
  64. }
  65. }
  66. if (debugflag)
  67. fprintf(stderr, "lpdsend: copyfile read %d bytes from %d\n",
  68. n, in);
  69. return(!n);
  70. }
  71. char strbuf[120];
  72. char hostname[MAXHOSTNAMELEN], *username, *printername, *killarg;
  73. char *inputname;
  74. char filetype = 'o'; /* 'o' is for PostScript */
  75. int seqno = 0;
  76. char *seqfilename;
  77. void
  78. killjob(int printerfd) {
  79. int strlength;
  80. if (printername==0) {
  81. fprintf(stderr, "no printer name\n");
  82. exit(1);
  83. }
  84. if (username==0) {
  85. fprintf(stderr, "no user name given\n");
  86. exit(1);
  87. }
  88. if (killarg==0) {
  89. fprintf(stderr, "no job to kill\n");
  90. exit(1);
  91. }
  92. sprintf(strbuf, "%c%s %s %s\n", '\5', printername, username, killarg);
  93. strlength = strlen(strbuf);
  94. if (write(printerfd, strbuf, strlength) != strlength) {
  95. fprintf(stderr, "write(printer) error\n");
  96. exit(1);
  97. }
  98. copyfile(printerfd, 2, 0L);
  99. }
  100. void
  101. checkqueue(int printerfd) {
  102. int n, strlength;
  103. unsigned char sendbuf[1];
  104. sprintf(strbuf, "%c%s\n", '\4', printername);
  105. strlength = strlen(strbuf);
  106. if (write(printerfd, strbuf, strlength) != strlength) {
  107. fprintf(stderr, "write(printer) error\n");
  108. exit(1);
  109. }
  110. copyfile(printerfd, 2, 0L);
  111. /*
  112. while ((n=read(printerfd, sendbuf, 1)) > 0) {
  113. write(2, sendbuf, n);
  114. }
  115. */
  116. }
  117. void
  118. getack(int printerfd, int as) {
  119. char resp;
  120. int rv;
  121. alarm(TIMEOUT); alarmstate = as;
  122. if ((rv=read(printerfd, &resp, 1)) != 1 || resp != '\0') {
  123. fprintf(stderr, "getack failed: read returned %d, read value (if any) %d, alarmstate=%d\n",
  124. rv, resp, alarmstate);
  125. exit(1);
  126. }
  127. alarm(0);
  128. }
  129. /* send control file */
  130. void
  131. sendctrl(int printerfd) {
  132. char cntrlstrbuf[256];
  133. int strlength, cntrlen;
  134. sprintf(cntrlstrbuf, "H%s\nP%s\n%cdfA%3.3d%s\n", hostname, username, filetype, seqno, hostname);
  135. cntrlen = strlen(cntrlstrbuf);
  136. sprintf(strbuf, "%c%d cfA%3.3d%s\n", '\2', cntrlen, seqno, hostname);
  137. strlength = strlen(strbuf);
  138. if (write(printerfd, strbuf, strlength) != strlength) {
  139. fprintf(stderr, "write(printer) error\n");
  140. exit(1);
  141. }
  142. getack(printerfd, 3);
  143. if (write(printerfd, cntrlstrbuf, cntrlen) != cntrlen) {
  144. fprintf(stderr, "write(printer) error\n");
  145. exit(1);
  146. }
  147. if (write(printerfd, "\0", 1) != 1) {
  148. fprintf(stderr, "write(printer) error\n");
  149. exit(1);
  150. }
  151. getack(printerfd, 4);
  152. }
  153. /* send data file */
  154. void
  155. senddata(int inputfd, int printerfd, long size) {
  156. int strlength;
  157. sprintf(strbuf, "%c%d dfA%3.3d%s\n", '\3', size, seqno, hostname);
  158. strlength = strlen(strbuf);
  159. if (write(printerfd, strbuf, strlength) != strlength) {
  160. fprintf(stderr, "write(printer) error\n");
  161. exit(1);
  162. }
  163. getack(printerfd, 5);
  164. if (!copyfile(inputfd, printerfd, size)) {
  165. fprintf(stderr, "failed to send file to printer\n");
  166. exit(1);
  167. }
  168. if (write(printerfd, "\0", 1) != 1) {
  169. fprintf(stderr, "write(printer) error\n");
  170. exit(1);
  171. }
  172. fprintf(stderr, "%d bytes sent, status: waiting for end of job\n", size);
  173. getack(printerfd, 6);
  174. }
  175. void
  176. sendjob(int inputfd, int printerfd) {
  177. struct stat statbuf;
  178. int strlength;
  179. if (fstat(inputfd, &statbuf) < 0) {
  180. fprintf(stderr, "fstat(%s) failed\n", inputname);
  181. exit(1);
  182. }
  183. sprintf(strbuf, "%c%s\n", '\2', printername);
  184. strlength = strlen(strbuf);
  185. if (write(printerfd, strbuf, strlength) != strlength) {
  186. fprintf(stderr, "write(printer) error\n");
  187. exit(1);
  188. }
  189. getack(printerfd, 2);
  190. debug("send data\n");
  191. senddata(inputfd, printerfd, statbuf.st_size);
  192. debug("send control info\n");
  193. sendctrl(printerfd);
  194. fprintf(stderr, "%ld bytes sent, status: end of job\n", statbuf.st_size);
  195. }
  196. /*
  197. * make an address, add the defaults
  198. */
  199. char *
  200. netmkaddr(char *linear, char *defnet, char *defsrv)
  201. {
  202. static char addr[512];
  203. char *cp;
  204. /*
  205. * dump network name
  206. */
  207. cp = strchr(linear, '!');
  208. if(cp == 0){
  209. if(defnet==0){
  210. if(defsrv)
  211. snprintf(addr, sizeof addr, "net!%s!%s", linear, defsrv);
  212. else
  213. snprintf(addr, sizeof addr, "net!%s", linear);
  214. }
  215. else {
  216. if(defsrv)
  217. snprintf(addr, sizeof addr, "%s!%s!%s", defnet, linear, defsrv);
  218. else
  219. snprintf(addr, sizeof addr, "%s!%s", defnet, linear);
  220. }
  221. return addr;
  222. }
  223. /*
  224. * if there is already a service, use it
  225. */
  226. cp = strchr(cp+1, '!');
  227. if(cp)
  228. return linear;
  229. /*
  230. * add default service
  231. */
  232. if(defsrv == 0)
  233. return linear;
  234. sprintf(addr, "%s!%s", linear, defsrv);
  235. return addr;
  236. }
  237. main(int argc, char *argv[]) {
  238. int c, usgflg = 0;
  239. char *desthostname;
  240. char *hnend;
  241. int printerfd;
  242. int inputfd;
  243. int sendport;
  244. char portstr[4];
  245. if (signal(SIGALRM, alarmhandler) == SIG_ERR) {
  246. fprintf(stderr, "failed to set alarm handler\n");
  247. exit(1);
  248. }
  249. while ((c = getopt(argc, argv, "Dd:k:qs:t:H:P:")) != -1)
  250. switch (c) {
  251. case 'D':
  252. debugflag = 1;
  253. debug("debugging on\n");
  254. break;
  255. case 'd':
  256. printername = optarg;
  257. break;
  258. case 'k':
  259. if (statflag) {
  260. fprintf(stderr, "cannot have both -k and -q flags\n");
  261. exit(1);
  262. }
  263. killflag = 1;
  264. killarg = optarg;
  265. break;
  266. case 'q':
  267. if (killflag) {
  268. fprintf(stderr, "cannot have both -q and -k flags\n");
  269. exit(1);
  270. }
  271. statflag = 1;
  272. break;
  273. case 's':
  274. seqno = strtol(optarg, NULL, 10);
  275. if (seqno < 0 || seqno > 999)
  276. seqno = 0;
  277. break;
  278. case 't':
  279. switch (filetype) {
  280. case 'c':
  281. case 'd':
  282. case 'f':
  283. case 'g':
  284. case 'l':
  285. case 'n':
  286. case 'o':
  287. case 'p':
  288. case 'r':
  289. case 't':
  290. case 'v':
  291. case 'z':
  292. filetype = optarg[0];
  293. break;
  294. default:
  295. usgflg++;
  296. break;
  297. }
  298. break;
  299. case 'H':
  300. strncpy(hostname, optarg, MAXHOSTNAMELEN);
  301. break;
  302. case 'P':
  303. username = optarg;
  304. break;
  305. default:
  306. case '?':
  307. fprintf(stderr, "unknown option %c\n", c);
  308. usgflg++;
  309. }
  310. if (argc < 2) usgflg++;
  311. if (optind < argc) {
  312. desthostname = argv[optind++];
  313. } else
  314. usgflg++;
  315. if (usgflg) {
  316. fprintf(stderr, "usage: to send a job - %s -d printer -H hostname -P username [-s seqno] [-t[cdfgklnoprtvz]] desthost [filename]\n", argv[0]);
  317. fprintf(stderr, " to check status - %s -d printer -q desthost\n", argv[0]);
  318. fprintf(stderr, " to kill a job - %s -d printer -P username -k jobname desthost\n", argv[0]);
  319. exit(1);
  320. }
  321. /* make sure the file to send is here and ready
  322. * otherwise the TCP connection times out.
  323. */
  324. if (!statflag && !killflag) {
  325. if (optind < argc) {
  326. inputname = argv[optind++];
  327. debug("open("); debug(inputname); debug(")\n");
  328. inputfd = open(inputname, O_RDONLY);
  329. if (inputfd < 0) {
  330. fprintf(stderr, "open(%s) failed\n", inputname);
  331. exit(1);
  332. }
  333. } else {
  334. inputname = "stdin";
  335. tmpnam(tmpfilename);
  336. debug("using stdin\n");
  337. if ((inputfd = open(tmpfilename, O_RDWR|O_CREAT, 0600)) < 0) {
  338. fprintf(stderr, "open(%s) failed\n", tmpfilename);
  339. exit(1);
  340. }
  341. atexit(cleanup);
  342. debug("copy input to temp file ");
  343. debug(tmpfilename);
  344. debug("\n");
  345. if (!copyfile(0, inputfd, 0L)) {
  346. fprintf(stderr, "failed to copy file to temporary file\n");
  347. exit(1);
  348. }
  349. if (lseek(inputfd, 0L, 0) < 0) {
  350. fprintf(stderr, "failed to seek back to the beginning of the temporary file\n");
  351. exit(1);
  352. }
  353. }
  354. }
  355. sprintf(strbuf, "%s", netmkaddr(desthostname, "tcp", "printer"));
  356. fprintf(stderr, "connecting to %s\n", strbuf);
  357. for (sendport=721; sendport<=731; sendport++) {
  358. sprintf(portstr, "%3.3d", sendport);
  359. fprintf(stderr, " trying from port %s...", portstr);
  360. debug(" dial("); debug(strbuf); debug(", "); debug(portstr); debug(", 0, 0) ...");
  361. printerfd = dial(strbuf, portstr, 0, 0);
  362. if (printerfd >= 0) {
  363. fprintf(stderr, "connected\n");
  364. break;
  365. }
  366. fprintf(stderr, "failed\n");
  367. sleep(REDIALTIMEOUT);
  368. }
  369. if (printerfd < 0) {
  370. fprintf(stderr, "Cannot open a valid port!\n");
  371. fprintf(stderr, "- All source ports [721-731] may be busy.\n");
  372. fprintf(stderr, "- Is recipient ready and online?\n");
  373. fprintf(stderr, "- If all else fails, cycle the power!\n");
  374. exit(1);
  375. }
  376. /* hostname[8] = '\0'; */
  377. #ifndef PLAN9
  378. if (gethostname(hostname, sizeof(hostname)) < 0) {
  379. perror("gethostname");
  380. exit(1);
  381. }
  382. #endif
  383. /* if ((hnend = strchr(hostname, '.')) != NULL)
  384. *hnend = '\0';
  385. */
  386. if (statflag) {
  387. checkqueue(printerfd);
  388. } else if (killflag) {
  389. killjob(printerfd);
  390. } else {
  391. sendjob(inputfd, printerfd);
  392. }
  393. exit(0);
  394. }