[asterisk-commits] branch kpfleming/stringfields - r7758 in /team/kpfleming/stringfields: channe...

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Tue Jan 3 19:07:09 CST 2006


Author: kpfleming
Date: Tue Jan  3 19:07:08 2006
New Revision: 7758

URL: http://svn.digium.com/view/asterisk?rev=7758&view=rev
Log:
convert more fields
add index-based field access

Modified:
    team/kpfleming/stringfields/channels/chan_sip.c
    team/kpfleming/stringfields/include/asterisk/stringfields.h

Modified: team/kpfleming/stringfields/channels/chan_sip.c
URL: http://svn.digium.com/view/asterisk/team/kpfleming/stringfields/channels/chan_sip.c?rev=7758&r1=7757&r2=7758&view=diff
==============================================================================
--- team/kpfleming/stringfields/channels/chan_sip.c (original)
+++ team/kpfleming/stringfields/channels/chan_sip.c Tue Jan  3 19:07:08 2006
@@ -598,9 +598,15 @@
 	ast_mutex_t lock;			/*!< Channel private lock */
 	int method;				/*!< SIP method of this packet */
 	AST_DECLARE_STRING_FIELDS(
-		AST_STRING_FIELD(callid);
+		AST_STRING_FIELD(callid);	/*!< Global CallID */
+		AST_STRING_FIELD(randdata);	/*!< Random data */
+		AST_STRING_FIELD(accountcode);	/*!< Account code */
+		AST_STRING_FIELD(realm);	/*!< Authorization realm */
+		AST_STRING_FIELD(nonce);	/*!< Authorization nonce */
+		AST_STRING_FIELD(opaque);	/*!< Opaque nonsense */
+		AST_STRING_FIELD(qop);		/*!< Quality of Protection, since SIP wasn't complicated enough yet. */
+		AST_STRING_FIELD(domain);	/*!< Authorization domain */
 	);
-	char randdata[80];			/*!< Random data */
 	struct ast_codec_pref prefs;		/*!< codec prefs */
 	unsigned int ocseq;			/*!< Current outgoing seqno */
 	unsigned int icseq;			/*!< Current incoming seqno */
@@ -660,16 +666,10 @@
 	char cid_name[256];			/*!< Caller*ID */
 	char via[256];				/*!< Via: header */
 	char fullcontact[128];			/*!< The Contact: that the UA registers with us */
-	char accountcode[AST_MAX_ACCOUNT_CODE];	/*!< Account code */
 	char our_contact[256];			/*!< Our contact header */
 	char *rpid;				/*!< Our RPID header */
 	char *rpid_from;			/*!< Our RPID From header */
-	char realm[MAXHOSTNAMELEN];		/*!< Authorization realm */
-	char nonce[256];			/*!< Authorization nonce */
 	int noncecount;				/*!< Nonce-count */
-	char opaque[256];			/*!< Opaque nonsense */
-	char qop[80];				/*!< Quality of Protection, since SIP wasn't complicated enough yet. */
-	char domain[MAXHOSTNAMELEN];		/*!< Authorization domain */
 	char lastmsg[256];			/*!< Last Message sent/received */
 	int amaflags;				/*!< AMA Flags */
 	int pendinginvite;			/*!< Any pending invite */
