nw_result mk_update(mach_port_t master_port, nw_update_type up_type, int *up_info) { nw_result rc; if (master_port == 0) { /* XXX */ rc = NW_FAILURE; } else { nw_lock(); switch (up_type) { case NW_HOST_ADDRESS_REGISTER: case NW_HOST_ADDRESS_UNREGISTER: if (invalid_user_access(current_task()->map, (vm_offset_t) up_info, (vm_offset_t) up_info + sizeof(nw_address_s) - 1, VM_PROT_READ | VM_PROT_WRITE)) { rc = NW_INVALID_ARGUMENT; } else { rc = nc_update(up_type, up_info); } break; case NW_INITIALIZE: nc_initialize(); rc = NW_SUCCESS; break; default: rc = NW_INVALID_ARGUMENT; } nw_unlock(); } return rc; }
int nc_peek(uint8_t sock_id) { nc_update(sock_id, 0); if(nc_read_len[sock_id]) { int nc_offset; nc_offset = SOCK_READ_CACHE_SIZE - nc_read_len[sock_id]; return (int)nc_read_buf[sock_id][nc_offset]; } else return EOF; }
int nc_read(uint8_t sock_id, uint8_t *rbuf, size_t rlen) { int copy_len, recv_len; nc_update(sock_id, 0); if(nc_read_len[sock_id]) { int nc_offset; if(rlen > nc_read_len[sock_id]) { copy_len = nc_read_len[sock_id]; recv_len = rlen - nc_read_len[sock_id]; } else { copy_len = rlen; recv_len = 0; } nc_offset = SOCK_READ_CACHE_SIZE - nc_read_len[sock_id]; memcpy(rbuf, nc_read_buf[sock_id] + nc_offset, copy_len); nc_read_len[sock_id] -= copy_len; } else return 0; if(!nc_tcp_rxlen[sock_id]) return copy_len; if(recv_len) { recv_len = Phpoc.command(F("tcp%u recv %u"), sock_id, recv_len); if(recv_len > 0) Phpoc.read(rbuf + copy_len, recv_len); nc_tcp_rxlen[sock_id] = Phpoc.tcpIoctlReadInt(F("rxlen"), sock_id); ct_start(sock_id, CT_ID_RXLEN); /* restart rxlen timer */ } return copy_len + recv_len; }
int nc_write(uint8_t sock_id, const uint8_t *wbuf, size_t wlen) { int wcnt; wcnt = 0; if(nc_write_len[sock_id] + wlen >= SOCK_WRITE_CACHE_SIZE) { if(nc_write_len[sock_id]) { int frag; if((frag = SOCK_WRITE_CACHE_SIZE - nc_write_len[sock_id])) { memcpy(nc_write_buf[sock_id] + nc_write_len[sock_id], wbuf, frag); wbuf += frag; wcnt += frag; wlen -= frag; } #ifdef INCLUDE_LIB_V1 if(Sppc.flags & PF_SYNC_V1) { Phpoc.command(F("tcp%u send"), sock_id); if(!Sppc.errno) Phpoc.write(nc_write_buf[sock_id], SOCK_WRITE_CACHE_SIZE); } else #endif { Phpoc.write(nc_write_buf[sock_id], SOCK_WRITE_CACHE_SIZE); if(!Sppc.errno) Phpoc.command(F("tcp%u send"), sock_id); } nc_write_len[sock_id] = 0; } if(wlen >= SOCK_WRITE_CACHE_SIZE) { #ifdef INCLUDE_LIB_V1 if(Sppc.flags & PF_SYNC_V1) { Phpoc.command(F("tcp%u send"), sock_id); if(!Sppc.errno) Phpoc.write(wbuf, wlen); } else #endif { Phpoc.write(wbuf, wlen); if(!Sppc.errno) Phpoc.command(F("tcp%u send"), sock_id); } wcnt += wlen; wlen = 0; } ct_stop(sock_id, CT_ID_WRITE); } if(wlen) { if(!nc_write_len[sock_id]) ct_start(sock_id, CT_ID_WRITE); memcpy(nc_write_buf[sock_id] + nc_write_len[sock_id], wbuf, wlen); nc_write_len[sock_id] += wlen; wcnt += wlen; wlen = 0; } nc_update(sock_id, 0); return wcnt; }
int nc_read_line(uint8_t sock_id, uint8_t *rbuf, size_t rlen) { int copy_len, recv_len, nc_offset; int copy_drop_len, recv_drop_len; uint8_t crlf; nc_update(sock_id, 0); if(!nc_read_len[sock_id]) return 0; copy_len = 0; recv_len = 0; copy_drop_len = 0; recv_drop_len = 0; crlf = 0; nc_offset = SOCK_READ_CACHE_SIZE - nc_read_len[sock_id]; while(nc_offset < SOCK_READ_CACHE_SIZE) { if(!crlf) { if(nc_read_buf[sock_id][nc_offset] == 0x0d) /* CR ? */ crlf++; } else { if(nc_read_buf[sock_id][nc_offset] == 0x0a) /* LF ? */ { crlf++; copy_len++; break; } else crlf = 0; } nc_offset++; copy_len++; } if(copy_len > rlen) { copy_drop_len = copy_len - rlen; copy_len = rlen; } if(crlf == 2) { /* CRLF */ nc_offset = SOCK_READ_CACHE_SIZE - nc_read_len[sock_id]; memcpy(rbuf, nc_read_buf[sock_id] + nc_offset, copy_len); nc_read_len[sock_id] -= (copy_len + copy_drop_len); return copy_len; } if(crlf == 1) { /* CR */ uint8_t byte; if(Phpoc.command(F("tcp%u peek 1"), sock_id) <= 0) return 0; Phpoc.read(&byte, 1); if(byte == 0x0a) /* LF ? */ recv_len = 1; else return 0; } else { #ifdef INCLUDE_LIB_V1 if(Sppc.flags & PF_SYNC_V1) { Phpoc.command(F("tcp%u ioctl get rxlen 0d0a"), sock_id); if(!Sppc.errno) recv_len = Phpoc.readInt(); else recv_len = 0; } else #endif recv_len = Phpoc.command(F("tcp%u ioctl get rxlen \r\n"), sock_id); if(!recv_len) return 0; } if(copy_drop_len) { recv_drop_len = recv_len; recv_len = 0; } else { if((copy_len + recv_len) > rlen) { recv_drop_len = (copy_len + recv_len) - rlen; recv_len -= recv_drop_len; } } nc_offset = SOCK_READ_CACHE_SIZE - nc_read_len[sock_id]; memcpy(rbuf, nc_read_buf[sock_id] + nc_offset, copy_len); nc_read_len[sock_id] -= (copy_len + copy_drop_len); if(recv_len) { recv_len = Phpoc.command(F("tcp%u recv %u"), sock_id, recv_len); if(recv_len > 0) Phpoc.read(rbuf + copy_len, recv_len); else return 0; nc_tcp_rxlen[sock_id] = Phpoc.tcpIoctlReadInt(F("rxlen"), sock_id); ct_start(sock_id, CT_ID_RXLEN); /* restart rxlen timer */ } if(recv_drop_len) { while(recv_drop_len) { uint8_t drop_buf[16]; int len; if(recv_drop_len > 16) len = 16; else len = recv_drop_len; len = Phpoc.command(F("tcp%u recv %u"), sock_id, len); Phpoc.read(drop_buf, len); recv_drop_len -= len; } nc_tcp_rxlen[sock_id] = Phpoc.tcpIoctlReadInt(F("rxlen"), sock_id); ct_start(sock_id, CT_ID_RXLEN); /* restart rxlen timer */ } return copy_len + recv_len; }