tools: Fix compilation in MeeGo
[framework/connectivity/connman.git] / tools / alg-test.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #define _GNU_SOURCE
27 #include <stdio.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <sys/stat.h>
32 #include <sys/sendfile.h>
33 #include <sys/socket.h>
34 #include <linux/if_alg.h>
35
36 static void build_hash(int sk, int fd, size_t size, const char *pathname)
37 {
38         unsigned char hash[20];
39         ssize_t written, length;
40         int i;
41
42         written = sendfile(sk, fd, NULL, size);
43         if (written < 0)
44                 perror("Failed to write data");
45
46         printf("send %zd bytes\n", written);
47
48         length = recv(sk, hash, sizeof(hash), 0);
49         if (length < 0)
50                 perror("Failed to read data");
51
52         printf("recv %zd bytes\n", length);
53
54         for (i = 0; i < length; i++)
55                 printf("%02x", hash[i]);
56         printf("  %s\n", pathname);
57 }
58
59 static int create_hash(int sk, const char *pathname)
60 {
61         struct stat st;
62         int fd;
63
64         fd = open(pathname, O_RDONLY | O_CLOEXEC);
65         if (fd < 0)
66                 return -1;
67
68         if (fstat(fd, &st) < 0) {
69                 close(fd);
70                 return -1;
71         }
72
73         build_hash(sk, fd, st.st_size, pathname);
74
75         close(fd);
76
77         return 0;
78 }
79
80 static int create_socket(void)
81 {
82         struct sockaddr_alg salg = {
83                 .salg_family = AF_ALG,
84                 .salg_type = "hash",
85                 .salg_name = "sha1",
86         };
87         int sk, nsk;
88
89         sk = socket(PF_ALG, SOCK_SEQPACKET | SOCK_CLOEXEC, 0);
90         if (sk < 0) {
91                 perror("Failed to create socket");
92                 return -1;
93         }
94
95         if (bind(sk, (struct sockaddr *) &salg, sizeof(salg)) < 0) {
96                 perror("Failed to bind socket");
97                 close(sk);
98                 return -1;
99         }
100
101         nsk = accept(sk, NULL, 0);
102         if (nsk < 0) {
103                 perror("Failed to accept socket");
104                 close(sk);
105                 return -1;
106         }
107
108         close(sk);
109
110         return nsk;
111 }
112
113 int main(int argc, char *argv[])
114 {
115         int sk;
116
117         if (argc < 2) {
118                 fprintf(stderr, "Missing argument\n");
119                 return 1;
120         }
121
122         sk = create_socket();
123         if (sk < 0)
124                 return 1;
125
126         create_hash(sk, argv[1]);
127
128         close(sk);
129
130         return 0;
131 }