如何使用内核libcrc32c(或相同功能)的用户空间程序? [英] How to use kernel libcrc32c(or same functions) in userspace programmes?

查看:543
本文介绍了如何使用内核libcrc32c(或相同功能)的用户空间程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做一些CRC校验在我自己的用户空间程序。而且我发现,内核加密的lib已经在系统中,并配备了SSE4.2支持。

I want to do some CRC check in my own userspace programme. And I find that the kernel crypto lib is already in the system, and come with SSE4.2 support.

我试过直接的#include< Linux的/ crc32c.h> 和运行gcc与 -I在/ usr / src / linux中/包括/ 。然而,它亘古不变的工作。

I tried to directly #include <linux/crc32c.h> and run gcc with -I/usr/src/linux/include/. However, it doesnot work.

任何方式使用某种libcrc32c的?

Any way to use some kind of libcrc32c ?

推荐答案

您可以通过插座用户空间使用内核加密 CRC32C (以及其他杂凑/加密功能)家庭AF_ALG在Linux上:

You can use kernel crypto CRC32c (and other hash/cipher functions) from user-space via socket family AF_ALG on Linux:

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/socket.h>
#include <linux/if_alg.h>
#include <sys/param.h>
#include <string.h>
#include <strings.h>

int
main (int argc, char **argv) {

    int sds[2] = { -1, -1 };

    struct sockaddr_alg sa = {
        .salg_family = AF_ALG,
        .salg_type   = "hash",
        .salg_name   = "crc32c"
    };

    if ((sds[0] = socket(AF_ALG, SOCK_SEQPACKET, 0)) == -1 )
        return -1;

    if( bind(sds[0], (struct sockaddr *) &sa, sizeof(sa)) != 0 )
        return -1;

    if( (sds[1] = accept(sds[0], NULL, 0)) == -1 )
        return -1;

    char *s = "hello";
    size_t n = strlen(s);
    if (send(sds[1], s, n, MSG_MORE) != n)
        return -1;

    int crc32c = 0x00000000;
    if(read(sds[1], &crc32c, 4) != 4)
        return -1;

    printf("%08X\n", crc32c);
    return 0;
}

如果你哈希文件或插座的数据,可以使用零拷贝的方法来避免内核加快步伐 - >用户空间缓冲区副本发送文件和/或拼接

If you're hashing files or socket data you can speed it up using zero-copy approach to avoid kernel -> user-space buffer copy with sendfile and/or splice.

快乐编码。

这篇关于如何使用内核libcrc32c(或相同功能)的用户空间程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