- case IDENT_STATE_CONNECT:
- uslen = sizeof(sock_us);
- themlen = sizeof(sock_them);
- if ((getsockname(this->u->fd,(sockaddr*)&sock_us,&uslen) || getpeername(this->u->fd, (sockaddr*)&sock_them, &themlen)))
- {
- Srv->Log(DEBUG,"Ident: failed to get socket names, bailing to state 3");
- shutdown(this->fd,2);
- close(this->fd);
- this->fd = -1;
- state = IDENT_STATE_DONE;
- }
- else
- {
- // send the request in the following format: theirsocket,oursocket
- snprintf(ident_request,127,"%d,%d\r\n",ntohs(sock_them.sin_port),ntohs(sock_us.sin_port));
- send(this->fd,ident_request,strlen(ident_request),0);
- Srv->Log(DEBUG,"Sent ident request, moving to state 2");
- state = IDENT_STATE_WAITDATA;
- }
- break;
- case IDENT_STATE_WAITDATA:
- nrecv = recv(this->fd,ibuf,sizeof(ibuf),0);
- if (nrecv > 0)
- {
- // we have the response line in the following format:
- // 6193, 23 : USERID : UNIX : stjohns
- // 6195, 23 : ERROR : NO-USER
- ibuf[nrecv] = '\0';
- Srv->Log(DEBUG,"Received ident response: "+std::string(ibuf));
- shutdown(this->fd,2);
- close(this->fd);
- this->fd = -1;
- char* savept;
- char* section = strtok_r(ibuf,":",&savept);
- while (section)
- {
- if (strstr(section,"USERID"))
- {
- section = strtok_r(NULL,":",&savept);
- if (section)
- {
- // ID type, usually UNIX or OTHER... we dont want it, so read the next token
- section = strtok_r(NULL,":",&savept);
- if (section)
- {
- while ((*section == ' ') && (strlen(section)>0)) section++; // strip leading spaces
- int t = strlen(section);
- for (int j = 0; j < t; j++)
- if ((section[j] < 33) || (section[j]>126))
- section[j] = '\0'; // truncate at invalid chars
- if (strlen(section))
- {
- strlcpy(u->ident,section,IDENTMAX);
- Srv->Log(DEBUG,"IDENT SET: "+std::string(u->ident));
- Srv->SendServ(u->fd,"NOTICE "+std::string(u->nick)+" :*** Found your ident: "+std::string(u->ident));
- }
- break;
- }
- }
- }
- section = strtok_r(NULL,":",&savept);
- }
- state = IDENT_STATE_DONE;
- }
- break;
- case IDENT_STATE_DONE:
- shutdown(this->fd,2);
- close(this->fd);
- this->fd = -1;
- Srv->Log(DEBUG,"Ident lookup is complete!");
- break;
- default:
- Srv->Log(DEBUG,"Ident: invalid ident state!!!");