ptg10805159
834 Communicating with a Network Printer Chapter 21
672 if ((sockfd = connect_retry(AF_INET, SOCK_STREAM, 0,
673 printer->ai_addr, printer->ai_addrlen)) < 0) {
674 log_msg("job %d deferred - can’t contact printer: %s",
675 jp->jobid, strerror(errno));
676 goto defer;
677 }
678 /*
679 * Set up the IPP header.
680 */
681 icp=ibuf;
682 hp=(struct ipp_hdr *)icp;
683 hp->major_version=1;
684 hp->minor_version=1;
685 hp->operation=htons(OP_PRINT_JOB);
686 hp->request_id=htonl(jp->jobid);
687 icp += offsetof(struct ipp_hdr, attr_group);
688 *icp++=TAG_OPERATION_ATTR;
689 icp=add_option(icp, TAG_CHARSET, "attributes-charset",
690 "utf-8");
691 icp=add_option(icp, TAG_NATULANG,
692 "attributes-natural-language", "en-us");
693 sprintf(str, "http://%s/ipp", printer_name);
694 icp=add_option(icp, TAG_URI, "printer-uri", str);
695 icp=add_option(icp, TAG_NAMEWOLANG,
696 "requesting-user-name", jp->req.usernm);
697 icp=add_option(icp, TAG_NAMEWOLANG, "job-name",
698 jp->req.jobnm);
[672 – 677] We open a stream socket connected to the printer.Iftheconnect_retry
call fails, we jump down todefer,wherewewill clean up, delay,and try
again later.
[678 – 698] Next, we set up the IPP header.The operation is a print-job request. Weuse
htonsto convert the 2-byte operation ID from host to network byte order
andhtonlto convert the 4-byte job ID from host to network byte order.
After the initial portion of the header, we set the tag value to indicate that
operation attributes follow.Wecalladd_optionto add attributes to the
message. Figure21.5 lists the required and optional attributes for print-job
requests; the first three arerequired. Wespecify the character set to be
UTF-8, which the printer must support. We specify the language asen-us,
which represents U.S. English. Another required attribute is the printer
Universal Resource Identifier(URI); we set it tohttp://printer_name/ipp.
Therequesting-user-nameattribute is recommended, but not required.
Thejob-nameattribute is optional. Recall that theprintcommand sends
the name of the file being printed as the job name, which can help users
distinguish among multiple pending jobs.