@@ -827,7 +827,12 @@
 struct sip_registry {
 	ASTOBJ_COMPONENTS_FULL(struct sip_registry,1,1);
 	AST_DECLARE_STRING_FIELDS(
-		AST_STRING_FIELD(callid);
+		AST_STRING_FIELD(callid);	/*!< Global Call-ID */
+		AST_STRING_FIELD(realm);	/*!< Authorization realm */
+		AST_STRING_FIELD(nonce);	/*!< Authorization nonce */
+		AST_STRING_FIELD(opaque);	/*!< Opaque nonsense */
+		AST_STRING_FIELD(qop);		/*!< Quality of Protection, since SIP wasn't complicated enough yet. */
+		AST_STRING_FIELD(domain);	/*!< Authorization domain */
 	);
 	int portno;			/*!<  Optional port override */
 	char username[80];		/*!<  Who we are registering as */
@@ -848,11 +853,6 @@
 	struct sockaddr_in us;		/*!< Who the server thinks we are */
  	
  					/* Saved headers */
- 	char realm[MAXHOSTNAMELEN];	/*!< Authorization realm */
- 	char nonce[256];		/*!< Authorization nonce */
- 	char domain[MAXHOSTNAMELEN];	/*!< Authorization domain */
- 	char opaque[256];		/*!< Opaque nonsense */
- 	char qop[80];			/*!< Quality of Protection. */
 	int noncecount;			/*!< Nonce-count */
  
  	char lastmsg[256];		/*!< Last Message sent/received */
@@ -896,7 +896,7 @@
 static int transmit_response(struct sip_pvt *p, char *msg, struct sip_request *req);
 static int transmit_response_with_sdp(struct sip_pvt *p, char *msg, struct sip_request *req, int retrans);
 static int transmit_response_with_unsupported(struct sip_pvt *p, char *msg, struct sip_request *req, char *unsupported);
-static int transmit_response_with_auth(struct sip_pvt *p, char *msg, struct sip_request *req, char *rand, int reliable, char *header, int stale);
+static int transmit_response_with_auth(struct sip_pvt *p, char *msg, struct sip_request *req, const char *rand, int reliable, char *header, int stale);
 static int transmit_request(struct sip_pvt *p, int sipmethod, int inc, int reliable, int newbranch);
 static int transmit_request_with_auth(struct sip_pvt *p, int sipmethod, int inc, int reliable, int newbranch);
 static int transmit_invite(struct sip_pvt *p, int sipmethod, int sendsdp, int init);
@@ -931,7 +931,7 @@
 static int sip_senddigit(struct ast_channel *ast, char digit);
 static int clear_realm_authentication(struct sip_auth *authlist);                            /* Clear realm authentication list (at reload) */
 static struct sip_auth *add_realm_authentication(struct sip_auth *authlist, char *configuration, int lineno);   /* Add realm authentication in list */
-static struct sip_auth *find_realm_authentication(struct sip_auth *authlist, char *realm);         /* Find authentication for a specific realm */
+static struct sip_auth *find_realm_authentication(struct sip_auth *authlist, const char *realm);         /* Find authentication for a specific realm */
 static int check_sip_domain(const char *domain, char *context, size_t len); /* Check if domain is one of our local domains */
 static void append_date(struct sip_request *req);	/* Append date to SIP packet */
 static int determine_firstline_parts(struct sip_request *req);
@@ -4216,7 +4216,7 @@
 }
 
 /* transmit_response_with_auth: Respond with authorization request */
