Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup #48

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/atcmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ m2k_atcmdD(const char *s, AtdAType at, AtdPType pt)
if (*s == '"')
s++;
/* "%[^:\"]:%[^\"]" */
sscanf(s, "%" LIT(ADDR_MAX) "[^ \"] %" LIT(PORT_MAX) "[^\"]",
sscanf(s, "%" LIT(HOST_NAME_MAX) "[^ \"] %" LIT(PORT_MAX) "[^\"]",
atcmd.d.addr.str, atcmd.d.port.str);
atcmd.d.addr.type = at;
atcmd.d.port.type = pt;
Expand Down
13 changes: 9 additions & 4 deletions src/modemu2k.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
*
* Copyright 2018 Andy Alt <[email protected]>
* Copyright 2022 Andy Alt <[email protected]>
*
* modemu2k is a fork of modemu
* Originally developed by Toru Egashira
Expand Down Expand Up @@ -29,11 +29,13 @@
* @brief core modemu2k API
*/

#include <limits.h>
#include <stdio.h> /*stderr,(sscanf,sprintf) */
#include <string.h> /*(strncpy) */
#include <stdbool.h>
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>

#ifdef TERMNET
#include <termnet.h>
Expand All @@ -47,6 +49,10 @@

#include "cmdarg.h"

#ifndef HOST_NAME_MAX
#define HOST_NAME_MAX 255
#endif

/* TODO: the API shouldn't be localized and really shouldn't contain any strings;
* but rather the functions should return values to the application can print
* any messages to stdout or stderr
Expand Down Expand Up @@ -76,7 +82,7 @@ typedef unsigned char uchar;
#define HAVE_GRANTPT
#endif

#define DEFAULT_PORT 23
extern const int DEFAULT_PORT;

// sock

Expand Down Expand Up @@ -123,7 +129,6 @@ typedef enum
ATDP_STR
} AtdPType;

#define ADDR_MAX 63
#define PORT_MAX 63
#define PT_MAX 40
#define SREG_MAX 12
Expand All @@ -134,7 +139,7 @@ typedef struct
{
struct
{
char str[ADDR_MAX + 1];
char str[HOST_NAME_MAX + 1];
AtdAType type;
} addr;
struct
Expand Down
25 changes: 12 additions & 13 deletions src/sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include <stdlib.h> /*(getenv) */
#include "modemu2k.h"

const int DEFAULT_PORT = 23;

void
sockInit(struct st_sock *sock)
{
Expand Down Expand Up @@ -65,36 +67,33 @@ sockShutdown(st_sock * sock)
int
m2k_sockDial(st_sock * sock)
{
struct addrinfo hints;
struct addrinfo hints, *result;
memset(&hints, 0, sizeof(struct addrinfo));

sockInit(sock);
struct addrinfo *result = NULL;

int s;

hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = 0;
hints.ai_protocol = 0; /* Any protocol */

char out_port[PORT_MAX + 1];
if ((size_t)
snprintf(out_port, sizeof out_port, "%d",
atcmd.d.port.type ==
ATDP_NUL ? DEFAULT_PORT : atoi(atcmd.d.port.str)) >=
sizeof out_port)
return 1;

if (atcmd.d.port.type == ATDP_NUL)
snprintf(out_port, sizeof out_port, "%d", DEFAULT_PORT);
else
{
strcpy(out_port, atcmd.d.port.str);
if (atcmd.d.port.type != ATDP_NUL)
telOpt.sentReqs = 1; /* skip sending option requests */
}

s = getaddrinfo(atcmd.d.addr.str, out_port, &hints, &result);
int s = getaddrinfo(atcmd.d.addr.str, out_port, &hints, &result);
if (s != 0)
{
fprintf(stderr, _("Host address lookup failed: %s\n"), gai_strerror(s));
return 1;
}

sockInit(sock);
for (sock->rp = result; sock->rp != NULL; sock->rp = sock->rp->ai_next)
{
sock->fd =
Expand Down