Example #1
0
/* send a command to the LCD */
int send_lcd(char *buf) {

  int res;
  char rcvbuf[256];
  size_t outlen;

  /* Limit the size of outgoing strings. */
  outlen = strlen(buf);
  if (outlen > 255) {
    outlen = 256;
  }

  /* send the command */
  (void)sockwriteline(sd,buf,outlen);

  /* TODO:  check return status */

  /* read the data */
  res=sockreadline(sd,rcvbuf,sizeof(rcvbuf)-1);

  /* null-terminate the string before printing */
  /* rcvbuf[res-1]=0; FIX-ME: not using this at the moment... */

  /* return the result */
  return(res);
}
Example #2
0
/* Must be at least 128 bytes long                         */
int http_get_fd(char *URL, size_t *len, char *error)
{
    char 	host[MAX_HOST_LEN+1];
    ulong	ip_addr;
    int		s=-1;
    int		port=80;
    char	*p;
    char	*path;
    struct sockaddr_in addr;
    char	header[MAX_HEADER+1];

    if(len != NULL)
        *len=0;
    if(error != NULL)
        *error=0;
    if(strncasecmp(URL, "http://", 7))  {
        if(error!=NULL)
            strcpy(error, "URL is not http");
        return(-1);
    }
    strncpy(host, URL+7, MAX_HOST_LEN);
    host[MAX_HOST_LEN+1]=0;
    if((p=strchr(host,'/'))==NULL)  {
        if(error!=NULL)
            strcpy(error, "Host too long or no path info found");
        return(-1);
    }
    *p=0;
    if((p=strchr(host,':'))!=NULL)  {
        *p=0;
        port=atoi(p+1);
    }
    if((ip_addr=resolve_ip(host))==INADDR_NONE)  {
        if(error!=NULL)
            strcpy(error, "Unable to resolve host");
        return(-1);
    }
    if((path=strchr(URL+7, '/'))==NULL)  {
        if(error!=NULL)
            strcpy(error, "Path info not found");
        return(-1);
    }
    if ((s = socket(AF_INET, SOCK_STREAM, IPPROTO_IP)) < 0)  {
        if(error!=NULL)
            strcpy(error, "Unable to create socket");
        return(-1);
    }
    memset(&addr,0,sizeof(addr));
    addr.sin_addr.s_addr = ip_addr;
    addr.sin_family = AF_INET;
    addr.sin_port   = htons(port);
    if (connect(s, (struct sockaddr*)&addr, sizeof (addr)) < 0) {
        close(s);
        if(error!=NULL)
            strcpy(error, "Unable to connect");
        return(-1);
    }
    if(error!=NULL)
        strcpy(error,"Unable to send request");
    if(write(s, "GET ", 4)!=4)  {
        close(s);
        return(-1);
    }
    if(write(s, path, strlen(path))!=strlen(path))  {
        close(s);
        return(-1);
    }
    if(write(s, " HTTP/1.0\r\nHost: ", 17)!=17)  {
        close(s);
        return(-1);
    }
    if(write(s, host, strlen(host))!=strlen(host))  {
        close(s);
        return(-1);
    }
    if(write(s, "\r\nConnection: close\r\n\r\n",23)!=23)  {
        close(s);
        return(-1);
    }
    if(error!=NULL)
        *error=0;
    if(sockreadline(s, header, sizeof(header), error) < 0)  {
        close(s);
        return(-1);
    }
    if(atoi(header+9)!=200)  {
        if(error != NULL)  {
            strncpy(error, header, 128);
            error[127]=0;
        }
        close(s);
        return(-1);
    }
    while(header[0])  {
        if(sockreadline(s, header, sizeof(header), error) < 0)  {
            close(s);
            return(-1);
        }
        if(len != NULL && strncasecmp(header,"content-length:",15)==0)  {
            *len=atol(header+15);
        }
    }
    return(s);
}