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