Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Macos NKE。Can';t处理文件上载和其他高负载连接_Macos_Networking_Kernel_Driver_Kernel Extension - Fatal编程技术网

Macos NKE。Can';t处理文件上载和其他高负载连接

Macos NKE。Can';t处理文件上载和其他高负载连接,macos,networking,kernel,driver,kernel-extension,Macos,Networking,Kernel,Driver,Kernel Extension,我在使用过滤网络流量的内核扩展时遇到了一个问题。 我的代码是根据苹果的tcplognke示例编写的 一切正常,但当我试图上传一个大于500KB的文件时,连接就会中断 这里是简化的kext代码: errno_t tl_data_fn(void *cookie, socket_t so, const struct sockaddr *addr, mbuf_t *data, mbuf_t *control, sflt_data_flag_t flags, FilterSocketDataDirecti

我在使用过滤网络流量的内核扩展时遇到了一个问题。 我的代码是根据苹果的tcplognke示例编写的

一切正常,但当我试图上传一个大于500KB的文件时,连接就会中断

这里是简化的kext代码

errno_t tl_data_fn(void *cookie, socket_t so, const struct sockaddr *addr, mbuf_t *data, mbuf_t *control, sflt_data_flag_t flags, FilterSocketDataDirection direction) {
    errno_t result = 0;

    if (check_tag(data, gidtag, FILTER_TAG_TYPE, direction == FilterSocketDataDirectionIn ? IN_DONE : OUT_DONE)) {
        return result;
    }

    if (!cookie) return result;

    filter_cookie *f_cookie = get_filter_cookie(cookie);

    uint32_t data_size = (uint32_t)mbuf_pkthdr_len(*data);
    uint32_t offset = 0;

    printf("tl_data_ft: %d", data_size);

    while (offset < data_size) {
        FilterNotification notification;

        if (direction == FilterSocketDataDirectionIn) {
            notification.event = FilterEventDataIn;
        } else {
            notification.event = FilterEventDataOut;
        }
        notification.socketId = (uint64_t)so;
        notification.inputoutput.dataSize = min(data_size - offset, sizeof(notification.inputoutput.data));

        mbuf_copydata(*data, offset, notification.inputoutput.dataSize, notification.inputoutput.data);
        offset += notification.inputoutput.dataSize;

        send_notification(f_cookie, &notification);
    }

    result = EJUSTRETURN;

    if (result == EJUSTRETURN) {
        mbuf_freem(*data);

        if (control != NULL && *control != NULL)
            mbuf_freem(*control);
    }

    return result;
}

errno_t tl_data_in_fn(void *cookie, socket_t so, const struct sockaddr *from, mbuf_t *data, mbuf_t *control, sflt_data_flag_t flags) {
    return tl_data_fn(cookie, so, from, data, control, flags, FilterSocketDataDirectionIn);
}

errno_t tl_data_out_fn(void *cookie, socket_t so, const struct sockaddr *to, mbuf_t *data, mbuf_t *control, sflt_data_flag_t flags) {
    return tl_data_fn(cookie, so, to, data, control, flags, FilterSocketDataDirectionOut);
}
int s = socket(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL);  

//connect to driver  

FilterNotification notification;  
while (recv(s, &notification, sizeof(FilterNotification), 0) == sizeof(FilterNotification)) {  
    FilterClientResponse response;  
    response.socketId = notification.socketId;  
    response.direction = (notification.event == FilterEventDataIn) ? FilterSocketDataDirectionIn : FilterSocketDataDirectionOut;  
    response.dataSize = notification.inputoutput.dataSize;  
    memcpy(response.data, notification.inputoutput.data, notification.inputoutput.dataSize);  
    send(s, &response, sizeof(response), 0);  
}  

当我在apple develper论坛上提问时,开发者说:“我看不到有人试图在这里处理发送端流控制。没有这一点,文件上传很容易耗尽所有可用的MBUF,事情会变得很糟糕”,但根本没有例子。有人能帮我吗?谢谢。

问题出在套接字缓冲区中。当我非常快地注入大量数据时,缓冲区会变满,inject\u data\u in/inject\u data\u out函数返回错误。
解决方法是在内核空间中存储挂起的数据包(例如,您可以使用TAILQ),然后,当套接字可用于写入时(要获取此事件,您可以在OS X上使用kqueue),继续注入

有人可以帮助解决此问题吗?