129 lines
2.8 KiB
C
129 lines
2.8 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdarg.h>
|
|
#include <unistd.h>
|
|
#include <sys/types.h>
|
|
#include <sys/stat.h>
|
|
#include <sys/socket.h>
|
|
#include <sys/wait.h>
|
|
#include <sys/un.h>
|
|
#include <fcntl.h>
|
|
#include <errno.h>
|
|
|
|
#include <patty/ax25.h>
|
|
|
|
extern char **environ;
|
|
|
|
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);
|
|
}
|
|
|
|
fprintf(stderr, "usage: %s /var/run/patty/patty.sock\n", argv[0]);
|
|
|
|
exit(1);
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
struct sockaddr_un unix_addr;
|
|
|
|
int fd,
|
|
local,
|
|
remote,
|
|
pty;
|
|
|
|
patty_ax25_addr addr,
|
|
peer;
|
|
|
|
char path[PATTY_AX25_SOCK_PATH_SIZE];
|
|
|
|
if (argc != 2) {
|
|
usage(argc, argv, "No patty socket provided");
|
|
}
|
|
|
|
patty_ax25_pton("KZ3ROX", 0, &addr);
|
|
|
|
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(&unix_addr, '\0', sizeof(unix_addr));
|
|
unix_addr.sun_family = AF_UNIX;
|
|
strncpy(unix_addr.sun_path, argv[1], sizeof(unix_addr.sun_path));
|
|
|
|
if (connect(fd, (struct sockaddr *)&unix_addr, sizeof(unix_addr)) < 0) {
|
|
goto error_connect;
|
|
}
|
|
|
|
if ((local = patty_ax25_call_socket(fd, PATTY_AX25_PROTO_NONE, PATTY_AX25_SOCK_STREAM, NULL, 0)) < 0) {
|
|
fprintf(stderr, "%s: %s: %s\n",
|
|
argv[0], "patty_ax25_call_socket()", strerror(errno));
|
|
|
|
goto error_call_socket;
|
|
}
|
|
|
|
if (patty_ax25_call_bind(fd, local, &addr) < 0) {
|
|
fprintf(stderr, "%s: %s: %s\n",
|
|
argv[0], "patty_ax25_call_connect()", strerror(errno));
|
|
|
|
goto error_call_bind;
|
|
}
|
|
|
|
if (patty_ax25_call_listen(fd, local) < 0) {
|
|
fprintf(stderr, "%s: %s: %s\n",
|
|
argv[0], "patty_ax25_call_listen()", strerror(errno));
|
|
|
|
goto error_call_listen;
|
|
}
|
|
|
|
if ((remote = patty_ax25_call_accept(fd, local, &peer, path, sizeof(path))) < 0) {
|
|
fprintf(stderr, "%s: %s: %s\n",
|
|
argv[0], "patty_ax25_call_accept()", strerror(errno));
|
|
|
|
goto error_call_accept;
|
|
}
|
|
|
|
if ((pty = open(path, O_RDWR)) < 0) {
|
|
goto error_open_pty;
|
|
}
|
|
|
|
if (write(pty, "hello world\n", 12) < 0) {
|
|
goto error_write;
|
|
}
|
|
|
|
close(pty);
|
|
|
|
patty_ax25_call_close(fd, remote);
|
|
patty_ax25_call_close(fd, local);
|
|
|
|
close(fd);
|
|
|
|
return 0;
|
|
|
|
error_write:
|
|
close(pty);
|
|
|
|
error_open_pty:
|
|
patty_ax25_call_close(fd, remote);
|
|
|
|
error_call_accept:
|
|
error_call_listen:
|
|
error_call_bind:
|
|
patty_ax25_call_close(fd, local);
|
|
|
|
error_call_socket:
|
|
error_connect:
|
|
close(fd);
|
|
|
|
error_socket:
|
|
return 1;
|
|
}
|