• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* sane - Scanner Access Now Easy.
2    Copyright (C) 2006 Tower Technologies
3    Author: Alessandro Zummo <a.zummo@towertech.it>
4    This file is part of the SANE package.
5 
6    This program is free software; you can redistribute it and/or
7    modify it under the terms of the GNU General Public License as
8    published by the Free Software Foundation; either version 2 of the
9    License, or (at your option) any later version.
10 
11    This program is distributed in the hope that it will be useful, but
12    WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <https://www.gnu.org/licenses/>.
18 
19    As a special exception, the authors of SANE give permission for
20    additional uses of the libraries contained in this release of SANE.
21 
22    The exception is that, if you link a SANE library with other files
23    to produce an executable, this does not by itself cause the
24    resulting executable to be covered by the GNU General Public
25    License.  Your use of that executable is in no way restricted on
26    account of linking the SANE library code into it.
27 
28    This exception does not, however, invalidate any other reasons why
29    the executable file might be covered by the GNU General Public
30    License.
31 
32    If you submit changes to SANE to the maintainers to be included in
33    a subsequent release, you agree by submitting the changes that
34    those changes may be distributed with this exception intact.
35 
36    If you write modifications of your own for SANE, it is your choice
37    whether to permit this exception to apply to your modifications.
38    If you do not wish that, delete this exception notice.  */
39 
40 #include "../include/sane/config.h"
41 
42 #include <errno.h>
43 #include <unistd.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <limits.h>
47 
48 #ifndef SSIZE_MAX
49 #define SSIZE_MAX LONG_MAX
50 #endif
51 
52 #ifdef HAVE_WINSOCK2_H
53 #include <winsock2.h>
54 #endif
55 #ifdef HAVE_SYS_SOCKET_H
56 #include <sys/socket.h>
57 #endif
58 
59 #define BACKEND_NAME sanei_tcp
60 
61 #include "../include/sane/sane.h"
62 #include "../include/sane/sanei_debug.h"
63 #include "../include/sane/sanei_tcp.h"
64 
65 SANE_Status
sanei_tcp_open(const char * host,int port,int * fdp)66 sanei_tcp_open(const char *host, int port, int *fdp)
67 {
68 	int fd, err;
69 	struct sockaddr_in saddr;
70 	struct hostent *h;
71 #ifdef HAVE_WINSOCK2_H
72 	WSADATA wsaData;
73 #endif
74 
75 	DBG_INIT();
76 	DBG(1, "%s: host = %s, port = %d\n", __func__, host, port);
77 
78 #ifdef HAVE_WINSOCK2_H
79 	err = WSAStartup(MAKEWORD(2, 2), &wsaData);
80 	if (err != 0)
81 	    return SANE_STATUS_INVAL;
82 #endif
83 
84 	h = gethostbyname(host);
85 
86 	if (h == NULL || h->h_addr_list[0] == NULL
87 	    || h->h_addrtype != AF_INET)
88 		return SANE_STATUS_INVAL;
89 
90 	if ((fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
91 		return SANE_STATUS_INVAL;
92 
93 	memset(&saddr, 0x00, sizeof(struct sockaddr_in));
94 
95 	saddr.sin_family = AF_INET;
96 	saddr.sin_port = htons(port);
97 	memcpy(&saddr.sin_addr, h->h_addr_list[0], h->h_length);
98 
99 	if ((err =
100 	     connect(fd, (struct sockaddr *) &saddr,
101 		     sizeof(struct sockaddr_in))) != 0) {
102 		close(fd);
103 		return SANE_STATUS_INVAL;
104 	}
105 
106 	*fdp = fd;
107 
108 	return SANE_STATUS_GOOD;
109 }
110 
111 void
sanei_tcp_close(int fd)112 sanei_tcp_close(int fd)
113 {
114 	close(fd);
115 #ifdef HAVE_WINSOCK2_H
116 	WSACleanup();
117 #endif
118 }
119 
120 ssize_t
sanei_tcp_write(int fd,const u_char * buf,size_t count)121 sanei_tcp_write(int fd, const u_char * buf, size_t count)
122 {
123 	return send(fd, buf, count, 0);
124 }
125 
126 ssize_t
sanei_tcp_read(int fd,u_char * buf,size_t count)127 sanei_tcp_read(int fd, u_char * buf, size_t count)
128 {
129 	size_t bytes_recv = 0;
130 	ssize_t rc = 1;
131 
132 	if (count > SSIZE_MAX) {
133 		errno = EINVAL;
134 		return -1;
135 	}
136 
137 	while (bytes_recv < count && rc > 0)
138 	{
139 		rc = recv(fd, buf+bytes_recv, count-bytes_recv, 0);
140 		if (rc > 0)
141 		  bytes_recv += rc;
142 
143 	}
144 	return bytes_recv;
145 }
146