patty/src/testclient.c

87 lines
1.8 KiB
C
Raw Normal View History

2020-06-25 00:07:20 -04:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
2020-06-25 01:54:39 -04:00
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <fcntl.h>
#include <errno.h>
2020-06-25 00:07:20 -04:00
#include <patty/ax25.h>
static void usage(int argc, char **argv, const char *message, ...) {
if (message != NULL) {
va_list args;
va_start(args, message);
vfprintf(stderr, message, args);
fprintf(stderr, "\n");
va_end(args);
}
2020-06-25 01:54:39 -04:00
fprintf(stderr, "usage: %s /var/run/patty/patty.sock\n", argv[0]);
2020-06-25 00:07:20 -04:00
exit(1);
}
int main(int argc, char **argv) {
2020-06-25 01:54:39 -04:00
struct sockaddr_un addr;
2020-06-25 20:37:12 -04:00
int fd,
sock;
2020-06-25 00:07:20 -04:00
patty_ax25_addr peer;
char path[256];
2020-06-25 00:07:20 -04:00
if (argc != 2) {
2020-06-25 01:54:39 -04:00
usage(argc, argv, "No patty socket provided");
2020-06-25 00:07:20 -04:00
}
patty_ax25_pton("GB9BLM", 0, &peer);
2020-06-25 01:54:39 -04:00
if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
fprintf(stderr, "%s: %s: %s: %s\n",
argv[0], "socket()", argv[1], strerror(errno));
goto error_socket;
}
memset(&addr, '\0', sizeof(addr));
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, argv[1], sizeof(addr.sun_path));
if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
goto error_connect;
2020-06-25 00:07:20 -04:00
}
2020-06-25 20:37:12 -04:00
if ((sock = patty_ax25_call_socket(fd, PATTY_AX25_SOCK_PTY)) < 0) {
fprintf(stderr, "%s: %s: %s\n",
argv[0], "patty_ax25_call_socket()", strerror(errno));
goto error_call_socket;
}
if (patty_ax25_call_connect(fd, sock, &peer, path) < 0) {
2020-06-25 20:37:12 -04:00
fprintf(stderr, "%s: %s: %s\n",
argv[0], "patty_ax25_call_connect()", strerror(errno));
2020-06-25 20:37:12 -04:00
goto error_call_close;
}
fprintf(stderr, "connected sock %d\n", sock);
2020-06-25 20:37:12 -04:00
2020-06-25 01:54:39 -04:00
close(fd);
2020-06-25 00:07:20 -04:00
return 0;
2020-06-25 20:37:12 -04:00
error_call_close:
error_call_socket:
close(fd);
2020-06-25 01:54:39 -04:00
error_connect:
error_socket:
2020-06-25 00:07:20 -04:00
return 1;
}