5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 #include <sys/sendfile.h>
33 #include <sys/socket.h>
34 #include <linux/if_alg.h>
36 static void build_hash(int sk, int fd, size_t size, const char *pathname)
38 unsigned char hash[20];
39 ssize_t written, length;
42 written = sendfile(sk, fd, NULL, size);
44 perror("Failed to write data");
46 printf("send %zd bytes\n", written);
48 length = recv(sk, hash, sizeof(hash), 0);
50 perror("Failed to read data");
52 printf("recv %zd bytes\n", length);
54 for (i = 0; i < length; i++)
55 printf("%02x", hash[i]);
56 printf(" %s\n", pathname);
59 static int create_hash(int sk, const char *pathname)
64 fd = open(pathname, O_RDONLY | O_CLOEXEC);
68 if (fstat(fd, &st) < 0) {
73 build_hash(sk, fd, st.st_size, pathname);
80 static int create_socket(void)
82 struct sockaddr_alg salg = {
83 .salg_family = AF_ALG,
89 sk = socket(PF_ALG, SOCK_SEQPACKET | SOCK_CLOEXEC, 0);
91 perror("Failed to create socket");
95 if (bind(sk, (struct sockaddr *) &salg, sizeof(salg)) < 0) {
96 perror("Failed to bind socket");
101 nsk = accept(sk, NULL, 0);
103 perror("Failed to accept socket");
113 int main(int argc, char *argv[])
118 fprintf(stderr, "Missing argument\n");
122 sk = create_socket();
126 create_hash(sk, argv[1]);