1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  * Copyright (C) 2016 Mopria Alliance, Inc.
4  * Copyright (C) 2013 Hewlett-Packard Development Company, L.P.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <sys/stat.h>
23 #include <unistd.h>
24 #include <errno.h>
25 #include <sys/socket.h>
26 #include <arpa/inet.h>
27 #include <fcntl.h>
28 #include <netdb.h>
29 
30 #include "ifc_print_job.h"
31 #include "wprint_debug.h"
32 
33 #define TAG "printer"
34 
35 #define DEFAULT_TIMEOUT (5000)
36 
37 typedef struct {
38     ifc_print_job_t ifc;
39     int port_num;
40     int psock;
41     wJob_t job_id;
42     status_t job_status;
43     int timeout_enabled;
44 } _print_job_t;
45 
46 static long int _wprint_timeout_msec = DEFAULT_TIMEOUT;
47 
_init(const ifc_print_job_t * this_p,const char * printer_addr,int port,const char * printer_uri,bool use_secure_uri)48 static status_t _init(const ifc_print_job_t *this_p, const char *printer_addr, int port,
49         const char *printer_uri, bool use_secure_uri) {
50     _print_job_t *print_job = IMPL(_print_job_t, ifc, this_p);
51 
52     if (!print_job) return ERROR;
53 
54     // if a print-to-file is requested, open a file
55 
56     if (print_job->port_num == PORT_FILE) {
57         print_job->psock = open(printer_addr, O_CREAT | O_WRONLY | O_TRUNC,
58                 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
59 
60         if (print_job->psock == ERROR) {
61             LOGE("cannot create output file : %s, %s", printer_addr, strerror(errno));
62         } else {
63             LOGI("opened %s for writing", printer_addr);
64         }
65     } else {
66         // open a socket to the printer:port
67         print_job->psock = wConnect(printer_addr, print_job->port_num, _wprint_timeout_msec);
68     }
69 
70     print_job->job_status = ((print_job->psock != -1) ? OK : ERROR);
71     return print_job->job_status;
72 }
73 
_destroy(const ifc_print_job_t * this_p)74 static void _destroy(const ifc_print_job_t *this_p) {
75     _print_job_t *print_job = IMPL(_print_job_t, ifc, this_p);
76     if (print_job) {
77         free(print_job);
78     }
79 }
80 
_start_job(const ifc_print_job_t * this_p,const wprint_job_params_t * job_params,const printer_capabilities_t * printer_caps)81 static int _start_job(const ifc_print_job_t *this_p, const wprint_job_params_t *job_params,
82         const printer_capabilities_t *printer_caps) {
83     _print_job_t *print_job = IMPL(_print_job_t, ifc, this_p);
84 
85     if (print_job) {
86         return OK;
87     } else {
88         return ERROR;
89     }
90 }
91 
_send_data(const ifc_print_job_t * this_p,const char * buffer,size_t length)92 static int _send_data(const ifc_print_job_t *this_p, const char *buffer, size_t length) {
93     status_t retval = OK;
94     size_t length_in = length;
95     ssize_t bytes_written;
96     _print_job_t *print_job = IMPL(_print_job_t, ifc, this_p);
97 
98     if (this_p && buffer && (print_job->job_status == OK)) {
99         if (print_job->port_num == PORT_FILE) {
100             while ((length > 0) && (retval != -1)) {
101                 bytes_written = write(print_job->psock, buffer, length);
102                 if (bytes_written < 0) {
103                     retval = ERROR;
104                 } else {
105                     length -= bytes_written;
106                     buffer += bytes_written;
107                 }
108             }
109         } else {
110             fd_set w_fds;
111             int selreturn;
112             struct timeval timeout;
113 
114             while ((length > 0) && (retval == OK)) {
115                 FD_ZERO(&w_fds);
116                 FD_SET(print_job->psock, &w_fds);
117                 timeout.tv_sec = 20;
118                 timeout.tv_usec = 0;
119                 selreturn = select(print_job->psock + 1, NULL, &w_fds, NULL, &timeout);
120                 if (selreturn < 0) {
121                     LOGE("select returned an errnor (%d)", errno);
122                     retval = ERROR;
123                 } else if (selreturn > 0) {
124                     if (FD_ISSET(print_job->psock, &w_fds)) {
125                         bytes_written = write(print_job->psock, buffer, length);
126                         if (bytes_written < 0) {
127                             LOGE("unable to transmit %zu bytes of data (errno %d)", length, errno);
128                             retval = ERROR;
129                         } else {
130                             length -= bytes_written;
131                             buffer += bytes_written;
132                         }
133                     } else {
134                         LOGE("select returned OK, but fd is not set");
135                         retval = ERROR;
136                     }
137                 } else {
138                     retval = (print_job->timeout_enabled ? ERROR : OK);
139                     if (retval == ERROR) {
140                         LOGE("select timed out");
141                     }
142                 }
143             }
144         }
145 
146         print_job->job_status = retval;
147     } else {
148         retval = ERROR;
149     }
150     return ((retval == OK) ? length_in : (int)ERROR);
151 }
152 
_end_job(const ifc_print_job_t * this_p)153 static int _end_job(const ifc_print_job_t *this_p) {
154     _print_job_t *print_job = IMPL(_print_job_t, ifc, this_p);
155     if (print_job) {
156         close(print_job->psock);
157         print_job->psock = -1;
158         return print_job->job_status;
159     }
160     return ERROR;
161 }
162 
_enable_timeout(const ifc_print_job_t * this_p,int enable)163 static void _enable_timeout(const ifc_print_job_t *this_p, int enable) {
164     _print_job_t *print_job = IMPL(_print_job_t, ifc, this_p);
165     if (print_job) {
166         print_job->timeout_enabled = enable;
167     }
168 }
169 
_check_status(const ifc_print_job_t * this_p)170 static int _check_status(const ifc_print_job_t *this_p) {
171     _print_job_t *print_job = IMPL(_print_job_t, ifc, this_p);
172 
173     if (print_job) return print_job->job_status;
174 
175     return ERROR;
176 }
177 
wConnect(const char * printer_addr,int port_num,long int timeout_msec)178 int wConnect(const char *printer_addr, int port_num, long int timeout_msec) {
179     struct sockaddr_in sin;
180     struct hostent *h_info;
181     fd_set fdset;
182     struct timeval tv;
183     int psock;
184 
185     psock = socket(PF_INET, SOCK_STREAM, 0);
186     if (psock == ERROR) return ERROR;
187 
188     memset((char *) &sin, 0, sizeof(sin));
189     sin.sin_family = AF_INET;
190     sin.sin_port = htons(port_num);
191 
192     if ((sin.sin_addr.s_addr = inet_addr(printer_addr)) == -1) {
193         /*
194          * The IP address is not in dotted decimal notation. Try to get the
195          * network peripheral IP address by host name.
196          */
197 
198         if ((h_info = gethostbyname(printer_addr)) != NULL) {
199             (void) memcpy(&(sin.sin_addr.s_addr), h_info->h_addr, h_info->h_length);
200         } else {
201             LOGE("ERROR: unknown host %s", printer_addr);
202             close(psock);
203             return ERROR;
204         }
205     }
206 
207     // temporarily set the socket to NONBLOCK'ing mode to catch timeout
208     fcntl(psock, F_SETFL, O_NONBLOCK);
209 
210     // open a TCP connection to the printer:port
211     int socketConnect = connect(psock, (const struct sockaddr *) &sin, sizeof(sin));
212     if (socketConnect == 0) {
213         FD_ZERO(&fdset);
214         FD_SET(psock, &fdset);
215 
216         tv.tv_sec = (timeout_msec / 1000);
217         tv.tv_usec = (timeout_msec % 1000) * 1000;
218 
219         /*  check if the socket is connected and available for write within
220          *  the specified timeout period
221          */
222         if (select(psock + 1, NULL, &fdset, NULL, &tv) == 1) {
223             int so_error, flags;
224             socklen_t len = sizeof so_error;
225 
226             getsockopt(psock, SOL_SOCKET, SO_ERROR, &so_error, &len);
227             if (so_error == 0) {
228                 // restore the socket back to normal blocking mode
229 
230                 flags = fcntl(psock, F_GETFL);
231                 fcntl(psock, F_SETFL, flags & ~O_NONBLOCK);
232 
233                 LOGI("connected to %s:%d", printer_addr, port_num);
234             } else {
235                 close(psock);
236                 psock = ERROR;
237                 LOGE("cannot connect on %s:%d, %s", printer_addr, port_num, strerror(errno));
238             }
239         } else {
240             LOGE("connecting to %s:%d .. timed out after %ld milliseconds", printer_addr,
241                     port_num, timeout_msec);
242             close(psock);
243             psock = ERROR;
244         }
245     }
246     return psock;
247 }
248 
249 static const ifc_print_job_t _print_job_ifc = {.init = _init, .validate_job = NULL,
250         .start_job = _start_job, .send_data = _send_data, .end_job = _end_job, .destroy = _destroy,
251         .enable_timeout = _enable_timeout, .check_status = _check_status,};
252 
printer_connect(int port_num)253 const ifc_print_job_t *printer_connect(int port_num) {
254     _print_job_t *print_job;
255     print_job = (_print_job_t *) malloc(sizeof(_print_job_t));
256 
257     if (print_job) {
258         print_job->port_num = port_num;
259         print_job->psock = -1;
260         print_job->job_id = WPRINT_BAD_JOB_HANDLE;
261         print_job->job_status = ERROR;
262         print_job->timeout_enabled = 0;
263         memcpy(&print_job->ifc, &_print_job_ifc, sizeof(ifc_print_job_t));
264 
265         return &print_job->ifc;
266     } else {
267         return NULL;
268     }
269 }
270