#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
/*
将to的路由器IP更换为from的SIP Server IP
*/
int main()
{
char *from ="\"Extension 1010\" <sip:[email protected]>";
char *to = "<sip:[email protected]:58926;transport=udp>";
if(indexOf(to,";") > 0){
int fromLen,fromStart,toStart,toEnd;
char *fromIP;
//1.取出from的IP
printf("to has ; in the string\n");
fromLen = strlen(from);
if(indexOf(from,">") > 0){
fromLen = fromLen-1;
}
fromStart = indexOf(from,"@");
fromIP= (char *)malloc(fromLen-fromStart);
substring(fromIP,from,fromStart,fromLen);
printf("from ip is %s\n",fromIP);
//2. 取出to的sip account
toStart = indexOf(to,"sip:");
toEnd = indexOf(to,"@");
if(toEnd > toStart){
char *toAccout;
toAccout = (char *)malloc(toEnd-toStart);
substring(toAccout,to,toStart,toEnd);
printf("to account=%s start=%d,end=%d\n",toAccout,toStart,toEnd);
to = (char *)malloc(strlen(fromIP) + strlen(toAccout) + 1);
strcpy(to, toAccout);
strcat(to, fromIP);
printf("new to address is %s\n",to);
free(toAccout);
}
free(fromIP);
}
getch();
return 0;
}
void substring(char *dest,char *src,int start,int end)
{
int i=start;
if(start>strlen(src))return;
if(end>strlen(src))
end=strlen(src);
while(i<end)
{
dest[i-start]=src[i];
i++;
}
dest[i-start]='\0';
return;
}
int indexOf(char *str1,char *str2)
{
char *p=str1;
int i=0;
p=strstr(str1,str2);
if(p==NULL)
return -1;
else{
while(str1!=p)
{
str1++;
i++;
}
}
return i;
}
因为第一次使用C语言,所以将一些基本注意事项记录一下:
1.所有声明必须紧跟在 { 后面
2.使用malloc动态分析内存,使用free释放