-static int transmit_response_with_auth(struct sip_pvt *p, char *msg, struct sip_request *req, char *randdata, int reliable, char *header, int stale)
+static int transmit_response_with_auth(struct sip_pvt *p, char *msg, struct sip_request *req, const char *randdata, int reliable, char *header, int stale)
 {
 	struct sip_request resp;
 	char tmp[256];
@@ -5465,11 +5465,11 @@
 		/* We have auth data to reuse, build a digest header! */
 		if (sipdebug)
 			ast_log(LOG_DEBUG, "   >>> Re-using Auth data for %s@%s\n", r->username, r->hostname);
-		ast_copy_string(p->realm, r->realm, sizeof(p->realm));
-		ast_copy_string(p->nonce, r->nonce, sizeof(p->nonce));
-		ast_copy_string(p->domain, r->domain, sizeof(p->domain));
-		ast_copy_string(p->opaque, r->opaque, sizeof(p->opaque));
-		ast_copy_string(p->qop, r->qop, sizeof(p->qop));
+		ast_string_field_set(p, realm, r->realm);
+		ast_string_field_set(p, nonce, r->nonce);
+		ast_string_field_set(p, domain, r->domain);
+		ast_string_field_set(p, opaque, r->opaque);
+		ast_string_field_set(p, qop, r->qop);
 		p->noncecount = r->noncecount++;
 
 		memset(digest,0,sizeof(digest));
@@ -6092,7 +6092,7 @@
 /*! \brief  check_auth: Check user authorization from peer definition ---*/
 /*      Some actions, like REGISTER and INVITEs from peers require
         authentication (if peer have secret set) */
-static int check_auth(struct sip_pvt *p, struct sip_request *req, char *randdata, int randlen, char *username, char *secret, char *md5secret, int sipmethod, char *uri, int reliable, int ignore)
+static int check_auth(struct sip_pvt *p, struct sip_request *req, char *username, char *secret, char *md5secret, int sipmethod, char *uri, int reliable, int ignore)
 {
 	int res = -1;
 	char *response = "407 Proxy Authentication Required";
@@ -6157,22 +6157,22 @@
  	}
 #endif	
 	authtoken =  get_header(req, reqheader);	
-	if (ignore && !ast_strlen_zero(randdata) && ast_strlen_zero(authtoken)) {
+	if (ignore && !ast_strlen_zero(p->randdata) && ast_strlen_zero(authtoken)) {
 		/* This is a retransmitted invite/register/etc, don't reconstruct authentication
 		   information */
-		if (!ast_strlen_zero(randdata)) {
+		if (!ast_strlen_zero(p->randdata)) {
 			if (!reliable) {
 				/* Resend message if this was NOT a reliable delivery.   Otherwise the
 				   retransmission should get it */
-				transmit_response_with_auth(p, response, req, randdata, reliable, respheader, 0);
+				transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, 0);
 				/* Schedule auto destroy in 15 seconds */
 				sip_scheddestroy(p, 15000);
 			}
 			res = 1;
 		}
-	} else if (ast_strlen_zero(randdata) || ast_strlen_zero(authtoken)) {
-		snprintf(randdata, randlen, "%08x", thread_safe_rand());
-		transmit_response_with_auth(p, response, req, randdata, reliable, respheader, 0);
+	} else if (ast_strlen_zero(p->randdata) || ast_strlen_zero(authtoken)) {
+		ast_string_field_build(p, randdata, "%08x", thread_safe_rand());
+		transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, 0);
 		/* Schedule auto destroy in 15 seconds */
 		sip_scheddestroy(p, 15000);
 		res = 1;
@@ -6193,7 +6193,7 @@
 		char *nonce = "";
 		char *digestusername = "";
 		int  wrongnonce = 0;
-		char *usednonce = randdata;
+		const char *usednonce = p->randdata;
 
 		/* Find their response among the mess that we'r sent for comparison */
 		ast_copy_string(tmp, authtoken, sizeof(tmp));
@@ -6263,7 +6263,7 @@
 		}
 
 		/* Verify nonce from request matches our nonce.  If not, send 401 with new nonce */
-		if (strncasecmp(randdata, nonce, randlen)) {
+		if (strcasecmp(p->randdata, nonce)) {
 			wrongnonce = 1;
 			usednonce = nonce;
 		}
@@ -6286,18 +6286,17 @@
 		ast_md5_hash(resp_hash, resp);
 
 		if (wrongnonce) {
-
-			snprintf(randdata, randlen, "%08x", thread_safe_rand());
+			ast_string_field_build(p, randdata, "%08x", thread_safe_rand());
 			if (ua_hash && !strncasecmp(ua_hash, resp_hash, strlen(resp_hash))) {
 				if (sipdebug)
 					ast_log(LOG_NOTICE, "stale nonce received from '%s'\n", get_header(req, "To"));
 				/* We got working auth token, based on stale nonce . */
-				transmit_response_with_auth(p, response, req, randdata, reliable, respheader, 1);
+				transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, 1);
 			} else {
 				/* Everything was wrong, so give the device one more try with a new challenge */
 				if (sipdebug)
 					ast_log(LOG_NOTICE, "Bad authentication received from '%s'\n", get_header(req, "To"));
-				transmit_response_with_auth(p, response, req, randdata, reliable, respheader, 0);
+				transmit_response_with_auth(p, response, req, p->randdata, reliable, respheader, 0);
 			}
 
 			/* Schedule auto destroy in 15 seconds */
@@ -6404,7 +6403,7 @@
 		} else {
 			ast_copy_flags(p, peer, SIP_NAT);
 			transmit_response(p, "100 Trying", req);
-			if (!(res = check_auth(p, req, p->randdata, sizeof(p->randdata), peer->name, peer->secret, peer->md5secret, SIP_REGISTER, uri, 0, ignore))) {
+			if (!(res = check_auth(p, req, peer->name, peer->secret, peer->md5secret, SIP_REGISTER, uri, 0, ignore))) {
 				sip_cancel_destroy(p);
 				switch (parse_register_contact(p, peer, req)) {
 				case PARSE_REGISTER_FAILED:
@@ -6560,7 +6559,7 @@
 		colon = strchr(a, ':'); /* Remove :port */
 		if (colon)
 			*colon = '\0';
-		ast_copy_string(p->domain, a, sizeof(p->domain));
+		ast_string_field_set(p, domain, a);
 	}
 	/* Skip any options */
 	if ((a = strchr(uri, ';'))) {
@@ -7039,7 +7038,7 @@
 				ast_log(LOG_DEBUG, "Setting NAT on VRTP to %d\n", (ast_test_flag(p, SIP_NAT) & SIP_NAT_ROUTE));
 			ast_rtp_setnat(p->vrtp, (ast_test_flag(p, SIP_NAT) & SIP_NAT_ROUTE));
 		}
-		if (!(res = check_auth(p, req, p->randdata, sizeof(p->randdata), user->name, user->secret, user->md5secret, sipmethod, uri, reliable, ignore))) {
+		if (!(res = check_auth(p, req, user->name, user->secret, user->md5secret, sipmethod, uri, reliable, ignore))) {
 			sip_cancel_destroy(p);
 			ast_copy_flags(p, user, SIP_FLAGS_TO_COPY);
 			/* Copy SIP extensions profile from INVITE */
@@ -7061,7 +7060,7 @@
 			ast_copy_string(p->peersecret, user->secret, sizeof(p->peersecret));
 			ast_copy_string(p->subscribecontext, user->subscribecontext, sizeof(p->subscribecontext));
 			ast_copy_string(p->peermd5secret, user->md5secret, sizeof(p->peermd5secret));
-			ast_copy_string(p->accountcode, user->accountcode, sizeof(p->accountcode));
+			ast_string_field_set(p, accountcode, user->accountcode);
 			ast_copy_string(p->language, user->language, sizeof(p->language));
 			ast_copy_string(p->musicclass, user->musicclass, sizeof(p->musicclass));
 			p->amaflags = user->amaflags;
@@ -7138,7 +7137,7 @@
 				p->peersecret[0] = '\0';
 				p->peermd5secret[0] = '\0';
 			}
-			if (!(res = check_auth(p, req, p->randdata, sizeof(p->randdata), peer->name, p->peersecret, p->peermd5secret, sipmethod, uri, reliable, ignore))) {
+			if (!(res = check_auth(p, req, peer->name, p->peersecret, p->peermd5secret, sipmethod, uri, reliable, ignore))) {
 				ast_copy_flags(p, peer, SIP_FLAGS_TO_COPY);
 				/* If we have a call limit, set flag */
 				if (peer->call_limit)
@@ -7171,7 +7170,7 @@
 				ast_copy_string(p->peersecret, peer->secret, sizeof(p->peersecret));
 				ast_copy_string(p->peermd5secret, peer->md5secret, sizeof(p->peermd5secret));
 				ast_copy_string(p->language, peer->language, sizeof(p->language));
-				ast_copy_string(p->accountcode, peer->accountcode, sizeof(p->accountcode));
+				ast_string_field_set(p, accountcode, peer->accountcode);
 				p->amaflags = peer->amaflags;
 				p->callgroup = peer->callgroup;
 				p->pickupgroup = peer->pickupgroup;
@@ -7196,7 +7195,7 @@
 #ifdef OSP_SUPPORT			
 			else if (global_allowguest == 2) {
 				ast_copy_flags(p, &global_flags, SIP_OSPAUTH);
-				res = check_auth(p, req, p->randdata, sizeof(p->randdata), "", "", "", sipmethod, uri, reliable, ignore); 
+				res = check_auth(p, req, "", "", "", sipmethod, uri, reliable, ignore); 
 			}
 #endif
 		}
@@ -8908,15 +8907,14 @@
 	/* table of recognised keywords, and places where they should be copied */
 	const struct x {
 		const char *key;
-		char *dst;
-		int dstlen;
+		int field_index;
 	} *i, keys[] = {
-		{ "realm=", p->realm, sizeof(p->realm) },
-		{ "nonce=", p->nonce, sizeof(p->nonce) },
-		{ "opaque=", p->opaque, sizeof(p->opaque) },
-		{ "qop=", p->qop, sizeof(p->qop) },
-		{ "domain=", p->domain, sizeof(p->domain) },
-		{ NULL, NULL, 0 },
+		{ "realm=", ast_string_field_index(p, realm) },
+		{ "nonce=", ast_string_field_index(p, nonce) },
+		{ "opaque=", ast_string_field_index(p, opaque) },
+		{ "qop=", ast_string_field_index(p, qop) },
+		{ "domain=", ast_string_field_index(p, domain) },
+		{ NULL, 0 },
 	};
 
 	ast_copy_string(tmp, get_header(req, header), sizeof(tmp));
@@ -8927,8 +8925,6 @@
 		return -1;
 	}
 	c = tmp + strlen("Digest ");
-	for (i = keys; i->key != NULL; i++)
-		i->dst[0] = '\0';	/* init all to empty strings */
 	ast_copy_string(oldnonce, p->nonce, sizeof(oldnonce));
 	while (c && *(c = ast_skip_blanks(c))) {	/* lookup for keys */
 		for (i = keys; i->key != NULL; i++) {
@@ -8945,7 +8941,7 @@
 				separator = ",";
 			}
 			strsep(&c, separator); /* clear separator and move ptr */
-			ast_copy_string(i->dst, src, i->dstlen);
+			ast_string_field_index_set(p, i->field_index, src);
 			break;
 		}
 		if (i->key == NULL) /* not found, try ',' */
@@ -8960,11 +8956,11 @@
 		struct sip_registry *r = p->registry;
 
 		if (strcmp(r->nonce, p->nonce)) {
-			ast_copy_string(r->realm, p->realm, sizeof(r->realm));
-			ast_copy_string(r->nonce, p->nonce, sizeof(r->nonce));
-			ast_copy_string(r->domain, p->domain, sizeof(r->domain));
-			ast_copy_string(r->opaque, p->opaque, sizeof(r->opaque));
-			ast_copy_string(r->qop, p->qop, sizeof(r->qop));
+			ast_string_field_set(r, realm, p->realm);
+			ast_string_field_set(r, nonce, p->nonce);
+			ast_string_field_set(r, domain, p->domain);
+			ast_string_field_set(r, opaque, p->opaque);
+			ast_string_field_set(r, qop, p->qop);
 			r->noncecount = 0;
 		}
 	}
@@ -11841,7 +11837,7 @@
 }
 
 /*! \brief  find_realm_authentication: Find authentication for a specific realm ---*/
-static struct sip_auth *find_realm_authentication(struct sip_auth *authlist, char *realm)
+static struct sip_auth *find_realm_authentication(struct sip_auth *authlist, const char *realm)
 {
 	struct sip_auth *a;
 

Modified: team/kpfleming/stringfields/include/asterisk/stringfields.h
URL: http://svn.digium.com/view/asterisk/team/kpfleming/stringfields/include/asterisk/stringfields.h?rev=7758&r1=7757&r2=7758&view=diff
==============================================================================
--- team/kpfleming/stringfields/include/asterisk/stringfields.h (original)
+++ team/kpfleming/stringfields/include/asterisk/stringfields.h Tue Jan  3 19:07:08 2006
@@ -36,17 +36,26 @@
 #define AST_STRING_FIELD(name) ast_string_field name;
 
 #define AST_DECLARE_STRING_FIELDS(field_list) \
-	char *__begin_field[0]; \
+	ast_string_field __begin_field[0]; \
 	field_list \
-	char *__end_field[0];
+	ast_string_field __end_field[0];
 
 #define ast_string_field_count(x) \
 	(offsetof(typeof(*x), __end_field) - offsetof(typeof(*x), __begin_field)) / sizeof(ast_string_field)
 
+#define ast_string_field_index(x, field) \
+	(offsetof(typeof(*x), field) - offsetof(typeof(*x), __begin_field)) / sizeof(ast_string_field)
+
 #define ast_string_field_set(x, field, data) do { \
 	if (x->field) \
 		free((char *) x->field); \
-	x->field = strdup(data); \
+	x->field = (ast_string_field) strdup(data); \
+	} while (0)
+
+#define ast_string_field_index_set(x, index, data) do { \
+	if (x->__begin_field[index]) \
+		free((char *) x->__begin_field[index]); \
+	x->__begin_field[index] = (ast_string_field) strdup(data); \
 	} while (0)
 
 #define ast_string_field_build(x, field, fmt, args...) do { \
@@ -55,16 +64,26 @@
 	asprintf((char **) &x->field, fmt, args); \
 	} while (0)
 
+#define ast_string_field_index_build(x, index, fmt, args...) do { \
+	if (x->__begin_field[index]) \
+		free((char *) x->__begin_field[index]); \
+	asprintf((char **) &x->__begin_field[index], fmt, args); \
+	} while (0)
+
 #define ast_string_field_free(x, field) do { \
 	if (x->field) \
-		free((char *) x->field) \
+		free((char *) x->field); \
+	} while(0)
+
+#define ast_string_field_index_free(x, index) do { \
+	if (x->__begin_field[index]) \
+		free((char *) x->__begin_field[index]); \
 	} while(0)
 
 #define ast_string_field_free_all(x) do { \
 	int index; \
 	for (index = 0; index < ast_string_field_count(x); index ++) \
-		if (x->__begin_field[index]) \
-			free(x->__begin_field[index]); \
+		ast_string_field_index_free(x, index); \
 	} while(0)
 
 #endif /* _ASTERISK_STRINGFIELDS_H */



More information about the asterisk-commits mailing list