56885

rpcgen-passing a string inside a struct

Question:

I'm trying to pass a string as part of a structure over the network using the rpcgen packages. This is my IDL code :

struct param { char* name; int voterid; }; program VOTECLIENT_PROG { version VOTECLIENT_VERS { string ZEROIZE() = 1; string ADDVOTER(int) = 2; string VOTEFOR(param) = 3; string LISTCANDIDATES() = 4; int VOTECOUNT(string) = 5; } = 1; } = 0x2345111;

Somehow, the string is being truncated to a single character at the server. For example, if I pass name = "abc", I get "a" at the server. It looks like this is happening because of some issue inside the stubs, but I can't seem to figure out where the bug is.

My client code for the function that passes the string as an argument :

void voteclient_prog_1(char *host, char* c, int id) { CLIENT *clnt; char * *result_3; param votefor_1_arg; #ifndef DEBUG clnt = clnt_create (host, VOTECLIENT_PROG, VOTECLIENT_VERS, "udp"); if (clnt == NULL) { clnt_pcreateerror (host); exit (1); } #endif /* DEBUG */ votefor_1_arg.name = c; votefor_1_arg.voterid = id; result_3 = votefor_1(&votefor_1_arg, clnt); if (result_3 == (char **) NULL) { clnt_perror (clnt, "call failed"); } clnt_perror (clnt, "call failed"); #ifndef DEBUG clnt_destroy (clnt); #endif /* DEBUG */ } int main (int argc, char *argv[]) { char *host; int id; char* c = new char[20]; if (argc < 4) { printf ("usage: %s server_host name voterid\n", argv[0]); exit (1); } host = argv[1]; c = argv[2]; id = atoi(argv[3]); voteclient_prog_1 (host, c, id); exit (0); }

Any help will be greatly appreciated.

Answer1:

From <a href="http://docs.freebsd.org/44doc/psd/22.rpcgen/paper.pdf" rel="nofollow" title="rpcgen Programming Guide">rpcgen Programming Guide</a>, 6.9. Special Cases:

<blockquote>

<strong>Strings:</strong> C has no built-in string type, but instead uses the null-terminated “char *” convention. In XDR language, strings are declared using the “string” keyword, and compiled into “char *”s in the output header file. The maximum size contained in the angle brackets specifies the maximum number of characters allowed in the strings (not counting the NULL character). The maximum size may be left off, indicating a string of arbitrary length.

Examples:

string name<32>; --> char *name; string longname<>; --> char *longname; </blockquote>

So, you should declare name like above, e. g. string name<20>;.

Recommend

  • Using stanford parser to parse Chinese
  • How to implement laravel function Crypt::encrypt() in Objective C?
  • Python 3 non-blocking read with pySerial (Cannot get pySerial's “in_waiting” property to work)
  • Adding to or updating an entity in a foreach loop takes too long time before calling SaveChanges()?
  • R's coreNLP::initCoreNLP() throws java.lang.OutOfMemoryError
  • Serialize HashMap as root element with Simple
  • Create an multidimensional array from a database table
  • Serial Communication between Arduino and Python, issue of using hexidecimal values
  • iPhone - UIWebView multiple locks
  • Reload UITableView from another tab
  • How to get to older Xcode beta version?
  • Safari PHP form submission -file upload hangs
  • Find tangent points on a curve from a user-given point outside the curve
  • Ruby: Why does this way of using map throw an error?
  • Android NFC read Tags issue. Activity starts each time on data received
  • openssl handshake failed
  • How to resolve docker host names (/etc/hosts) in containers
  • Using extern @class in order to add a category?
  • Replace and retrieve placeholder value
  • Getting error java.io.FileNotFoundException (log4j log file) at the time of publish project on cloud
  • In loopback documentation what does variable 'cb' stands for?
  • Setting the run time properties on SpringApplicationBuilder()
  • Spring Cloud Microservice Architecture Confusion
  • Transactional Create with Validation in ServiceStack Redis Client
  • d3 v4 drag and drop with TypeScript
  • Why does access(2) check for real and not effective UID?
  • Handling un-mapped Rest path
  • Illegal mix of collations for operation for date/time comparison
  • PHP - How to update data to MySQL when click a radio button
  • RectangularRangeIndicator format like triangular using dojo
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • TFS: Get latest causes slow project reloading
  • Controls, properties, events and timers running in design time
  • R: gsub and capture
  • How do I rollback to a specific git commit
  • need help with bizarre java.net.HttpURLConnection behavior
  • Binding checkboxes to object values in AngularJs
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?