aboutsummaryrefslogtreecommitdiff
path: root/tests/wmediumd_ack_test_client.c
blob: 8eb172b9e78d102526218a61ac84fb3d2746c43d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <errno.h>
#include <getopt.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>

#include "wmediumd/api.h"

void print_help(int exit_code) {
  printf(
      "wmediumd_ack_test_client - test client for wmediumd crash that is "
      "related with ack\n\n");
  printf("Usage: wmediumd_ack_test_client -s PATH\n");
  printf("  Options:\n");
  printf("     - h : Print help\n");
  printf("     - s : Path for unix socket of wmediumd api server\n");

  exit(exit_code);
}

int write_fixed(int sock, void *data, int len) {
  int remain = len;
  int pos = 0;

  while (remain > 0) {
    int actual_written = write(sock, ((char *)data) + pos, remain);

    if (actual_written <= 0) {
      return actual_written;
    }

    remain -= actual_written;
    pos += actual_written;
  }

  return pos;
}

int read_fixed(int sock, void *data, int len) {
  int remain = len;
  int pos = 0;

  while (remain > 0) {
    int actual_read = read(sock, ((char *)data) + pos, remain);

    if (actual_read <= 0) {
      return actual_read;
    }

    remain -= actual_read;
    pos += actual_read;
  }

  return pos;
}

int wmediumd_send_packet(int sock, uint32_t type, void *data, uint32_t len) {
  struct wmediumd_message_header header;

  header.type = type;
  header.data_len = len;

  write_fixed(sock, &header, sizeof(uint32_t) * 2);

  if (len != 0) {
    write_fixed(sock, data, len);
  }

  return 0;
}

int wmediumd_read_packet(int sock) {
  struct wmediumd_message_header header;

  read_fixed(sock, &header, sizeof(uint32_t) * 2);

  if (header.data_len != 0) {
    char buf[4096];

    read_fixed(sock, buf, header.data_len);
  }

  return 0;
}

int main(int argc, char **argv) {
  int opt;
  char *wmediumd_api_server_path = NULL;

  while ((opt = getopt(argc, argv, "hs:")) != -1) {
    switch (opt) {
      case ':':
        fprintf(stderr,
                "error: Option `%c' "
                "needs a value\n\n",
                optopt);
        break;
      case 'h':
        print_help(0);
        break;
      case 's':
        if (wmediumd_api_server_path != NULL) {
          fprintf(stderr,
                  "error: You must provide just one option for `%c`\n\n",
                  optopt);
        }

        wmediumd_api_server_path = strdup(optarg);
        break;
      default:
        break;
    }
  }

  if (wmediumd_api_server_path == NULL) {
    fprintf(stderr, "error: must specify wmediumd api server path\n\n");
    print_help(-1);
  }

  int sock = socket(AF_UNIX, SOCK_STREAM, 0);

  struct sockaddr_un addr;

  addr.sun_family = AF_UNIX;

  if (strlen(wmediumd_api_server_path) >= sizeof(addr.sun_path)) {
    fprintf(stderr, "error: unix socket path is too long(maximum %d)\n",
            sizeof(addr.sun_path) - 1);
    print_help(-1);
  }

  strncpy(addr.sun_path, wmediumd_api_server_path,
          strlen(wmediumd_api_server_path));

  if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
    fprintf(stderr, "Cannot connect to %s\n", wmediumd_api_server_path);
    return -1;
  }

  struct wmediumd_message_control control_message;

  control_message.flags = WMEDIUMD_CTL_RX_ALL_FRAMES;

  wmediumd_send_packet(sock, WMEDIUMD_MSG_REGISTER, NULL, 0);
  wmediumd_read_packet(sock); /* Ack */
  wmediumd_send_packet(sock, WMEDIUMD_MSG_SET_CONTROL, &control_message,
                       sizeof(control_message));
  wmediumd_read_packet(sock); /* Ack */

  wmediumd_read_packet(sock);

  /* Send packet while receiving packet from wmediumd */
  wmediumd_send_packet(sock, WMEDIUMD_MSG_SET_CONTROL, &control_message,
                       sizeof(control_message));
  wmediumd_read_packet(sock);

  wmediumd_send_packet(sock, WMEDIUMD_MSG_ACK, NULL, 0);

  close(sock);

  free(wmediumd_api_server_path);

  return 0;
}