Dylan
Dylan

Reputation: 45

copy a sub part of a string

I'm trying to copy a part of a string in a other string. I know the 2 identifiers starting and ending the sub-string.

I'm trying to copy the IP from this string:

0x200085f6 <memp_memory+4502> "GET / HTTP/1.1\r\nHost: 192.168.1.200\r\nConnection

The beginning of the string will be "Host: " or 192 The ending will be "\r\nC" or the 2nd occurrence of "\r\n" So the desired output is:

192.168.1.200

I tried using strcpy and memcpy but the IP has to be variable so I don't know how long it will be or what it will be, the minimum will be 11 and the maximum 15 chars.

I hope you can help me further.

Upvotes: 0

Views: 178

Answers (3)

sg7
sg7

Reputation: 6298

One solution is to extract all the characters which are number or . from the

"GET / HTTP/1.1\r\nHost: 192.168.1.200\r\nConnection" till `\r` is encounter.

Start extraction after Host: Remember to terminate the string with '\0'.

Simplified version of the program:

#include<stdio.h>
#include<string.h>

int main() {

  char ip[64];
  char *p = NULL;
  char *str = "GET / HTTP/1.1\r\nHost: 192.168.1.200\r\nConnection";
  char c;
  int i = 0;
  int len;

  len = strlen("Host ");
  p = strstr(str,"Host: ");

  if(p)
  {    
      while(1)
      {
        c = *(p+i+len);

        if(c != '\r' && c!= '\0')
        {
            if( c == ' '){ 
            i++;
            continue;
            }

            ip[i++] = c;
        }
        else
        {
            ip[i] = '\0';
            break;
        }

     } // while

  }


   printf("Ip=%s", ip);
   return 0;
}

OUTPUT:

Ip=192.168.1.200

Upvotes: 0

ad absurdum
ad absurdum

Reputation: 21321

If the format of the input string is known, and is as suggested by the posted example input, sscanf() can be used with the scanset directive to extract the IP address.

Using %*[^:] causes sscanf() to match any characters in the input until a colon is encountered. The * suppresses assignment. Scanning resumes with the : character, so a literal : must be placed in the format string to match this character. Then the %s directive can be used to match and store the IP address as a string. Here is an example:

#include <stdio.h>

#define BUF_SZ  256

int main(void)
{
    const char *input = "GET / HTTP/1.1\r\nHost: 192.168.1.200\r\nConnection";

    char ip_address[BUF_SZ];
    if (sscanf(input, "%*[^:]: %255s", ip_address) == 1) {
        puts(ip_address);
    }

    return 0;
}

Program output:

192.168.1.200

Upvotes: 1

LSerni
LSerni

Reputation: 57408

You will need a buffer of 16 bytes (max 3 bytes for each octet, total 12, plus three points is 15, plus zero-terminator).

Then, as you say, you need to precisely position your read:

host = strstr(string, "Host: ");
if (!host) {
    // Problem. Cannot continue, the string has not the format expected.
}
host += strlen("Host: ");
end  = strstr(host, "\r\n");
if (!end) {
    // Problem
}
// Sanity check: end - host must be less than 16. Someone could send you
// Host: 192.168.25.12.Hello.I.Am.Little.Bobby.Headers\r\n
if ((end - host) > 15) {
    // Big problem
}
// We now know where the address starts, and where it ends.
// Copy the string into address
strncpy(address, host, (end-host));
// Add zero terminator to make this a C string
address[end-host] = 0x0;

Upvotes: 1

Related Questions