1/*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <netinet/ip.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <time.h>
20#include <string.h>
21#include <sys/socket.h>
22#include "test.h"
23
24/**
25 * @tc.name      : sendmmsg_0100
26 * @tc.desc      : Test sendmmsg to send messages through socket
27 * @tc.level     : Level 0
28 */
29void sendmmsg_0100(void)
30{
31    int sockfd;
32    struct sockaddr_in addr;
33    struct mmsghdr msg[2];
34    struct iovec msg1[2], msg2;
35    int retval;
36
37    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
38    if (sockfd == -1) {
39        t_error("sendmmsg_0100 socket error");
40        exit(EXIT_FAILURE);
41    }
42
43    addr.sin_family = AF_INET;
44    addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
45    addr.sin_port = htons(1234);
46    if (connect(sockfd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
47        t_error("sendmmsg_0100 connect error");
48        exit(EXIT_FAILURE);
49    }
50
51    memset(msg1, 0, sizeof(msg1));
52    msg1[0].iov_base = "one";
53    msg1[0].iov_len = 3;
54    msg1[1].iov_base = "two";
55    msg1[1].iov_len = 3;
56
57    memset(&msg2, 0, sizeof(msg2));
58    msg2.iov_base = "three";
59    msg2.iov_len = 5;
60
61    memset(msg, 0, sizeof(msg));
62    msg[0].msg_hdr.msg_iov = msg1;
63    msg[0].msg_hdr.msg_iovlen = 2;
64
65    msg[1].msg_hdr.msg_iov = &msg2;
66    msg[1].msg_hdr.msg_iovlen = 1;
67
68    retval = sendmmsg(sockfd, msg, 2, 0);
69    if (retval == -1)
70        t_error("sendmmsg_0100 sendmmsg error");
71}
72
73int main(int argc, char *argv[])
74{
75    sendmmsg_0100();
76    return t_status;
77}