Refactor patty_kiss_tnc_new()
Refactor patty_kiss_tnc_new() into the following initialization methods: * init_sock() * init_device() * init_termios()
This commit is contained in:
parent
ab3679b788
commit
4f2f01ec0a
1 changed files with 115 additions and 84 deletions
125
src/tnc.c
125
src/tnc.c
|
@ -49,29 +49,7 @@ struct _patty_kiss_tnc {
|
|||
offset_o;
|
||||
};
|
||||
|
||||
patty_kiss_tnc *patty_kiss_tnc_new(patty_kiss_tnc_info *info) {
|
||||
patty_kiss_tnc *tnc;
|
||||
|
||||
if ((tnc = malloc(sizeof(*tnc))) == NULL) {
|
||||
goto error_malloc_tnc;
|
||||
}
|
||||
|
||||
if ((tnc->buf = malloc(PATTY_KISS_TNC_BUFSZ)) == NULL) {
|
||||
goto error_malloc_buf;
|
||||
}
|
||||
|
||||
if (info->flags & PATTY_KISS_TNC_DEVICE) {
|
||||
struct stat st;
|
||||
|
||||
if (strcmp(info->device, "/dev/ptmx") == 0) {
|
||||
int ptysub;
|
||||
|
||||
if (openpty(&tnc->fd, &ptysub, NULL, NULL, NULL) < 0) {
|
||||
goto error_open;
|
||||
}
|
||||
} else if (stat(info->device, &st) < 0) {
|
||||
goto error_stat;
|
||||
} else if ((st.st_mode & S_IFMT) == S_IFSOCK) {
|
||||
static int init_sock(patty_kiss_tnc *tnc, patty_kiss_tnc_info *info) {
|
||||
struct sockaddr_un addr;
|
||||
|
||||
if (strlen(info->device) > sizeof(addr.sun_path)) {
|
||||
|
@ -91,22 +69,18 @@ patty_kiss_tnc *patty_kiss_tnc_new(patty_kiss_tnc_info *info) {
|
|||
if (connect(tnc->fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
|
||||
goto error_connect;
|
||||
}
|
||||
} else {
|
||||
if ((tnc->fd = open(info->device, O_RDWR | O_NOCTTY)) < 0) {
|
||||
goto error_open;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
error_connect:
|
||||
(void)close(tnc->fd);
|
||||
|
||||
error_socket:
|
||||
error_overflow:
|
||||
return -1;
|
||||
}
|
||||
|
||||
tnc->opts |= TNC_CLOSE_ON_DESTROY;
|
||||
} else if (info->flags & PATTY_KISS_TNC_FD) {
|
||||
tnc->fd = info->fd;
|
||||
} else {
|
||||
errno = EINVAL;
|
||||
|
||||
goto error_no_fd;
|
||||
}
|
||||
|
||||
if (isatty(tnc->fd) && ptsname(tnc->fd) == NULL) {
|
||||
static int init_termios(patty_kiss_tnc *tnc, patty_kiss_tnc_info *info) {
|
||||
if (tcgetattr(tnc->fd, &tnc->attrs) < 0) {
|
||||
goto error_tcgetattr;
|
||||
}
|
||||
|
@ -143,8 +117,71 @@ patty_kiss_tnc *patty_kiss_tnc_new(patty_kiss_tnc_info *info) {
|
|||
if (tcsetattr(tnc->fd, TCSANOW, &tnc->attrs) < 0) {
|
||||
goto error_tcsetattr;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
error_tcsetattr:
|
||||
error_tcflush:
|
||||
error_tcgetattr:
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int init_device(patty_kiss_tnc *tnc, patty_kiss_tnc_info *info) {
|
||||
struct stat st;
|
||||
|
||||
if (strcmp(info->device, "/dev/ptmx") == 0) {
|
||||
int ptysub;
|
||||
|
||||
if (openpty(&tnc->fd, &ptysub, NULL, NULL, NULL) < 0) {
|
||||
goto error;
|
||||
}
|
||||
} else if (stat(info->device, &st) < 0) {
|
||||
goto error;
|
||||
} else if ((st.st_mode & S_IFMT) == S_IFSOCK) {
|
||||
if (init_sock(tnc, info) < 0) {
|
||||
goto error;
|
||||
}
|
||||
} else {
|
||||
errno = 0;
|
||||
if ((tnc->fd = open(info->device, O_RDWR | O_NOCTTY)) < 0) {
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
|
||||
tnc->opts |= TNC_CLOSE_ON_DESTROY;
|
||||
|
||||
return 0;
|
||||
|
||||
error:
|
||||
return -1;
|
||||
}
|
||||
|
||||
patty_kiss_tnc *patty_kiss_tnc_new(patty_kiss_tnc_info *info) {
|
||||
patty_kiss_tnc *tnc;
|
||||
|
||||
if ((tnc = malloc(sizeof(*tnc))) == NULL) {
|
||||
goto error_malloc_tnc;
|
||||
}
|
||||
|
||||
if ((tnc->buf = malloc(PATTY_KISS_TNC_BUFSZ)) == NULL) {
|
||||
goto error_malloc_buf;
|
||||
}
|
||||
|
||||
if (info->flags & PATTY_KISS_TNC_DEVICE) {
|
||||
if (init_device(tnc, info) < 0) {
|
||||
goto error_init_device;
|
||||
}
|
||||
} else if (info->flags & PATTY_KISS_TNC_FD) {
|
||||
tnc->fd = info->fd;
|
||||
} else {
|
||||
errno = EINVAL;
|
||||
|
||||
goto error_invalid;
|
||||
}
|
||||
|
||||
if (isatty(tnc->fd)) {
|
||||
if (init_termios(tnc, info) < 0) {
|
||||
goto error_init_termios;
|
||||
}
|
||||
}
|
||||
|
||||
memset(&tnc->stats, '\0', sizeof(tnc->stats));
|
||||
|
@ -160,19 +197,13 @@ patty_kiss_tnc *patty_kiss_tnc_new(patty_kiss_tnc_info *info) {
|
|||
|
||||
return tnc;
|
||||
|
||||
error_tcflush:
|
||||
error_tcsetattr:
|
||||
error_tcgetattr:
|
||||
error_connect:
|
||||
error_init_termios:
|
||||
if (info->flags & PATTY_KISS_TNC_DEVICE) {
|
||||
(void)close(tnc->fd);
|
||||
}
|
||||
|
||||
error_open:
|
||||
error_no_fd:
|
||||
error_socket:
|
||||
error_overflow:
|
||||
error_stat:
|
||||
error_init_device:
|
||||
error_invalid:
|
||||
free(tnc->buf);
|
||||
|
||||
error_malloc_buf:
|
||||
|
|
Loading…
Add table
Reference in a new issue