⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dataxfer.c

📁 This project provides a proxy that allows telnet/tcp connections to be made to serial ports on a mac
💻 C
📖 第 1 页 / 共 4 页
字号:
	port_info_t *curr, *prev;	prev = NULL;	curr = ports;	while ((curr != NULL) && (curr != port)) {	    prev = curr;	    curr = curr->next;	}	if (curr != NULL) {	    if (prev == NULL) {		ports = curr->next;	    } else {		prev->next = curr->next;	    }	    free_port(curr);	}	return; /* We have to return here because we no longer have a port. */    }    if (port->new_config != NULL) {	port_info_t *curr, *prev;	prev = NULL;	curr = ports;	while ((curr != NULL) && (curr != port)) {	    prev = curr;	    curr = curr->next;	}	if (curr != NULL) {	    port = curr->new_config;	    port->acceptfd = curr->acceptfd;	    sel_set_fd_handlers(ser2net_sel,				port->acceptfd,				port,				handle_accept_port_read,				NULL,				NULL);	    curr->acceptfd = -1;	    port->next = curr->next;	    if (prev == NULL) {		ports = port;	    } else {		prev->next = port;	    }	    curr->enabled = PORT_DISABLED;	    curr->new_config = NULL;	    free_port(curr);	}    }}voidgot_timeout(selector_t  *sel,	    sel_timer_t *timer,	    void        *data){    port_info_t *port = (port_info_t *) data;    struct timeval then;    unsigned char modemstate;    int val;    if (port->timeout) {	port->timeout_left--;	if (port->timeout_left < 0) {	    shutdown_port(port, "timeout");	    return;	}    }    if (port->is_2217 && (ioctl(port->devfd, TIOCMGET, &val) != -1)) {	modemstate = 0;	if (val & TIOCM_CD)	    modemstate |= 0x80;	if (val & TIOCM_RI)	    modemstate |= 0x40;	if (val & TIOCM_DSR)	    modemstate |= 0x20;	if (val & TIOCM_CTS)	    modemstate |= 0x10;	modemstate &= port->modemstate_mask;	if (modemstate != port->last_modemstate) {	    unsigned char data[3];	    data[0] = TN_OPT_COM_PORT;	    data[1] = 7; /* Notify modemstate */	    data[2] = modemstate;	    port->last_modemstate = modemstate;	    telnet_send_option(&port->tn_data, data, 3);	}    }        gettimeofday(&then, NULL);    then.tv_sec += 1;    sel_start_timer(port->timer, &then);}/* Create a port based on a set of parameters passed in. */char *portconfig(char *portnum,	   char *state,	   char *timeout,	   char *devname,	   char *devcfg,	   int  config_num){    port_info_t *new_port, *curr, *prev;    char        *rv = NULL;    new_port = malloc(sizeof(port_info_t));    if (new_port == NULL) {	return "Could not allocate a port data structure";    }    if (sel_alloc_timer(ser2net_sel,			got_timeout, new_port,			&new_port->timer))    {	free(new_port);	return "Could not allocate timer data";    }    /* Error from here on out must goto errout. */    init_port_data(new_port);    new_port->portname = malloc(strlen(portnum)+1);    if (new_port->portname == NULL) {	rv = "unable to allocate port name";	goto errout;    }    strcpy(new_port->portname, portnum);    if (scan_tcp_port(portnum, &(new_port->tcpport)) == -1) {	rv = "port number was invalid";	goto errout;    }    if (strcmp(state, "raw") == 0) {	new_port->enabled = PORT_RAW;    } else if (strcmp(state, "rawlp") == 0) {	new_port->enabled = PORT_RAWLP;    } else if (strcmp(state, "telnet") == 0) {	new_port->enabled = PORT_TELNET;    } else if (strcmp(state, "off") == 0) {	new_port->enabled = PORT_DISABLED;    } else {	rv = "state was invalid";	goto errout;    }    new_port->timeout = scan_int(timeout);    if (new_port->timeout == -1) {	rv = "timeout was invalid";	goto errout;    }    devinit(&(new_port->termctl));    if (devconfig(devcfg, &(new_port->termctl), &new_port->allow_2217,		  &new_port->banner)	== -1)    {	rv = "device configuration invalid";	goto errout;    }    new_port->devname = malloc(strlen(devname) + 1);    if (new_port->devname == NULL) {	rv = "could not allocate device name";	goto errout;    }    strcpy(new_port->devname, devname);    new_port->config_num = config_num;    /* See if the port already exists, and reconfigure it if so. */    curr = ports;    prev = NULL;    while (curr != NULL) {	if (strcmp(curr->portname, new_port->portname) == 0) {	    /* We are reconfiguring this port. */	    if (curr->dev_to_tcp_state == PORT_UNCONNECTED) {		/* Port is disconnected, just remove it. */		int new_state = new_port->enabled;		new_port->enabled = curr->enabled;		new_port->acceptfd = curr->acceptfd;		curr->enabled = PORT_DISABLED;		curr->acceptfd = -1;		sel_set_fd_handlers(ser2net_sel,				    new_port->acceptfd,				    new_port,				    handle_accept_port_read,				    NULL,				    NULL);		/* Just replace with the new data. */		if (prev == NULL) {		    ports = new_port;		} else {		    prev->next = new_port;		}		new_port->next = curr->next;		free_port(curr);		change_port_state(new_port, new_state);	    } else {		/* Mark it to be replaced later. */		if (curr->new_config != NULL) {		    curr->enabled = PORT_DISABLED;		    free(curr->new_config);		}		curr->config_num = config_num;		curr->new_config = new_port;	    }	    return rv;	} else {	    prev = curr;	    curr = curr->next;	}    }    /* If we get here, the port is brand new, so don't do anything that       would affect a port replacement here. */    if (new_port->enabled != PORT_DISABLED) {	rv = startup_port(new_port);	if (rv != NULL) {	    goto errout;	}    }    /* Tack it on to the end of the list of ports. */    new_port->next = NULL;    if (ports == NULL) {	ports = new_port;    } else {	curr = ports;	while (curr->next != NULL) {	    curr = curr->next;	}	curr->next = new_port;    }    return rv;errout:    free_port(new_port);    return rv;}voidclear_old_port_config(int curr_config){    port_info_t *curr, *prev;    curr = ports;    prev = NULL;    while (curr != NULL) {	if (curr->config_num != curr_config) {	    /* The port was removed, remove it. */	    if (curr->dev_to_tcp_state == PORT_UNCONNECTED) {		if (prev == NULL) {		    ports = curr->next;		    free_port(curr);		    curr = ports;		} else {		    prev->next = curr->next;		    free_port(curr);		    curr = prev->next;		}	    } else {		curr->config_num = -1;		prev = curr;		curr = curr->next;	    }	} else {	    prev = curr;	    curr = curr->next;	}    }}/* Print information about a port to the control port given in cntlr. */static voidshowshortport(struct controller_info *cntlr, port_info_t *port){    char buffer[128];    int  count;    int  need_space = 0;    snprintf(buffer, 23, "%-22s", port->portname);    controller_output(cntlr, buffer, strlen(buffer));    sprintf(buffer, " %-6s ", enabled_str[port->enabled]);    controller_output(cntlr, buffer, strlen(buffer));    sprintf(buffer, "%7d ", port->timeout);    controller_output(cntlr, buffer, strlen(buffer));    inet_ntop(AF_INET, &(port->remote.sin_addr), buffer, sizeof(buffer));    count = strlen(buffer);    controller_output(cntlr, buffer, count);    sprintf(buffer, ",%d ", ntohs(port->remote.sin_port));    count += strlen(buffer);    controller_output(cntlr, buffer, strlen(buffer));    while (count < 23) {	controller_output(cntlr, " ", 1);	count++;    }    snprintf(buffer, 23, "%-22s", port->devname);    controller_output(cntlr, buffer, strlen(buffer));    sprintf(buffer, " %-14s ", state_str[port->tcp_to_dev_state]);    controller_output(cntlr, buffer, strlen(buffer));    sprintf(buffer, "%-14s ", state_str[port->dev_to_tcp_state]);    controller_output(cntlr, buffer, strlen(buffer));    sprintf(buffer, "%9d ", port->tcp_bytes_received);    controller_output(cntlr, buffer, strlen(buffer));    sprintf(buffer, "%9d ", port->tcp_bytes_sent);    controller_output(cntlr, buffer, strlen(buffer));    sprintf(buffer, "%9d ", port->dev_bytes_received);    controller_output(cntlr, buffer, strlen(buffer));    sprintf(buffer, "%9d ", port->dev_bytes_sent);    controller_output(cntlr, buffer, strlen(buffer));    if (port->enabled != PORT_RAWLP) {	show_devcfg(cntlr, &(port->termctl));	need_space = 1;    }    if (port->tcp_to_dev_state != PORT_UNCONNECTED) {	if (need_space) {	    controller_output(cntlr, " ", 1);	}	    	show_devcontrol(cntlr, port->devfd);    }    controller_output(cntlr, "\n\r", 2);}/* Print information about a port to the control port given in cntlr. */static voidshowport(struct controller_info *cntlr, port_info_t *port){    char *str;    char buffer[128];    str = "TCP Port ";    controller_output(cntlr, str, strlen(str));    sprintf(buffer, "%s", port->portname);    controller_output(cntlr, buffer, strlen(buffer));    controller_output(cntlr, "\n\r", 2);    str = "  enable state: ";    controller_output(cntlr, str, strlen(str));    str = enabled_str[port->enabled];    controller_output(cntlr, str, strlen(str));    controller_output(cntlr, "\n\r", 2);    str = "  timeout: ";    controller_output(cntlr, str, strlen(str));    sprintf(buffer, "%d", port->timeout);    controller_output(cntlr, buffer, strlen(buffer));    controller_output(cntlr, "\n\r", 2);    str = "  connected to (or last connection): ";    controller_output(cntlr, str, strlen(str));    inet_ntop(AF_INET, &(port->remote.sin_addr), buffer, sizeof(buffer));    controller_output(cntlr, buffer, strlen(buffer));    controller_output(cntlr, ":", 1);    sprintf(buffer, "%d", ntohs(port->remote.sin_port));    controller_output(cntlr, buffer, strlen(buffer));    controller_output(cntlr, "\n\r", 2);    str = "  device: ";    controller_output(cntlr, str, strlen(str));    str = port->devname;    controller_output(cntlr, str, strlen(str));    controller_output(cntlr, "\n\r", 2);    str = "  device config: ";    controller_output(cntlr, str, strlen(str));    if (port->enabled == PORT_RAWLP) {	str = "none\n\r";	controller_output(cntlr, str, strlen(str));    } else {	show_devcfg(cntlr, &(port->termctl));	controller_output(cntlr, "\n\r", 2);    }    str = "  device controls: ";    controller_output(cntlr, str, strlen(str));    if (port->tcp_to_dev_state == PORT_UNCONNECTED) {	str = "not currently connected\n\r";	controller_output(cntlr, str, strlen(str));    } else {	show_devcontrol(cntlr, port->devfd);	controller_output(cntlr, "\n\r", 2);    }    str = "  tcp to device state: ";    controller_output(cntlr, str, strlen(str));    str = state_str[port->tcp_to_dev_state];    controller_output(cntlr, str, strlen(str));    controller_output(cntlr, "\n\r", 2);    str = "  device to tcp state: ";    controller_output(cntlr, str, strlen(str));    str = state_str[port->dev_to_tcp_state];    controller_output(cntlr, str, strlen(str));    controller_output(cntlr, "\n\r", 2);    str = "  bytes read from TCP: ";    controller_output(cntlr, str, strlen(str));    sprintf(buffer, "%d", port->tcp_bytes_received);    controller_output(cntlr, buffer, strlen(buffer));    controller_output(cntlr, "\n\r", 2);    str = "  bytes written to TCP: ";    controller_output(cntlr, str, strlen(str));    sprintf(buffer, "%d", port->tcp_bytes_sent);    controller_output(cntlr, buffer, strlen(buffer));    controller_output(cntlr, "\n\r", 2);    str = "  bytes read from device: ";    controller_output(cntlr, str, strlen(str));    sprintf(buffer, "%d", port->dev_bytes_received);    controller_output(cntlr, buffer, strlen(buffer));    controller_output(cntlr, "\n\r", 2);    str = "  bytes written to device: ";    controller_output(cntlr, str, strlen(str));    sprintf(buffer, "%d", port->dev_bytes_sent);    controller_output(cntlr, buffer, strlen(buffer));    controller_output(cntlr, "\n\r", 2);    if (port->config_num == -1) {	str = "  Port will be deleted when current session closes.\n\r";	controller_output(cntlr, str, strlen(str));    } else if (port->new_config != NULL) {	str = "  Port will be reconfigured when current session closes.\n\r";	controller_output(cntlr, str, strlen(str));    }}/* Find a port data structure given a port number. */static port_info_t *find_port_by_num(char *portstr){    port_info_t *port;    port = ports;    while (port != NULL) {	if (strcmp(portstr, port->portname) == 0) {	    return port;	}	port = port->next;    }    return NULL;}/* Handle a showport command from the control port. */voidshowports(struct controller_info *cntlr, char *portspec){    port_info_t *port;    if (portspec == NULL) {	/* Dump everything. */	port = ports;	while (port != NULL) {	    showport(cntlr, port);	    port = port->next;	}    } else {	port = find_port_by_num(portspec);	if (port == NULL) {	    char *err = "Invalid port number: ";	    controller_output(cntlr, err, strlen(err));	    controller_output(cntlr, portspec, strlen(portspec));	    controller_output(cntlr, "\n\r", 2);	} else {	    showport(cntlr, port);	    	}    }}/* Handle a showport command from the control port. */voidshowshortports(struct controller_info *cntlr, char *portspec){    port_info_t *port;    char        buffer[512];    sprintf(buffer,	    "%-22s %-6s %7s %-22s %-22s %-14s %-14s %9s %9s %9s %9s %s\n\r",	    "Port name",	    "Type",	    "Timeout",	    "Remote address",	    "Device",	    "TCP to device",	    "Device to TCP",	    "TCP in",	    "TCP out",	    "Dev in",	    "Dev out",	    "State");    controller_output(cntlr, buffer, strlen(buffer));    if (portspec == NULL) {	/* Dump everything. */	port = ports;	while (port != NULL) {	    showshortport(cntlr, port);	    port = port->next;	}    } else {	port = find_port_by_num(portspec);	if (port == NULL) {	    char *err = "Invalid port number: ";	    controller_output(cntlr, err, strlen(err));	    controller_output(cntlr, portspec, strlen(portspec));	    controller_output(cntlr, "\n\r", 2);	} else {	    showshortport(cntlr, port);	    	}    }}/* Set the timeout on a port.  The port number and timeout are passed   in as strings, this code will convert them, return any errors, and   perform the operation. */voidsetporttimeout(struct controller_info *cntlr, char *portspec, char *timeout){    int timeout_num;    port_info_t *port;    port = find_port_by_num(portspec);    if (port == NULL) {	char *err = "Invalid port number: ";	controller_output(cntlr, err, strlen(err));	controller_output(cntlr, portspec, strlen(portspec));	controller_output(cntlr, "\n\r", 2);    } else {	timeout_num = scan_int(timeout);	if (timeout_num == -1) {	    char *err = "Invalid timeout: ";	    controller_output(cntlr, err, strlen(err));	    controller_output(cntlr, timeout, strlen(timeout));	    controller_output(cntlr, "\n\r", 2);	} else {	    port->timeout = timeout_num;	    if (port->tcpfd != -1) {		reset_timer(port);	    }	}    }}/* Configure a port.  The port number and configuration are passed in   as strings, this code will get the port and then call the code to   configure the device. */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -