static int pap_send_auth_req(ppp_protocol_t *self, int resend) { int nl = strlen(ppp_state->username); int pl = strlen(ppp_state->passwd); uint16_t len = 6 + nl + pl; uint8_t buf[len]; int i = 0, j; pap_pkt_t *pkt = (pap_pkt_t *)buf; (void)self; /* Fill in the header. */ pkt->code = PAP_AUTHENTICATE_REQ; if(resend) pkt->id = pap_id; else pkt->id = ++pap_id; pkt->len = htons(len); /* Fill in the rest of the packet. */ pkt->data[i++] = nl; for(j = 0; j < nl; ++j) { pkt->data[i++] = ppp_state->username[j]; } pkt->data[i++] = pl; for(j = 0; j < pl; ++j) { pkt->data[i++] = ppp_state->passwd[j]; } /* Set the resend timer for 3 seconds. */ next_resend = timer_ms_gettime64() + 3000; if(!resend) resend_cnt = 10; return ppp_send(buf, len, PPP_PROTOCOL_PAP); }
/*---------------------------------------------------------------------------*/ static void output(u8_t *hdr, u16_t hdrlen, u8_t *data, u16_t datalen) { ppp_send(); }
int pppd(struct pppd_settings_s *pppd_settings) { struct pollfd fds[2]; int ret; struct ppp_context_s *ctx; ctx = (struct ppp_context_s*)malloc(sizeof(struct ppp_context_s)); memset(ctx, 0, sizeof(struct ppp_context_s)); strcpy((char*)ctx->ifname, "ppp%d"); ctx->settings = pppd_settings; ctx->if_fd = tun_alloc((char*)ctx->ifname); if (ctx->if_fd < 0) { free(ctx); return 2; } ctx->ctl.fd = open_tty(pppd_settings->ttyname); if (ctx->ctl.fd < 0) { close(ctx->ctl.fd); free(ctx); return 2; } ctx->ctl.echo = true; ctx->ctl.verbose = true; ctx->ctl.timeout = 30; fds[0].fd = ctx->if_fd; fds[0].events = POLLIN; fds[1].fd = ctx->ctl.fd; fds[1].events = POLLIN; ppp_init(ctx); ppp_reconnect(ctx); while (1) { fds[0].revents = fds[1].revents = 0; ret = poll(fds, 2, 1000); if (ret > 0 && fds[0].revents & POLLIN) { ret = read(ctx->if_fd, ctx->ip_buf, PPP_RX_BUFFER_SIZE); printf("read from tun :%i\n", ret); if (ret > 0) { ctx->ip_len = ret; ppp_send(ctx); ctx->ip_len = 0; } } ppp_poll(ctx); if (ppp_check_errors(ctx)) { ppp_reconnect(ctx); } else { if (ctx->ip_len > 0) { ret = write(ctx->if_fd, ctx->ip_buf, ctx->ip_len); ctx->ip_len = 0; ret = read(ctx->if_fd, ctx->ip_buf, PPP_RX_BUFFER_SIZE); if (ret > 0) { ctx->ip_len = ret; ppp_send(ctx); ctx->ip_len = 0; } } } } return 1; }