#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <papi.h>
#include <ipp.h>
#include <ipp-listener.h>
papi_status_t
ipp_print_job(papi_service_t svc, papi_attribute_t **request,
papi_attribute_t ***response, ipp_reader_t iread, void *fd)
{
papi_status_t status;
papi_stream_t s = NULL;
papi_job_t j = NULL;
papi_attribute_t **operational = NULL;
papi_attribute_t **job_attributes = NULL;
char *queue = NULL;
ssize_t rc;
char buf[BUFSIZ];
char *host = NULL;
int fp = -1;
char *keys[] = { "attributes-natural-language", "attributes-charset",
"printer-uri", NULL };
(void) papiAttributeListGetCollection(request, NULL,
"operational-attributes-group", &operational);
get_printer_id(operational, &queue, NULL);
if (queue == NULL) {
ipp_set_status(response, status, "printer-uri: %s",
papiStatusString(status));
return (PAPI_BAD_REQUEST);
}
split_and_copy_attributes(keys, operational, NULL, &job_attributes);
if (papiAttributeListGetCollection(request, NULL,
"job-attributes-group", &operational) == PAPI_OK) {
char *user = NULL;
copy_attributes(&job_attributes, operational);
if (papiAttributeListGetString(operational, NULL,
"requesting-user-name", &user) == PAPI_OK) {
papiAttributeListAddString(&job_attributes,
PAPI_ATTR_REPLACE, "requesting-user-name", user);
}
}
(void) papiAttributeListGetInteger(request, NULL,
"peer-socket", &fp);
if (fp != -1) {
struct sockaddr_in peer;
socklen_t peer_len = sizeof (peer);
if (getpeername(fp, (struct sockaddr *)&peer,
&peer_len) == 0) {
struct hostent *he;
int error_num;
if ((he = getipnodebyaddr((const char *)&peer.sin_addr,
sizeof (peer.sin_addr), peer.sin_family,
&error_num)) == NULL) {
char tmp_buf[INET6_ADDRSTRLEN];
papiAttributeListAddString(
&job_attributes,
PAPI_ATTR_REPLACE,
"job-originating-host-name",
(char *)inet_ntop(peer.sin_family,
&peer.sin_addr, tmp_buf,
sizeof (tmp_buf)));
} else {
if (is_localhost(he->h_name) != 0)
papiAttributeListAddString(
&job_attributes,
PAPI_ATTR_REPLACE,
"job-originating-host-name",
"localhost");
else if (he->h_name != NULL)
papiAttributeListAddString(
&job_attributes,
PAPI_ATTR_REPLACE,
"job-originating-host-name",
he->h_name);
}
}
}
status = papiJobStreamOpen(svc, queue, job_attributes, NULL, &s);
papiAttributeListFree(job_attributes);
if (status != PAPI_OK) {
ipp_set_status(response, status, "job submission: %s",
ipp_svc_status_mesg(svc, status));
return (status);
}
while ((status == PAPI_OK) && ((rc = iread(fd, buf, sizeof (buf))) > 0))
status = papiJobStreamWrite(svc, s, buf, rc);
if (status != PAPI_OK) {
ipp_set_status(response, status, "write job data: %s",
ipp_svc_status_mesg(svc, status));
return (status);
}
status = papiJobStreamClose(svc, s, &j);
if (status != PAPI_OK) {
ipp_set_status(response, status, "close job stream: %s",
ipp_svc_status_mesg(svc, status));
papiJobFree(j);
return (status);
}
if (j != NULL) {
papi_to_ipp_job_group(response, request, PAPI_ATTR_REPLACE, j);
papiJobFree(j);
}
return (status);
}