Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[request] two new natives (classic mode) #1

Open
Destro- opened this issue May 16, 2015 · 0 comments
Open

[request] two new natives (classic mode) #1

Destro- opened this issue May 16, 2015 · 0 comments

Comments

@Destro-
Copy link

Destro- commented May 16, 2015

  • socket_recvfrom(_socket, _data[], _len, &_port, _ip[], _iplen)
  • socket_sendto(_socket, _data[], _ip[], _port)

// native socket_recvfrom(_socket, _data[], _len, &_port, _ip[], _iplen);
static cell AMX_NATIVE_CALL socket_recvfrom(AMX *amx, cell *params)   /* 6 param */
{
    int socket = params[1];
    int length = params[3];
    int tmp = -1;

    char *tmpchar = new char[length];
    if(tmpchar == NULL)
        return -1;

    memset(tmpchar, 0, length);

    struct sockaddr_in cliaddr;
    int slen = sizeof(cliaddr);
    tmp = recvfrom(socket, tmpchar, length-1, 0, (struct sockaddr *)&cliaddr, &slen);
    if (tmp == -1)
    {
        delete [] tmpchar;
        return -1;
    }


    tmpchar[tmp]='\0';
    int nlen = 0;

    int max = length-1;
    const char* src = tmpchar;
    cell* dest = MF_GetAmxAddr(amx,params[2]);
    while(max--&&nlen<tmp)
    {
        *dest++ = (cell)*src++;
        nlen++;
    }
    *dest = 0;

    delete [] tmpchar;

    char ip[25];
    memcpy(ip, inet_ntoa(cliaddr.sin_addr), sizeof(ip));
    MF_SetAmxString(amx, params[5], ip, params[6]);

    cell *r_port = MF_GetAmxAddr(amx, params[4]);
    *r_port = ntohs(cliaddr.sin_port);

    return tmp;
}
_____________________________________________________________________
// native socket_sendto(_socket, _data[], _ip[], _port);
static cell AMX_NATIVE_CALL socket_sendto(AMX *amx, cell *params)  /* 4 param */
{
    int len, len2;
    int socket = params[1];
    char* data = MF_GetAmxString(amx,params[2],0,&len);
    unsigned int port = params[4];
    char* ip = MF_GetAmxString(amx,params[3],0,&len2);

    struct sockaddr_in cliaddr;
    cliaddr.sin_family = AF_INET;
    cliaddr.sin_port = htons(port);

    unsigned long addr;

    if((addr = inet_addr(ip)) != INADDR_NONE)
    {
        memcpy((char *)&cliaddr.sin_addr, &addr, sizeof(addr));
    }
    else
    {
        struct hostent *host_info;
        host_info = gethostbyname(ip);
        if (host_info == NULL)
        {
            return -1;
        }
        memcpy((char *)&cliaddr.sin_addr, host_info->h_addr, host_info->h_length);
    }

    return sendto(socket, data, len, 0,  (struct sockaddr *)&cliaddr, sizeof(cliaddr));
}

_____________________________________________________________________
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants