• 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 <fcntl.h>
47 
48 #ifdef HAVE_WINSOCK2_H
49 #include <winsock2.h>
50 #endif
51 #ifdef HAVE_SYS_SOCKET_H
52 #include <sys/socket.h>
53 #include <netinet/in.h>
54 #include <netdb.h>
55 #include <arpa/inet.h>
56 #endif
57 
58 #define BACKEND_NAME sanei_udp
59 
60 #include "../include/sane/sane.h"
61 #include "../include/sane/sanei_debug.h"
62 #include "../include/sane/sanei_udp.h"
63 
64 static SANE_Status
sanei_udp_socket(int * fdp,int broadcast)65 sanei_udp_socket(int *fdp, int broadcast)
66 {
67 	int fd;
68 
69 	if ((fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
70 		return SANE_STATUS_INVAL;
71 
72 	if (broadcast) {
73 		int opt = 1;
74 		if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST,
75 			       (char *) &opt, sizeof(opt)) < 0) {
76 			close(fd);
77 			return SANE_STATUS_INVAL;
78 		}
79 	}
80 
81 	*fdp = fd;
82 
83 	return SANE_STATUS_GOOD;
84 }
85 
86 static SANE_Status
sanei_udp_connect(int fd,const char * host,int port)87 sanei_udp_connect(int fd, const char *host, int port)
88 {
89 	int err;
90 	struct sockaddr_in saddr;
91 	struct hostent *h;
92 
93 	h = gethostbyname(host);
94 
95 	if (h == NULL || h->h_addr_list[0] == NULL
96 	    || h->h_addrtype != AF_INET)
97 		return SANE_STATUS_INVAL;
98 
99 	memset(&saddr, 0x00, sizeof(struct sockaddr_in));
100 
101 	saddr.sin_family = AF_INET;
102 	saddr.sin_port = htons(port);
103 	memcpy(&saddr.sin_addr, h->h_addr_list[0], h->h_length);
104 
105 	if ((err = connect(fd, (struct sockaddr *) &saddr,
106 		     sizeof(struct sockaddr_in))) != 0) {
107 		return SANE_STATUS_INVAL;
108 	}
109 
110 	return SANE_STATUS_GOOD;
111 }
112 
113 SANE_Status
sanei_udp_open(const char * host,int port,int * fdp)114 sanei_udp_open(const char *host, int port, int *fdp)
115 {
116 	int status;
117 #ifdef HAVE_WINSOCK2_H
118 	WSADATA wsaData;
119 #endif
120 
121 	DBG_INIT();
122 	DBG(1, "%s\n", __func__);
123 
124 #ifdef HAVE_WINSOCK2_H
125 	status = WSAStartup(MAKEWORD(2, 2), &wsaData);
126 	if (status != 0)
127 	    return SANE_STATUS_INVAL;
128 #endif
129 
130 	status = sanei_udp_socket(fdp, 0);
131 	if (status != SANE_STATUS_GOOD)
132 		return status;
133 
134 	status = sanei_udp_connect(*fdp, host, port);
135 	if (status != SANE_STATUS_GOOD) {
136 		close(*fdp);
137 		return status;
138 	}
139 
140 	return status;
141 }
142 
143 SANE_Status
sanei_udp_open_broadcast(int * fdp)144 sanei_udp_open_broadcast(int *fdp)
145 {
146 	int status;
147 
148 	DBG_INIT();
149 	DBG(1, "%s\n", __func__);
150 
151 	status = sanei_udp_socket(fdp, 1);
152 	if (status != SANE_STATUS_GOOD)
153 		return status;
154 
155 	return status;
156 }
157 
158 void
sanei_udp_close(int fd)159 sanei_udp_close(int fd)
160 {
161 	close(fd);
162 #ifdef HAVE_WINSOCK2_H
163 	WSACleanup();
164 #endif
165 }
166 
167 ssize_t
sanei_udp_write(int fd,const u_char * buf,int count)168 sanei_udp_write(int fd, const u_char * buf, int count)
169 {
170 	return send(fd, buf, count, 0);
171 }
172 
173 ssize_t
sanei_udp_write_broadcast(int fd,int port,const u_char * buf,int count)174 sanei_udp_write_broadcast(int fd, int port, const u_char * buf, int count)
175 {
176 	struct sockaddr_in saddr;
177 
178 	memset(&saddr, 0x00, sizeof(struct sockaddr_in));
179 
180 	saddr.sin_family = AF_INET;
181 	saddr.sin_port = htons(port);
182 	saddr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
183 
184 	return sendto(fd, buf, count, 0,
185 		(struct sockaddr *)&saddr, sizeof(saddr));
186 }
187 
188 void
sanei_udp_set_nonblock(int fd,SANE_Bool nonblock)189 sanei_udp_set_nonblock(int fd, SANE_Bool nonblock)
190 {
191 #ifdef HAVE_WINSOCK2_H
192 	u_long mode=nonblock;
193 
194 	ioctlsocket(fd, FIONBIO, &mode);
195 #else
196 	long flags;
197 
198 	flags = fcntl(fd, F_GETFL, 0L);
199 	if (nonblock)
200 		flags |= O_NONBLOCK;
201 	else
202 		flags &= ~O_NONBLOCK;
203 	fcntl(fd, F_SETFL, flags);
204 #endif
205 }
206 
207 ssize_t
sanei_udp_read(int fd,u_char * buf,int count)208 sanei_udp_read(int fd, u_char * buf, int count)
209 {
210 	return recv(fd, buf, count, 0);
211 }
212 
213 ssize_t
sanei_udp_recvfrom(int fd,u_char * buf,int count,char ** fromp)214 sanei_udp_recvfrom(int fd, u_char * buf, int count, char **fromp)
215 {
216 	ssize_t l;
217 	socklen_t fl;
218 	struct sockaddr_in from;
219 
220 	fl = sizeof(from);
221 
222 	l = recvfrom(fd, buf, count, 0, (struct sockaddr *) &from, &fl);
223 
224 	if (l > 0 && fromp) {
225 		*fromp = inet_ntoa(from.sin_addr);
226 	}
227 
228 	return l;
229 }
230