void act_nopen_a(int8 type, int8 save, uint16 sport, uint8 *dip, uint16 dport) { int8 ret, sock, i; for(i=ATC_SOCK_NUM_START; i<=ATC_SOCK_NUM_END; i++) { if(sockstat[i] != SOCK_STAT_IDLE && sockport[i] == sport) { DBGA("src port(%d) is using now by sock(%d)", sport, i); MAKE_TCMD_DIGIT(atci.tcmd.arg1, 2); CMD_RESP_RET(RET_WRONG_ARG, VAL_NONE); } } if(save == 'S' || save == 'A') { } if(save == 'O' || save == 'A') { if(type == 'S') { sock = sock_get(SOCK_STAT_TCP_SRV, sport); if(sock == RET_NOK) CMD_RESP_RET(RET_NO_SOCK, VAL_NONE); ret = TCPServerOpen(sock, sport); if(ret != RET_OK) CMD_RESP_RET(RET_UNSPECIFIED, VAL_NONE); sockwatch_set(sock, WATCH_SOCK_CONN_EVT); CMD_RESP_RET(RET_OK, sock); } else if(type == 'C') { sock = sock_get(SOCK_STAT_TCP_CLT, sport); if(sock == RET_NOK) CMD_RESP_RET(RET_NO_SOCK, VAL_NONE); ret = TCPCltOpenNB(sock, sport, dip, dport); if(ret != RET_OK) { DBGA("TCPCltOpenNB fail - ret(%d)", ret); CMD_RESP_RET(RET_WRONG_ADDR, VAL_NONE); } sockwatch_set(sock, WATCH_SOCK_CONN_TRY); sockbusy[sock] = VAL_TRUE; CMD_RESP_RET(RET_ASYNC, sock); } else { if(dip != NULL) { memcpy(udpip[sock], dip, 4); udpport[sock] = dport; } sock = sock_get(SOCK_STAT_UDP, sport); if(sock == RET_NOK) CMD_RESP_RET(RET_NO_SOCK, VAL_NONE); UDPOpen(sock, sport); sockwatch_set(sock, WATCH_SOCK_RECV); CMD_RESP_RET(RET_OK, sock); } } }
/* Note: We don't do address return, the library can handle it */ arg_t _accept(void) { uint8_t flag; struct socket *s = sock_get(fd, &flag); struct socket *n; int8_t nfd; if (s == NULL) return -1; if (s->s_state == SS_LISTENING) { udata.u_error = EALREADY; return -1; } /* Needs locking versus interrupts */ while ((n = sock_pending(s)) == NULL) { if (psleep_flags(s, flag)) return -1; if (s->s_error) return sock_error(s); } if ((nfd = make_socket(&socktypes[SOCKTYPE_TCP], &n)) == -1) return -1; n->s_state = SS_CONNECTED; return nfd; }
arg_t _connect(void) { uint8_t flag; struct socket *s = sock_get(fd, &flag); struct sockaddr_in sin; if (s == NULL) return -1; if (s->s_state == SS_CONNECTING) { udata.u_error = EALREADY; return -1; } if (s->s_state == SS_UNCONNECTED && sock_autobind(s)) return -1; if (s->s_state == SS_BOUND) { if (sa_getremote(uaddr, &sin) == -1) return -1; s->s_addr[SADDR_DST].addr = sin.sin_addr.s_addr; s->s_addr[SADDR_DST].port = sin.sin_port; if (net_connect(s)) return -1; if (sock_wait_leave(s, 0, SS_CONNECTING)) { /* API oddity, thanks Berkeley */ if (udata.u_error == EAGAIN) udata.u_error = EINPROGRESS; return -1; } return sock_error(s); } udata.u_error = EINVAL; return -1; }
arg_t _connect(void) { uint8_t flag; struct socket *s = sock_get(fd, &flag); struct sockaddr_in sin; if (s == NULL) return -1; if (s->s_state == SS_CONNECTING) { udata.u_error = EALREADY; return -1; } if (s->s_state == SS_UNCONNECTED && sock_autobind(s)) return -1; if (s->s_state == SS_BOUND) { if (sa_getremote(uaddr, &sin) == -1) return -1; s->s_addr[SADDR_DST].addr = sin.sin_addr.s_addr; s->s_addr[SADDR_DST].port = sin.sin_port; s->s_state = SS_CONNECTING; /* Protocol op to kick off */ } do { /* FIXME: return EINPROGRESS not EINTR for SS_CONNECTING */ if (psleep_flags(s, flag)) return -1; /* Protocol state check */ } while (s->s_state == SS_CONNECTING); return sock_error(s); }
arg_t _shutdown(void) { struct socket *s = sock_get(fd, NULL); if (s == NULL) return -1; if (how > 2) { udata.u_error = EINVAL; return -1; } return net_shutdown(s, how + 1); }
arg_t _getsockaddrs(void) { struct socket *s = sock_get(fd, NULL); if (s == NULL) return -1; if (type > 1) { udata.u_error = EINVAL; return -1; } return sa_put(s, type, uaddr); }
int main(void) { int sock_fd ; /*将sock_fd创建为侦听套结字*/ if(sock_get (&sock_fd) < 0) { fprintf (stderr, "sock_get error\n") ; exit (1) ; } poll_server (&sock_fd) ; return 0 ; }
arg_t _sendto(void) { struct socket *s = sock_get(fd, NULL); struct sockaddr_in sin; uint16_t flags; uint16_t alen; uint16_t err; if (s == NULL) return -1; if (s->s_state == SS_UNCONNECTED) { err = sock_autobind(s); if (err) return err; } if (s->s_state < SS_BOUND) { udata.u_error = EINVAL; return -1; } if (s->s_state != SS_BOUND && s->s_state < SS_CONNECTED) { udata.u_error = ENOTCONN; return -1; } flags = ugetw(&uaddr->sio_flags); if (flags) { udata.u_error = EINVAL; return -1; } alen = ugetw(&uaddr->sio_flags); /* Save the address and then just do a 'write' */ if (s->s_type != SOCKTYPE_TCP && alen) { if (s->s_state >= SS_CONNECTING) { udata.u_error = EISCONN; return -1; } /* Use the address in atmp */ s->s_flag |= SFLAG_ATMP; if (sa_getremote(&uaddr->sio_addr, &sin) == -1) return -1; s->s_addr[SADDR_TMP].addr = sin.sin_addr.s_addr; s->s_addr[SADDR_TMP].port = sin.sin_port; } else { s->s_flag &= ~SFLAG_ATMP; if (s->s_state < SS_CONNECTED) { udata.u_error = EDESTADDRREQ; return -1; } } return _write(); }
arg_t _listen(void) { struct socket *s = sock_get(fd, NULL); if (s == NULL) return -1; if (s->s_state == SS_UNCONNECTED && sock_autobind(s)) return -1; if (s->s_type != SOCKTYPE_TCP || s->s_state != SS_BOUND) { udata.u_error = EINVAL; return -1; } /* Call the protocol services */ return net_listen(s); }
arg_t _recvfrom(void) { struct socket *s = sock_get(d, NULL); int ret; if (s == NULL) return -1; ret = _read(); if (ret < 0) return ret; if (sa_put(s, SADDR_TMP, uaddr)) return -1; return ret; }
arg_t _sendto(void) { struct socket *s = sock_get(d, NULL); struct sockaddr_in sin; if (s == NULL) return -1; /* Save the address and then just do a 'write' */ if (s->s_type != SOCKTYPE_TCP) { /* Use the address in atmp */ s->s_flag |= SFLAG_ATMP; if (sa_getremote(uaddr, &sin) == -1) return -1; s->s_addr[SADDR_TMP].addr = sin.sin_addr.s_addr; s->s_addr[SADDR_TMP].port = sin.sin_port; } return _write(); }
arg_t _bind(void) { struct socket *s = sock_get(fd, NULL); struct sockaddr_in sin; if (s == NULL) return -1; if (s->s_state != SS_UNCONNECTED) return -1; if (sa_getlocal(uaddr, &sin) == -1) return -1; if (sock_find_local(sin.sin_addr.s_addr, sin.sin_port)) { udata.u_error = EADDRINUSE; return -1; } s->s_addr[SADDR_SRC].addr = sin.sin_addr.s_addr; s->s_addr[SADDR_SRC].port = sin.sin_port; return net_bind(s); }
arg_t _recvfrom(void) { struct socket *s = sock_get(fd, NULL); int ret; uint16_t flags; /* FIXME: will need _read redone for banked syscalls */ if (s == NULL) return -1; flags = ugetw(&uaddr->sio_flags); if (flags) { udata.u_error = EINVAL; return -1; } ret = _read(); if (ret < 0) return ret; if (sa_put(s, SADDR_TMP, &uaddr->sio_addr)) return -1; return ret; }
/* reads one line from server into ftp->reply * returns 0 on success or -1 on failure */ static int ftp_gets(void) { int c, i=0; ftp->reply[0] = 0; if(!sock_connected(ftp->ctrl)) { ftp_err(_("No control connection\n")); return -1; } while(true) { c = sock_get(ftp->ctrl); if(c == EOF) { ftp_err(_("Server has closed control connection\n")); ftp_close(); return -1; } else if(c == 255/*IAC*/) { /* handle telnet commands */ switch(c = sock_get(ftp->ctrl)) { case 251/*WILL*/: case 252/*WONT*/: c = sock_get(ftp->ctrl); sock_printf(ftp->ctrl, "%c%c%c", 255/*IAC*/, 254/*DONT*/, c); sock_flush(ftp->ctrl); break; case 253/*DO*/: case 254/*DONT*/: c = sock_get(ftp->ctrl); sock_printf(ftp->ctrl, "%c%c%c", 255/*IAC*/, 252/*WONT*/, c); sock_flush(ftp->ctrl); break; default: break; } continue; } else if(c == '\r') { c = sock_get(ftp->ctrl); if(c == 0) c = '\r'; else if(c == '\n') { ftp->reply[i++] = (char)c; break; } else if(c == EOF) /* do nothing */ ; else { /* telnet protocol violation, hmpf... */ sock_unget(ftp->ctrl, c); continue; } } else if(c == '\n') break; if(i < MAXREPLY) ftp->reply[i++] = (char)c; } if(i >= MAXREPLY) { ftp_err(_("Reply too long (truncated)\n")); i = MAXREPLY; } ftp->reply[i] = '\0'; ftp->fullcode = atoi(ftp->reply); #ifdef SECFTP { int r = 0; switch(ftp->fullcode) { /* handle protected responses 6xx */ case 631: r = sec_read_msg(ftp->reply, prot_safe); break; case 632: r = sec_read_msg(ftp->reply, prot_private); break; case 633: r = sec_read_msg(ftp->reply, prot_confidential); break; } if(r == -1) { ftp->fullcode = 0; ftp->code = vbNone; return 0; } else ftp->fullcode = atoi(ftp->reply); } #endif strip_trailing_chars(ftp->reply, "\n\r"); ftp->code = ftp->fullcode / 100; return ftp->fullcode; }
static int FILE_recv_ascii(Socket* in, FILE *out) { time_t then = time(0) - 1; time_t now; ftp_set_close_handler(); if(foo_hookf) foo_hookf(&ftp->ti); ftp->ti.begin = false; sock_clearerr_in(in); clearerr(out); bool next = true; int nc = 0; while (true) { int c = 0; if (next) { c = sock_get(in); if (c == EOF) break; } else { c = nc; next = true; } if (ftp_sigints() > 0 || wait_for_input() != 0) break; if (c == '\n') ftp->ti.barelfs++; else if (c == '\r') { nc = sock_get(in); if (nc == EOF) break; if (nc != '\n') next = false; else c = nc; } if (fputc(c, out) == EOF) break; ftp->ti.size++; if(foo_hookf) { now = time(0); if(now > then) { foo_hookf(&ftp->ti); then = now; } } } return maybe_abort_in(in, out); }