MCPcopy Create free account
hub / github.com/OpenSIPS/opensips / str2ip

Function str2ip

ip_addr.h:555–623  ·  view source on GitHub ↗

! \brief converts a str to an ipv4 address, returns the address or 0 on error Warning: the result is a pointer to a statically allocated structure */

Source from the content-addressed store, hash-verified

553/*! \brief converts a str to an ipv4 address, returns the address or 0 on error
554 Warning: the result is a pointer to a statically allocated structure */
555static inline struct ip_addr* str2ip(str* st)
556{
557 int i, j;
558 unsigned char *limit;
559 static struct ip_addr ip;
560 unsigned char *s;
561
562 if (st == NULL || st->s == NULL) goto error_null;
563 s=(unsigned char*)st->s;
564
565 /*init*/
566 ip.u.addr32[0]=0;
567 i=j=0;
568 limit=(unsigned char*)(st->s + st->len);
569
570 /* first char must be different than '0' */
571 if ((*s > '9' ) || (*s < '0')) goto error_char;
572 ip.u.addr[i]=ip.u.addr[i]*10+*s-'0';
573 s++;
574 j++;
575 for(;s<limit ;s++){
576 if (*s=='.'){
577 i++;
578 j=0;
579 if (i>3) goto error_dots;
580 s++;
581 if (s==limit) break;
582 if ( (*s <= '9' ) && (*s >= '0') ){
583 j++;
584 ip.u.addr[i]=ip.u.addr[i]*10+*s-'0';
585 } else {
586 goto error_char;
587 }
588 }else if ( (j==1) && (*s <= '9' ) && (*s >= '0') ){
589 /* if first char is '0' then fail conversion */
590 if (ip.u.addr[i]==0) goto error_char;
591 j++;
592 ip.u.addr[i]=ip.u.addr[i]*10+*s-'0';
593 }else if ( (j==2) && (*s <= '9' ) && (*s >= '0') ){
594 /* if first two chars are bigger then '25' then fail conversion */
595 if (ip.u.addr[i]>25) goto error_char;
596 /* if first three chars are bigger then '255' then fail conversion */
597 if (ip.u.addr[i]==25 && *s > '5') goto error_char;
598 j++;
599 ip.u.addr[i]=ip.u.addr[i]*10+*s-'0';
600 }else{
601 //error unknown char
602 goto error_char;
603 }
604 }
605 if (i<3) goto error_dots;
606 ip.af=AF_INET;
607 ip.len=4;
608
609 return &ip;
610error_null:
611 LM_DBG("Null pointer detected\n");
612 return NULL;

Callers 15

fixup_blacklist_ipFunction · 0.85
ip_addr_is_1918Function · 0.85
mk_net_cidrFunction · 0.85
comp_ipFunction · 0.85
tr_eval_ipFunction · 0.85
resolvehostFunction · 0.85
check_ip_addressFunction · 0.85
sip_resolvehostFunction · 0.85
mi_bl_get_ipFunction · 0.85
parse_ip_netFunction · 0.85
parse_domain_addressFunction · 0.85
reload_address_tableFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected