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
31 #include <sys/sendfile.h>
32 #include <sys/socket.h>
33 #include <linux/if_alg.h>
35 static void build_hash(int sk, int fd, size_t size, const char *pathname)
37 unsigned char hash[20];
38 ssize_t written, length;
41 written = sendfile(sk, fd, NULL, size);
43 perror("Failed to write data");
45 printf("send %zd bytes\n", written);
47 length = recv(sk, hash, sizeof(hash), 0);
49 perror("Failed to read data");
51 printf("recv %zd bytes\n", length);
53 for (i = 0; i < length; i++)
54 printf("%02x", hash[i]);
55 printf(" %s\n", pathname);
58 static int create_hash(int sk, const char *pathname)
63 fd = open(pathname, O_RDONLY);
67 if (fstat(fd, &st) < 0) {
72 build_hash(sk, fd, st.st_size, pathname);
79 static int create_socket(void)
81 struct sockaddr_alg salg = {
82 .salg_family = AF_ALG,
88 sk = socket(PF_ALG, SOCK_SEQPACKET, 0);
90 perror("Failed to create socket");
94 if (bind(sk, (struct sockaddr *) &salg, sizeof(salg)) < 0) {
95 perror("Failed to bind socket");
100 nsk = accept(sk, NULL, 0);
102 perror("Failed to accept socket");
112 int main(int argc, char *argv[])
117 fprintf(stderr, "Missing argument\n");
121 sk = create_socket();
125 create_hash(sk, argv[1]);